@@ -4,7 +4,7 @@ import { createConnection as createMySqlConnection } from 'mysql2';
44import { 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' ;
88import { DataSource } from '.' ;
99import { Env } from './'
1010import { MongoClient } from 'mongodb' ;
@@ -35,19 +35,18 @@ export type ConnectionDetails = {
3535
3636async 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
4242function 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.
5150async 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}
0 commit comments