Skip to content

Commit

Permalink
refactor: simplify turso edge connection
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Mar 21, 2024
1 parent 42f4cb9 commit 794c886
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
12 changes: 2 additions & 10 deletions src/app/api/ops/[database_id]/batch/route.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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) });
Expand Down
12 changes: 2 additions & 10 deletions src/app/api/ops/[database_id]/query/route.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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({
Expand Down
12 changes: 2 additions & 10 deletions src/app/api/ops/[database_id]/schema/route.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
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";

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;");
Expand Down
16 changes: 16 additions & 0 deletions src/app/api/ops/[database_id]/turso-edge-client.ts
Original file line number Diff line number Diff line change
@@ -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,
});
}

0 comments on commit 794c886

Please sign in to comment.