Skip to content

Commit 01898d6

Browse files
committed
Fix REST endpoints
1 parent a34e6f9 commit 01898d6

File tree

5 files changed

+48
-50
lines changed

5 files changed

+48
-50
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface Env {
2828
// MongoDB specific
2929
EXTERNAL_DB_MONGODB_URI?: string;
3030
EXTERNAL_DB_TURSO_URI?: string;
31+
EXTERNAL_DB_TURSO_TOKEN?: string;
3132
EXTERNAL_DB_STARBASEDB_URI?: string;
3233
EXTERNAL_DB_STARBASEDB_TOKEN?: string;
3334
EXTERNAL_DB_CLOUDFLARE_API_KEY?: string;

src/literest/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,19 @@ export class LiteREST {
5454
}
5555
}
5656

57+
const isSQLite = this.dataSource.source === Source.internal || this.env.EXTERNAL_DB_TYPE?.toLowerCase() === 'sqlite'
5758
const schemaInfo = (await executeQuery(query, this.dataSource.source === Source.external ? {} : [], false, this.dataSource, this.env)) as any[];
58-
const pkColumns = schemaInfo.map(col => col.name as string);
59+
60+
let pkColumns = []
61+
62+
if (isSQLite) {
63+
pkColumns = schemaInfo
64+
.filter(col => typeof col.pk === 'number' && col.pk > 0 && col.name !== null)
65+
.map(col => col.name as string);
66+
} else {
67+
pkColumns = schemaInfo.map(col => col.name as string);
68+
}
69+
5970
return pkColumns;
6071
}
6172

@@ -175,8 +186,8 @@ export class LiteREST {
175186
}
176187

177188
const tableName = this.sanitizeIdentifier(pathParts.length === 1 ? pathParts[0] : pathParts[1]);
178-
const schemaName = pathParts.length === 1 ? undefined : this.sanitizeIdentifier(pathParts[0])
179-
const id = pathParts.length === 1 ? pathParts[1] : pathParts[2];
189+
const schemaName = this.sanitizeIdentifier(pathParts[0]) //pathParts.length === 1 ? undefined : this.sanitizeIdentifier(pathParts[0])
190+
const id = pathParts.length === 3 ? pathParts[2] : undefined; // pathParts.length === 3 ? pathParts[1] : pathParts[2];
180191
const body = ['POST', 'PUT', 'PATCH'].includes(liteRequest.method) ? await liteRequest.json() : undefined;
181192

182193
return {
@@ -397,7 +408,9 @@ export class LiteREST {
397408
private async handleDelete(tableName: string, schemaName: string | undefined, id: string | undefined): Promise<Response> {
398409
const pkColumns = await this.getPrimaryKeyColumns(tableName, schemaName);
399410

400-
const { conditions: pkConditions, params: pkParams, error } = this.getPrimaryKeyConditions(pkColumns, id, {}, new URLSearchParams());
411+
let data: any = {}
412+
data[pkColumns[0]] = id
413+
const { conditions: pkConditions, params: pkParams, error } = this.getPrimaryKeyConditions(pkColumns, id, data, new URLSearchParams());
401414

402415
if (error) {
403416
console.error('DELETE Operation Error:', error);

src/operation.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Import the native Node libraries for connecting to various databases
22
import { Client as PgClient } from 'pg';
33
import { createConnection as createMySqlConnection } from 'mysql2';
4-
import { createClient as createTursoConnection } from '@libsql/client';
4+
import { createClient as createTursoConnection } from '@libsql/client/web';
55

66
// Import how we interact with the databases through the Outerbase SDK
77
import { CloudflareD1Connection, MongoDBConnection, MySQLConnection, PostgreSQLConnection, StarbaseConnection, TursoConnection } from '@outerbase/sdk';
@@ -52,6 +52,11 @@ async function executeExternalQuery(sql: string, params: any, isRaw: boolean, da
5252
throw new Error('External connection not found.');
5353
}
5454

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+
5560
// Convert params from array to object if needed
5661
let convertedParams = params;
5762
if (Array.isArray(params)) {
@@ -91,13 +96,7 @@ export async function executeQuery(sql: string, params: any | undefined, isRaw:
9196
const response = await dataSource.internalConnection?.durableObject.executeQuery(sql, params, isRaw);
9297
return await afterQuery(sql, response, isRaw, dataSource, env);
9398
} 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);
101100
}
102101
}
103102

@@ -180,9 +179,10 @@ async function createSDKMongoConnection(env: Env): Promise<ConnectionDetails> {
180179
}
181180

182181
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+
}));
186186

187187
return {
188188
database: client,
@@ -229,7 +229,7 @@ export async function executeSDKQuery(sql: string, params: any | undefined, isRa
229229
} else if (env?.EXTERNAL_DB_TYPE === 'mysql' && env) {
230230
const { database } = await createSDKMySQLConnection(env)
231231
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) {
233233
const { database } = await createSDKTursoConnection(env)
234234
db = database
235235
} else if (env?.EXTERNAL_DB_TYPE === 'mongo' && env) {

worker-configuration.d.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,5 @@
33
interface Env {
44
AUTHORIZATION_TOKEN: "ABC123";
55
REGION: "auto";
6-
STUDIO_USER: "admin";
7-
STUDIO_PASS: "123456";
8-
EXTERNAL_DB_TYPE: "postgres";
9-
EXTERNAL_DB_HOST: "";
10-
EXTERNAL_DB_PORT: 0;
11-
EXTERNAL_DB_USER: "";
12-
EXTERNAL_DB_PASS: "";
13-
EXTERNAL_DB_DATABASE: "";
14-
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: "";
226
DATABASE_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").DatabaseDurableObject>;
23-
DATA_MASKING: Fetcher;
247
}

wrangler.toml

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,25 @@ 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
4141
# OUTERBASE_API_KEY = ""
42-
EXTERNAL_DB_TYPE = "postgres"
43-
EXTERNAL_DB_HOST = ""
44-
EXTERNAL_DB_PORT = 0
45-
EXTERNAL_DB_USER = ""
46-
EXTERNAL_DB_PASS = ""
47-
EXTERNAL_DB_DATABASE = ""
48-
EXTERNAL_DB_DEFAULT_SCHEMA = "public"
49-
50-
EXTERNAL_DB_MONGODB_URI = ""
51-
EXTERNAL_DB_TURSO_URI = ""
52-
EXTERNAL_DB_STARBASEDB_URI = ""
53-
EXTERNAL_DB_STARBASEDB_TOKEN = ""
54-
EXTERNAL_DB_CLOUDFLARE_API_KEY = ""
55-
EXTERNAL_DB_CLOUDFLARE_ACCOUNT_ID = ""
56-
EXTERNAL_DB_CLOUDFLARE_DATABASE_ID = ""
42+
# EXTERNAL_DB_TYPE = "sqlite"
43+
# EXTERNAL_DB_HOST = ""
44+
# EXTERNAL_DB_PORT = 0
45+
# EXTERNAL_DB_USER = ""
46+
# EXTERNAL_DB_PASS = ""
47+
# EXTERNAL_DB_DATABASE = ""
48+
# EXTERNAL_DB_DEFAULT_SCHEMA = "public"
49+
50+
# EXTERNAL_DB_MONGODB_URI = ""
51+
# EXTERNAL_DB_TURSO_URI = ""
52+
# EXTERNAL_DB_TURSO_TOKEN = ""
53+
# EXTERNAL_DB_STARBASEDB_URI = ""
54+
# EXTERNAL_DB_STARBASEDB_TOKEN = ""
55+
# EXTERNAL_DB_CLOUDFLARE_API_KEY = ""
56+
# EXTERNAL_DB_CLOUDFLARE_ACCOUNT_ID = ""
57+
# EXTERNAL_DB_CLOUDFLARE_DATABASE_ID = ""

0 commit comments

Comments
 (0)