Skip to content

Commit

Permalink
sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
yxshv committed Apr 11, 2024
1 parent b97def8 commit 43005b3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
28 changes: 27 additions & 1 deletion apps/web/src/actions/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,36 @@ import {
StoredContent,
storedContent,
users,
space
} from "@/server/db/schema";
import { like, eq, and } from "drizzle-orm";
import { like, eq, and, sql } from "drizzle-orm";
import { union } from "drizzle-orm/sqlite-core"
import { auth as authOptions } from "@/server/auth";

// @todo: (future) pagination not yet needed
export async function searchMemoriesAndSpaces(userId: string, query: string) {
const searchMemoriesQuery = db.select({
type: sql<string>`'memory'`,
space: sql`NULL`,
memory: storedContent as any
}).from(storedContent).where(and(
eq(storedContent.user, userId),
like(storedContent.title, `%${query}%`)
))

const searchSpacesQuery = db.select({
type: sql<string>`'space'`,
space: space as any,
memory: sql`NULL`,
}).from(space).where(
and(
eq(space.user, userId),
like(space.name, `%${query}%`)
)
)

return await union(searchMemoriesQuery, searchSpacesQuery)
}

async function getUser() {
const token =
Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { redirect } from "next/navigation";
import { fetchContentForSpace, fetchFreeMemories, transformContent } from "../../types/memory";
import { MemoryProvider } from "@/contexts/MemoryContext";
import Content from "./content";
import { searchMemoriesAndSpaces } from "@/actions/db";

export const runtime = "edge";

Expand Down Expand Up @@ -68,6 +69,11 @@ export default async function Home() {
// freeMemories
const freeMemories = await fetchFreeMemories(userData.id)

// @dhravya test these 3 functions
fetchFreeMemories
fetchContentForSpace
searchMemoriesAndSpaces

collectedSpaces.push({
id: 1,
name: "Cool tech",
Expand Down

0 comments on commit 43005b3

Please sign in to comment.