Skip to content

Commit

Permalink
Fix: remove db.transaction() from types until supported (#10501)
Browse files Browse the repository at this point in the history
* fix: remove transaction from db types

* fix: transaction() runtime error

* chore: changeset
  • Loading branch information
bholmesdev committed Mar 20, 2024
1 parent 2fc7231 commit 4831051
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-years-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Remove `db.transaction()` from type definitions until it is supported by our remote database adapter.
4 changes: 3 additions & 1 deletion packages/db/src/runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import type {
TextColumnOpts,
} from '../core/types.js';

export type { LibSQLDatabase } from 'drizzle-orm/libsql';
import type { LibSQLDatabase } from 'drizzle-orm/libsql';

export type Database = Omit<LibSQLDatabase, 'transaction'>;

function createColumn<S extends string, T extends Record<string, unknown>>(type: S, schema: T) {
return {
Expand Down
14 changes: 13 additions & 1 deletion packages/db/src/runtime/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ import type { InStatement } from '@libsql/client';
import { createClient } from '@libsql/client';
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql';
import { drizzle as drizzleProxy } from 'drizzle-orm/sqlite-proxy';
import { type SqliteRemoteDatabase, drizzle as drizzleProxy } from 'drizzle-orm/sqlite-proxy';
import { z } from 'zod';
import { safeFetch } from './utils.js';

const isWebContainer = !!process.versions?.webcontainer;

function applyTransactionNotSupported(db: SqliteRemoteDatabase) {
Object.assign(db, {
transaction() {
throw new Error(
'`db.transaction()` is not currently supported. We recommend `db.batch()` for automatic error rollbacks across multiple queries.'
);
},
});
}

export function createLocalDatabaseClient({ dbUrl }: { dbUrl: string }): LibSQLDatabase {
const url = isWebContainer ? 'file:content.db' : dbUrl;
const client = createClient({ url });
const db = drizzleLibsql(client);

applyTransactionNotSupported(db);
return db;
}

Expand Down Expand Up @@ -131,5 +142,6 @@ export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string
return results;
}
);
applyTransactionNotSupported(db);
return db;
}
2 changes: 1 addition & 1 deletion packages/db/virtual.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare module 'astro:db' {
type RuntimeConfig = typeof import('./dist/_internal/runtime/config.js');

export const db: import('./dist/_internal/runtime/config.js').LibSQLDatabase;
export const db: import('./dist/_internal/runtime/config.js').Database;
export const dbUrl: string;

export const sql: RuntimeConfig['sql'];
Expand Down

0 comments on commit 4831051

Please sign in to comment.