mirror of
https://github.com/withastro/astro.git
synced 2025-01-27 22:19:04 -05:00
Fix: remove db.transaction()
from types until supported (#10501)
* fix: remove transaction from db types * fix: transaction() runtime error * chore: changeset
This commit is contained in:
parent
2fc7231df2
commit
4831051260
4 changed files with 22 additions and 3 deletions
5
.changeset/grumpy-years-lick.md
Normal file
5
.changeset/grumpy-years-lick.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@astrojs/db": patch
|
||||
---
|
||||
|
||||
Remove `db.transaction()` from type definitions until it is supported by our remote database adapter.
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -131,5 +142,6 @@ export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string
|
|||
return results;
|
||||
}
|
||||
);
|
||||
applyTransactionNotSupported(db);
|
||||
return db;
|
||||
}
|
||||
|
|
2
packages/db/virtual.d.ts
vendored
2
packages/db/virtual.d.ts
vendored
|
@ -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'];
|
||||
|
|
Loading…
Add table
Reference in a new issue