diff --git a/apps/api/src/lib/db.ts b/apps/api/src/lib/db.ts new file mode 100644 index 000000000..f631db16a --- /dev/null +++ b/apps/api/src/lib/db.ts @@ -0,0 +1,4 @@ +import { createDatabase, createPool } from "@echo-webkom/db/create"; + +const pool = createPool(); +export const db = createDatabase(pool); diff --git a/apps/api/src/services/admin.ts b/apps/api/src/services/admin.ts index de563e328..3e8f70ac6 100644 --- a/apps/api/src/services/admin.ts +++ b/apps/api/src/services/admin.ts @@ -3,9 +3,9 @@ import { and, eq, gte, lte, or, sql } from "drizzle-orm"; import { Hono } from "hono"; import { z } from "zod"; -import { db } from "@echo-webkom/db"; import { AnswerInsert, answers, comments, registrations, users } from "@echo-webkom/db/schemas"; +import { db } from "../lib/db"; import { admin } from "../middleware/admin"; import { getCorrectSpotrange } from "../utils/correct-spot-range"; import { parseJson } from "../utils/json"; diff --git a/apps/api/src/services/degrees.ts b/apps/api/src/services/degrees.ts index 579f45db2..60a0b4966 100644 --- a/apps/api/src/services/degrees.ts +++ b/apps/api/src/services/degrees.ts @@ -2,9 +2,9 @@ import { eq } from "drizzle-orm"; import { Hono } from "hono"; import { z } from "zod"; -import { db } from "@echo-webkom/db"; import { degrees, insertDegreeSchema } from "@echo-webkom/db/schemas"; +import { db } from "../lib/db"; import { admin } from "../middleware/admin"; import { parseJson } from "../utils/json"; diff --git a/apps/api/src/services/feedback.ts b/apps/api/src/services/feedback.ts index 65e7722dd..1e8502e32 100644 --- a/apps/api/src/services/feedback.ts +++ b/apps/api/src/services/feedback.ts @@ -1,7 +1,6 @@ import { Hono } from "hono"; -import { db } from "@echo-webkom/db"; - +import { db } from "../lib/db"; import { admin } from "../middleware/admin"; const app = new Hono(); diff --git a/apps/api/src/services/happening.ts b/apps/api/src/services/happening.ts index f914e72ce..bc71959c5 100644 --- a/apps/api/src/services/happening.ts +++ b/apps/api/src/services/happening.ts @@ -1,7 +1,6 @@ import { Hono } from "hono"; -import { db } from "@echo-webkom/db"; - +import { db } from "../lib/db"; import { admin } from "../middleware/admin"; const app = new Hono(); diff --git a/apps/api/src/services/shopping-list.ts b/apps/api/src/services/shopping-list.ts index 6215d1734..0d690d5e4 100644 --- a/apps/api/src/services/shopping-list.ts +++ b/apps/api/src/services/shopping-list.ts @@ -1,7 +1,6 @@ import { Hono } from "hono"; -import { db } from "@echo-webkom/db"; - +import { db } from "../lib/db"; import { admin } from "../middleware/admin"; const app = new Hono(); diff --git a/apps/web/src/actions/delete-comment.ts b/apps/web/src/actions/delete-comment.ts index c79ac2688..051237a59 100644 --- a/apps/web/src/actions/delete-comment.ts +++ b/apps/web/src/actions/delete-comment.ts @@ -2,8 +2,8 @@ import { and, eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { comments } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { revalidateComments } from "@/data/comments/revalidate"; import { getUser } from "@/lib/get-user"; diff --git a/apps/web/src/actions/deregister.ts b/apps/web/src/actions/deregister.ts index 4dab9cb79..f519bf6f8 100644 --- a/apps/web/src/actions/deregister.ts +++ b/apps/web/src/actions/deregister.ts @@ -3,8 +3,8 @@ import { and, eq } from "drizzle-orm"; import { z } from "zod"; -import { db } from "@echo-webkom/db"; import { answers, registrations } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { DeregistrationNotificationEmail } from "@echo-webkom/email"; import { emailClient } from "@echo-webkom/email/client"; diff --git a/apps/web/src/actions/images.ts b/apps/web/src/actions/images.ts index ebf49319c..84d1b0445 100644 --- a/apps/web/src/actions/images.ts +++ b/apps/web/src/actions/images.ts @@ -3,8 +3,8 @@ import { File } from "node:buffer"; import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { users } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { ppFor } from "@/lib/echogram"; import { getUser } from "@/lib/get-user"; diff --git a/apps/web/src/actions/remove-all-registrations.ts b/apps/web/src/actions/remove-all-registrations.ts index c0064c523..66907741c 100644 --- a/apps/web/src/actions/remove-all-registrations.ts +++ b/apps/web/src/actions/remove-all-registrations.ts @@ -3,8 +3,8 @@ import { revalidateTag } from "next/cache"; import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { registrations } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { cacheKeyFactory } from "@/data/registrations/revalidate"; import { getUser } from "@/lib/get-user"; diff --git a/apps/web/src/actions/strikes.ts b/apps/web/src/actions/strikes.ts index f085c60d3..874f8b680 100644 --- a/apps/web/src/actions/strikes.ts +++ b/apps/web/src/actions/strikes.ts @@ -3,8 +3,8 @@ import { and, eq } from "drizzle-orm"; import { z } from "zod"; -import { db } from "@echo-webkom/db"; import { type StrikeInfoInsert } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { StrikeNotificationEmail } from "@echo-webkom/email"; import { emailClient } from "@echo-webkom/email/client"; import { type StrikeType } from "@echo-webkom/lib/src/constants"; diff --git a/apps/web/src/actions/update-registration.ts b/apps/web/src/actions/update-registration.ts index 26a72298e..5d4f8a313 100644 --- a/apps/web/src/actions/update-registration.ts +++ b/apps/web/src/actions/update-registration.ts @@ -3,8 +3,8 @@ import { and, eq } from "drizzle-orm"; import { z } from "zod"; -import { db } from "@echo-webkom/db"; import { registrations, registrationStatusEnum } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { GotSpotNotificationEmail } from "@echo-webkom/email"; import { emailClient } from "@echo-webkom/email/client"; diff --git a/apps/web/src/actions/user.ts b/apps/web/src/actions/user.ts index eb2930a9b..5242fc2ac 100644 --- a/apps/web/src/actions/user.ts +++ b/apps/web/src/actions/user.ts @@ -3,8 +3,8 @@ import { eq } from "drizzle-orm"; import { z } from "zod"; -import { db } from "@echo-webkom/db"; import { insertUserSchema, users, usersToGroups } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { getUser } from "@/lib/get-user"; import { isWebkom } from "@/lib/memberships"; diff --git a/apps/web/src/actions/whitelist.ts b/apps/web/src/actions/whitelist.ts index d416598dc..36e36f3e6 100644 --- a/apps/web/src/actions/whitelist.ts +++ b/apps/web/src/actions/whitelist.ts @@ -2,8 +2,8 @@ import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { insertWhitelistSchema, whitelist } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { getUser } from "@/lib/get-user"; import { isMemberOf } from "@/lib/memberships"; diff --git a/apps/web/src/app/(default)/admin/brukere/page.tsx b/apps/web/src/app/(default)/admin/brukere/page.tsx index 2f6debb2a..b1301183b 100644 --- a/apps/web/src/app/(default)/admin/brukere/page.tsx +++ b/apps/web/src/app/(default)/admin/brukere/page.tsx @@ -1,6 +1,6 @@ import { RxDotsHorizontal as Dots } from "react-icons/rx"; -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; import { Container } from "@/components/container"; import { Heading } from "@/components/typography/heading"; diff --git a/apps/web/src/app/(default)/admin/whitelist/_actions/delete-access-request.ts b/apps/web/src/app/(default)/admin/whitelist/_actions/delete-access-request.ts index 42b8c337f..6a3da3847 100644 --- a/apps/web/src/app/(default)/admin/whitelist/_actions/delete-access-request.ts +++ b/apps/web/src/app/(default)/admin/whitelist/_actions/delete-access-request.ts @@ -2,8 +2,8 @@ import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { accessRequests } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { getUser } from "@/lib/get-user"; import { isMemberOf } from "@/lib/memberships"; diff --git a/apps/web/src/app/(default)/admin/whitelist/_actions/grant-access.ts b/apps/web/src/app/(default)/admin/whitelist/_actions/grant-access.ts index 437c69e34..813242551 100644 --- a/apps/web/src/app/(default)/admin/whitelist/_actions/grant-access.ts +++ b/apps/web/src/app/(default)/admin/whitelist/_actions/grant-access.ts @@ -2,8 +2,9 @@ import { eq } from "drizzle-orm"; -import { db, isPostgresIshError } from "@echo-webkom/db"; +import { isPostgresIshError } from "@echo-webkom/db/error"; import { accessRequests, whitelist } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { AccessGrantedEmail } from "@echo-webkom/email"; import { emailClient } from "@echo-webkom/email/client"; diff --git a/apps/web/src/app/(default)/auth/profil/page.tsx b/apps/web/src/app/(default)/auth/profil/page.tsx index b8c18304e..3ea28f9bc 100644 --- a/apps/web/src/app/(default)/auth/profil/page.tsx +++ b/apps/web/src/app/(default)/auth/profil/page.tsx @@ -2,7 +2,7 @@ import Link from "next/link"; import { redirect } from "next/navigation"; import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; import { Chip } from "@/components/typography/chip"; import { Heading } from "@/components/typography/heading"; diff --git a/apps/web/src/app/(default)/auth/tilgang/[id]/_actions/request-access.ts b/apps/web/src/app/(default)/auth/tilgang/[id]/_actions/request-access.ts index f22737042..72f6957a9 100644 --- a/apps/web/src/app/(default)/auth/tilgang/[id]/_actions/request-access.ts +++ b/apps/web/src/app/(default)/auth/tilgang/[id]/_actions/request-access.ts @@ -3,8 +3,9 @@ import { nanoid } from "nanoid"; import { z } from "zod"; -import { db, isPostgresIshError } from "@echo-webkom/db"; +import { isPostgresIshError } from "@echo-webkom/db/error"; import { accessRequests } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { AccessRequestNotificationEmail } from "@echo-webkom/email"; import { emailClient } from "@echo-webkom/email/client"; diff --git a/apps/web/src/app/(default)/dashbord/[slug]/page.tsx b/apps/web/src/app/(default)/dashbord/[slug]/page.tsx index d54deea8d..a0665db27 100644 --- a/apps/web/src/app/(default)/dashbord/[slug]/page.tsx +++ b/apps/web/src/app/(default)/dashbord/[slug]/page.tsx @@ -2,8 +2,8 @@ import Image from "next/image"; import Link from "next/link"; import { notFound } from "next/navigation"; -import { db } from "@echo-webkom/db"; import { type RegistrationStatus } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { Container } from "@/components/container"; import { HappeningInfoBox } from "@/components/happening-info-box"; diff --git a/apps/web/src/app/(default)/gruppe/[id]/actions.ts b/apps/web/src/app/(default)/gruppe/[id]/actions.ts index ac29c007e..71e30f7d2 100644 --- a/apps/web/src/app/(default)/gruppe/[id]/actions.ts +++ b/apps/web/src/app/(default)/gruppe/[id]/actions.ts @@ -2,8 +2,8 @@ import { and, eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { usersToGroups } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { getUser } from "@/lib/get-user"; diff --git a/apps/web/src/app/(default)/gruppe/[id]/page.tsx b/apps/web/src/app/(default)/gruppe/[id]/page.tsx index 0f50f5f1f..19e285edc 100644 --- a/apps/web/src/app/(default)/gruppe/[id]/page.tsx +++ b/apps/web/src/app/(default)/gruppe/[id]/page.tsx @@ -1,7 +1,7 @@ import { notFound } from "next/navigation"; import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; import { Container } from "@/components/container"; import { Heading } from "@/components/typography/heading"; diff --git a/apps/web/src/app/(default)/prikker/[userId]/page.tsx b/apps/web/src/app/(default)/prikker/[userId]/page.tsx index 246e11219..b6f10f6f3 100644 --- a/apps/web/src/app/(default)/prikker/[userId]/page.tsx +++ b/apps/web/src/app/(default)/prikker/[userId]/page.tsx @@ -2,7 +2,7 @@ import Link from "next/link"; import { notFound } from "next/navigation"; import { and, desc, eq, lt } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; import { Container } from "@/components/container"; import { Heading } from "@/components/typography/heading"; diff --git a/apps/web/src/app/api/groups/route.ts b/apps/web/src/app/api/groups/route.ts index bd3798f1d..36836e19a 100644 --- a/apps/web/src/app/api/groups/route.ts +++ b/apps/web/src/app/api/groups/route.ts @@ -1,4 +1,4 @@ -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; import { withBearerAuth } from "@/lib/checks/with-bearer-auth"; diff --git a/apps/web/src/app/api/sanity/route.ts b/apps/web/src/app/api/sanity/route.ts index 8e699da20..a28e2ea9d 100644 --- a/apps/web/src/app/api/sanity/route.ts +++ b/apps/web/src/app/api/sanity/route.ts @@ -1,7 +1,6 @@ import { NextResponse } from "next/server"; import { and, eq, inArray } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { happenings, happeningsToGroups, @@ -10,6 +9,7 @@ import { type HappeningInsert, type QuestionInsert, } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { isBoard } from "@echo-webkom/lib"; import { withBasicAuth } from "@/lib/checks/with-basic-auth"; diff --git a/apps/web/src/data/access-requests/queries.ts b/apps/web/src/data/access-requests/queries.ts index e81079b96..65401960c 100644 --- a/apps/web/src/data/access-requests/queries.ts +++ b/apps/web/src/data/access-requests/queries.ts @@ -1,4 +1,4 @@ -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; export const getAccessRequests = async () => { return await db.query.accessRequests.findMany(); diff --git a/apps/web/src/data/groups/mutations.ts b/apps/web/src/data/groups/mutations.ts index 920cdd0af..ee89db140 100644 --- a/apps/web/src/data/groups/mutations.ts +++ b/apps/web/src/data/groups/mutations.ts @@ -1,7 +1,7 @@ import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { groups, type Group, type GroupInsert } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { revalidateGroups } from "./revalidate"; diff --git a/apps/web/src/data/groups/queries.ts b/apps/web/src/data/groups/queries.ts index 5e85d9ada..57e7dba9c 100644 --- a/apps/web/src/data/groups/queries.ts +++ b/apps/web/src/data/groups/queries.ts @@ -1,8 +1,8 @@ import { unstable_cache as cache } from "next/cache"; import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { type User } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { cacheKeyFactory } from "./revalidate"; diff --git a/apps/web/src/data/happenings/queries.ts b/apps/web/src/data/happenings/queries.ts index 8ab01a4c9..58ad5afc9 100644 --- a/apps/web/src/data/happenings/queries.ts +++ b/apps/web/src/data/happenings/queries.ts @@ -1,7 +1,7 @@ import { and, asc, eq, gt, lt } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { type Happening, type HappeningType } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { isErrorMessage } from "@/utils/error"; diff --git a/apps/web/src/data/kv/kv-adapter.ts b/apps/web/src/data/kv/kv-adapter.ts index 67b2fb529..f2ee2b679 100644 --- a/apps/web/src/data/kv/kv-adapter.ts +++ b/apps/web/src/data/kv/kv-adapter.ts @@ -1,6 +1,7 @@ import { eq } from "drizzle-orm"; -import { isPostgresIshError, type Database } from "@echo-webkom/db"; +import { type Database } from "@echo-webkom/db/create"; +import { isPostgresIshError } from "@echo-webkom/db/error"; import { kv } from "@echo-webkom/db/schemas"; import { isExpired } from "./utils"; diff --git a/apps/web/src/data/kv/namespaces.ts b/apps/web/src/data/kv/namespaces.ts index 16b01fcbf..60b41252a 100644 --- a/apps/web/src/data/kv/namespaces.ts +++ b/apps/web/src/data/kv/namespaces.ts @@ -1,6 +1,6 @@ import z from "zod"; -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; import { KVDrizzleAdapter } from "./kv-adapter"; import { KVNamespace } from "./kv-namespace"; diff --git a/apps/web/src/data/reactions/mutations.ts b/apps/web/src/data/reactions/mutations.ts index 4d5b72da3..b50f507bd 100644 --- a/apps/web/src/data/reactions/mutations.ts +++ b/apps/web/src/data/reactions/mutations.ts @@ -1,7 +1,7 @@ import { and, eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { reactions, type ReactionInsert } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { revalidateReactions } from "./revalidate"; diff --git a/apps/web/src/data/reactions/queries.ts b/apps/web/src/data/reactions/queries.ts index 4d621d655..543492d7a 100644 --- a/apps/web/src/data/reactions/queries.ts +++ b/apps/web/src/data/reactions/queries.ts @@ -1,7 +1,7 @@ import { unstable_cache as cache } from "next/cache"; import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; import { cacheKeyFactory } from "./revalidate"; diff --git a/apps/web/src/data/registrations/mutations.ts b/apps/web/src/data/registrations/mutations.ts index 591b69e92..fce61c03b 100644 --- a/apps/web/src/data/registrations/mutations.ts +++ b/apps/web/src/data/registrations/mutations.ts @@ -1,5 +1,5 @@ -import { db } from "@echo-webkom/db"; import { registrations, type RegistrationInsert } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { revalidateRegistrations } from "./revalidate"; diff --git a/apps/web/src/data/registrations/queries.ts b/apps/web/src/data/registrations/queries.ts index 82e0dcaaf..6c06e2648 100644 --- a/apps/web/src/data/registrations/queries.ts +++ b/apps/web/src/data/registrations/queries.ts @@ -1,8 +1,8 @@ import { unstable_cache as cache } from "next/cache"; import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { type Registration, type User } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { apiServer } from "@/api/server"; import { cacheKeyFactory } from "./revalidate"; diff --git a/apps/web/src/data/shopping-list-item/mutations.ts b/apps/web/src/data/shopping-list-item/mutations.ts index 51896a679..9843551fc 100644 --- a/apps/web/src/data/shopping-list-item/mutations.ts +++ b/apps/web/src/data/shopping-list-item/mutations.ts @@ -1,6 +1,5 @@ import { and, eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { shoppingListItems, usersToShoppingListItems, @@ -9,6 +8,7 @@ import { type UsersToShoppingListItems, type UsersToShoppingListItemsInsert, } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; export const createShoppinglistItem = async (newItem: ShoppingListItemsInsert) => { const [insertedShoppingListItem] = await db diff --git a/apps/web/src/data/site-feedbacks/mutations.ts b/apps/web/src/data/site-feedbacks/mutations.ts index 88f40d597..eb7047ee2 100644 --- a/apps/web/src/data/site-feedbacks/mutations.ts +++ b/apps/web/src/data/site-feedbacks/mutations.ts @@ -1,7 +1,7 @@ import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { siteFeedback, type SiteFeedbackInsert } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; export const createFeedback = async (feedback: SiteFeedbackInsert) => { const [insertedFeedback] = await db diff --git a/apps/web/src/data/strikes/mutations.ts b/apps/web/src/data/strikes/mutations.ts index 122e01b8d..3abdc0beb 100644 --- a/apps/web/src/data/strikes/mutations.ts +++ b/apps/web/src/data/strikes/mutations.ts @@ -1,6 +1,5 @@ import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { strikeInfos, strikes, @@ -8,6 +7,7 @@ import { type StrikeInfoInsert, type StrikeInsert, } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { revalidateStrikes } from "./revalidate"; diff --git a/apps/web/src/data/strikes/queries.ts b/apps/web/src/data/strikes/queries.ts index ff3dac740..5adad22ab 100644 --- a/apps/web/src/data/strikes/queries.ts +++ b/apps/web/src/data/strikes/queries.ts @@ -1,8 +1,8 @@ import { unstable_cache as cache } from "next/cache"; import { and, count, eq, gt, isNull, or } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { strikes, users } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { cacheKeyFactory } from "./revalidate"; diff --git a/apps/web/src/data/users/mutations.ts b/apps/web/src/data/users/mutations.ts index 0637aefef..20193d100 100644 --- a/apps/web/src/data/users/mutations.ts +++ b/apps/web/src/data/users/mutations.ts @@ -2,8 +2,8 @@ import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { users } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; export const unbanUser = async (userId: string) => { const user = await db diff --git a/apps/web/src/data/users/queries.ts b/apps/web/src/data/users/queries.ts index 20db6db61..e9b5b4b84 100644 --- a/apps/web/src/data/users/queries.ts +++ b/apps/web/src/data/users/queries.ts @@ -1,8 +1,8 @@ import { unstable_cache as cache } from "next/cache"; import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { type User } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; export const getUserById = async (id: User["id"]) => { return await db.query.users.findFirst({ diff --git a/apps/web/src/data/whitelist/queries.ts b/apps/web/src/data/whitelist/queries.ts index 36da8d1be..504a05fad 100644 --- a/apps/web/src/data/whitelist/queries.ts +++ b/apps/web/src/data/whitelist/queries.ts @@ -1,6 +1,6 @@ import { isFuture } from "date-fns"; -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; export const getWhitelist = async () => { return await db.query.whitelist diff --git a/apps/web/src/lib/ban-info.ts b/apps/web/src/lib/ban-info.ts index 6a60d00e2..59fc80bf8 100644 --- a/apps/web/src/lib/ban-info.ts +++ b/apps/web/src/lib/ban-info.ts @@ -1,7 +1,7 @@ import { eq } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { type User } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { getHappeningsFromDate, getHappeningsFromDateToDate } from "@/data/happenings/queries"; import { type fetchHappeningBySlug } from "@/sanity/happening"; diff --git a/packages/auth/src/auth-options.ts b/packages/auth/src/auth-options.ts index 3d326f1d0..00f71773d 100644 --- a/packages/auth/src/auth-options.ts +++ b/packages/auth/src/auth-options.ts @@ -1,8 +1,8 @@ import { eq } from "drizzle-orm"; import type { AuthOptions, DefaultSession } from "next-auth"; -import { db } from "@echo-webkom/db"; import { users } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { DrizzleAdapter } from "./drizzle-adapter"; import { Feide } from "./feide"; diff --git a/packages/auth/src/helpers.ts b/packages/auth/src/helpers.ts index 5d1013df7..814ea570b 100644 --- a/packages/auth/src/helpers.ts +++ b/packages/auth/src/helpers.ts @@ -1,7 +1,7 @@ import { eq } from "drizzle-orm"; import { getServerSession as _getServerSession } from "next-auth/next"; -import { db } from "@echo-webkom/db"; +import { db } from "@echo-webkom/db/serverless"; import { createAuthOptions } from "./auth-options"; diff --git a/packages/db/drizzle/migrate.ts b/packages/db/drizzle/migrate.ts index cb490c0df..6e5bc3543 100644 --- a/packages/db/drizzle/migrate.ts +++ b/packages/db/drizzle/migrate.ts @@ -2,7 +2,7 @@ import process from "node:process"; import { migrate } from "drizzle-orm/postgres-js/migrator"; -import { db } from "../src"; +import { db } from "../src/serverless"; /** * Do not run migrations in preview deployments. diff --git a/packages/db/package.json b/packages/db/package.json index f1663489f..7f385f25e 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -4,7 +4,10 @@ "exports": { ".": "./src/index.ts", "./schemas": "./src/schemas/index.ts", - "./utils": "./src/utils.ts" + "./utils": "./src/utils.ts", + "./create": "./src/create.ts", + "./serverless": "./src/serverless.ts", + "./error": "./src/error.ts" }, "scripts": { "clean": "rm -rf .turbo node_modules", diff --git a/packages/db/src/create.ts b/packages/db/src/create.ts new file mode 100644 index 000000000..ab0673e1e --- /dev/null +++ b/packages/db/src/create.ts @@ -0,0 +1,21 @@ +import { drizzle } from "drizzle-orm/postgres-js"; +import postgres from "postgres"; + +import * as schema from "./schemas"; + +export type Database = ReturnType; + +// eslint-disable-next-line @typescript-eslint/ban-types +export const createPool = (options?: postgres.Options<{}> | undefined) => { + return postgres(process.env.DATABASE_URL!, { + prepare: false, + ...options, + }); +}; + +export const createDatabase = (pool: ReturnType) => { + return drizzle(pool, { + schema, + logger: process.env.DATABASE_LOG === "true", + }); +}; diff --git a/packages/db/src/error.ts b/packages/db/src/error.ts new file mode 100644 index 000000000..a35abc282 --- /dev/null +++ b/packages/db/src/error.ts @@ -0,0 +1,17 @@ +/** + * PostgresError is hærk to work with. Just check if it has a code property. + * We can deduct enough information from that. + * + * @see https://www.postgresql.org/docs/current/errcodes-appendix.html + */ +export type PostgresIshError = { + code: string; +}; + +export const isPostgresIshError = (e: unknown): e is PostgresIshError => { + if (typeof e !== "object" || e === null) { + return false; + } + + return "code" in e && e instanceof Error; +}; diff --git a/packages/db/src/index.ts b/packages/db/src/index.ts deleted file mode 100644 index 89215df50..000000000 --- a/packages/db/src/index.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { drizzle } from "drizzle-orm/postgres-js"; -import postgres from "postgres"; - -import * as schema from "./schemas"; - -export type Database = ReturnType; - -const globalForPool = globalThis as unknown as { - pool: ReturnType | undefined; -}; - -let pool; - -const createPool = () => { - return postgres(process.env.DATABASE_URL!, { - max: 1, - prepare: false, - idle_timeout: 10000, // 10 seconds - connect_timeout: 1000, // 1 seconds - }); -}; - -if (process.env.NODE_ENV !== "production") { - if (!globalForPool.pool) { - globalForPool.pool = createPool(); - } - pool = globalForPool.pool; -} else { - pool = createPool(); -} - -const createDatabase = (pool: ReturnType) => { - return drizzle(pool, { - schema, - logger: process.env.DATABASE_LOG === "true", - }); -}; - -export const db = createDatabase(pool); - -/** - * PostgresError is hærk to work with. Just check if it has a code property. - * We can deduct enough information from that. - * - * @see https://www.postgresql.org/docs/current/errcodes-appendix.html - */ -type PostgresIshError = { - code: string; -}; - -export const isPostgresIshError = (e: unknown): e is PostgresIshError => { - if (typeof e !== "object" || e === null) { - return false; - } - - return "code" in e && e instanceof Error; -}; diff --git a/packages/db/src/serverless.ts b/packages/db/src/serverless.ts new file mode 100644 index 000000000..d071a8d6e --- /dev/null +++ b/packages/db/src/serverless.ts @@ -0,0 +1,32 @@ +import type postgres from "postgres"; + +import { createDatabase, createPool } from "./create"; + +const MAX_POOL = 1; +const IDLE_TIMEOUT = 10000; // 10 seconds +const CONNECT_TIMEOUT = 1000; // 1 seconds + +const globalForPool = globalThis as unknown as { + pool: ReturnType | undefined; +}; + +let pool; + +if (process.env.NODE_ENV !== "production") { + if (!globalForPool.pool) { + globalForPool.pool = createPool({ + max: MAX_POOL, + idle_timeout: IDLE_TIMEOUT, + connect_timeout: CONNECT_TIMEOUT, + }); + } + pool = globalForPool.pool; +} else { + pool = createPool({ + max: MAX_POOL, + idle_timeout: IDLE_TIMEOUT, + connect_timeout: CONNECT_TIMEOUT, + }); +} + +export const db = createDatabase(pool); diff --git a/packages/seeder/src/db/index.ts b/packages/seeder/src/db/index.ts index b12cba904..39a37dfa6 100644 --- a/packages/seeder/src/db/index.ts +++ b/packages/seeder/src/db/index.ts @@ -1,7 +1,7 @@ import chalk from "chalk"; -import { db } from "@echo-webkom/db"; import { degrees, groups, spotRanges, usersToGroups } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import * as message from "../utils"; import { degrees as defaultDegrees } from "./data/degrees"; diff --git a/packages/seeder/src/db/repo/happening.ts b/packages/seeder/src/db/repo/happening.ts index 7afd5acfd..c67c328af 100644 --- a/packages/seeder/src/db/repo/happening.ts +++ b/packages/seeder/src/db/repo/happening.ts @@ -1,5 +1,5 @@ -import { db } from "@echo-webkom/db"; import { happenings, type HappeningInsert } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; export const create = async (happening: HappeningInsert) => { await db.insert(happenings).values(happening).onConflictDoNothing(); diff --git a/packages/seeder/src/db/repo/user.ts b/packages/seeder/src/db/repo/user.ts index 93f108a0d..b21e713e6 100644 --- a/packages/seeder/src/db/repo/user.ts +++ b/packages/seeder/src/db/repo/user.ts @@ -1,5 +1,5 @@ -import { db } from "@echo-webkom/db"; import { accounts, sessions, users, type UserType } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; export const create = async ({ id, diff --git a/packages/seeder/src/sanity/index.ts b/packages/seeder/src/sanity/index.ts index efd41ae34..938728fff 100644 --- a/packages/seeder/src/sanity/index.ts +++ b/packages/seeder/src/sanity/index.ts @@ -1,6 +1,5 @@ import { inArray, sql } from "drizzle-orm"; -import { db } from "@echo-webkom/db"; import { happenings, happeningsToGroups, @@ -9,6 +8,7 @@ import { type HappeningInsert, type QuestionInsert, } from "@echo-webkom/db/schemas"; +import { db } from "@echo-webkom/db/serverless"; import { isBoard } from "@echo-webkom/lib"; import { clientWith, type Dataset } from "@echo-webkom/sanity";