Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

mobile-runtime-test-setup.ts

87 lines3.0 KB

1import { isAbsolute, resolve } from 'node:path'
2import { pathToFileURL } from 'node:url'
3import type { MobileSQLiteTestDatabaseFactory } from './op-sqlite-test-db'
4​
5const FACTORY_MODULE_ENV_VAR = `TANSTACK_DB_MOBILE_SQLITE_FACTORY_MODULE`
6const FACTORY_EXPORT_ENV_VAR = `TANSTACK_DB_MOBILE_SQLITE_FACTORY_EXPORT`
7const REQUIRE_FACTORY_ENV_VAR = `TANSTACK_DB_MOBILE_REQUIRE_RUNTIME_FACTORY`
8const DEFAULT_FACTORY_EXPORT_NAME = `createMobileSQLiteTestDatabaseFactory`
9​
10type MobileSQLiteFactoryExport =
11 | MobileSQLiteTestDatabaseFactory
12 | (() => MobileSQLiteTestDatabaseFactory)
13 | (() => Promise<MobileSQLiteTestDatabaseFactory>)
14​
15function toImportSpecifier(rawSpecifier: string): string {
16 if (rawSpecifier.startsWith(`.`) || isAbsolute(rawSpecifier)) {
17 const absolutePath = isAbsolute(rawSpecifier)
18 ? rawSpecifier
19 : resolve(process.cwd(), rawSpecifier)
20 return pathToFileURL(absolutePath).href
21 }
22​
23 return rawSpecifier
25​
26async function resolveFactoryFromExport(
27 exportedFactory: unknown,
28): Promise<MobileSQLiteTestDatabaseFactory | null> {
29 if (typeof exportedFactory !== `function`) {
30 return null
31 }
32​
33 const candidate = exportedFactory as MobileSQLiteFactoryExport
34 if (candidate.length === 0) {
35 try {
36 const resolvedFactory = await (
37 candidate as
38 | (() => MobileSQLiteTestDatabaseFactory)
39 | (() => Promise<MobileSQLiteTestDatabaseFactory>)
40 )()
41 if (typeof resolvedFactory === `function`) {
42 return resolvedFactory
43 }
44 } catch {
45 // Some direct factory implementations use defaulted params and report
46 // function.length === 0. Fall back to treating the export itself as the
47 // database factory in that case.
48 }
49 }
50​
51 return candidate as MobileSQLiteTestDatabaseFactory
53​
54globalThis.__tanstackDbCreateMobileSQLiteTestDatabase = undefined
55​
56const runtimeFactoryModule = process.env[FACTORY_MODULE_ENV_VAR]?.trim()
57const requireRuntimeFactory =
58 process.env[REQUIRE_FACTORY_ENV_VAR]?.trim() === `1`
59​
60if (requireRuntimeFactory && !runtimeFactoryModule) {
61 throw new Error(
62 `Missing ${FACTORY_MODULE_ENV_VAR}. ` +
63 `Set it to a module exporting a runtime mobile SQLite test database factory ` +
64 `when ${REQUIRE_FACTORY_ENV_VAR}=1.`,
65 )
67​
68if (runtimeFactoryModule) {
69 const runtimeModule = (await import(
70 toImportSpecifier(runtimeFactoryModule)
71 )) as Record<string, unknown>
72 const factoryExportName =
73 process.env[FACTORY_EXPORT_ENV_VAR]?.trim() || DEFAULT_FACTORY_EXPORT_NAME
74 const selectedExport =
75 runtimeModule[factoryExportName] ?? runtimeModule.default
76 const resolvedFactory = await resolveFactoryFromExport(selectedExport)
77​
78 if (!resolvedFactory) {
79 throw new Error(
80 `Unable to resolve a mobile SQLite test database factory from "${runtimeFactoryModule}". ` +
81 `Expected export "${factoryExportName}" (or default export) to be a database factory ` +
82 `or a zero-argument function that returns one.`,
83 )
84 }
85​
86 globalThis.__tanstackDbCreateMobileSQLiteTestDatabase = resolvedFactory