Skip to content

Commit 0a02342

Browse files
committed
refctor: Migrate handler to use config
1 parent 460bc21 commit 0a02342

File tree

13 files changed

+139
-89
lines changed

13 files changed

+139
-89
lines changed

src/allowlist/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Env } from "..";
1+
import { HandlerConfig } from "../handler";
22
import { DataSource } from "../types";
33

44
const parser = new (require('node-sql-parser').Parser)();
@@ -25,15 +25,15 @@ async function loadAllowlist(dataSource?: DataSource): Promise<string[]> {
2525
}
2626
}
2727

28-
export async function isQueryAllowed(sql: string, isEnabled: boolean, dataSource?: DataSource, env?: Env): Promise<boolean | Error> {
28+
export async function isQueryAllowed(sql: string, isEnabled: boolean, dataSource?: DataSource, config?: HandlerConfig): Promise<boolean | Error> {
2929
// If the feature is not turned on then by default the query is allowed
3030
if (!isEnabled) return true;
3131

3232
// If we are using the administrative AUTHORIZATION token value, this request is allowed.
3333
// We want database UI's to be able to have more free reign to run queries so we can load
3434
// tables, run queries, and more. If you want to block queries with the allowlist then we
3535
// advise you to do so by implementing user authentication with JWT.
36-
if (dataSource?.request.headers.get('Authorization') === `Bearer ${env?.ADMIN_AUTHORIZATION_TOKEN}`) {
36+
if (dataSource?.request.headers.get('Authorization') === `Bearer ${config?.adminAuthorizationToken}`) {
3737
return true;
3838
}
3939

src/export/csv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getTableData, createExportResponse } from './index';
22
import { createResponse } from '../utils';
3-
import { DataSource } from '..';
3+
import { DataSource } from '../types';
44

55
export async function exportTableToCsvRoute(
66
tableName: string,

src/export/dump.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { executeOperation } from '.';
2-
import { DataSource } from '..';
2+
import { DataSource } from '../types';
33
import { createResponse } from '../utils';
44

55
export async function dumpDatabaseRoute(

src/export/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DataSource } from "..";
1+
import { DataSource } from "../types";
22
import { executeTransaction } from "../operation";
33

44
export async function executeOperation(queries: { sql: string, params?: any[] }[], dataSource: DataSource): Promise<any> {

src/export/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getTableData, createExportResponse } from './index';
22
import { createResponse } from '../utils';
3-
import { DataSource } from '..';
3+
import { DataSource } from '../types';
44

55
export async function exportTableToJsonRoute(
66
tableName: string,

src/handler.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,45 @@ import { importDumpRoute } from "./import/dump";
1111
import { importTableFromJsonRoute } from "./import/json";
1212
import { importTableFromCsvRoute } from "./import/csv";
1313

14+
export interface HandlerConfig {
15+
adminAuthorizationToken: string;
16+
outerbaseApiKey?: string;
17+
enableAllowlist?: boolean;
18+
enableRls?: boolean;
19+
20+
externalDbType?: string;
21+
22+
externalDbHost?: string;
23+
externalDbPort?: number;
24+
externalDbUser?: string;
25+
externalDbPassword?: string;
26+
externalDbName?: string;
27+
externalDbDefaultSchema?: string;
28+
29+
externalDbMongodbUri?: string;
30+
31+
externalDbTursoUri?: string;
32+
externalDbTursoToken?: string;
33+
34+
externalDbCloudflareApiKey?: string;
35+
externalDbCloudflareAccountId?: string;
36+
externalDbCloudflareDatabaseId?: string;
37+
38+
externalDbStarbaseUri?: string;
39+
externalDbStarbaseToken?: string;
40+
}
41+
1442
export class Handler {
1543
private liteREST?: LiteREST;
1644
private dataSource?: DataSource;
17-
private env?: Env;
45+
private config?: HandlerConfig;
1846

1947
constructor() { }
2048

21-
public async handle(request: Request, dataSource: DataSource, env: Env): Promise<Response> {
49+
public async handle(request: Request, dataSource: DataSource, config: HandlerConfig): Promise<Response> {
2250
this.dataSource = dataSource;
23-
this.liteREST = new LiteREST(dataSource, env);
24-
this.env = env;
51+
this.liteREST = new LiteREST(dataSource, config);
52+
this.config = config;
2553
const url = new URL(request.url);
2654

2755
if (request.method === 'POST' && url.pathname === '/query/raw') {
@@ -113,15 +141,15 @@ export class Handler {
113141
return { sql, params };
114142
});
115143

116-
const response = await executeTransaction(queries, isRaw, this.dataSource, this.env);
144+
const response = await executeTransaction(queries, isRaw, this.dataSource, this.config);
117145
return createResponse(response, undefined, 200);
118146
} else if (typeof sql !== 'string' || !sql.trim()) {
119147
return createResponse(undefined, 'Invalid or empty "sql" field.', 400);
120148
} else if (params !== undefined && !Array.isArray(params) && typeof params !== 'object') {
121149
return createResponse(undefined, 'Invalid "params" field. Must be an array or object.', 400);
122150
}
123151

124-
const response = await executeQuery(sql, params, isRaw, this.dataSource, this.env);
152+
const response = await executeQuery(sql, params, isRaw, this.dataSource, this.config);
125153
return createResponse(response, undefined, 200);
126154
} catch (error: any) {
127155
console.error('Query Route Error:', error);

src/import/csv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createResponse } from '../utils';
2-
import { DataSource } from '..';
2+
import { DataSource } from '../types';
33
import { executeOperation } from '../export';
44

55
interface ColumnMapping {

src/import/dump.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createResponse } from '../utils';
2-
import { DataSource } from '..';
2+
import { DataSource } from '../types';
33
import { executeOperation } from '../export';
44

55
function parseSqlStatements(sqlContent: string): string[] {

src/import/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createResponse } from '../utils';
22
import { executeOperation } from '../export';
3-
import { DataSource } from '..';
3+
import { DataSource } from '../types';
44

55
interface ColumnMapping {
66
[key: string]: string;

src/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createResponse } from './utils';
22
import handleStudioRequest from "./studio";
3-
import { Handler } from "./handler";
3+
import { Handler, HandlerConfig } from "./handler";
44
import { DatabaseStub, DataSource, RegionLocationHint, Source } from './types';
55
import { corsPreflight } from './cors';
66
export { DatabaseDurableObject } from './do';
@@ -163,8 +163,30 @@ export default {
163163
// Non-blocking operation to remove expired cache entries from our DO
164164
expireCache(dataSource)
165165

166+
const config = {
167+
adminAuthorizationToken: env.ADMIN_AUTHORIZATION_TOKEN,
168+
outerbaseApiKey: env.OUTERBASE_API_KEY,
169+
enableAllowlist: env.ENABLE_ALLOWLIST,
170+
enableRls: env.ENABLE_RLS,
171+
externalDbType: env.EXTERNAL_DB_TYPE,
172+
externalDbHost: env.EXTERNAL_DB_HOST,
173+
externalDbPort: env.EXTERNAL_DB_PORT,
174+
externalDbUser: env.EXTERNAL_DB_USER,
175+
externalDbPassword: env.EXTERNAL_DB_PASS,
176+
externalDbName: env.EXTERNAL_DB_DATABASE,
177+
externalDbDefaultSchema: env.EXTERNAL_DB_DEFAULT_SCHEMA,
178+
externalDbMongodbUri: env.EXTERNAL_DB_MONGODB_URI,
179+
externalDbTursoUri: env.EXTERNAL_DB_TURSO_URI,
180+
externalDbTursoToken: env.EXTERNAL_DB_TURSO_TOKEN,
181+
externalDbCloudflareApiKey: env.EXTERNAL_DB_CLOUDFLARE_API_KEY,
182+
externalDbCloudflareAccountId: env.EXTERNAL_DB_CLOUDFLARE_ACCOUNT_ID,
183+
externalDbCloudflareDatabaseId: env.EXTERNAL_DB_CLOUDFLARE_DATABASE_ID,
184+
externalDbStarbaseUri: env.EXTERNAL_DB_STARBASEDB_URI,
185+
externalDbStarbaseToken: env.EXTERNAL_DB_STARBASEDB_TOKEN,
186+
} satisfies HandlerConfig;
187+
166188
// Return the final response to our user
167-
return await new Handler().handle(request, dataSource, env);
189+
return await new Handler().handle(request, dataSource, config);
168190
} catch (error) {
169191
// Return error response to client
170192
return createResponse(

0 commit comments

Comments
 (0)