@dads-framework/postgres-access 0.2.0 → 0.4.0
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/BENCHMARK.md +94 -8
- package/README.md +157 -101
- package/dist/compile-access-policy.d.ts +2 -1
- package/dist/compile-access-policy.d.ts.map +1 -1
- package/dist/compile-access-policy.js +9 -4
- package/dist/compile-access-policy.js.map +1 -1
- package/dist/drizzle-executor.d.ts +2 -2
- package/dist/drizzle-executor.d.ts.map +1 -1
- package/dist/drizzle-executor.js.map +1 -1
- package/dist/drizzle-structural.d.ts +23 -0
- package/dist/drizzle-structural.d.ts.map +1 -0
- package/dist/drizzle-structural.js +22 -0
- package/dist/drizzle-structural.js.map +1 -0
- package/dist/drizzle.d.ts +4 -0
- package/dist/drizzle.d.ts.map +1 -0
- package/dist/drizzle.js +4 -0
- package/dist/drizzle.js.map +1 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/lookup-primary-pool.d.ts +2 -3
- package/dist/lookup-primary-pool.d.ts.map +1 -1
- package/dist/lookup-primary-pool.js +1 -1
- package/dist/lookup-primary-pool.js.map +1 -1
- package/dist/lookup-revision-query.d.ts.map +1 -1
- package/dist/lookup-revision-query.js +1 -2
- package/dist/lookup-revision-query.js.map +1 -1
- package/dist/lower-permissions.d.ts.map +1 -1
- package/dist/lower-permissions.js +38 -15
- package/dist/lower-permissions.js.map +1 -1
- package/dist/pg-transaction-lifecycle.d.ts +9 -0
- package/dist/pg-transaction-lifecycle.d.ts.map +1 -0
- package/dist/pg-transaction-lifecycle.js +26 -0
- package/dist/pg-transaction-lifecycle.js.map +1 -0
- package/dist/policy/column-relations.d.ts +34 -0
- package/dist/policy/column-relations.d.ts.map +1 -0
- package/dist/policy/column-relations.js +127 -0
- package/dist/policy/column-relations.js.map +1 -0
- package/dist/policy/compile-filter.d.ts +53 -0
- package/dist/policy/compile-filter.d.ts.map +1 -0
- package/dist/policy/compile-filter.js +133 -0
- package/dist/policy/compile-filter.js.map +1 -0
- package/dist/policy/compile-forward-policy.d.ts +20 -0
- package/dist/policy/compile-forward-policy.d.ts.map +1 -0
- package/dist/policy/compile-forward-policy.js +128 -0
- package/dist/policy/compile-forward-policy.js.map +1 -0
- package/dist/policy/compile-linear-batch.d.ts +1 -0
- package/dist/policy/compile-linear-batch.d.ts.map +1 -1
- package/dist/policy/compile-linear-batch.js +3 -6
- package/dist/policy/compile-linear-batch.js.map +1 -1
- package/dist/policy/compile-linear-check.d.ts.map +1 -1
- package/dist/policy/compile-linear-check.js +53 -18
- package/dist/policy/compile-linear-check.js.map +1 -1
- package/dist/policy/compile-postgres-policy.d.ts +9 -4
- package/dist/policy/compile-postgres-policy.d.ts.map +1 -1
- package/dist/policy/compile-postgres-policy.js +89 -118
- package/dist/policy/compile-postgres-policy.js.map +1 -1
- package/dist/policy/compile-recursive-policy.d.ts +53 -0
- package/dist/policy/compile-recursive-policy.d.ts.map +1 -0
- package/dist/policy/compile-recursive-policy.js +231 -0
- package/dist/policy/compile-recursive-policy.js.map +1 -0
- package/dist/policy/compile-recursive-subjects.d.ts +27 -0
- package/dist/policy/compile-recursive-subjects.d.ts.map +1 -0
- package/dist/policy/compile-recursive-subjects.js +180 -0
- package/dist/policy/compile-recursive-subjects.js.map +1 -0
- package/dist/policy/compile-reverse-policy.d.ts +12 -2
- package/dist/policy/compile-reverse-policy.d.ts.map +1 -1
- package/dist/policy/compile-reverse-policy.js +164 -39
- package/dist/policy/compile-reverse-policy.js.map +1 -1
- package/dist/policy/compile-shared-point-check.d.ts.map +1 -1
- package/dist/policy/compile-shared-point-check.js +166 -16
- package/dist/policy/compile-shared-point-check.js.map +1 -1
- package/dist/policy/compile-subject-lookup.d.ts +8 -0
- package/dist/policy/compile-subject-lookup.d.ts.map +1 -0
- package/dist/policy/compile-subject-lookup.js +253 -0
- package/dist/policy/compile-subject-lookup.js.map +1 -0
- package/dist/policy/permission-cycles.d.ts +27 -0
- package/dist/policy/permission-cycles.d.ts.map +1 -0
- package/dist/policy/permission-cycles.js +179 -0
- package/dist/policy/permission-cycles.js.map +1 -0
- package/dist/policy/policy-model.d.ts +24 -0
- package/dist/policy/policy-model.d.ts.map +1 -1
- package/dist/policy/policy-sql.d.ts +39 -5
- package/dist/policy/policy-sql.d.ts.map +1 -1
- package/dist/policy/policy-sql.js +184 -15
- package/dist/policy/policy-sql.js.map +1 -1
- package/dist/policy/resolve-filter.d.ts +25 -0
- package/dist/policy/resolve-filter.d.ts.map +1 -0
- package/dist/policy/resolve-filter.js +143 -0
- package/dist/policy/resolve-filter.js.map +1 -0
- package/dist/policy/run-filter.d.ts +6 -0
- package/dist/policy/run-filter.d.ts.map +1 -0
- package/dist/policy/run-filter.js +36 -0
- package/dist/policy/run-filter.js.map +1 -0
- package/dist/policy/tarjan-scc.d.ts +3 -0
- package/dist/policy/tarjan-scc.d.ts.map +1 -0
- package/dist/policy/tarjan-scc.js +58 -0
- package/dist/policy/tarjan-scc.js.map +1 -0
- package/dist/postgres-access.tsbuildinfo +1 -1
- package/dist/postgres-authorization.d.ts +21 -17
- package/dist/postgres-authorization.d.ts.map +1 -1
- package/dist/postgres-authorization.js +52 -35
- package/dist/postgres-authorization.js.map +1 -1
- package/dist/postgres-options.d.ts +25 -0
- package/dist/postgres-options.d.ts.map +1 -0
- package/dist/postgres-options.js +34 -0
- package/dist/postgres-options.js.map +1 -0
- package/dist/postgres-response.d.ts +5 -0
- package/dist/postgres-response.d.ts.map +1 -1
- package/dist/postgres-response.js +38 -0
- package/dist/postgres-response.js.map +1 -1
- package/dist/postgres-serialization-conflict.d.ts.map +1 -1
- package/dist/postgres-serialization-conflict.js.map +1 -1
- package/dist/postgres-tuples.d.ts.map +1 -1
- package/dist/postgres-tuples.js +68 -23
- package/dist/postgres-tuples.js.map +1 -1
- package/dist/postgres-types.d.ts +11 -0
- package/dist/postgres-types.d.ts.map +1 -1
- package/dist/postgres-types.js.map +1 -1
- package/dist/prepared-check-executor.d.ts.map +1 -1
- package/dist/prepared-check-executor.js.map +1 -1
- package/dist/protected-authorization-scope.d.ts +6 -8
- package/dist/protected-authorization-scope.d.ts.map +1 -1
- package/dist/protected-authorization-scope.js +13 -8
- package/dist/protected-authorization-scope.js.map +1 -1
- package/dist/relationship-schema.d.ts +9 -0
- package/dist/relationship-schema.d.ts.map +1 -1
- package/dist/relationship-schema.js +22 -2
- package/dist/relationship-schema.js.map +1 -1
- package/dist/revisioned-lookup.d.ts.map +1 -1
- package/dist/revisioned-lookup.js +3 -8
- package/dist/revisioned-lookup.js.map +1 -1
- package/dist/testing.d.ts +20 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +86 -0
- package/dist/testing.js.map +1 -0
- package/dist/transactional-access-changes.d.ts +3 -3
- package/dist/transactional-access-changes.d.ts.map +1 -1
- package/dist/transactional-access-changes.js +6 -4
- package/dist/transactional-access-changes.js.map +1 -1
- package/dist/with-protected-drizzle-transaction.d.ts +18 -0
- package/dist/with-protected-drizzle-transaction.d.ts.map +1 -0
- package/dist/with-protected-drizzle-transaction.js +52 -0
- package/dist/with-protected-drizzle-transaction.js.map +1 -0
- package/dist/with-protected-pg-transaction.d.ts +29 -0
- package/dist/with-protected-pg-transaction.d.ts.map +1 -0
- package/dist/with-protected-pg-transaction.js +84 -0
- package/dist/with-protected-pg-transaction.js.map +1 -0
- package/dist/with-protected-postgres-transaction.d.ts +6 -21
- package/dist/with-protected-postgres-transaction.d.ts.map +1 -1
- package/dist/with-protected-postgres-transaction.js +4 -45
- package/dist/with-protected-postgres-transaction.js.map +1 -1
- package/dist/with-read-snapshot.d.ts +21 -0
- package/dist/with-read-snapshot.d.ts.map +1 -0
- package/dist/with-read-snapshot.js +46 -0
- package/dist/with-read-snapshot.js.map +1 -0
- package/package.json +26 -4
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { abortPgTransaction, releasePgClient } from "./pg-transaction-lifecycle.js";
|
|
2
|
+
import { readPostgresSerializationConflict } from "./postgres-serialization-conflict.js";
|
|
3
|
+
import { assertProtectedAuthorizationCanJoin, openProtectedAuthorizationScope, } from "./protected-authorization-scope.js";
|
|
4
|
+
/**
|
|
5
|
+
* Run one protected write on a plain node-postgres pool using
|
|
6
|
+
* `BEGIN ISOLATION LEVEL SERIALIZABLE` / `COMMIT` / `ROLLBACK`.
|
|
7
|
+
*
|
|
8
|
+
* Binds the scoped authorization handle to the checked-out client, closes
|
|
9
|
+
* the scope as soon as the callback finishes, and translates PostgreSQL
|
|
10
|
+
* `40001` serialization failures into `PostgresSerializationConflict` (retrying
|
|
11
|
+
* automatically up to `retries` times when configured).
|
|
12
|
+
*/
|
|
13
|
+
export async function withProtectedPgTransaction(options, work) {
|
|
14
|
+
const maxRetries = validateRetryCount(options.retries);
|
|
15
|
+
const baseDelayMs = validateRetryBaseDelayMs(options.retryBaseDelayMs);
|
|
16
|
+
assertProtectedAuthorizationCanJoin(options.authorization, undefined);
|
|
17
|
+
for (let attempt = 0;; attempt += 1) {
|
|
18
|
+
try {
|
|
19
|
+
return await runSinglePgTransactionAttempt(options, work);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
const conflict = readPostgresSerializationConflict(error);
|
|
23
|
+
if (conflict === undefined)
|
|
24
|
+
throw error;
|
|
25
|
+
if (attempt >= maxRetries)
|
|
26
|
+
throw conflict;
|
|
27
|
+
await waitBeforeRetry(baseDelayMs, attempt);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function runSinglePgTransactionAttempt(options, work) {
|
|
32
|
+
const client = await options.pool.connect();
|
|
33
|
+
const state = { began: false, committed: false, destroyClient: false };
|
|
34
|
+
try {
|
|
35
|
+
return await executePgTransaction(client, options.authorization, work, state);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
state.destroyClient = await abortPgTransaction(client, state);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
releasePgClient(client, state.destroyClient);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function validateRetryCount(retries) {
|
|
46
|
+
const value = retries ?? 0;
|
|
47
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
48
|
+
throw new Error("Protected Postgres transaction retries must be a non-negative integer");
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
function validateRetryBaseDelayMs(delay) {
|
|
53
|
+
const value = delay ?? 15;
|
|
54
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
55
|
+
throw new Error("Protected Postgres transaction retryBaseDelayMs must be a non-negative integer");
|
|
56
|
+
}
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
function waitBeforeRetry(baseDelayMs, attempt) {
|
|
60
|
+
if (baseDelayMs === 0)
|
|
61
|
+
return Promise.resolve();
|
|
62
|
+
const cappedExponent = Math.min(attempt, 6);
|
|
63
|
+
const backoffMs = baseDelayMs * 2 ** cappedExponent;
|
|
64
|
+
const jitterMs = Math.floor(Math.random() * baseDelayMs);
|
|
65
|
+
return new Promise((resolve) => {
|
|
66
|
+
setTimeout(resolve, backoffMs + jitterMs);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
async function executePgTransaction(client, authorization, work, state) {
|
|
70
|
+
await client.query("BEGIN ISOLATION LEVEL SERIALIZABLE");
|
|
71
|
+
state.began = true;
|
|
72
|
+
const scope = openProtectedAuthorizationScope(authorization, client, client);
|
|
73
|
+
let result;
|
|
74
|
+
try {
|
|
75
|
+
result = await work({ client, authorization: scope.authorization });
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
scope.close();
|
|
79
|
+
}
|
|
80
|
+
await client.query("COMMIT");
|
|
81
|
+
state.committed = true;
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=with-protected-pg-transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":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AI,CAAC,MAAM,EAAE,GAAG,WAAW,CAAC,CAAC;IACzD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,UAAU,CAAC,OAAO,EAAE,SAAS,GAAG,QAAQ,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;AACL,CAAC;AAED,KAAK,UAAU,oBAAoB,CACjC,MAAc,EACd,aAAoC,EACpC,IAAoE,EACpE,KAAyB;IAEzB,MAAM,MAAM,CAAC,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACzD,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;IACnB,MAAM,KAAK,GAAG,+BAA+B,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7E,IAAI,MAAc,CAAC;IACnB,IAAI,CAAC;QACH,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE,MAAM,EAAE,aAAa,EAAE,KAAK,CAAC,aAAa,EAAE,CAAC,CAAC;IACtE,CAAC;YAAS,CAAC;QACT,KAAK,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC;IACD,MAAM,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC7B,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC;IACvB,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,22 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
3
|
-
type
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
export
|
|
7
|
-
readonly authorization: PostgresAuthorization;
|
|
8
|
-
} & ({
|
|
9
|
-
readonly database: Database<TSchema>;
|
|
10
|
-
readonly transaction?: never;
|
|
11
|
-
} | {
|
|
12
|
-
readonly transaction: Transaction<TSchema>;
|
|
13
|
-
readonly database?: never;
|
|
14
|
-
});
|
|
15
|
-
export type ProtectedPostgresTransaction<TSchema extends Schema = Schema> = {
|
|
16
|
-
readonly tx: Transaction<TSchema>;
|
|
17
|
-
readonly authorization: PostgresAuthorization;
|
|
18
|
-
};
|
|
19
|
-
/** Run one protected write with authorization and application data on the same client. */
|
|
20
|
-
export declare function withProtectedPostgresTransaction<TSchema extends Schema, Result>(options: ProtectedPostgresTransactionOptions<TSchema>, work: (scope: ProtectedPostgresTransaction<TSchema>) => Promise<Result>): Promise<Result>;
|
|
21
|
-
export {};
|
|
1
|
+
import type { PgClientLike } from "./postgres-types.js";
|
|
2
|
+
import { type ProtectedPgTransaction, type ProtectedPgTransactionOptions } from "./with-protected-pg-transaction.js";
|
|
3
|
+
export type ProtectedPostgresTransactionOptions<Client extends PgClientLike = PgClientLike> = ProtectedPgTransactionOptions<Client>;
|
|
4
|
+
export type ProtectedPostgresTransaction<Client extends PgClientLike = PgClientLike> = ProtectedPgTransaction<Client>;
|
|
5
|
+
/** Run one protected write on a plain `pg.Pool` with `BEGIN ISOLATION LEVEL SERIALIZABLE`. */
|
|
6
|
+
export declare function withProtectedPostgresTransaction<Client extends PgClientLike, Result>(options: ProtectedPostgresTransactionOptions<Client>, work: (scope: ProtectedPostgresTransaction<Client>) => PromiseLike<Result>): Promise<Result>;
|
|
22
7
|
//# sourceMappingURL=with-protected-postgres-transaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-protected-postgres-transaction.d.ts","sourceRoot":"","sources":["../src/with-protected-postgres-transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"with-protected-postgres-transaction.d.ts","sourceRoot":"","sources":["../src/with-protected-postgres-transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAEL,KAAK,sBAAsB,EAC3B,KAAK,6BAA6B,EACnC,MAAM,oCAAoC,CAAC;AAE5C,MAAM,MAAM,mCAAmC,CAAC,MAAM,SAAS,YAAY,GAAG,YAAY,IACxF,6BAA6B,CAAC,MAAM,CAAC,CAAC;AAExC,MAAM,MAAM,4BAA4B,CAAC,MAAM,SAAS,YAAY,GAAG,YAAY,IAAI,sBAAsB,CAAC,MAAM,CAAC,CAAC;AAEtH,8FAA8F;AAC9F,wBAAgB,gCAAgC,CAAC,MAAM,SAAS,YAAY,EAAE,MAAM,EAClF,OAAO,EAAE,mCAAmC,CAAC,MAAM,CAAC,EACpD,IAAI,EAAE,CAAC,KAAK,EAAE,4BAA4B,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC,MAAM,CAAC,GACzE,OAAO,CAAC,MAAM,CAAC,CAEjB"}
|
|
@@ -1,47 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/** Run one protected write with authorization and application data on the same client. */
|
|
6
|
-
export async function withProtectedPostgresTransaction(options, work) {
|
|
7
|
-
try {
|
|
8
|
-
assertProtectedAuthorizationCanJoin(options.authorization, options.transaction);
|
|
9
|
-
if (options.transaction !== undefined) {
|
|
10
|
-
return await runProtectedWork(options.transaction, options.authorization, work);
|
|
11
|
-
}
|
|
12
|
-
if (is(options.database, PgTransaction)) {
|
|
13
|
-
throw new Error("Pass an existing Postgres transaction as transaction");
|
|
14
|
-
}
|
|
15
|
-
let started = false;
|
|
16
|
-
return await options.database.transaction((tx) => {
|
|
17
|
-
if (started)
|
|
18
|
-
throw new Error("Protected Postgres callbacks cannot be replayed");
|
|
19
|
-
started = true;
|
|
20
|
-
return runProtectedWork(tx, options.authorization, work);
|
|
21
|
-
}, { isolationLevel: "serializable" });
|
|
22
|
-
}
|
|
23
|
-
catch (error) {
|
|
24
|
-
const conflict = readPostgresSerializationConflict(error);
|
|
25
|
-
if (conflict !== undefined)
|
|
26
|
-
throw conflict;
|
|
27
|
-
throw error;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
async function runProtectedWork(tx, authorization, work) {
|
|
31
|
-
if (!is(tx, PgTransaction)) {
|
|
32
|
-
throw new Error("Protected Postgres writes require a Drizzle transaction");
|
|
33
|
-
}
|
|
34
|
-
// A SELECT takes the transaction snapshot, so isolation cannot later be lowered.
|
|
35
|
-
const isolation = await tx.execute(sql `select current_setting('transaction_isolation') as isolation`);
|
|
36
|
-
if (isolation.rows[0]?.isolation !== "serializable") {
|
|
37
|
-
throw new Error("Protected Postgres writes require serializable isolation");
|
|
38
|
-
}
|
|
39
|
-
const scope = openProtectedAuthorization(authorization, tx);
|
|
40
|
-
try {
|
|
41
|
-
return await work({ tx, authorization: scope.authorization });
|
|
42
|
-
}
|
|
43
|
-
finally {
|
|
44
|
-
scope.close();
|
|
45
|
-
}
|
|
1
|
+
import { withProtectedPgTransaction, } from "./with-protected-pg-transaction.js";
|
|
2
|
+
/** Run one protected write on a plain `pg.Pool` with `BEGIN ISOLATION LEVEL SERIALIZABLE`. */
|
|
3
|
+
export function withProtectedPostgresTransaction(options, work) {
|
|
4
|
+
return withProtectedPgTransaction(options, work);
|
|
46
5
|
}
|
|
47
6
|
//# sourceMappingURL=with-protected-postgres-transaction.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-protected-postgres-transaction.js","sourceRoot":"","sources":["../src/with-protected-postgres-transaction.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"with-protected-postgres-transaction.js","sourceRoot":"","sources":["../src/with-protected-postgres-transaction.ts"],"names":[],"mappings":"AACA,OAAO,EACL,0BAA0B,GAG3B,MAAM,oCAAoC,CAAC;AAO5C,8FAA8F;AAC9F,MAAM,UAAU,gCAAgC,CAC9C,OAAoD,EACpD,IAA0E;IAE1E,OAAO,0BAA0B,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACnD,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { PostgresAuthorization } from "./postgres-authorization.js";
|
|
2
|
+
import type { PgClientLike, PgPoolLike } from "./postgres-types.js";
|
|
3
|
+
export type ReadSnapshotOptions<Client extends PgClientLike = PgClientLike> = {
|
|
4
|
+
readonly pool: PgPoolLike<Client>;
|
|
5
|
+
readonly authorization: PostgresAuthorization;
|
|
6
|
+
};
|
|
7
|
+
export type ReadSnapshotScope<Client extends PgClientLike = PgClientLike> = {
|
|
8
|
+
readonly client: Client;
|
|
9
|
+
readonly authorization: PostgresAuthorization;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Run one consistent read (for example `resolveFilter` plus a paginated
|
|
13
|
+
* `SELECT`) on a plain node-postgres pool using
|
|
14
|
+
* `BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY`.
|
|
15
|
+
*
|
|
16
|
+
* Keeps authorization pruning and the row query on the exact same PostgreSQL
|
|
17
|
+
* MVCC snapshot without `SERIALIZABLE` write-conflict retry overhead, and
|
|
18
|
+
* closes the scoped authorization handle as soon as the callback returns.
|
|
19
|
+
*/
|
|
20
|
+
export declare function withReadSnapshot<Client extends PgClientLike, Result>(options: ReadSnapshotOptions<Client>, work: (scope: ReadSnapshotScope<Client>) => PromiseLike<Result>): Promise<Result>;
|
|
21
|
+
//# sourceMappingURL=with-read-snapshot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-read-snapshot.d.ts","sourceRoot":"","sources":["../src/with-read-snapshot.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AACzE,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAMpE,MAAM,MAAM,mBAAmB,CAAC,MAAM,SAAS,YAAY,GAAG,YAAY,IAAI;IAC5E,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;IAClC,QAAQ,CAAC,aAAa,EAAE,qBAAqB,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,iBAAiB,CAAC,MAAM,SAAS,YAAY,GAAG,YAAY,IAAI;IAC1E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,aAAa,EAAE,qBAAqB,CAAC;CAC/C,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAsB,gBAAgB,CAAC,MAAM,SAAS,YAAY,EAAE,MAAM,EACxE,OAAO,EAAE,mBAAmB,CAAC,MAAM,CAAC,EACpC,IAAI,EAAE,CAAC,KAAK,EAAE,iBAAiB,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC,MAAM,CAAC,GAC9D,OAAO,CAAC,MAAM,CAAC,CAgBjB"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { abortPgTransaction, releasePgClient } from "./pg-transaction-lifecycle.js";
|
|
2
|
+
import { assertProtectedAuthorizationCanJoin, openProtectedAuthorizationScope, } from "./protected-authorization-scope.js";
|
|
3
|
+
/**
|
|
4
|
+
* Run one consistent read (for example `resolveFilter` plus a paginated
|
|
5
|
+
* `SELECT`) on a plain node-postgres pool using
|
|
6
|
+
* `BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY`.
|
|
7
|
+
*
|
|
8
|
+
* Keeps authorization pruning and the row query on the exact same PostgreSQL
|
|
9
|
+
* MVCC snapshot without `SERIALIZABLE` write-conflict retry overhead, and
|
|
10
|
+
* closes the scoped authorization handle as soon as the callback returns.
|
|
11
|
+
*/
|
|
12
|
+
export async function withReadSnapshot(options, work) {
|
|
13
|
+
assertProtectedAuthorizationCanJoin(options.authorization, undefined);
|
|
14
|
+
const client = await options.pool.connect();
|
|
15
|
+
const state = {
|
|
16
|
+
began: false,
|
|
17
|
+
committed: false,
|
|
18
|
+
destroyClient: false,
|
|
19
|
+
};
|
|
20
|
+
try {
|
|
21
|
+
return await executeReadSnapshot(client, options.authorization, work, state);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
state.destroyClient = await abortPgTransaction(client, state);
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
releasePgClient(client, state.destroyClient);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function executeReadSnapshot(client, authorization, work, state) {
|
|
32
|
+
await client.query("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY");
|
|
33
|
+
state.began = true;
|
|
34
|
+
const scope = openProtectedAuthorizationScope(authorization, client, client);
|
|
35
|
+
let result;
|
|
36
|
+
try {
|
|
37
|
+
result = await work({ client, authorization: scope.authorization });
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
scope.close();
|
|
41
|
+
}
|
|
42
|
+
await client.query("COMMIT");
|
|
43
|
+
state.committed = true;
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=with-read-snapshot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-read-snapshot.js","sourceRoot":"","sources":["../src/with-read-snapshot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAA2B,MAAM,+BAA+B,CAAC;AAG7G,OAAO,EACL,mCAAmC,EACnC,+BAA+B,GAChC,MAAM,oCAAoC,CAAC;AAY5C;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,OAAoC,EACpC,IAA+D;IAE/D,mCAAmC,CAAC,OAAO,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;IACtE,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;IAC5C,MAAM,KAAK,GAAuB;QAChC,KAAK,EAAE,KAAK;QACZ,SAAS,EAAE,KAAK;QAChB,aAAa,EAAE,KAAK;KACrB,CAAC;IACF,IAAI,CAAC;QACH,OAAO,MAAM,mBAAmB,CAAC,MAAM,EAAE,OAAO,CAAC,aAAa,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IAC/E,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,KAAK,CAAC,aAAa,GAAG,MAAM,kBAAkB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAC9D,MAAM,KAAK,CAAC;IACd,CAAC;YAAS,CAAC;QACT,eAAe,CAAC,MAAM,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC;AAED,KAAK,UAAU,mBAAmB,CAChC,MAAc,EACd,aAAoC,EACpC,IAA+D,EAC/D,KAAyB;IAEzB,MAAM,MAAM,CAAC,KAAK,CAAC,6DAA6D,CAAC,CAAC;IAClF,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;IACnB,MAAM,KAAK,GAAG,+BAA+B,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7E,IAAI,MAAc,CAAC;IACnB,IAAI,CAAC;QACH,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE,MAAM,EAAE,aAAa,EAAE,KAAK,CAAC,aAAa,EAAE,CAAC,CAAC;IACtE,CAAC;YAAS,CAAC;QACT,KAAK,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC;IACD,MAAM,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC7B,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC;IACvB,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dads-framework/postgres-access",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22"
|
|
@@ -9,6 +9,14 @@
|
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./drizzle": {
|
|
14
|
+
"types": "./dist/drizzle.d.ts",
|
|
15
|
+
"default": "./dist/drizzle.js"
|
|
16
|
+
},
|
|
17
|
+
"./testing": {
|
|
18
|
+
"types": "./dist/testing.d.ts",
|
|
19
|
+
"default": "./dist/testing.js"
|
|
12
20
|
}
|
|
13
21
|
},
|
|
14
22
|
"files": [
|
|
@@ -17,12 +25,26 @@
|
|
|
17
25
|
"BENCHMARK.md"
|
|
18
26
|
],
|
|
19
27
|
"dependencies": {
|
|
20
|
-
"@dads-framework/access": "0.
|
|
21
|
-
|
|
28
|
+
"@dads-framework/access": "0.4.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@dads-framework/postgres": "0.4.0",
|
|
22
32
|
"drizzle-orm": "^0.45.2"
|
|
23
33
|
},
|
|
34
|
+
"peerDependenciesMeta": {
|
|
35
|
+
"@dads-framework/postgres": {
|
|
36
|
+
"optional": true
|
|
37
|
+
},
|
|
38
|
+
"drizzle-orm": {
|
|
39
|
+
"optional": true
|
|
40
|
+
}
|
|
41
|
+
},
|
|
24
42
|
"devDependencies": {
|
|
25
|
-
"@dads-framework/
|
|
43
|
+
"@dads-framework/postgres": "0.4.0",
|
|
44
|
+
"@dads-framework/spicedb-access": "0.4.0",
|
|
45
|
+
"@types/pg": "^8.20.0",
|
|
46
|
+
"drizzle-orm": "^0.45.2",
|
|
47
|
+
"pg": "^8.21.0",
|
|
26
48
|
"vitest": "~4.1.0",
|
|
27
49
|
"zod": "^4.4.3"
|
|
28
50
|
},
|