summaryrefslogtreecommitdiff
path: root/packages/db/src/runtime/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/db/src/runtime/index.ts')
-rw-r--r--packages/db/src/runtime/index.ts26
1 files changed, 25 insertions, 1 deletions
diff --git a/packages/db/src/runtime/index.ts b/packages/db/src/runtime/index.ts
index 501ae7a22..22958c7da 100644
--- a/packages/db/src/runtime/index.ts
+++ b/packages/db/src/runtime/index.ts
@@ -11,12 +11,36 @@ import {
} from 'drizzle-orm/sqlite-core';
import { type DBColumn, type DBTable } from '../core/types.js';
import { type SerializedSQL, isSerializedSQL } from './types.js';
+import { SEED_DEFAULT_EXPORT_ERROR, SEED_ERROR } from '../core/errors.js';
+import { LibsqlError } from '@libsql/client';
export { sql };
export type SqliteDB = LibSQLDatabase;
export type { Table } from './types.js';
export { createRemoteDatabaseClient, createLocalDatabaseClient } from './db-client.js';
-export { seedLocal } from './queries.js';
+
+export async function seedLocal({
+ // Glob all potential seed files to catch renames and deletions.
+ fileGlob,
+}: {
+ fileGlob: Record<string, { default?: () => Promise<void> }>;
+}) {
+ const seedFilePath = Object.keys(fileGlob)[0];
+ if (!seedFilePath) return;
+ const mod = fileGlob[seedFilePath];
+
+ if (!mod.default) {
+ throw new Error(SEED_DEFAULT_EXPORT_ERROR(seedFilePath));
+ }
+ try {
+ await mod.default();
+ } catch (e) {
+ if (e instanceof LibsqlError) {
+ throw new Error(SEED_ERROR(e.message));
+ }
+ throw e;
+ }
+}
export function hasPrimaryKey(column: DBColumn) {
return 'primaryKey' in column.schema && !!column.schema.primaryKey;