@coffre/db 0.1.10 → 0.1.11
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-BhLco7OA.d.ts} +11 -1
- package/dist/dialect.d.ts +1 -1
- package/dist/hyperdrive.d.ts +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +54 -2
- package/dist/log.d.ts +1 -1
- package/dist/portable.d.ts +1 -1
- package/dist/schema-version.d.ts +1 -1
- package/package.json +2 -2
- package/src/database.ts +62 -1
package/dist/connect.d.ts
CHANGED
|
@@ -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
1
|
import { l as schema_d_exports } from "./schema-Cd4WOnB5.js";
|
|
2
|
-
import { a as
|
|
3
|
-
export { Database, PoolLike, Queryable, Transaction, createDatabase, own, schema_d_exports as schema, tablesOf };
|
|
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-BhLco7OA.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
package/dist/portable.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { l as schema_d_exports } from "./schema-Cd4WOnB5.js";
|
|
2
|
-
import { t as Database } from "./database-
|
|
2
|
+
import { t as Database } from "./database-BhLco7OA.js";
|
|
3
3
|
import { t as schema_sqlite_d_exports } from "./schema-sqlite.js";
|
|
4
4
|
import { LibSQLDatabase } from "drizzle-orm/libsql";
|
|
5
5
|
//#region src/portable.d.ts
|
package/dist/schema-version.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coffre/db",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "coffre's database: the schema, its migrations and the connections, on Postgres or SQLite.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"drizzle-orm": "0.45.2",
|
|
75
75
|
"pg": "8.16.3",
|
|
76
76
|
"pg-connection-string": "2.14.0",
|
|
77
|
-
"@coffre/core": "0.1.
|
|
77
|
+
"@coffre/core": "0.1.11"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"@types/node": "26.1.1",
|
package/src/database.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
|
|
1
3
|
import { getTableName, is, Table } from 'drizzle-orm';
|
|
2
4
|
import { drizzle, type NodePgDatabase } from 'drizzle-orm/node-postgres';
|
|
3
5
|
import type pg from 'pg';
|
|
@@ -27,8 +29,67 @@ export type PoolLike = pg.Pool | {
|
|
|
27
29
|
connect: () => Promise<Pick<pg.PoolClient, 'query'> & { release: () => void }>;
|
|
28
30
|
};
|
|
29
31
|
|
|
32
|
+
/**
|
|
33
|
+
* A query made on the database from inside one of its own transactions,
|
|
34
|
+
* rather than through the transaction. With one connection per call, as on
|
|
35
|
+
* Workers, it would wait for the transaction, which waits for it: a call
|
|
36
|
+
* Cloudflare cancels as hung, and nothing says why. So it is refused at
|
|
37
|
+
* once, with this, wherever the database runs.
|
|
38
|
+
*/
|
|
39
|
+
export class QueryOutsideTransaction extends Error {
|
|
40
|
+
constructor() {
|
|
41
|
+
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');
|
|
42
|
+
this.name = 'QueryOutsideTransaction';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The transaction a flow of work runs inside, with the pool it holds, while it is open. */
|
|
47
|
+
const inside = new AsyncLocalStorage<{ pool: PoolLike; open: boolean }>();
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* `pool`, refusing a query or a transaction asked from inside a transaction
|
|
51
|
+
* it is already running. Work from any other flow, a concurrent request's,
|
|
52
|
+
* or what the transaction left running after it ended, goes on as before.
|
|
53
|
+
*/
|
|
54
|
+
function guarded(pool: PoolLike): PoolLike {
|
|
55
|
+
return new Proxy(pool, {
|
|
56
|
+
get(target, key) {
|
|
57
|
+
const value: unknown = Reflect.get(target, key, target);
|
|
58
|
+
if (typeof value !== 'function') return value;
|
|
59
|
+
if (key !== 'query' && key !== 'connect') return value.bind(target);
|
|
60
|
+
return (...args: unknown[]) => {
|
|
61
|
+
const open = inside.getStore();
|
|
62
|
+
if (open?.open === true && open.pool === target) throw new QueryOutsideTransaction();
|
|
63
|
+
return (value as (...args: unknown[]) => unknown).apply(target, args);
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The refusal `error` is, or was caused by. */
|
|
70
|
+
function brokenRule(error: unknown): QueryOutsideTransaction | undefined {
|
|
71
|
+
for (let cause = error; cause instanceof Error; cause = cause.cause) {
|
|
72
|
+
if (cause instanceof QueryOutsideTransaction) return cause;
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
30
77
|
export function createDatabase(pool: PoolLike): Database {
|
|
31
|
-
|
|
78
|
+
const db = drizzle(guarded(pool) as pg.Pool, { schema });
|
|
79
|
+
// Each transaction's work runs knowing which one it is in, until the transaction ends.
|
|
80
|
+
const run = db.transaction.bind(db);
|
|
81
|
+
db.transaction = (async (work, config) => {
|
|
82
|
+
const open = { pool, open: true };
|
|
83
|
+
try {
|
|
84
|
+
return await run((tx) => inside.run(open, () => work(tx)), config);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
// Drizzle wraps what a query throws: the rule broken is what to say.
|
|
87
|
+
throw brokenRule(error) ?? error;
|
|
88
|
+
} finally {
|
|
89
|
+
open.open = false;
|
|
90
|
+
}
|
|
91
|
+
}) as Database['transaction'];
|
|
92
|
+
return trackCommits(db);
|
|
32
93
|
}
|
|
33
94
|
|
|
34
95
|
/**
|