@coffre/db 0.1.10 → 0.1.12
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.
- package/dist/connect.d.ts +1 -1
- package/dist/{database-CWpo5aF0.d.ts → database-CNMfHZwF.d.ts} +12 -2
- package/dist/dialect.d.ts +1 -1
- package/dist/hyperdrive.d.ts +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +54 -2
- package/dist/log.d.ts +1 -1
- package/dist/migrations/postgres/0001_remove_syncs.sql +13 -0
- package/dist/migrations/postgres/meta/0001_snapshot.json +1707 -0
- package/dist/migrations/postgres/meta/_journal.json +7 -0
- package/dist/migrations/sqlite/0001_remove_syncs.sql +13 -0
- package/dist/migrations/sqlite/meta/0001_snapshot.json +1575 -0
- package/dist/migrations/sqlite/meta/_journal.json +7 -0
- package/dist/portable.d.ts +2 -2
- package/dist/{relations-DqgxtlKu.js → relations-BfR3JjT1.js} +2 -22
- package/dist/{schema-Cd4WOnB5.d.ts → schema-AmvM6w0D.d.ts} +2 -362
- package/dist/schema-sqlite.d.ts +1 -369
- package/dist/schema-sqlite.js +5 -59
- package/dist/schema-version.d.ts +5 -3
- package/dist/schema-version.js +4 -2
- package/dist/schema.d.ts +2 -2
- package/dist/schema.js +5 -79
- package/package.json +2 -2
- package/src/baseline.ts +11 -19
- package/src/database.ts +62 -1
- package/src/migrations/postgres/0001_remove_syncs.sql +13 -0
- package/src/migrations/postgres/meta/0001_snapshot.json +1707 -0
- package/src/migrations/postgres/meta/_journal.json +7 -0
- package/src/migrations/sqlite/0001_remove_syncs.sql +13 -0
- package/src/migrations/sqlite/meta/0001_snapshot.json +1575 -0
- package/src/migrations/sqlite/meta/_journal.json +7 -0
- package/src/relations.ts +1 -14
- package/src/schema-version.ts +4 -2
- package/src/schema.sqlite.ts +1 -65
- package/src/schema.ts +1 -81
package/dist/connect.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { l as schema_d_exports } from "./schema-
|
|
1
|
+
import { l as schema_d_exports } from "./schema-AmvM6w0D.js";
|
|
2
2
|
import { Table } from "drizzle-orm";
|
|
3
3
|
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
4
4
|
import pg from "pg";
|
|
@@ -23,6 +23,16 @@ type PoolLike = pg.Pool | {
|
|
|
23
23
|
release: () => void;
|
|
24
24
|
}>;
|
|
25
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* A query made on the database from inside one of its own transactions,
|
|
28
|
+
* rather than through the transaction. With one connection per call, as on
|
|
29
|
+
* Workers, it would wait for the transaction, which waits for it: a call
|
|
30
|
+
* Cloudflare cancels as hung, and nothing says why. So it is refused at
|
|
31
|
+
* once, with this, wherever the database runs.
|
|
32
|
+
*/
|
|
33
|
+
declare class QueryOutsideTransaction extends Error {
|
|
34
|
+
constructor();
|
|
35
|
+
}
|
|
26
36
|
declare function createDatabase(pool: PoolLike): Database;
|
|
27
37
|
/**
|
|
28
38
|
* The tables of the database's own dialect: the Postgres ones on Postgres,
|
|
@@ -36,4 +46,4 @@ declare function tablesOf(db: Queryable): typeof schema_d_exports;
|
|
|
36
46
|
*/
|
|
37
47
|
declare function own<T extends Table>(db: Queryable, table: T): T;
|
|
38
48
|
//#endregion
|
|
39
|
-
export {
|
|
49
|
+
export { Transaction as a, tablesOf as c, Queryable as i, PoolLike as n, createDatabase as o, QueryOutsideTransaction as r, own as s, Database as t };
|
package/dist/dialect.d.ts
CHANGED
package/dist/hyperdrive.d.ts
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { l as schema_d_exports } from "./schema-
|
|
2
|
-
import { a as
|
|
3
|
-
export { Database, PoolLike, Queryable, Transaction, createDatabase, own, schema_d_exports as schema, tablesOf };
|
|
1
|
+
import { l as schema_d_exports } from "./schema-AmvM6w0D.js";
|
|
2
|
+
import { a as Transaction, c as tablesOf, i as Queryable, n as PoolLike, o as createDatabase, r as QueryOutsideTransaction, s as own, t as Database } from "./database-CNMfHZwF.js";
|
|
3
|
+
export { Database, PoolLike, QueryOutsideTransaction, Queryable, Transaction, createDatabase, own, schema_d_exports as schema, tablesOf };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,62 @@
|
|
|
1
1
|
import { n as trackCommits } from "./commits-BuW-NL3n.js";
|
|
2
2
|
import { t as schema_exports } from "./schema.js";
|
|
3
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
4
|
import { Table, getTableName, is } from "drizzle-orm";
|
|
4
5
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
5
6
|
//#region src/database.ts
|
|
7
|
+
/**
|
|
8
|
+
* A query made on the database from inside one of its own transactions,
|
|
9
|
+
* rather than through the transaction. With one connection per call, as on
|
|
10
|
+
* Workers, it would wait for the transaction, which waits for it: a call
|
|
11
|
+
* Cloudflare cancels as hung, and nothing says why. So it is refused at
|
|
12
|
+
* once, with this, wherever the database runs.
|
|
13
|
+
*/
|
|
14
|
+
var QueryOutsideTransaction = class extends Error {
|
|
15
|
+
constructor() {
|
|
16
|
+
super("query outside its transaction: use tx. A query on the database inside one of its own transactions waits for that transaction, which waits for it");
|
|
17
|
+
this.name = "QueryOutsideTransaction";
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
/** The transaction a flow of work runs inside, with the pool it holds, while it is open. */
|
|
21
|
+
const inside = new AsyncLocalStorage();
|
|
22
|
+
/**
|
|
23
|
+
* `pool`, refusing a query or a transaction asked from inside a transaction
|
|
24
|
+
* it is already running. Work from any other flow, a concurrent request's,
|
|
25
|
+
* or what the transaction left running after it ended, goes on as before.
|
|
26
|
+
*/
|
|
27
|
+
function guarded(pool) {
|
|
28
|
+
return new Proxy(pool, { get(target, key) {
|
|
29
|
+
const value = Reflect.get(target, key, target);
|
|
30
|
+
if (typeof value !== "function") return value;
|
|
31
|
+
if (key !== "query" && key !== "connect") return value.bind(target);
|
|
32
|
+
return (...args) => {
|
|
33
|
+
const open = inside.getStore();
|
|
34
|
+
if (open?.open === true && open.pool === target) throw new QueryOutsideTransaction();
|
|
35
|
+
return value.apply(target, args);
|
|
36
|
+
};
|
|
37
|
+
} });
|
|
38
|
+
}
|
|
39
|
+
/** The refusal `error` is, or was caused by. */
|
|
40
|
+
function brokenRule(error) {
|
|
41
|
+
for (let cause = error; cause instanceof Error; cause = cause.cause) if (cause instanceof QueryOutsideTransaction) return cause;
|
|
42
|
+
}
|
|
6
43
|
function createDatabase(pool) {
|
|
7
|
-
|
|
44
|
+
const db = drizzle(guarded(pool), { schema: schema_exports });
|
|
45
|
+
const run = db.transaction.bind(db);
|
|
46
|
+
db.transaction = (async (work, config) => {
|
|
47
|
+
const open = {
|
|
48
|
+
pool,
|
|
49
|
+
open: true
|
|
50
|
+
};
|
|
51
|
+
try {
|
|
52
|
+
return await run((tx) => inside.run(open, () => work(tx)), config);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw brokenRule(error) ?? error;
|
|
55
|
+
} finally {
|
|
56
|
+
open.open = false;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return trackCommits(db);
|
|
8
60
|
}
|
|
9
61
|
/**
|
|
10
62
|
* The tables of the database's own dialect: the Postgres ones on Postgres,
|
|
@@ -31,4 +83,4 @@ function own(db, table) {
|
|
|
31
83
|
return found;
|
|
32
84
|
}
|
|
33
85
|
//#endregion
|
|
34
|
-
export { createDatabase, own, schema_exports as schema, tablesOf };
|
|
86
|
+
export { QueryOutsideTransaction, createDatabase, own, schema_exports as schema, tablesOf };
|
package/dist/log.d.ts
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
-- Stop old app instances before upgrading. Never silently discard a configured
|
|
2
|
+
-- destination, including archived ones. Keep the audit and sealed legacy rows.
|
|
3
|
+
LOCK TABLE syncs, sync_keys IN ACCESS EXCLUSIVE MODE;
|
|
4
|
+
--> statement-breakpoint
|
|
5
|
+
DO $$ BEGIN
|
|
6
|
+
IF EXISTS (SELECT 1 FROM syncs) OR EXISTS (SELECT 1 FROM sync_keys) THEN
|
|
7
|
+
RAISE EXCEPTION 'syncs are removed: migrate destinations to service tokens, back up and clear syncs and sync_keys before upgrading';
|
|
8
|
+
END IF;
|
|
9
|
+
END $$;
|
|
10
|
+
--> statement-breakpoint
|
|
11
|
+
DROP TABLE sync_keys;
|
|
12
|
+
--> statement-breakpoint
|
|
13
|
+
DROP TABLE syncs;
|