@guren/orm 2.6.1 → 2.6.2

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 +58 -7
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -3424,6 +3424,54 @@ function isInMemory(dbPath) {
3424
3424
  return dbPath === ":memory:" || dbPath === "" || dbPath.startsWith("file::memory:");
3425
3425
  }
3426
3426
  /**
3427
+ * The filesystem path to open for `dbPath`, or undefined when there is no file
3428
+ * to name — the in-memory forms, and the URIs this cannot resolve.
3429
+ *
3430
+ * `file:` is sqlite's own URI scheme, so `file://…` names a *file* and not a
3431
+ * server — which is why the connection-URI guard below lets every form of it
3432
+ * through. What it is not is a path: handed to `resolve()` it is taken as a
3433
+ * relative name, so the `mkdir -p` below used to prepare a `file:/…` tree under
3434
+ * the cwd.
3435
+ *
3436
+ * **Whether the host's sqlite would have parsed it is not knowable from here,
3437
+ * which is why the driver parses it itself and opens the plain path.** URI
3438
+ * filenames are a compile-time option (`SQLITE_USE_URI`), and the two builds
3439
+ * Guren runs on disagree: measured, Bun on macOS goes through the system
3440
+ * libsqlite3, which has it on, while Bun's own Linux build has it off and takes
3441
+ * `file:local.db` as a filename that literally starts with `file:`. The stray
3442
+ * tree was therefore not the same defect on both — empty on macOS, holding the
3443
+ * actual database on Linux. Resolving here is what makes one filename mean one
3444
+ * file on every host.
3445
+ *
3446
+ * The rules are sqlite's (https://sqlite.org/uri.html), not the WHATWG URL
3447
+ * parser's — `new URL('file:local.db')` resolves to `/local.db`, an absolute
3448
+ * path at the filesystem root, where sqlite resolves it against the cwd.
3449
+ * A URI sqlite itself rejects (an authority that is neither empty nor
3450
+ * `localhost`) resolves to undefined, and the original string is handed to
3451
+ * `new Database()` to be refused there, with no directory created on the way.
3452
+ */
3453
+ function sqliteFilePath(dbPath) {
3454
+ if (isInMemory(dbPath)) return void 0;
3455
+ if (!dbPath.startsWith("file:")) return resolve(dbPath);
3456
+ let rest = dbPath.slice(5);
3457
+ if (rest.startsWith("//")) {
3458
+ const pathStart = rest.indexOf("/", 2);
3459
+ const authority = pathStart === -1 ? rest.slice(2) : rest.slice(2, pathStart);
3460
+ if (authority !== "" && authority !== "localhost") return void 0;
3461
+ rest = pathStart === -1 ? "" : rest.slice(pathStart);
3462
+ }
3463
+ const marker = rest.search(/[?#]/);
3464
+ const encodedPath = marker === -1 ? rest : rest.slice(0, marker);
3465
+ const query = marker !== -1 && rest[marker] === "?" ? rest.slice(marker + 1).split("#")[0] : "";
3466
+ 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.`);
3467
+ if (encodedPath === "") return void 0;
3468
+ try {
3469
+ return resolve(decodeURIComponent(encodedPath));
3470
+ } catch {
3471
+ return;
3472
+ }
3473
+ }
3474
+ /**
3427
3475
  * A scheme *with an authority* — the `//` is what separates a connection URI
3428
3476
  * from a filename — for every scheme that could name a database server.
3429
3477
  *
@@ -3475,21 +3523,24 @@ function createSqliteDatabase(options) {
3475
3523
  }
3476
3524
  const database = singleFlight(async () => {
3477
3525
  const dbPath = resolveFilename();
3478
- const { mkdirSync } = await import("node:fs");
3479
- const { dirname } = await import("node:path");
3480
- try {
3481
- mkdirSync(dirname(resolve(dbPath)), { recursive: true });
3482
- } catch {}
3526
+ const dbFile = sqliteFilePath(dbPath);
3527
+ if (dbFile) {
3528
+ const { mkdirSync } = await import("node:fs");
3529
+ const { dirname } = await import("node:path");
3530
+ try {
3531
+ mkdirSync(dirname(dbFile), { recursive: true });
3532
+ } catch {}
3533
+ }
3483
3534
  const { Database } = await import("bun:sqlite");
3484
3535
  const { drizzle } = await import("drizzle-orm/bun-sqlite");
3485
- const sqlite = new Database(dbPath);
3536
+ const sqlite = new Database(dbFile ?? dbPath);
3486
3537
  sqlite.exec("PRAGMA journal_mode = WAL;");
3487
3538
  sqliteClient = sqlite;
3488
3539
  const handle = drizzle({
3489
3540
  client: sqlite,
3490
3541
  ...relations ? { relations } : {}
3491
3542
  });
3492
- activeKey = isInMemory(dbPath) ? void 0 : hotReloadKey("sqlite", callSite, resolve(dbPath));
3543
+ activeKey = dbFile === void 0 ? void 0 : hotReloadKey("sqlite", callSite, dbFile);
3493
3544
  if (activeKey) await replaceActiveConnection(activeKey, closeDatabase);
3494
3545
  return handle;
3495
3546
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guren/orm",
3
- "version": "2.6.1",
3
+ "version": "2.6.2",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
6
  "./src/instance-guard.ts",