nowity 1.0.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/README.md +751 -0
- package/dist/api/contract.d.ts +186 -0
- package/dist/api/contract.d.ts.map +1 -0
- package/dist/api/contract.js +251 -0
- package/dist/api/errors.d.ts +50 -0
- package/dist/api/errors.d.ts.map +1 -0
- package/dist/api/errors.js +50 -0
- package/dist/api/index.d.ts +13 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +32 -0
- package/dist/client/confirmed-store.d.ts +33 -0
- package/dist/client/confirmed-store.d.ts.map +1 -0
- package/dist/client/confirmed-store.js +123 -0
- package/dist/client/connection.d.ts +102 -0
- package/dist/client/connection.d.ts.map +1 -0
- package/dist/client/connection.js +285 -0
- package/dist/client/list.d.ts +102 -0
- package/dist/client/list.d.ts.map +1 -0
- package/dist/client/list.js +257 -0
- package/dist/client/live-query.d.ts +153 -0
- package/dist/client/live-query.d.ts.map +1 -0
- package/dist/client/live-query.js +244 -0
- package/dist/client/nowity-client.d.ts +188 -0
- package/dist/client/nowity-client.d.ts.map +1 -0
- package/dist/client/nowity-client.js +580 -0
- package/dist/client/null-transport.d.ts +16 -0
- package/dist/client/null-transport.d.ts.map +1 -0
- package/dist/client/null-transport.js +17 -0
- package/dist/client/overlay.d.ts +44 -0
- package/dist/client/overlay.d.ts.map +1 -0
- package/dist/client/overlay.js +90 -0
- package/dist/client/query-source.d.ts +78 -0
- package/dist/client/query-source.d.ts.map +1 -0
- package/dist/client/query-source.js +57 -0
- package/dist/client/ssr-source.d.ts +70 -0
- package/dist/client/ssr-source.d.ts.map +1 -0
- package/dist/client/ssr-source.js +134 -0
- package/dist/client/transport.d.ts +19 -0
- package/dist/client/transport.d.ts.map +1 -0
- package/dist/client/transport.js +2 -0
- package/dist/client/ws-transport.d.ts +30 -0
- package/dist/client/ws-transport.d.ts.map +1 -0
- package/dist/client/ws-transport.js +89 -0
- package/dist/client.d.ts +47 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +80 -0
- package/dist/core/diff.d.ts +14 -0
- package/dist/core/diff.d.ts.map +1 -0
- package/dist/core/diff.js +56 -0
- package/dist/core/normalize.d.ts +16 -0
- package/dist/core/normalize.d.ts.map +1 -0
- package/dist/core/normalize.js +67 -0
- package/dist/core/protocol.d.ts +215 -0
- package/dist/core/protocol.d.ts.map +1 -0
- package/dist/core/protocol.js +14 -0
- package/dist/core/ssr-bridge.d.ts +69 -0
- package/dist/core/ssr-bridge.d.ts.map +1 -0
- package/dist/core/ssr-bridge.js +40 -0
- package/dist/core/types.d.ts +96 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +17 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +98 -0
- package/dist/ironbean/client.d.ts +7 -0
- package/dist/ironbean/client.d.ts.map +1 -0
- package/dist/ironbean/client.js +38 -0
- package/dist/ironbean/server.d.ts +42 -0
- package/dist/ironbean/server.d.ts.map +1 -0
- package/dist/ironbean/server.js +62 -0
- package/dist/ironbean/tokens.d.ts +26 -0
- package/dist/ironbean/tokens.d.ts.map +1 -0
- package/dist/ironbean/tokens.js +23 -0
- package/dist/next/config.d.ts +7 -0
- package/dist/next/config.d.ts.map +1 -0
- package/dist/next/config.js +77 -0
- package/dist/next/live.d.ts +69 -0
- package/dist/next/live.d.ts.map +1 -0
- package/dist/next/live.js +70 -0
- package/dist/next/serve.d.ts +44 -0
- package/dist/next/serve.d.ts.map +1 -0
- package/dist/next/serve.js +107 -0
- package/dist/next/server.d.ts +62 -0
- package/dist/next/server.d.ts.map +1 -0
- package/dist/next/server.js +127 -0
- package/dist/react/connection-context.d.ts +4 -0
- package/dist/react/connection-context.d.ts.map +1 -0
- package/dist/react/connection-context.js +28 -0
- package/dist/react/grid-list.d.ts +34 -0
- package/dist/react/grid-list.d.ts.map +1 -0
- package/dist/react/grid-list.js +62 -0
- package/dist/react/hydrate.d.ts +22 -0
- package/dist/react/hydrate.d.ts.map +1 -0
- package/dist/react/hydrate.js +18 -0
- package/dist/react/index.d.ts +35 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +51 -0
- package/dist/react/list.d.ts +31 -0
- package/dist/react/list.d.ts.map +1 -0
- package/dist/react/list.js +35 -0
- package/dist/react/prerendered.d.ts +33 -0
- package/dist/react/prerendered.d.ts.map +1 -0
- package/dist/react/prerendered.js +165 -0
- package/dist/react/provider.d.ts +64 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/provider.js +130 -0
- package/dist/react/refusals.d.ts +31 -0
- package/dist/react/refusals.d.ts.map +1 -0
- package/dist/react/refusals.js +51 -0
- package/dist/react/restricted.d.ts +76 -0
- package/dist/react/restricted.d.ts.map +1 -0
- package/dist/react/restricted.js +126 -0
- package/dist/react/use-list.d.ts +42 -0
- package/dist/react/use-list.d.ts.map +1 -0
- package/dist/react/use-list.js +56 -0
- package/dist/react/use-query.d.ts +66 -0
- package/dist/react/use-query.d.ts.map +1 -0
- package/dist/react/use-query.js +126 -0
- package/dist/react/virtual.d.ts +60 -0
- package/dist/react/virtual.d.ts.map +1 -0
- package/dist/react/virtual.js +131 -0
- package/dist/server/api/auth.d.ts +37 -0
- package/dist/server/api/auth.d.ts.map +1 -0
- package/dist/server/api/auth.js +54 -0
- package/dist/server/api/bridge.d.ts +28 -0
- package/dist/server/api/bridge.d.ts.map +1 -0
- package/dist/server/api/bridge.js +330 -0
- package/dist/server/api/collector.d.ts +25 -0
- package/dist/server/api/collector.d.ts.map +1 -0
- package/dist/server/api/collector.js +184 -0
- package/dist/server/api/configure.d.ts +31 -0
- package/dist/server/api/configure.d.ts.map +1 -0
- package/dist/server/api/configure.js +67 -0
- package/dist/server/api/execution.d.ts +77 -0
- package/dist/server/api/execution.d.ts.map +1 -0
- package/dist/server/api/execution.js +97 -0
- package/dist/server/api/list.d.ts +141 -0
- package/dist/server/api/list.d.ts.map +1 -0
- package/dist/server/api/list.js +281 -0
- package/dist/server/api/prerender.d.ts +32 -0
- package/dist/server/api/prerender.d.ts.map +1 -0
- package/dist/server/api/prerender.js +69 -0
- package/dist/server/api/read-keys.d.ts +128 -0
- package/dist/server/api/read-keys.d.ts.map +1 -0
- package/dist/server/api/read-keys.js +464 -0
- package/dist/server/api/read-set.d.ts +103 -0
- package/dist/server/api/read-set.d.ts.map +1 -0
- package/dist/server/api/read-set.js +210 -0
- package/dist/server/api/runtime.d.ts +36 -0
- package/dist/server/api/runtime.d.ts.map +1 -0
- package/dist/server/api/runtime.js +49 -0
- package/dist/server/api/tracked-client.d.ts +6 -0
- package/dist/server/api/tracked-client.d.ts.map +1 -0
- package/dist/server/api/tracked-client.js +56 -0
- package/dist/server/change-source/change-source.d.ts +41 -0
- package/dist/server/change-source/change-source.d.ts.map +1 -0
- package/dist/server/change-source/change-source.js +2 -0
- package/dist/server/change-source/outbox-change-source.d.ts +106 -0
- package/dist/server/change-source/outbox-change-source.d.ts.map +1 -0
- package/dist/server/change-source/outbox-change-source.js +237 -0
- package/dist/server/change-source/wal-change-source.d.ts +85 -0
- package/dist/server/change-source/wal-change-source.d.ts.map +1 -0
- package/dist/server/change-source/wal-change-source.js +193 -0
- package/dist/server/db.d.ts +51 -0
- package/dist/server/db.d.ts.map +1 -0
- package/dist/server/db.js +53 -0
- package/dist/server/evaluate.d.ts +39 -0
- package/dist/server/evaluate.d.ts.map +1 -0
- package/dist/server/evaluate.js +61 -0
- package/dist/server/invalidation-index.d.ts +64 -0
- package/dist/server/invalidation-index.d.ts.map +1 -0
- package/dist/server/invalidation-index.js +0 -0
- package/dist/server/mutator.d.ts +59 -0
- package/dist/server/mutator.d.ts.map +1 -0
- package/dist/server/mutator.js +77 -0
- package/dist/server/query.d.ts +113 -0
- package/dist/server/query.d.ts.map +1 -0
- package/dist/server/query.js +35 -0
- package/dist/server/setup.d.ts +30 -0
- package/dist/server/setup.d.ts.map +1 -0
- package/dist/server/setup.js +107 -0
- package/dist/server/sql/setup.sql +215 -0
- package/dist/server/ssr.d.ts +29 -0
- package/dist/server/ssr.d.ts.map +1 -0
- package/dist/server/ssr.js +43 -0
- package/dist/server/sync-engine.d.ts +256 -0
- package/dist/server/sync-engine.d.ts.map +1 -0
- package/dist/server/sync-engine.js +805 -0
- package/dist/server/ws-server.d.ts +56 -0
- package/dist/server/ws-server.d.ts.map +1 -0
- package/dist/server/ws-server.js +241 -0
- package/package.json +81 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.emptyOutput = emptyOutput;
|
|
4
|
+
exports.evaluateQuery = evaluateQuery;
|
|
5
|
+
exports.outputRows = outputRows;
|
|
6
|
+
const normalize_1 = require("../core/normalize");
|
|
7
|
+
const query_1 = require("./query");
|
|
8
|
+
function emptyOutput() {
|
|
9
|
+
return { index: new Map(), ids: [], extra: new Map() };
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Run a query definition against one read snapshot and normalise its result.
|
|
13
|
+
*
|
|
14
|
+
* Shared by the sync engine (which diffs consecutive outputs) and by the
|
|
15
|
+
* server-side rendering path (which serialises a single output for hydration), so
|
|
16
|
+
* the two can never disagree about what a query returns.
|
|
17
|
+
*/
|
|
18
|
+
async function evaluateQuery(def, args, session, snap) {
|
|
19
|
+
const result = (0, query_1.toQueryResult)(await def.run(args, { db: snap, session }));
|
|
20
|
+
const idField = def.idField ?? "id";
|
|
21
|
+
const ids = [];
|
|
22
|
+
const index = new Map();
|
|
23
|
+
for (const raw of result.rows) {
|
|
24
|
+
const row = (0, normalize_1.normalizeRow)(raw);
|
|
25
|
+
const id = (0, normalize_1.idOf)(row, idField);
|
|
26
|
+
ids.push(id);
|
|
27
|
+
index.set(id, row);
|
|
28
|
+
}
|
|
29
|
+
const extra = new Map();
|
|
30
|
+
for (const [entity, rows] of Object.entries(result.extra ?? {})) {
|
|
31
|
+
const field = def.extraIdFields?.[entity] ?? "id";
|
|
32
|
+
const map = new Map();
|
|
33
|
+
for (const raw of rows) {
|
|
34
|
+
const row = (0, normalize_1.normalizeRow)(raw);
|
|
35
|
+
map.set((0, normalize_1.idOf)(row, field), row);
|
|
36
|
+
}
|
|
37
|
+
extra.set(entity, map);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
index,
|
|
41
|
+
ids,
|
|
42
|
+
extra,
|
|
43
|
+
...(result.tables ? { tables: result.tables } : {}),
|
|
44
|
+
...(result.keys ? { keys: result.keys } : {}),
|
|
45
|
+
...(result.entity ? { entity: result.entity } : {}),
|
|
46
|
+
...(result.single ? { single: true } : {}),
|
|
47
|
+
...(result.order ? { order: result.order } : {}),
|
|
48
|
+
...(result.filter ? { filter: result.filter } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Rows of an output as plain arrays, ready to be serialised. */
|
|
52
|
+
function outputRows(output) {
|
|
53
|
+
const extra = {};
|
|
54
|
+
for (const [entity, map] of output.extra) {
|
|
55
|
+
extra[entity] = [...map.values()];
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
rows: output.ids.map((id) => output.index.get(id)),
|
|
59
|
+
...(Object.keys(extra).length ? { extra } : {}),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which subscriptions a change can possibly affect.
|
|
3
|
+
*
|
|
4
|
+
* The engine used to answer this by table name: a change to `Game` meant every
|
|
5
|
+
* subscription that had read `Game`, which is every open game in the process.
|
|
6
|
+
* One move therefore cost one query re-execution per subscription, and since the
|
|
7
|
+
* number of moves grows with the number of games too, the total cost grew with
|
|
8
|
+
* their product. Nothing was wrong with the answers; there were just far too
|
|
9
|
+
* many of them.
|
|
10
|
+
*
|
|
11
|
+
* This is the same question asked of {@link ReadKey}s instead. A subscription
|
|
12
|
+
* that read `Game` *where id = 42* is filed under that value, and a move in game
|
|
13
|
+
* 7 never finds it. What the engine gets back is the set of subscriptions whose
|
|
14
|
+
* result the change could actually have altered — usually two or three, whatever
|
|
15
|
+
* the size of the process.
|
|
16
|
+
*
|
|
17
|
+
* ## Soundness
|
|
18
|
+
*
|
|
19
|
+
* Missing a subscription here is invisible: it stops updating and nothing
|
|
20
|
+
* reports it. Returning one too many costs a recomputation whose diff comes out
|
|
21
|
+
* empty. So every uncertainty resolves towards returning more:
|
|
22
|
+
*
|
|
23
|
+
* - a subscription whose read set could not be narrowed is in {@link always},
|
|
24
|
+
* and comes back for every change;
|
|
25
|
+
* - a table read without a usable filter is watched whole;
|
|
26
|
+
* - both images of a change are matched, because a row *leaving* a filter
|
|
27
|
+
* matters exactly as much as one entering it, and only the before-image can
|
|
28
|
+
* show it;
|
|
29
|
+
* - when the before-image is not complete — logical replication without
|
|
30
|
+
* `REPLICA IDENTITY FULL` carries only the primary key — every subscription
|
|
31
|
+
* filed under a column whose old value is therefore unknown is returned. That
|
|
32
|
+
* is the old table-wide behaviour, confined to the columns that actually lost
|
|
33
|
+
* information rather than applied to the whole table.
|
|
34
|
+
*/
|
|
35
|
+
import type { ChangeEvent, ReadKeySet } from "../core/types";
|
|
36
|
+
export declare class InvalidationIndex<T> {
|
|
37
|
+
/** Subscriptions whose read set is not known, so every change may matter. */
|
|
38
|
+
private readonly always;
|
|
39
|
+
/** table → subscriptions watching all of it. */
|
|
40
|
+
private readonly anyTable;
|
|
41
|
+
/** table → column → value token → subscriptions. */
|
|
42
|
+
private readonly byValue;
|
|
43
|
+
private readonly placements;
|
|
44
|
+
get size(): number;
|
|
45
|
+
/**
|
|
46
|
+
* File a subscription under everything it read.
|
|
47
|
+
*
|
|
48
|
+
* `keys` of `null` means the read set is unknown, which is the pessimistic
|
|
49
|
+
* value: such a subscription is returned for every change until an execution
|
|
50
|
+
* reports what it actually read.
|
|
51
|
+
*/
|
|
52
|
+
add(sub: T, keys: ReadKeySet | null): void;
|
|
53
|
+
private watchWholeTable;
|
|
54
|
+
remove(sub: T): void;
|
|
55
|
+
/** Every subscription one batch of changes could have affected. */
|
|
56
|
+
collect(events: readonly ChangeEvent[], into: Set<T>): Set<T>;
|
|
57
|
+
/** Diagnostics: how many distinct value buckets are being maintained. */
|
|
58
|
+
stats(): {
|
|
59
|
+
always: number;
|
|
60
|
+
anyTables: number;
|
|
61
|
+
valueBuckets: number;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=invalidation-index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invalidation-index.d.ts","sourceRoot":"","sources":["../../src/server/invalidation-index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,OAAO,KAAK,EAAE,WAAW,EAAW,UAAU,EAAE,MAAM,eAAe,CAAC;AAYtE,qBAAa,iBAAiB,CAAC,CAAC;IAC5B,6EAA6E;IAC7E,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;IACvC,gDAAgD;IAChD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA6B;IACtD,oDAAoD;IACpD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAuD;IAC/E,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA2B;IAEtD,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;;;;OAMG;IACH,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI,GAAG,IAAI;IAqC1C,OAAO,CAAC,eAAe;IAOvB,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,IAAI;IA4BpB,mEAAmE;IACnE,OAAO,CAAC,MAAM,EAAE,SAAS,WAAW,EAAE,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;IAiC7D,yEAAyE;IACzE,KAAK,IAAI;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE;CAOvE"}
|
|
Binary file
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Pool } from "pg";
|
|
2
|
+
import type { Queryable } from "./db";
|
|
3
|
+
import type { SessionInfo } from "./query";
|
|
4
|
+
export interface WriteContext extends Queryable {
|
|
5
|
+
/** Adapter-specific client inside the write transaction. */
|
|
6
|
+
readonly client: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface WriteRunner {
|
|
9
|
+
withWrite<T>(fn: (w: WriteContext) => Promise<T>): Promise<T>;
|
|
10
|
+
}
|
|
11
|
+
export declare class PgWriteRunner implements WriteRunner {
|
|
12
|
+
private readonly pool;
|
|
13
|
+
constructor(pool: Pool);
|
|
14
|
+
withWrite<T>(fn: (w: WriteContext) => Promise<T>): Promise<T>;
|
|
15
|
+
}
|
|
16
|
+
export declare class PrismaWriteRunner implements WriteRunner {
|
|
17
|
+
private readonly prisma;
|
|
18
|
+
private readonly timeoutMs;
|
|
19
|
+
constructor(prisma: {
|
|
20
|
+
$transaction<T>(fn: (tx: any) => Promise<T>, opts?: {
|
|
21
|
+
timeout?: number;
|
|
22
|
+
}): Promise<T>;
|
|
23
|
+
}, timeoutMs?: number);
|
|
24
|
+
withWrite<T>(fn: (w: WriteContext) => Promise<T>): Promise<T>;
|
|
25
|
+
}
|
|
26
|
+
export interface MutatorContext {
|
|
27
|
+
/** Runs inside the mutation's write transaction. */
|
|
28
|
+
db: WriteContext;
|
|
29
|
+
session: SessionInfo;
|
|
30
|
+
mutationId: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A named mutator — the write path primitive (§3 of the design doc).
|
|
34
|
+
*
|
|
35
|
+
* The same *operation* exists twice: `server` is authoritative and runs inside
|
|
36
|
+
* one database transaction, `local` (registered on the client, see
|
|
37
|
+
* `client/mutators.ts`) runs immediately against the local store and can be
|
|
38
|
+
* replayed. Not "patch this entity" but a domain operation, because only a
|
|
39
|
+
* domain operation can be re-applied on top of newly arrived server state
|
|
40
|
+
* without changing its meaning.
|
|
41
|
+
*
|
|
42
|
+
* Throwing from `server` rejects the mutation: the transaction rolls back and
|
|
43
|
+
* the client removes the optimistic effect.
|
|
44
|
+
*/
|
|
45
|
+
export interface MutatorDef<A = any> {
|
|
46
|
+
name: string;
|
|
47
|
+
server(args: A, ctx: MutatorContext): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
/** Thrown by a mutator to reject the write with a message meant for the user. */
|
|
50
|
+
export declare class MutationRejected extends Error {
|
|
51
|
+
constructor(message: string);
|
|
52
|
+
}
|
|
53
|
+
export declare class MutatorRegistry {
|
|
54
|
+
private readonly defs;
|
|
55
|
+
define<A>(def: MutatorDef<A>): MutatorDef<A>;
|
|
56
|
+
get(name: string): MutatorDef<any>;
|
|
57
|
+
has(name: string): boolean;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=mutator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutator.d.ts","sourceRoot":"","sources":["../../src/server/mutator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAc,MAAM,IAAI,CAAC;AAC3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AACtC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,MAAM,WAAW,YAAa,SAAQ,SAAS;IAC3C,4DAA4D;IAC5D,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAC5B;AAED,MAAM,WAAW,WAAW;IACxB,SAAS,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,YAAY,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CACjE;AAED,qBAAa,aAAc,YAAW,WAAW;IACjC,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,EAAE,IAAI;IAEjC,SAAS,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,YAAY,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAkBtE;AAED,qBAAa,iBAAkB,YAAW,WAAW;IAE7C,OAAO,CAAC,QAAQ,CAAC,MAAM;IAGvB,OAAO,CAAC,QAAQ,CAAC,SAAS;gBAHT,MAAM,EAAE;QACrB,YAAY,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE;YAAE,OAAO,CAAC,EAAE,MAAM,CAAA;SAAE,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;KACzF,EACgB,SAAS,SAAS;IAGjC,SAAS,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,YAAY,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAiBtE;AAED,MAAM,WAAW,cAAc;IAC3B,oDAAoD;IACpD,EAAE,EAAE,YAAY,CAAC;IACjB,OAAO,EAAE,WAAW,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,UAAU,CAAC,CAAC,GAAG,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACvD;AAED,iFAAiF;AACjF,qBAAa,gBAAiB,SAAQ,KAAK;gBAC3B,OAAO,EAAE,MAAM;CAI9B;AAED,qBAAa,eAAe;IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAsC;IAE3D,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;IAQ5C,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC,GAAG,CAAC;IAMlC,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;CAG7B"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MutatorRegistry = exports.MutationRejected = exports.PrismaWriteRunner = exports.PgWriteRunner = void 0;
|
|
4
|
+
class PgWriteRunner {
|
|
5
|
+
constructor(pool) {
|
|
6
|
+
this.pool = pool;
|
|
7
|
+
}
|
|
8
|
+
async withWrite(fn) {
|
|
9
|
+
const client = await this.pool.connect();
|
|
10
|
+
try {
|
|
11
|
+
await client.query("BEGIN");
|
|
12
|
+
const w = {
|
|
13
|
+
client,
|
|
14
|
+
query: (text, values) => client.query(text, values),
|
|
15
|
+
};
|
|
16
|
+
const result = await fn(w);
|
|
17
|
+
await client.query("COMMIT");
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
await client.query("ROLLBACK").catch(() => undefined);
|
|
22
|
+
throw err;
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
client.release();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.PgWriteRunner = PgWriteRunner;
|
|
30
|
+
class PrismaWriteRunner {
|
|
31
|
+
constructor(prisma, timeoutMs = 15000) {
|
|
32
|
+
this.prisma = prisma;
|
|
33
|
+
this.timeoutMs = timeoutMs;
|
|
34
|
+
}
|
|
35
|
+
async withWrite(fn) {
|
|
36
|
+
return this.prisma.$transaction(async (tx) => {
|
|
37
|
+
const w = {
|
|
38
|
+
client: tx,
|
|
39
|
+
query: async (text, values) => ({
|
|
40
|
+
rows: (await tx.$queryRawUnsafe(text, ...(values ?? []))),
|
|
41
|
+
}),
|
|
42
|
+
};
|
|
43
|
+
return fn(w);
|
|
44
|
+
}, { timeout: this.timeoutMs });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.PrismaWriteRunner = PrismaWriteRunner;
|
|
48
|
+
/** Thrown by a mutator to reject the write with a message meant for the user. */
|
|
49
|
+
class MutationRejected extends Error {
|
|
50
|
+
constructor(message) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.name = "MutationRejected";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.MutationRejected = MutationRejected;
|
|
56
|
+
class MutatorRegistry {
|
|
57
|
+
constructor() {
|
|
58
|
+
this.defs = new Map();
|
|
59
|
+
}
|
|
60
|
+
define(def) {
|
|
61
|
+
if (this.defs.has(def.name)) {
|
|
62
|
+
throw new Error(`Mutator "${def.name}" is already defined`);
|
|
63
|
+
}
|
|
64
|
+
this.defs.set(def.name, def);
|
|
65
|
+
return def;
|
|
66
|
+
}
|
|
67
|
+
get(name) {
|
|
68
|
+
const def = this.defs.get(name);
|
|
69
|
+
if (!def)
|
|
70
|
+
throw new Error(`Unknown mutator "${name}"`);
|
|
71
|
+
return def;
|
|
72
|
+
}
|
|
73
|
+
has(name) {
|
|
74
|
+
return this.defs.has(name);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.MutatorRegistry = MutatorRegistry;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { OrderKey, RowFilter } from "../core/protocol";
|
|
2
|
+
import type { ReadKeySet, Row } from "../core/types";
|
|
3
|
+
import type { SnapshotContext } from "./db";
|
|
4
|
+
/** Identity of the connection a query or mutation runs for. */
|
|
5
|
+
export interface SessionInfo {
|
|
6
|
+
clientId: string;
|
|
7
|
+
userId: string | null;
|
|
8
|
+
meta: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
export interface QueryContext {
|
|
11
|
+
/** Snapshot-bound database access. Every query of one batch shares it. */
|
|
12
|
+
db: SnapshotContext;
|
|
13
|
+
session: SessionInfo;
|
|
14
|
+
}
|
|
15
|
+
export interface QueryResult {
|
|
16
|
+
/** Rows of the primary entity, in the order the client should show them. */
|
|
17
|
+
rows: Row[];
|
|
18
|
+
/**
|
|
19
|
+
* Related entities pulled in by the same query, keyed by entity name. They
|
|
20
|
+
* land in the client's identity map and are diffed like primary rows, but
|
|
21
|
+
* they are not part of the query's ordered result.
|
|
22
|
+
*/
|
|
23
|
+
extra?: Record<string, Row[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Tables this particular execution read, when the query can tell.
|
|
26
|
+
*
|
|
27
|
+
* A run-time read set beats the declared {@link QueryDef.tables}: it is what
|
|
28
|
+
* the query actually asked the database for, so it cannot drift out of date
|
|
29
|
+
* the way a hand-written list does. `undefined` means "no better information
|
|
30
|
+
* than the declaration".
|
|
31
|
+
*/
|
|
32
|
+
tables?: string[];
|
|
33
|
+
/**
|
|
34
|
+
* The same read set, narrowed to rows.
|
|
35
|
+
*
|
|
36
|
+
* `tables` says the query read `Game`; this says it read the game whose id
|
|
37
|
+
* is `42`, so a move in any other game never reaches it. Every table named
|
|
38
|
+
* here is also in `tables`; a table named there and missing here is watched
|
|
39
|
+
* whole, which is correct and merely slower.
|
|
40
|
+
*/
|
|
41
|
+
keys?: ReadKeySet;
|
|
42
|
+
/**
|
|
43
|
+
* Entity name for these rows, when only the execution can tell (the API
|
|
44
|
+
* layer derives it from the Prisma model that produced them).
|
|
45
|
+
*/
|
|
46
|
+
entity?: string;
|
|
47
|
+
/** The endpoint returned a single row (or null), not a list. */
|
|
48
|
+
single?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* How the rows are ordered, when the execution could read it off the query.
|
|
51
|
+
*
|
|
52
|
+
* Travels to the browser so a locally changed row can be placed before the
|
|
53
|
+
* server answers. Derived, never declared: it is the `orderBy` the endpoint
|
|
54
|
+
* already wrote.
|
|
55
|
+
*/
|
|
56
|
+
order?: OrderKey[];
|
|
57
|
+
/** Which rows belong to this result, when the browser can decide it itself. */
|
|
58
|
+
filter?: RowFilter;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A live query — the unit of subscription (§1.1 of the design doc).
|
|
62
|
+
*
|
|
63
|
+
* Not `entity:id`, and not a table: a client subscribes to the *result of a
|
|
64
|
+
* query*, so a row that enters or leaves the result is handled by construction
|
|
65
|
+
* even when none of the data already in the result changed.
|
|
66
|
+
*
|
|
67
|
+
* `run` must contain the permission check itself (§1.2). There is deliberately
|
|
68
|
+
* no separate "permission filter" stage between the database and the socket: a
|
|
69
|
+
* query that cannot be seen must return no rows, so that every recomputation is
|
|
70
|
+
* correct by construction instead of correct by remembering to filter.
|
|
71
|
+
*/
|
|
72
|
+
export interface QueryDef<A = any> {
|
|
73
|
+
name: string;
|
|
74
|
+
/** Entity name under which rows are stored in the client identity map. */
|
|
75
|
+
entity: string;
|
|
76
|
+
/**
|
|
77
|
+
* True when this query reports its own read set (see {@link QueryResult.tables}).
|
|
78
|
+
* Until the first execution reports one, the query is treated as depending on
|
|
79
|
+
* everything: not knowing what it reads may never mean not being invalidated.
|
|
80
|
+
*/
|
|
81
|
+
tracksReads?: boolean;
|
|
82
|
+
/** Field holding the entity identity. Default `id`. */
|
|
83
|
+
idField?: string;
|
|
84
|
+
/**
|
|
85
|
+
* Tables whose change may affect this query's result — its invalidation
|
|
86
|
+
* dependencies. Include permission tables if the *result* depends on them.
|
|
87
|
+
*/
|
|
88
|
+
tables: string[];
|
|
89
|
+
/** Id fields of the `extra` entity sets, when not `id`. */
|
|
90
|
+
extraIdFields?: Record<string, string>;
|
|
91
|
+
/**
|
|
92
|
+
* Authorization scope this subscription belongs to, e.g. `project:123`.
|
|
93
|
+
* A permission change in that scope invalidates the subscription.
|
|
94
|
+
*/
|
|
95
|
+
scope?(args: A, ctx: QueryContext): string | null;
|
|
96
|
+
/**
|
|
97
|
+
* Optional explicit check, evaluated on subscribe and on every permission
|
|
98
|
+
* invalidation. Returning false revokes the subscription instead of leaving
|
|
99
|
+
* the client with an empty result. It is an addition to the check inside
|
|
100
|
+
* `run`, never a replacement for it.
|
|
101
|
+
*/
|
|
102
|
+
authorize?(args: A, ctx: QueryContext): Promise<boolean> | boolean;
|
|
103
|
+
run(args: A, ctx: QueryContext): Promise<QueryResult | Row[]>;
|
|
104
|
+
}
|
|
105
|
+
export declare class QueryRegistry {
|
|
106
|
+
private readonly defs;
|
|
107
|
+
define<A>(def: QueryDef<A>): QueryDef<A>;
|
|
108
|
+
get(name: string): QueryDef<any>;
|
|
109
|
+
has(name: string): boolean;
|
|
110
|
+
names(): string[];
|
|
111
|
+
}
|
|
112
|
+
export declare function toQueryResult(result: QueryResult | Row[]): QueryResult;
|
|
113
|
+
//# sourceMappingURL=query.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../src/server/query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAE5C,+DAA+D;AAC/D,MAAM,WAAW,WAAW;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC;AAED,MAAM,WAAW,YAAY;IACzB,0EAA0E;IAC1E,EAAE,EAAE,eAAe,CAAC;IACpB,OAAO,EAAE,WAAW,CAAC;CACxB;AAED,MAAM,WAAW,WAAW;IACxB,4EAA4E;IAC5E,IAAI,EAAE,GAAG,EAAE,CAAC;IACZ;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;IAC9B;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB;;;;;;;OAOG;IACH,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAC;IACnB,+EAA+E;IAC/E,MAAM,CAAC,EAAE,SAAS,CAAC;CACtB;AAED;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,QAAQ,CAAC,CAAC,GAAG,GAAG;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,0EAA0E;IAC1E,MAAM,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,2DAA2D;IAC3D,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACvC;;;OAGG;IACH,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,YAAY,GAAG,MAAM,GAAG,IAAI,CAAC;IAClD;;;;;OAKG;IACH,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;IACnE,GAAG,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,GAAG,GAAG,EAAE,CAAC,CAAC;CACjE;AAED,qBAAa,aAAa;IACtB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAoC;IAEzD,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC;IAaxC,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAAC,GAAG,CAAC;IAMhC,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI1B,KAAK,IAAI,MAAM,EAAE;CAGpB;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,WAAW,GAAG,GAAG,EAAE,GAAG,WAAW,CAEtE"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueryRegistry = void 0;
|
|
4
|
+
exports.toQueryResult = toQueryResult;
|
|
5
|
+
class QueryRegistry {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.defs = new Map();
|
|
8
|
+
}
|
|
9
|
+
define(def) {
|
|
10
|
+
if (this.defs.has(def.name)) {
|
|
11
|
+
throw new Error(`Query "${def.name}" is already defined`);
|
|
12
|
+
}
|
|
13
|
+
if (def.tables.length === 0 && !def.tracksReads) {
|
|
14
|
+
throw new Error(`Query "${def.name}" declares no tables; it would never be recomputed`);
|
|
15
|
+
}
|
|
16
|
+
this.defs.set(def.name, def);
|
|
17
|
+
return def;
|
|
18
|
+
}
|
|
19
|
+
get(name) {
|
|
20
|
+
const def = this.defs.get(name);
|
|
21
|
+
if (!def)
|
|
22
|
+
throw new Error(`Unknown query "${name}"`);
|
|
23
|
+
return def;
|
|
24
|
+
}
|
|
25
|
+
has(name) {
|
|
26
|
+
return this.defs.has(name);
|
|
27
|
+
}
|
|
28
|
+
names() {
|
|
29
|
+
return [...this.defs.keys()];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.QueryRegistry = QueryRegistry;
|
|
33
|
+
function toQueryResult(result) {
|
|
34
|
+
return Array.isArray(result) ? { rows: result } : result;
|
|
35
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Pool } from "pg";
|
|
2
|
+
/**
|
|
3
|
+
* Tables to capture changes for, with their primary key columns.
|
|
4
|
+
* `['id']` is assumed when a plain array of table names is given.
|
|
5
|
+
*/
|
|
6
|
+
export type TrackedTables = string[] | Record<string, string[]>;
|
|
7
|
+
export declare const NOWITY_SETUP_SQL_PATH: string;
|
|
8
|
+
/**
|
|
9
|
+
* Install Nowity's change capture and attach it to the given tables.
|
|
10
|
+
*
|
|
11
|
+
* Idempotent, so it can run at boot. In a real deployment put the contents of
|
|
12
|
+
* `setup.sql` into a migration instead and call {@link trackTable} from it.
|
|
13
|
+
*/
|
|
14
|
+
export declare function setupNowity(pool: Pool, tables?: TrackedTables): Promise<void>;
|
|
15
|
+
/** Attach the capture trigger to one table. */
|
|
16
|
+
export declare function trackTable(pool: Pool, table: string, pk?: string[]): Promise<void>;
|
|
17
|
+
export declare function untrackTable(pool: Pool, table: string): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Make `UPDATE`/`DELETE` put the whole previous row into the WAL.
|
|
20
|
+
*
|
|
21
|
+
* Required for every permission table when using the WAL change source. With the
|
|
22
|
+
* default replica identity a `DELETE` is logged as its primary key alone, so
|
|
23
|
+
* `DELETE FROM "ProjectMember"` arrives without the `projectId` — and the scope to
|
|
24
|
+
* invalidate cannot be derived from it. The outbox source does not need this: its
|
|
25
|
+
* trigger sees the full `OLD` row.
|
|
26
|
+
*
|
|
27
|
+
* Costs WAL volume on wide tables, which is why it is opt-in and per table.
|
|
28
|
+
*/
|
|
29
|
+
export declare function setReplicaIdentityFull(pool: Pool, table: string): Promise<void>;
|
|
30
|
+
//# sourceMappingURL=setup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/server/setup.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAE/B;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;AAchE,eAAO,MAAM,qBAAqB,QAA2C,CAAC;AAE9E;;;;;GAKG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,aAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAUvF;AAED,+CAA+C;AAC/C,wBAAsB,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,GAAE,MAAM,EAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAEhG;AAED,wBAAsB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3E;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,sBAAsB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAErF"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.NOWITY_SETUP_SQL_PATH = void 0;
|
|
37
|
+
exports.setupNowity = setupNowity;
|
|
38
|
+
exports.trackTable = trackTable;
|
|
39
|
+
exports.untrackTable = untrackTable;
|
|
40
|
+
exports.setReplicaIdentityFull = setReplicaIdentityFull;
|
|
41
|
+
const fs = __importStar(require("fs"));
|
|
42
|
+
const path = __importStar(require("path"));
|
|
43
|
+
function readSetupSql() {
|
|
44
|
+
// Works both from `src` (ts-node) and from `dist` (the build copies sql/).
|
|
45
|
+
const candidates = [
|
|
46
|
+
path.join(__dirname, "sql", "setup.sql"),
|
|
47
|
+
path.join(__dirname, "..", "..", "src", "server", "sql", "setup.sql"),
|
|
48
|
+
];
|
|
49
|
+
for (const candidate of candidates) {
|
|
50
|
+
if (fs.existsSync(candidate))
|
|
51
|
+
return fs.readFileSync(candidate, "utf8");
|
|
52
|
+
}
|
|
53
|
+
throw new Error(`Cannot locate setup.sql (looked in ${candidates.join(", ")})`);
|
|
54
|
+
}
|
|
55
|
+
exports.NOWITY_SETUP_SQL_PATH = path.join(__dirname, "sql", "setup.sql");
|
|
56
|
+
/**
|
|
57
|
+
* Install Nowity's change capture and attach it to the given tables.
|
|
58
|
+
*
|
|
59
|
+
* Idempotent, so it can run at boot. In a real deployment put the contents of
|
|
60
|
+
* `setup.sql` into a migration instead and call {@link trackTable} from it.
|
|
61
|
+
*/
|
|
62
|
+
async function setupNowity(pool, tables = []) {
|
|
63
|
+
await pool.query(readSetupSql());
|
|
64
|
+
const entries = Array.isArray(tables)
|
|
65
|
+
? tables.map((table) => [table, ["id"]])
|
|
66
|
+
: Object.entries(tables);
|
|
67
|
+
for (const [table, pk] of entries) {
|
|
68
|
+
await trackTable(pool, table, pk);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Attach the capture trigger to one table. */
|
|
72
|
+
async function trackTable(pool, table, pk = ["id"]) {
|
|
73
|
+
await pool.query(`SELECT nowity_track($1::regclass, $2::text[])`, [quoteIdent(table), pk]);
|
|
74
|
+
}
|
|
75
|
+
async function untrackTable(pool, table) {
|
|
76
|
+
await pool.query(`SELECT nowity_untrack($1::regclass)`, [quoteIdent(table)]);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Make `UPDATE`/`DELETE` put the whole previous row into the WAL.
|
|
80
|
+
*
|
|
81
|
+
* Required for every permission table when using the WAL change source. With the
|
|
82
|
+
* default replica identity a `DELETE` is logged as its primary key alone, so
|
|
83
|
+
* `DELETE FROM "ProjectMember"` arrives without the `projectId` — and the scope to
|
|
84
|
+
* invalidate cannot be derived from it. The outbox source does not need this: its
|
|
85
|
+
* trigger sees the full `OLD` row.
|
|
86
|
+
*
|
|
87
|
+
* Costs WAL volume on wide tables, which is why it is opt-in and per table.
|
|
88
|
+
*/
|
|
89
|
+
async function setReplicaIdentityFull(pool, table) {
|
|
90
|
+
await pool.query(`ALTER TABLE ${quoteIdent(table)} REPLICA IDENTITY FULL`);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Quote a table name for `regclass` lookup.
|
|
94
|
+
*
|
|
95
|
+
* Prisma models keep their PascalCase names as table names, and unquoted
|
|
96
|
+
* identifiers are folded to lower case by PostgreSQL, so `Document` would resolve
|
|
97
|
+
* to `document` and fail.
|
|
98
|
+
*/
|
|
99
|
+
function quoteIdent(table) {
|
|
100
|
+
if (table.includes(".")) {
|
|
101
|
+
return table
|
|
102
|
+
.split(".")
|
|
103
|
+
.map((part) => (part.startsWith('"') ? part : `"${part}"`))
|
|
104
|
+
.join(".");
|
|
105
|
+
}
|
|
106
|
+
return table.startsWith('"') ? table : `"${table}"`;
|
|
107
|
+
}
|