(options: {
readonly tables: ReturnType<typeof collectTables>;
readonly backend: TestDatabaseBackend;
readonly dataDir?: string;
})
| 24 | }; |
| 25 | |
| 26 | const makeLazyTestFumaDb = (options: { |
| 27 | readonly tables: ReturnType<typeof collectTables>; |
| 28 | readonly backend: TestDatabaseBackend; |
| 29 | readonly dataDir?: string; |
| 30 | }): TestFumaDb => { |
| 31 | let started: Promise<SqliteTestFumaDb> | undefined; |
| 32 | const start = () => { |
| 33 | if (!started) { |
| 34 | started = import("./sqlite-test-db").then(({ createSqliteTestFumaDb }) => |
| 35 | createSqliteTestFumaDb({ |
| 36 | tables: options.tables, |
| 37 | namespace: "executor_test", |
| 38 | path: options.dataDir ? `${options.dataDir}/test.db` : undefined, |
| 39 | }), |
| 40 | ); |
| 41 | } |
| 42 | return started; |
| 43 | }; |
| 44 | |
| 45 | const internal: FumaDb["internal"] = { |
| 46 | tables: options.tables, |
| 47 | count: async (table, value) => (await start()).db.internal.count(table, value), |
| 48 | create: async (table, values) => (await start()).db.internal.create(table, values), |
| 49 | createMany: async (table, values) => (await start()).db.internal.createMany(table, values), |
| 50 | deleteMany: async (table, value) => (await start()).db.internal.deleteMany(table, value), |
| 51 | findFirst: async (table, value) => (await start()).db.internal.findFirst(table, value), |
| 52 | findMany: async (table, value) => (await start()).db.internal.findMany(table, value), |
| 53 | transaction: async (run) => (await start()).db.internal.transaction(run), |
| 54 | updateMany: async (table, value) => (await start()).db.internal.updateMany(table, value), |
| 55 | upsert: async (table, value) => (await start()).db.internal.upsert(table, value), |
| 56 | }; |
| 57 | |
| 58 | const queryMethods = new Set<PropertyKey>([ |
| 59 | "count", |
| 60 | "create", |
| 61 | "createMany", |
| 62 | "deleteMany", |
| 63 | "findFirst", |
| 64 | "findMany", |
| 65 | "transaction", |
| 66 | "updateMany", |
| 67 | "upsert", |
| 68 | ]); |
| 69 | |
| 70 | const makeDb = (context?: ExecutorOwnerPolicyContext): FumaDb => |
| 71 | new Proxy( |
| 72 | { internal: context === undefined ? internal : { ...internal, context } }, |
| 73 | { |
| 74 | get(target, prop) { |
| 75 | if (prop === "internal") return target.internal; |
| 76 | if (prop === "withContext") { |
| 77 | return (nextContext: ExecutorOwnerPolicyContext) => makeDb(nextContext); |
| 78 | } |
| 79 | if (!queryMethods.has(prop)) return undefined; |
| 80 | |
| 81 | return async (...args: unknown[]) => { |
| 82 | const actual = await start(); |
| 83 | const actualDb = |
no test coverage detected