-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
1,176 additions
and
632 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { DatabaseOperationHandler } from "@/lib/with-database-ops"; | ||
import { NextResponse } from "next/server"; | ||
import createTursoEdgeClient from "./turso-edge-client"; | ||
import { RequestOperationBatch } from "@/lib/api/api-request-types"; | ||
|
||
const handleBatchRequest: DatabaseOperationHandler< | ||
RequestOperationBatch | ||
> = async ({ permission, database, body }) => { | ||
if (!permission.canExecuteQuery) { | ||
return NextResponse.json( | ||
{ | ||
error: "No permission to execute query", | ||
}, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
const client = await createTursoEdgeClient(database); | ||
|
||
try { | ||
return NextResponse.json({ data: await client.batch(body.statements) }); | ||
} catch (e) { | ||
return NextResponse.json({ error: (e as Error).message }); | ||
} | ||
}; | ||
|
||
export default handleBatchRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { DatabaseOperationHandler } from "@/lib/with-database-ops"; | ||
import { NextResponse } from "next/server"; | ||
import createTursoEdgeClient from "./turso-edge-client"; | ||
import { RequestOperationQuery } from "@/lib/api/api-request-types"; | ||
|
||
const handleQueryRequest: DatabaseOperationHandler< | ||
RequestOperationQuery | ||
> = async ({ permission, database, body }) => { | ||
if (!permission.canExecuteQuery) { | ||
return NextResponse.json( | ||
{ | ||
error: "No permission to execute query", | ||
}, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
const client = await createTursoEdgeClient(database); | ||
|
||
try { | ||
return NextResponse.json({ | ||
data: await client.execute(body.statement), | ||
}); | ||
} catch (e) { | ||
return NextResponse.json({ error: (e as Error).message }); | ||
} | ||
}; | ||
|
||
export default handleQueryRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { DatabaseOperationHandler } from "@/lib/with-database-ops"; | ||
import { NextResponse } from "next/server"; | ||
import { createTursoEdgeDriver } from "./turso-edge-client"; | ||
import { RequestOperationSchema } from "@/lib/api/api-request-types"; | ||
|
||
const handleSchemaRequest: DatabaseOperationHandler< | ||
RequestOperationSchema | ||
> = async ({ database, body }) => { | ||
const client = await createTursoEdgeDriver(database); | ||
|
||
try { | ||
return NextResponse.json({ | ||
data: await client.tableSchema(body.tableName), | ||
}); | ||
} catch (e) { | ||
return NextResponse.json({ error: (e as Error).message }); | ||
} | ||
}; | ||
|
||
export default handleSchemaRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { DatabaseOperationHandler } from "@/lib/with-database-ops"; | ||
import { NextResponse } from "next/server"; | ||
import { createTursoEdgeDriver } from "./turso-edge-client"; | ||
|
||
const handleSchemasRequest: DatabaseOperationHandler = async ({ | ||
permission, | ||
database, | ||
}) => { | ||
const client = await createTursoEdgeDriver(database); | ||
|
||
try { | ||
let tables = await client.schemas(); | ||
|
||
// If you don't have permssion to execute query | ||
// We will only allow you to see tables that | ||
// you have permission to | ||
if (!permission.canExecuteQuery) { | ||
const allowedTables = new Set(permission.roles.map((t) => t.tableName)); | ||
tables = tables.filter((table) => allowedTables.has(table.name)); | ||
} | ||
|
||
return NextResponse.json({ | ||
data: tables, | ||
}); | ||
} catch (e) { | ||
return NextResponse.json({ error: (e as Error).message }); | ||
} | ||
}; | ||
|
||
export default handleSchemasRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { DatabaseOperationHandler } from "@/lib/with-database-ops"; | ||
import { NextResponse } from "next/server"; | ||
import { createTursoEdgeDriver } from "./turso-edge-client"; | ||
import { RequestOperationSelectTable } from "@/lib/api/api-request-types"; | ||
|
||
const handleSelectTableRequest: DatabaseOperationHandler< | ||
RequestOperationSelectTable | ||
> = async ({ database, body, permission }) => { | ||
const client = await createTursoEdgeDriver(database); | ||
|
||
if (body.whereRaw && !permission.canExecuteQuery) { | ||
return NextResponse.json( | ||
{ | ||
error: "No permission to execute query", | ||
}, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
try { | ||
return NextResponse.json( | ||
await client.selectTable(body.tableName, { | ||
limit: body.limit, | ||
offset: body.offset, | ||
whereRaw: body.whereRaw, | ||
}) | ||
); | ||
} catch (e) { | ||
return NextResponse.json({ error: (e as Error).message }); | ||
} | ||
}; | ||
|
||
export default handleSelectTableRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { DatabaseOperationHandler } from "@/lib/with-database-ops"; | ||
import { NextResponse } from "next/server"; | ||
import { createTursoEdgeDriver } from "./turso-edge-client"; | ||
import { RequestOperationUpdateTableData } from "@/lib/api/api-request-types"; | ||
|
||
const handleUpdateTableDataRequest: DatabaseOperationHandler< | ||
RequestOperationUpdateTableData | ||
> = async ({ database, body }) => { | ||
const client = await createTursoEdgeDriver(database); | ||
const tableName = body.tableName; | ||
|
||
const tableSchema = await client.tableSchema(tableName); | ||
|
||
try { | ||
return NextResponse.json( | ||
await client.updateTableData(tableName, body.ops, tableSchema) | ||
); | ||
} catch (e) { | ||
return NextResponse.json({ error: (e as Error).message }); | ||
} | ||
}; | ||
|
||
export default handleUpdateTableDataRequest; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { NextResponse } from "next/server"; | ||
import withDatabaseOperation from "@/lib/with-database-ops"; | ||
import handleBatchRequest from "./handle-batch"; | ||
import handleQueryRequest from "./handle-query"; | ||
import handleSchemasRequest from "./handle-schemas"; | ||
import handleSelectTableRequest from "./handle-select-table"; | ||
import handleUpdateTableDataRequest from "./handle-update-table-data"; | ||
import handleSchemaRequest from "./handle-schema"; | ||
import { RequestOperationBody } from "@/lib/api/api-request-types"; | ||
|
||
export const POST = withDatabaseOperation<RequestOperationBody>(async function ( | ||
props | ||
) { | ||
const body = props.body; | ||
|
||
if (body.type === "batch") { | ||
return await handleBatchRequest({ ...props, body }); | ||
} else if (body.type === "query") { | ||
return await handleQueryRequest({ ...props, body }); | ||
} else if (body.type === "schemas") { | ||
return await handleSchemasRequest(props); | ||
} else if (body.type === "select-table") { | ||
return await handleSelectTableRequest({ ...props, body }); | ||
} else if (body.type === "update-table-data") { | ||
return await handleUpdateTableDataRequest({ ...props, body }); | ||
} else if (body.type === "schema") { | ||
return await handleSchemaRequest({ ...props, body }); | ||
} | ||
|
||
return NextResponse.json({ error: "Unknown command" }, { status: 500 }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { database } from "@/db/schema"; | ||
import DatabaseDriver from "@/drivers/turso-driver"; | ||
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, | ||
}); | ||
} | ||
|
||
export async function createTursoEdgeDriver(db: typeof database.$inferSelect) { | ||
const url = db.host ?? ""; | ||
const token = await decrypt(env.ENCRYPTION_KEY, db.token ?? ""); | ||
return new DatabaseDriver(url, token); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.