|
1 | 1 | import type { DataSource } from "../../data-source/DataSource" |
2 | 2 | import { DriverPackageNotInstalledError, TypeORMError } from "../../error" |
| 3 | +import { PlatformTools } from "../../platform/PlatformTools" |
3 | 4 | import type { QueryRunner } from "../../query-runner/QueryRunner" |
4 | 5 | import { AbstractSqliteDriver } from "../sqlite-abstract/AbstractSqliteDriver" |
5 | 6 | import type { ExpoDataSourceOptions } from "./ExpoDataSourceOptions" |
6 | 7 | import { ExpoQueryRunner } from "./ExpoQueryRunner" |
7 | 8 |
|
8 | | -// Node raises `MODULE_NOT_FOUND` when the requested module can't be resolved, |
9 | | -// but the same code also fires for transitive failures (a dependency of |
10 | | -// `expo-sqlite` that's missing, a `require` inside the module body). Node's |
11 | | -// error message has the shape: |
12 | | -// |
13 | | -// Cannot find module '<name>' |
14 | | -// Require stack: |
15 | | -// - /node_modules/<caller>/index.js |
16 | | -// - ... |
17 | | -// |
18 | | -// The stack lists the calling modules and can include `expo-sqlite` when the |
19 | | -// unresolved module is a transitive dependency — so matching the whole |
20 | | -// message would misreport such failures as "install expo-sqlite". Only the |
21 | | -// first line identifies the actually-missing module, so match against that. |
22 | | -const isExpoSqliteNotFoundError = (err: unknown): boolean => { |
23 | | - if (typeof err !== "object" || err === null) return false |
24 | | - const e = err as { code?: string; message?: string } |
25 | | - if (e.code !== "MODULE_NOT_FOUND") return false |
26 | | - if (typeof e.message !== "string") return false |
27 | | - const firstLine = e.message.split("\n", 1)[0] |
28 | | - return firstLine.startsWith("Cannot find module 'expo-sqlite'") |
29 | | -} |
30 | | - |
31 | 9 | export class ExpoDriver extends AbstractSqliteDriver { |
32 | 10 | declare options: ExpoDataSourceOptions |
33 | 11 |
|
@@ -56,50 +34,23 @@ export class ExpoDriver extends AbstractSqliteDriver { |
56 | 34 | } |
57 | 35 |
|
58 | 36 | /** |
59 | | - * Loads the `expo-sqlite` module when `options.driver` is not set. Extracted |
60 | | - * as a method so tests can stub the module resolution without depending on |
61 | | - * whether `expo-sqlite` happens to be installed in the workspace. |
62 | | - * |
63 | | - * A literal `require("expo-sqlite")` is used so Metro's static bundler picks |
64 | | - * the dependency up at build time — `PlatformTools.load()` is a throwing |
65 | | - * stub in the browser/React-Native build and cannot be used here. |
66 | | - */ |
67 | | - protected requireExpoSqlite(): unknown { |
68 | | - return require("expo-sqlite") |
69 | | - } |
70 | | - |
71 | | - /** |
72 | | - * If driver dependency is not given explicitly, resolve it via |
73 | | - * `requireExpoSqlite()` and validate that the loaded module exposes the |
74 | | - * modern async API introduced in Expo SDK v52. |
| 37 | + * If driver dependency is not given explicitly, then try to load it via "require". |
75 | 38 | */ |
76 | 39 | protected loadDependencies(): void { |
77 | | - if (this.options.driver) { |
78 | | - this.sqlite = this.options.driver |
79 | | - } else { |
80 | | - try { |
81 | | - this.sqlite = this.requireExpoSqlite() |
82 | | - } catch (err) { |
83 | | - if (isExpoSqliteNotFoundError(err)) { |
84 | | - throw new DriverPackageNotInstalledError( |
85 | | - "Expo SQLite", |
86 | | - "expo-sqlite", |
87 | | - ) |
88 | | - } |
89 | | - throw err |
90 | | - } |
| 40 | + try { |
| 41 | + this.sqlite = |
| 42 | + this.options.driver ?? PlatformTools.load("expo-sqlite") |
| 43 | + } catch { |
| 44 | + throw new DriverPackageNotInstalledError( |
| 45 | + "Expo SQLite", |
| 46 | + "expo-sqlite", |
| 47 | + ) |
91 | 48 | } |
92 | 49 |
|
93 | | - // Expo SDK v52 removed the legacy synchronous API. The modern async API |
94 | | - // exposes `openDatabaseAsync` as a function — anything else (missing, |
95 | | - // non-callable, non-object `driver`) means the user is on a pre-v52 SDK |
96 | | - // or has passed something that isn't the expo-sqlite module. |
97 | | - if (typeof this.sqlite?.openDatabaseAsync !== "function") { |
98 | | - const hint = this.options.driver |
99 | | - ? "check that the provided `driver` exposes `openDatabaseAsync` — custom overrides must match the expo-sqlite v52+ surface" |
100 | | - : "upgrade to Expo SDK v52 or later, which ships the modern async SQLite API" |
| 50 | + // The modern Expo SQLite API that TypeORM supports exposes `openDatabaseAsync` as a function |
| 51 | + if (typeof this.sqlite.openDatabaseAsync !== "function") { |
101 | 52 | throw new TypeORMError( |
102 | | - `Legacy Expo SQLite driver is not supported — ${hint}.`, |
| 53 | + `The provided Expo SQLite client is not supported. Please upgrade your Expo SDK v52 or higher!`, |
103 | 54 | ) |
104 | 55 | } |
105 | 56 | } |
|
0 commit comments