-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use remote connection instead of pass real credential to client (…
- Loading branch information
Showing
8 changed files
with
136 additions
and
63 deletions.
There are no files selected for viewing
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,34 @@ | ||
import { RequestOperationBody } from "@/lib/api/api-request-types"; | ||
import { DatabaseOperationHandler } 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 handleTriggerRequest from "./handle-trigger"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export const databaseOperationHandler: DatabaseOperationHandler< | ||
RequestOperationBody | ||
> = async (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 }); | ||
} else if (body.type === "trigger") { | ||
return await handleTriggerRequest({ ...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 |
---|---|---|
@@ -1,36 +1,9 @@ | ||
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"; | ||
import handleTriggerRequest from "./handle-trigger"; | ||
import { databaseOperationHandler } from "./handler"; | ||
|
||
export const runtime = "edge"; | ||
|
||
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 }); | ||
} else if (body.type === "trigger") { | ||
return await handleTriggerRequest({ ...props, body }); | ||
} | ||
|
||
return NextResponse.json({ error: "Unknown command" }, { status: 500 }); | ||
} | ||
databaseOperationHandler | ||
); |
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,78 @@ | ||
import { headers } from "next/headers"; | ||
import { databaseOperationHandler } from "../../ops/[database_id]/handler"; | ||
import { get_database } from "@/db"; | ||
import { eq } from "drizzle-orm"; | ||
import { dbTempSession } from "@/db/schema-temp-session"; | ||
import withErrorHandler from "@/lib/with-error-handler"; | ||
import { ApiError } from "@/lib/api-error"; | ||
import { HttpStatus } from "@/constants/http-status"; | ||
import parseSafeJson from "@/lib/json-safe"; | ||
import { SavedConnectionItemConfigConfig } from "@/app/connect/saved-connection-storage"; | ||
|
||
export const runtime = "edge"; | ||
|
||
export const POST = withErrorHandler<{ params: { session_id: string } }>( | ||
async function ({ req, params }) { | ||
const db = get_database(); | ||
const session = await db.query.dbTempSession.findFirst({ | ||
where: eq(dbTempSession.id, params.params.session_id), | ||
}); | ||
|
||
if (!session) { | ||
throw new ApiError({ | ||
message: "Session does not exist", | ||
status: HttpStatus.BAD_REQUEST, | ||
}); | ||
} | ||
|
||
const now = Math.floor(Date.now() / 1000); | ||
if ((session.expiredAt ?? 0) < now) { | ||
throw new ApiError({ | ||
message: "Session has expired", | ||
status: HttpStatus.BAD_REQUEST, | ||
}); | ||
} | ||
|
||
const body = | ||
headers().get("Content-Type") === "application/json" | ||
? await req.json() | ||
: {}; | ||
|
||
const config = parseSafeJson<SavedConnectionItemConfigConfig | null>( | ||
session.credential ?? "", | ||
null | ||
); | ||
|
||
if (config === null) { | ||
throw new ApiError({ | ||
message: "Problem parsing the credential", | ||
status: HttpStatus.BAD_REQUEST, | ||
}); | ||
} | ||
|
||
return await databaseOperationHandler({ | ||
body, | ||
permission: { canExecuteQuery: true, isOwner: true, roles: [] }, | ||
database: { | ||
driver: session.driver, | ||
host: config.url, | ||
password: config.password ?? null, | ||
token: config.token, | ||
color: null, | ||
createdAt: null, | ||
deletedAt: null, | ||
description: "", | ||
id: "", | ||
name: "", | ||
userId: "", | ||
username: config.username ?? null, | ||
}, | ||
user: { | ||
id: "0", | ||
name: "Guest", | ||
picture: "", | ||
storageUsage: 0, | ||
}, | ||
}); | ||
} | ||
); |
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 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 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 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 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