diff --git a/src/app/api/ops/[database_id]/batch/route.ts b/src/app/api/ops/[database_id]/batch/route.ts index df71970b..20b40670 100644 --- a/src/app/api/ops/[database_id]/batch/route.ts +++ b/src/app/api/ops/[database_id]/batch/route.ts @@ -1,8 +1,6 @@ -import { env } from "@/env"; -import { decrypt } from "@/lib/encryption-edge"; import withDatabaseOperation from "@/lib/with-database-ops"; -import { createClient } from "@libsql/client/web"; import { NextResponse } from "next/server"; +import createTursoEdgeClient from "../turso-edge-client"; export const runtime = "edge"; @@ -21,13 +19,7 @@ export const POST = withDatabaseOperation<{ ); } - const url = database.host ?? ""; - const token = await decrypt(env.ENCRYPTION_KEY, database.token ?? ""); - - const client = createClient({ - url, - authToken: token, - }); + const client = await createTursoEdgeClient(database); try { return NextResponse.json({ data: await client.batch(body.batch) }); diff --git a/src/app/api/ops/[database_id]/query/route.ts b/src/app/api/ops/[database_id]/query/route.ts index 36fc9433..0132bbcc 100644 --- a/src/app/api/ops/[database_id]/query/route.ts +++ b/src/app/api/ops/[database_id]/query/route.ts @@ -1,8 +1,6 @@ -import { env } from "@/env"; -import { decrypt } from "@/lib/encryption-edge"; import withDatabaseOperation from "@/lib/with-database-ops"; -import { createClient } from "@libsql/client/web"; import { NextResponse } from "next/server"; +import createTursoEdgeClient from "../turso-edge-client"; export const runtime = "edge"; @@ -19,13 +17,7 @@ export const POST = withDatabaseOperation<{ ); } - const url = database.host ?? ""; - const token = await decrypt(env.ENCRYPTION_KEY, database.token ?? ""); - - const client = createClient({ - url, - authToken: token, - }); + const client = await createTursoEdgeClient(database); try { return NextResponse.json({ diff --git a/src/app/api/ops/[database_id]/schema/route.ts b/src/app/api/ops/[database_id]/schema/route.ts index 152c738c..3b62da82 100644 --- a/src/app/api/ops/[database_id]/schema/route.ts +++ b/src/app/api/ops/[database_id]/schema/route.ts @@ -1,8 +1,6 @@ -import { env } from "@/env"; -import { decrypt } from "@/lib/encryption-edge"; import withDatabaseOperation from "@/lib/with-database-ops"; -import { createClient } from "@libsql/client/web"; import { NextResponse } from "next/server"; +import createTursoEdgeClient from "../turso-edge-client"; export const runtime = "edge"; @@ -10,13 +8,7 @@ export const GET = withDatabaseOperation(async function ({ permission, database, }) { - const url = database.host ?? ""; - const token = await decrypt(env.ENCRYPTION_KEY, database.token ?? ""); - - const client = createClient({ - url, - authToken: token, - }); + const client = await createTursoEdgeClient(database); try { const result = await client.execute("SELECT * FROM sqlite_schema;"); diff --git a/src/app/api/ops/[database_id]/turso-edge-client.ts b/src/app/api/ops/[database_id]/turso-edge-client.ts new file mode 100644 index 00000000..64c28a97 --- /dev/null +++ b/src/app/api/ops/[database_id]/turso-edge-client.ts @@ -0,0 +1,16 @@ +import { database } from "@/db/schema"; +import { env } from "@/env"; +import { decrypt } from "@/lib/encryption-edge"; +import { createClient } from "@libsql/client/web"; + +export default async function createTursoEdgeClient( + db: typeof database.$inferSelect +) { + const url = db.host ?? ""; + const token = await decrypt(env.ENCRYPTION_KEY, db.token ?? ""); + + return createClient({ + url, + authToken: token, + }); +}