Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Mar 21, 2024
2 parents bfeaf74 + 253f15c commit 6535cb7
Show file tree
Hide file tree
Showing 42 changed files with 1,176 additions and 632 deletions.
55 changes: 53 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
},
"dependencies": {
"@codemirror/lang-sql": "^6.5.5",
"@dnd-kit/core": "^6.1.0",
"@dnd-kit/sortable": "^8.0.0",
"@lezer/common": "^1.2.1",
"@lezer/lr": "^1.4.0",
"@libsql/client": "^0.5.3",
Expand Down
37 changes: 0 additions & 37 deletions src/app/api/ops/[database_id]/batch/route.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/app/api/ops/[database_id]/handle-batch.ts
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;
29 changes: 29 additions & 0 deletions src/app/api/ops/[database_id]/handle-query.ts
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;
20 changes: 20 additions & 0 deletions src/app/api/ops/[database_id]/handle-schema.ts
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;
30 changes: 30 additions & 0 deletions src/app/api/ops/[database_id]/handle-schemas.ts
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;
33 changes: 33 additions & 0 deletions src/app/api/ops/[database_id]/handle-select-table.ts
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;
23 changes: 23 additions & 0 deletions src/app/api/ops/[database_id]/handle-update-table-data.ts
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;
37 changes: 0 additions & 37 deletions src/app/api/ops/[database_id]/query/route.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/app/api/ops/[database_id]/route.ts
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 });
});
23 changes: 23 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,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);
}
2 changes: 1 addition & 1 deletion src/app/client/page-client.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import MainScreen from "@/components/main-connection";
import DatabaseDriver from "@/drivers/DatabaseDriver";
import DatabaseDriver from "@/drivers/turso-driver";
import { useMemo } from "react";

export default function ClientPageBody() {
Expand Down
Loading

0 comments on commit 6535cb7

Please sign in to comment.