Skip to content

Commit a34e6f9

Browse files
committed
Clean up SDK usage
1 parent 69f1a9e commit a34e6f9

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

src/operation.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createConnection as createMySqlConnection } from 'mysql2';
44
import { createClient as createTursoConnection } from '@libsql/client';
55

66
// Import how we interact with the databases through the Outerbase SDK
7-
import { CloudflareD1Connection, DuckDBConnection, MongoDBConnection, MySQLConnection, PostgreSQLConnection, StarbaseConnection, TursoConnection } from '@outerbase/sdk';
7+
import { CloudflareD1Connection, MongoDBConnection, MySQLConnection, PostgreSQLConnection, StarbaseConnection, TursoConnection } from '@outerbase/sdk';
88
import { DataSource } from '.';
99
import { Env } from './'
1010
import { MongoClient } from 'mongodb';
@@ -35,19 +35,18 @@ export type ConnectionDetails = {
3535

3636
async function afterQuery(sql: string, result: any, isRaw: boolean, dataSource?: DataSource, env?: Env): Promise<any> {
3737
// ## DO NOT REMOVE: TEMPLATE AFTER QUERY HOOK ##
38-
38+
3939
return result;
4040
}
4141

4242
function cleanseQuery(sql: string): string {
4343
return sql.replaceAll('\n', ' ')
4444
}
4545

46-
// NOTE: This is a temporary stop-gap solution to connect to external data sources. Outerbase offers
47-
// an API to handle connecting to a large number of database types in a secure manner. However, the
48-
// goal here is to optimize on query latency from your data sources by connecting to them directly.
49-
// An upcoming update will move the Outerbase SDK to be used in StarbaseDB so this service can connect
50-
// to those database types without being required to funnel requests through the Outerbase API.
46+
// Outerbase API supports more data sources than can be supported via Cloudflare Workers. For those data
47+
// sources we recommend you connect your database to Outerbase and provide the bases API key for queries
48+
// to be made. Otherwise, for supported data sources such as Postgres, MySQL, D1, StarbaseDB, Turso and Mongo
49+
// we can connect to the database directly and remove the additional hop to the Outerbase API.
5150
async function executeExternalQuery(sql: string, params: any, isRaw: boolean, dataSource: DataSource, env?: Env): Promise<any> {
5251
if (!dataSource.externalConnection) {
5352
throw new Error('External connection not found.');
@@ -92,9 +91,13 @@ export async function executeQuery(sql: string, params: any | undefined, isRaw:
9291
const response = await dataSource.internalConnection?.durableObject.executeQuery(sql, params, isRaw);
9392
return await afterQuery(sql, response, isRaw, dataSource, env);
9493
} else {
95-
// TODO: For testing purposes at the moment
94+
// If an Outerbase API key is present, external requests route through the Outerbase API
95+
if (env?.OUTERBASE_API_KEY) {
96+
return executeExternalQuery(sql, params, isRaw, dataSource, env);
97+
}
98+
99+
// Fallback to determining which SDK library to use to query the database via native drivers.
96100
return executeSDKQuery(sql, params, isRaw, dataSource, env);
97-
// return executeExternalQuery(sql, params, isRaw, dataSource, env);
98101
}
99102
}
100103

@@ -243,5 +246,5 @@ export async function executeSDKQuery(sql: string, params: any | undefined, isRa
243246
await db.connect();
244247
const { data } = await db.raw(sql, params);
245248

246-
return data
249+
return await afterQuery(sql, data, isRaw, dataSource, env);
247250
}

worker-configuration.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@
33
interface Env {
44
AUTHORIZATION_TOKEN: "ABC123";
55
REGION: "auto";
6+
STUDIO_USER: "admin";
7+
STUDIO_PASS: "123456";
68
EXTERNAL_DB_TYPE: "postgres";
79
EXTERNAL_DB_HOST: "";
810
EXTERNAL_DB_PORT: 0;
911
EXTERNAL_DB_USER: "";
1012
EXTERNAL_DB_PASS: "";
1113
EXTERNAL_DB_DATABASE: "";
1214
EXTERNAL_DB_DEFAULT_SCHEMA: "public";
15+
EXTERNAL_DB_MONGODB_URI: "";
16+
EXTERNAL_DB_TURSO_URI: "";
17+
EXTERNAL_DB_STARBASEDB_URI: "";
18+
EXTERNAL_DB_STARBASEDB_TOKEN: "";
19+
EXTERNAL_DB_CLOUDFLARE_API_KEY: "";
20+
EXTERNAL_DB_CLOUDFLARE_ACCOUNT_ID: "";
21+
EXTERNAL_DB_CLOUDFLARE_DATABASE_ID: "";
1322
DATABASE_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").DatabaseDurableObject>;
23+
DATA_MASKING: Fetcher;
1424
}

wrangler.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ REGION = "auto"
3333

3434
# Uncomment the section below to create a user for logging into your database UI.
3535
# You can access the Studio UI at: https://your_endpoint/studio
36-
# STUDIO_USER = "admin"
37-
# STUDIO_PASS = "123456"
36+
STUDIO_USER = "admin"
37+
STUDIO_PASS = "123456"
3838

3939
# External database source details
4040
# This enables Starbase to connect to an external data source

0 commit comments

Comments
 (0)