|
1 | 1 | // Import the native Node libraries for connecting to various databases |
2 | 2 | import { Client as PgClient } from 'pg'; |
3 | 3 | import { createConnection as createMySqlConnection } from 'mysql2'; |
4 | | -import { createClient as createTursoConnection } from '@libsql/client'; |
| 4 | +import { createClient as createTursoConnection } from '@libsql/client/web'; |
5 | 5 |
|
6 | 6 | // Import how we interact with the databases through the Outerbase SDK |
7 | 7 | import { CloudflareD1Connection, MongoDBConnection, MySQLConnection, PostgreSQLConnection, StarbaseConnection, TursoConnection } from '@outerbase/sdk'; |
@@ -52,6 +52,11 @@ async function executeExternalQuery(sql: string, params: any, isRaw: boolean, da |
52 | 52 | throw new Error('External connection not found.'); |
53 | 53 | } |
54 | 54 |
|
| 55 | + // If not an Outerbase API request, forward to external database. |
| 56 | + if (!env?.OUTERBASE_API_KEY) { |
| 57 | + return await executeSDKQuery(sql, params, isRaw, dataSource, env); |
| 58 | + } |
| 59 | + |
55 | 60 | // Convert params from array to object if needed |
56 | 61 | let convertedParams = params; |
57 | 62 | if (Array.isArray(params)) { |
@@ -91,13 +96,7 @@ export async function executeQuery(sql: string, params: any | undefined, isRaw: |
91 | 96 | const response = await dataSource.internalConnection?.durableObject.executeQuery(sql, params, isRaw); |
92 | 97 | return await afterQuery(sql, response, isRaw, dataSource, env); |
93 | 98 | } else { |
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. |
100 | | - return executeSDKQuery(sql, params, isRaw, dataSource, env); |
| 99 | + return executeExternalQuery(sql, params, isRaw, dataSource, env); |
101 | 100 | } |
102 | 101 | } |
103 | 102 |
|
@@ -180,9 +179,10 @@ async function createSDKMongoConnection(env: Env): Promise<ConnectionDetails> { |
180 | 179 | } |
181 | 180 |
|
182 | 181 | async function createSDKTursoConnection(env: Env): Promise<ConnectionDetails> { |
183 | | - const client = new TursoConnection( |
184 | | - createTursoConnection({ url: env.EXTERNAL_DB_TURSO_URI || '' }) |
185 | | - ); |
| 182 | + const client = new TursoConnection(createTursoConnection({ |
| 183 | + url: env.EXTERNAL_DB_TURSO_URI || '', |
| 184 | + authToken: env.EXTERNAL_DB_TURSO_TOKEN || '' |
| 185 | + })); |
186 | 186 |
|
187 | 187 | return { |
188 | 188 | database: client, |
@@ -229,7 +229,7 @@ export async function executeSDKQuery(sql: string, params: any | undefined, isRa |
229 | 229 | } else if (env?.EXTERNAL_DB_TYPE === 'mysql' && env) { |
230 | 230 | const { database } = await createSDKMySQLConnection(env) |
231 | 231 | db = database |
232 | | - } else if (env?.EXTERNAL_DB_TYPE === 'turso' && env) { |
| 232 | + } else if (env?.EXTERNAL_DB_TYPE === 'sqlite' && env?.EXTERNAL_DB_TURSO_URI && env) { |
233 | 233 | const { database } = await createSDKTursoConnection(env) |
234 | 234 | db = database |
235 | 235 | } else if (env?.EXTERNAL_DB_TYPE === 'mongo' && env) { |
|
0 commit comments