diff options
Diffstat (limited to 'packages/db/src/runtime')
-rw-r--r-- | packages/db/src/runtime/db-client.ts | 2 | ||||
-rw-r--r-- | packages/db/src/runtime/index.ts | 26 | ||||
-rw-r--r-- | packages/db/src/runtime/queries.ts | 53 |
3 files changed, 28 insertions, 53 deletions
diff --git a/packages/db/src/runtime/db-client.ts b/packages/db/src/runtime/db-client.ts index a92f6714a..bd892a4dd 100644 --- a/packages/db/src/runtime/db-client.ts +++ b/packages/db/src/runtime/db-client.ts @@ -9,7 +9,7 @@ const isWebContainer = !!process.versions?.webcontainer; export function createLocalDatabaseClient({ dbUrl }: { dbUrl: string }): LibSQLDatabase { const url = isWebContainer ? 'file:content.db' : dbUrl; - const client = createClient({ url: process.env.TEST_IN_MEMORY_DB ? ':memory:' : url }); + const client = createClient({ url }); const db = drizzleLibsql(client); return db; 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; diff --git a/packages/db/src/runtime/queries.ts b/packages/db/src/runtime/queries.ts index 6a2aff99f..08e2f5e29 100644 --- a/packages/db/src/runtime/queries.ts +++ b/packages/db/src/runtime/queries.ts @@ -1,5 +1,4 @@ -import { LibsqlError } from '@libsql/client'; -import { type SQL, sql } from 'drizzle-orm'; +import { type SQL } from 'drizzle-orm'; import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core'; import { bold } from 'kleur/colors'; import { @@ -7,72 +6,24 @@ import { FOREIGN_KEY_REFERENCES_EMPTY_ERROR, FOREIGN_KEY_REFERENCES_LENGTH_ERROR, REFERENCE_DNE_ERROR, - SEED_DEFAULT_EXPORT_ERROR, - SEED_ERROR, } from '../core/errors.js'; import type { BooleanColumn, ColumnType, DBColumn, DBTable, - DBTables, DateColumn, JsonColumn, NumberColumn, TextColumn, } from '../core/types.js'; -import { type SqliteDB, hasPrimaryKey } from './index.js'; +import { hasPrimaryKey } from './index.js'; import { isSerializedSQL } from './types.js'; const sqlite = new SQLiteAsyncDialect(); export const SEED_DEV_FILE_NAME = ['seed.ts', 'seed.js', 'seed.mjs', 'seed.mts']; -export async function seedLocal({ - db, - tables, - // Glob all potential seed files to catch renames and deletions. - fileGlob, -}: { - db: SqliteDB; - tables: DBTables; - fileGlob: Record<string, () => Promise<{ default?: () => Promise<void> }>>; -}) { - await recreateTables({ db, tables }); - for (const fileName of SEED_DEV_FILE_NAME) { - const key = Object.keys(fileGlob).find((f) => f.endsWith(fileName)); - if (key) { - try { - const mod = await fileGlob[key](); - if (!mod.default) { - throw new Error(SEED_DEFAULT_EXPORT_ERROR(key)); - } - await mod.default(); - } catch (e) { - if (e instanceof LibsqlError) { - throw new Error(SEED_ERROR(e.message)); - } - throw e; - } - break; - } - } -} - -export async function recreateTables({ db, tables }: { db: SqliteDB; tables: DBTables }) { - const setupQueries: SQL[] = []; - for (const [name, table] of Object.entries(tables)) { - const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`); - const createQuery = sql.raw(getCreateTableQuery(name, table)); - const indexQueries = getCreateIndexQueries(name, table); - setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s))); - } - await db.batch([ - db.run(sql`pragma defer_foreign_keys=true;`), - ...setupQueries.map((q) => db.run(q)), - ]); -} - export function getDropTableIfExistsQuery(tableName: string) { return `DROP TABLE IF EXISTS ${sqlite.escapeName(tableName)}`; } |