@guren/orm 2.10.0 → 2.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +72 -29
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -3,6 +3,19 @@ import { extname, relative, resolve } from "node:path";
3
3
  import { fileURLToPath, pathToFileURL } from "node:url";
4
4
  import { existsSync, readdirSync } from "node:fs";
5
5
  import { readdir } from "node:fs/promises";
6
+ //#region src/hot-reload-runtime.ts
7
+ /**
8
+ * `bun --hot` re-runs the module graph in-process: module-level state resets
9
+ * while `globalThis` survives, so state parked there has to tell a
10
+ * re-evaluation from a new participant. `bun --watch` restarts the process
11
+ * instead, and this answers for no other in-process re-evaluator (a Vite SSR
12
+ * runner, `vi.resetModules()`). Twin of
13
+ * `packages/server/src/hot-reload/hot-disposables.ts` — keep the two in step.
14
+ */
15
+ function isHotReloadRuntime() {
16
+ return typeof process !== "undefined" && Array.isArray(process.execArgv) && process.execArgv.includes("--hot");
17
+ }
18
+ //#endregion
6
19
  //#region src/instance-guard.ts
7
20
  /**
8
21
  * Detects multiple loaded copies of @guren/orm in one process. Model and
@@ -12,20 +25,37 @@ import { readdir } from "node:fs/promises";
12
25
  */
13
26
  const INSTANCE_KEY = Symbol.for("guren.orm.loaded");
14
27
  const globalScope = globalThis;
15
- const marker = globalScope[INSTANCE_KEY];
16
- if (!marker) globalScope[INSTANCE_KEY] = {
17
- count: 1,
18
- warned: false
19
- };
20
- else {
28
+ /** Empty where a bundler shims `import.meta.url` away, which no copy can be told apart by. */
29
+ function moduleIdentity() {
30
+ return import.meta.url || void 0;
31
+ }
32
+ /**
33
+ * Counts this evaluation as a copy unless the same module URL already
34
+ * registered under `bun --hot`, which re-evaluates a file against a surviving
35
+ * `globalThis`. A bundle inlines both copies at one URL, so outside `--hot` a
36
+ * repeat is a duplicate rather than a reload. Any other in-process
37
+ * re-evaluator (a Vite SSR runner, `vi.resetModules()`) still counts.
38
+ */
39
+ function registerOrmInstance(identity) {
40
+ const marker = globalScope[INSTANCE_KEY] ??= {
41
+ count: 0,
42
+ warned: false,
43
+ identities: /* @__PURE__ */ new Set()
44
+ };
45
+ const identities = marker.identities ??= /* @__PURE__ */ new Set();
46
+ if (identity !== void 0) {
47
+ if (identities.has(identity) && isHotReloadRuntime()) return;
48
+ identities.add(identity);
49
+ }
21
50
  marker.count += 1;
22
51
  const quiet = typeof process !== "undefined" && process.env.GUREN_QUIET_DUPLICATE_ORM === "1";
23
- if (!marker.warned && !quiet) {
52
+ if (marker.count > 1 && !marker.warned && !quiet) {
24
53
  marker.warned = true;
25
54
  console.warn(`[guren/orm] ${marker.count} copies of @guren/orm are loaded in this process. Adapter configuration and model state are NOT shared between copies, so database access will fail with "database has not been configured" for models imported through the extra copy.
26
55
  [guren/orm] This usually means mixed @guren/* versions (check \`bun pm ls | grep @guren\`). Fix it by aligning all @guren/* packages to the same release, e.g. \`bunx guren upgrade\` or updating every @guren/* entry in package.json together, then reinstalling. Set GUREN_QUIET_DUPLICATE_ORM=1 to silence this warning (e.g. monorepo dev where src and dist copies coexist by design).`);
27
56
  }
28
57
  }
58
+ registerOrmInstance(moduleIdentity());
29
59
  //#endregion
30
60
  //#region src/internal-keys.ts
31
61
  /**
@@ -2250,6 +2280,16 @@ function SoftDeletes(Base) {
2250
2280
  }
2251
2281
  //#endregion
2252
2282
  //#region src/active-connections.ts
2283
+ /**
2284
+ * Closes the database client a previous module evaluation left open.
2285
+ *
2286
+ * `bun --hot` re-runs the module graph in-process: a `create*Database()`
2287
+ * factory's cached client resets but its connection leaks, one per reload.
2288
+ * Bun exposes no `import.meta.hot` under `--hot` (checked on 1.3.14), so the
2289
+ * teardown is parked on `globalThis`, which survives re-evaluation. Handles are
2290
+ * keyed by caller file + target, not line, so two handles built in one file
2291
+ * against one database share a key; give them separate modules.
2292
+ */
2253
2293
  const REGISTRY_KEY = Symbol.for("guren.orm.activeConnections");
2254
2294
  /**
2255
2295
  * Awaiting the teardown is the back-pressure that keeps a burst of reloads from
@@ -2265,10 +2305,6 @@ function getRegistry() {
2265
2305
  scope[REGISTRY_KEY] = registry;
2266
2306
  return registry;
2267
2307
  }
2268
- /** `bun --watch` restarts the process instead, so `--hot` is the only mode that leaks. */
2269
- function isHotReloadRuntime() {
2270
- return typeof process !== "undefined" && Array.isArray(process.execArgv) && process.execArgv.includes("--hot");
2271
- }
2272
2308
  /** Both match one character, so neither can backtrack the way `\s*` in a pattern can. */
2273
2309
  const WHITESPACE = /\s/;
2274
2310
  const LINE_TERMINATOR = /[\n\r\u2028\u2029]/;
@@ -3086,35 +3122,41 @@ async function closePool(pool) {
3086
3122
  }
3087
3123
  //#endregion
3088
3124
  //#region src/sqlite.ts
3089
- function isInMemory(dbPath) {
3090
- return dbPath === ":memory:" || dbPath === "" || dbPath.startsWith("file::memory:");
3091
- }
3125
+ const MEMORY = { kind: "memory" };
3126
+ const UNRESOLVABLE = { kind: "unresolvable" };
3092
3127
  /**
3093
- * Path to open for `dbPath`; undefined for in-memory forms and unresolvable
3094
- * URIs, which `new Database()` refuses with no directory created. `file:` URI
3095
- * support is compile-time `SQLITE_USE_URI` (measured: on for Bun/macOS via system
3096
- * libsqlite3, off in Bun's Linux build, which opens `file:local.db` literally), so
3097
- * parsing follows https://sqlite.org/uri.html, not WHATWG (`/local.db` vs the cwd).
3128
+ * What `dbPath` opens. `file:` URI support is compile-time `SQLITE_USE_URI`
3129
+ * (measured: on for Bun/macOS via system libsqlite3, off in Bun's Linux build,
3130
+ * which opens `file:local.db` and `file::memory:` as literal files), so only an
3131
+ * unresolvable URI reaches `new Database()`, and parsing follows
3132
+ * https://sqlite.org/uri.html, not WHATWG (`/local.db` vs the cwd).
3098
3133
  */
3099
- function sqliteFilePath(dbPath) {
3100
- if (isInMemory(dbPath)) return void 0;
3101
- if (!dbPath.startsWith("file:")) return resolve(dbPath);
3134
+ function sqliteTarget(dbPath) {
3135
+ if (dbPath === ":memory:" || dbPath === "") return MEMORY;
3136
+ if (!dbPath.startsWith("file:")) return {
3137
+ kind: "file",
3138
+ path: resolve(dbPath)
3139
+ };
3102
3140
  let rest = dbPath.slice(5);
3103
3141
  if (rest.startsWith("//")) {
3104
3142
  const pathStart = rest.indexOf("/", 2);
3105
3143
  const authority = pathStart === -1 ? rest.slice(2) : rest.slice(2, pathStart);
3106
- if (authority !== "" && authority !== "localhost") return void 0;
3144
+ if (authority !== "" && authority !== "localhost") return UNRESOLVABLE;
3107
3145
  rest = pathStart === -1 ? "" : rest.slice(pathStart);
3108
3146
  }
3109
3147
  const marker = rest.search(/[?#]/);
3110
3148
  const encodedPath = marker === -1 ? rest : rest.slice(0, marker);
3111
3149
  const query = marker !== -1 && rest[marker] === "?" ? rest.slice(marker + 1).split("#")[0] : "";
3112
- if (query !== "") throw new Error(`createSqliteDatabase() cannot honour the URI parameters in ${dbPath} (?${query}). They are only read by a sqlite built with SQLITE_USE_URI, which Bun provides on some platforms and not others, so this driver resolves the URI to a path itself. Pass a plain path, and set the behaviour those parameters asked for in code.`);
3113
- if (encodedPath === "") return void 0;
3150
+ if (query !== "") throw new Error(`createSqliteDatabase() cannot honour the URI parameters in ${dbPath} (?${query}). They are only read by a sqlite built with SQLITE_USE_URI, which Bun provides on some platforms and not others, so this driver resolves the URI to a path itself. Pass a plain path or ":memory:", and set the behaviour those parameters asked for in code.`);
3151
+ if (encodedPath === "") return UNRESOLVABLE;
3114
3152
  try {
3115
- return resolve(decodeURIComponent(encodedPath));
3153
+ const path = decodeURIComponent(encodedPath);
3154
+ return path === ":memory:" ? MEMORY : {
3155
+ kind: "file",
3156
+ path: resolve(path)
3157
+ };
3116
3158
  } catch {
3117
- return;
3159
+ return UNRESOLVABLE;
3118
3160
  }
3119
3161
  }
3120
3162
  /**
@@ -3157,7 +3199,8 @@ function createSqliteDatabase(options) {
3157
3199
  }
3158
3200
  const database = singleFlight(async () => {
3159
3201
  const dbPath = resolveFilename();
3160
- const dbFile = sqliteFilePath(dbPath);
3202
+ const target = sqliteTarget(dbPath);
3203
+ const dbFile = target.kind === "file" ? target.path : void 0;
3161
3204
  if (dbFile) {
3162
3205
  const { mkdirSync } = await import("node:fs");
3163
3206
  const { dirname } = await import("node:path");
@@ -3167,7 +3210,7 @@ function createSqliteDatabase(options) {
3167
3210
  }
3168
3211
  const { Database } = await import("bun:sqlite");
3169
3212
  const { drizzle } = await import("drizzle-orm/bun-sqlite");
3170
- const sqlite = new Database(dbFile ?? dbPath);
3213
+ const sqlite = new Database(target.kind === "memory" ? ":memory:" : dbFile ?? dbPath);
3171
3214
  sqlite.exec("PRAGMA journal_mode = WAL;");
3172
3215
  sqliteClient = sqlite;
3173
3216
  const handle = drizzle({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guren/orm",
3
- "version": "2.10.0",
3
+ "version": "2.10.1",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
6
  "./src/instance-guard.ts",