| 32 | } |
| 33 | |
| 34 | export const createSqliteTestFumaDb = async <const TTables extends FumaTables>( |
| 35 | options: CreateSqliteTestFumaDbOptions<TTables>, |
| 36 | ): Promise<SqliteTestFumaDb<TTables>> => { |
| 37 | const version = options.version ?? "1.0.0"; |
| 38 | const namespace = options.namespace ?? "executor_test"; |
| 39 | if (options.path && options.path !== ":memory:") { |
| 40 | mkdirSync(dirname(options.path), { recursive: true }); |
| 41 | } |
| 42 | // libSQL `:memory:` is a single connection per client, matching the test's |
| 43 | // single-handle expectation. foreign_keys is per-connection (no shared |
| 44 | // handle to inherit it), so set it on this one. |
| 45 | const url = |
| 46 | !options.path || options.path === ":memory:" ? ":memory:" : `file:${resolve(options.path)}`; |
| 47 | const client = createClient({ url }); |
| 48 | await client.execute("PRAGMA foreign_keys = ON"); |
| 49 | |
| 50 | const schema = createDrizzleRuntimeSchemaFromTables({ |
| 51 | tables: options.tables, |
| 52 | namespace, |
| 53 | version, |
| 54 | provider: "sqlite", |
| 55 | }); |
| 56 | const drizzleDb = drizzle({ client, schema }); |
| 57 | |
| 58 | for (const statement of createDrizzleRuntimeSchemaSqlFromTables({ |
| 59 | tables: options.tables, |
| 60 | namespace, |
| 61 | version, |
| 62 | provider: "sqlite", |
| 63 | })) { |
| 64 | await client.execute(statement); |
| 65 | } |
| 66 | |
| 67 | const { db, fuma } = createExecutorFumaDb(drizzleDb, { |
| 68 | tables: options.tables, |
| 69 | namespace, |
| 70 | version, |
| 71 | provider: "sqlite", |
| 72 | }); |
| 73 | |
| 74 | return { |
| 75 | db, |
| 76 | fuma, |
| 77 | drizzle: drizzleDb, |
| 78 | client, |
| 79 | close: async () => { |
| 80 | client.close(); |
| 81 | }, |
| 82 | }; |
| 83 | }; |