Skip to content

Commit

Permalink
feat: add trigger to remote api
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Apr 10, 2024
1 parent d68d6fe commit 8c73bf4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/app/api/ops/[database_id]/handle-trigger.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 { RequestOperationTrigger } from "@/lib/api/api-request-types";

const handleTriggerRequest: DatabaseOperationHandler<
RequestOperationTrigger
> = async ({ database, body }) => {
const client = await createTursoEdgeDriver(database);

try {
return NextResponse.json({
data: await client.trigger(body.name),
});
} catch (e) {
return NextResponse.json({ error: (e as Error).message });
}
};

export default handleTriggerRequest;
3 changes: 3 additions & 0 deletions src/app/api/ops/[database_id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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";
import handleTriggerRequest from "./handle-trigger";

export const runtime = "edge";

Expand All @@ -27,6 +28,8 @@ export const POST = withDatabaseOperation<RequestOperationBody>(async function (
return await handleUpdateTableDataRequest({ ...props, body });
} else if (body.type === "schema") {
return await handleSchemaRequest({ ...props, body });
} else if (body.type === "trigger") {
return await handleTriggerRequest({ ...props, body });
}

return NextResponse.json({ error: "Unknown command" }, { status: 500 });
Expand Down
7 changes: 7 additions & 0 deletions src/drivers/remote-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
DatabaseTableOperation,
DatabaseTableOperationReslt,
DatabaseTableSchema,
DatabaseTriggerSchema,
SelectFromTableOptions,
} from "./base-driver";
import {
ApiOpsBatchResponse,
ApiOpsQueryResponse,
ApiSchemaListResponse,
ApiSchemaResponse,
ApiTriggerResponse,
} from "@/lib/api-response-types";
import { RequestOperationBody } from "@/lib/api/api-request-types";

Expand Down Expand Up @@ -76,6 +78,11 @@ export default class RemoteDriver implements BaseDriver {
).data;
}

async trigger(name: string): Promise<DatabaseTriggerSchema> {
return (await this.request<ApiTriggerResponse>({ type: "trigger", name }))
.data;
}

async updateTableData(
tableName: string,
ops: DatabaseTableOperation[]
Expand Down
6 changes: 6 additions & 0 deletions src/lib/api-response-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DatabaseResultSet,
DatabaseSchemaItem,
DatabaseTableSchema,
DatabaseTriggerSchema,
} from "@/drivers/base-driver";

export interface ApiOpsBatchResponse {
Expand Down Expand Up @@ -32,3 +33,8 @@ export interface ApiSchemaResponse {
data: DatabaseTableSchema;
error?: string;
}

export interface ApiTriggerResponse {
data: DatabaseTriggerSchema;
error?: string;
}
14 changes: 13 additions & 1 deletion src/lib/api/api-request-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ export interface RequestOperationSchema {
tableName: string;
}

export interface RequestTriggerSchema {
type: "trigger";
name: string;
}

export interface RequestOperationTrigger {
type: "trigger";
name: string;
}

export interface RequestOperationSelectTable {
type: "select-table";
tableName: string;
Expand All @@ -41,4 +51,6 @@ export type RequestOperationBody =
| RequestOperationSchemas
| RequestOperationSelectTable
| RequestOperationUpdateTableData
| RequestOperationSchema;
| RequestOperationSchema
| RequestOperationTrigger
| RequestTriggerSchema;

0 comments on commit 8c73bf4

Please sign in to comment.