@kairos-es/store-postgres 1.0.0-alpha.2 → 1.0.0-alpha.4
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 +13 -5
- package/dist/cjs/ensureSchema.js +6 -1
- package/dist/cjs/ensureSchema.js.map +1 -1
- package/dist/cjs/internal/ddl.js +29 -12
- package/dist/cjs/internal/ddl.js.map +1 -1
- package/dist/cjs/internal/matchSql.js +46 -4
- package/dist/cjs/internal/matchSql.js.map +1 -1
- package/dist/cjs/internal/readPlan.js +8 -4
- package/dist/cjs/internal/readPlan.js.map +1 -1
- package/dist/cjs/internal/transport.js +27 -4
- package/dist/cjs/internal/transport.js.map +1 -1
- package/dist/cjs/store.js.map +1 -1
- package/dist/dts/ensureSchema.d.ts +6 -1
- package/dist/dts/ensureSchema.d.ts.map +1 -1
- package/dist/dts/internal/ddl.d.ts.map +1 -1
- package/dist/dts/internal/matchSql.d.ts +26 -2
- package/dist/dts/internal/matchSql.d.ts.map +1 -1
- package/dist/dts/internal/readPlan.d.ts.map +1 -1
- package/dist/dts/internal/transport.d.ts +17 -7
- package/dist/dts/internal/transport.d.ts.map +1 -1
- package/dist/dts/store.d.ts.map +1 -1
- package/dist/esm/ensureSchema.js +6 -1
- package/dist/esm/ensureSchema.js.map +1 -1
- package/dist/esm/internal/ddl.js +30 -13
- package/dist/esm/internal/ddl.js.map +1 -1
- package/dist/esm/internal/matchSql.js +44 -2
- package/dist/esm/internal/matchSql.js.map +1 -1
- package/dist/esm/internal/readPlan.js +8 -4
- package/dist/esm/internal/readPlan.js.map +1 -1
- package/dist/esm/internal/transport.js +26 -3
- package/dist/esm/internal/transport.js.map +1 -1
- package/dist/esm/store.js.map +1 -1
- package/package.json +2 -2
- package/src/ensureSchema.ts +6 -1
- package/src/internal/ddl.ts +32 -12
- package/src/internal/matchSql.ts +52 -1
- package/src/internal/readPlan.ts +8 -4
- package/src/internal/transport.ts +46 -7
- package/src/store.ts +2 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ensureSchema.js","names":["SqlClient","Effect","decodePostgresStoreConfig","resolveNames","runDdl","ensureSchema","config","gen","sql","names","all","discard"],"sources":["../../src/ensureSchema.ts"],"sourcesContent":[null],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"ensureSchema.js","names":["SqlClient","Effect","decodePostgresStoreConfig","resolveNames","runDdl","ensureSchema","config","gen","sql","names","all","discard"],"sources":["../../src/ensureSchema.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,SAASA,SAAS,QAAuB,aAAa;AACtD,SAASC,MAAM,QAAQ,QAAQ;AAC/B,SAASC,yBAAyB,QAAkC,aAAU;AAC9E,SAASC,YAAY,EAAEC,MAAM,QAAQ,mBAAgB;AAErD;;;;;AAKA,OAAO,MAAMC,YAAY,GACvBC,MAA2B,IAE3BL,MAAM,CAACM,GAAG,CAAC,aAAS;EAClB,MAAMC,GAAG,GAAG,OAAOR,SAAS,CAACA,SAAS;EACtC;EACA;EACA,MAAMS,KAAK,GAAGN,YAAY,CAACD,yBAAyB,CAACI,MAAM,CAAC,CAAC;EAC7D;EACA,OAAOL,MAAM,CAACS,GAAG,CAACN,MAAM,CAACI,GAAG,EAAEC,KAAK,CAAC,EAAE;IAAEE,OAAO,EAAE;EAAI,CAAE,CAAC;AAC1D,CAAC,CAAC","ignoreList":[]}
|
package/dist/esm/internal/ddl.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { allowedTypesPredicate, eventRecordsetColumns, qualifiedRows, taggedMatchChain } from "./matchSql.js";
|
|
1
|
+
import { allowedTypesPredicate, eventColumnList, eventInsertSelectList, eventRecordsetColumns, qualifiedRows, RECORDSET_ALIAS, taggedMatchChain } from "./matchSql.js";
|
|
2
2
|
/** Default schema when the config omits one. */
|
|
3
3
|
export const DEFAULT_SCHEMA = 'public';
|
|
4
4
|
/** Default table prefix when the config omits one. */
|
|
@@ -120,6 +120,16 @@ const lockClause = names => `LOCK TABLE ${quoteQualified(names.mainTable)} IN EX
|
|
|
120
120
|
* preserves batch order, and `RETURNING id, tags` is captured into a temp
|
|
121
121
|
* result set so the tag fan-out and the head can both read it. `data` arrives as
|
|
122
122
|
* a base64 text field and is `decode(…, 'base64')`-restored to `bytea`.
|
|
123
|
+
*
|
|
124
|
+
* The recordset definition, its projection, the `INSERT INTO (…)` target list and
|
|
125
|
+
* the `SELECT` list are all derived from the one `EVENT_COLUMNS` descriptor in
|
|
126
|
+
* `matchSql.ts`, never written out here. The last two must agree POSITIONALLY and a
|
|
127
|
+
* mismatch is not an error, so deriving both from one walk is what keeps them from
|
|
128
|
+
* falling out of step; a column added without a source is a compile error there.
|
|
129
|
+
* What the compiler does NOT reach is whether the source READS the right field, or
|
|
130
|
+
* whether the column comes back out on the read; the round-trip contract group run
|
|
131
|
+
* against this engine is what proves a new column, and only with a value distinct
|
|
132
|
+
* per column, which is what makes a swap visible.
|
|
123
133
|
*/
|
|
124
134
|
const insertFanoutNotify = (names, options = {}) => {
|
|
125
135
|
const main = quoteQualified(names.mainTable);
|
|
@@ -137,18 +147,18 @@ const insertFanoutNotify = (names, options = {}) => {
|
|
|
137
147
|
IF new_head IS NOT NULL THEN
|
|
138
148
|
PERFORM pg_notify(${channel}, '1');
|
|
139
149
|
END IF;` : '';
|
|
140
|
-
|
|
141
|
-
|
|
150
|
+
const ne = RECORDSET_ALIAS;
|
|
151
|
+
return ` WITH ${ne} AS (
|
|
152
|
+
SELECT ${eventColumnList}, ordinality
|
|
142
153
|
FROM ROWS FROM(
|
|
143
154
|
jsonb_to_recordset(new_events) AS (${eventRecordsetColumns})
|
|
144
155
|
) WITH ORDINALITY
|
|
145
156
|
),
|
|
146
157
|
inserted AS (
|
|
147
|
-
INSERT INTO ${main} (
|
|
148
|
-
SELECT
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
ORDER BY ne.ordinality
|
|
158
|
+
INSERT INTO ${main} (${eventColumnList})
|
|
159
|
+
SELECT ${eventInsertSelectList}
|
|
160
|
+
FROM ${ne}
|
|
161
|
+
ORDER BY ${ne}.ordinality
|
|
152
162
|
RETURNING id, tags
|
|
153
163
|
),
|
|
154
164
|
fanned AS (
|
|
@@ -197,9 +207,12 @@ const conditionalAppendBody = (names, options = {}) => {
|
|
|
197
207
|
// its ITEMS — none at all, an item carrying no tags, an item carrying tags —
|
|
198
208
|
// which between them cover every `Query` value the grammar can express, so a new
|
|
199
209
|
// SHAPE changes which SQL the read compiles without changing what this body has
|
|
200
|
-
// to match. The exposure is the GRAMMAR itself: a
|
|
201
|
-
// meaning for an existing one, is something this body ignores rather than
|
|
202
|
-
// on, and no `switch` over the shape union would have caught that either.
|
|
210
|
+
// to match. The exposure is the GRAMMAR itself: a field ADDED to `QueryItem`, or a
|
|
211
|
+
// new meaning for an existing one, is something this body ignores rather than
|
|
212
|
+
// fails on, and no `switch` over the shape union would have caught that either. A
|
|
213
|
+
// key the grammar does NOT define never reaches this body: core's
|
|
214
|
+
// `servableQueryFault` refuses it at the contract boundary, which is what keeps a
|
|
215
|
+
// smuggled predicate from being dropped here in silence.
|
|
203
216
|
const after = 'COALESCE(after_id, 0)';
|
|
204
217
|
return `
|
|
205
218
|
DECLARE
|
|
@@ -368,14 +381,18 @@ export const ddlStatements = names => {
|
|
|
368
381
|
// positions — a rolled-back insert permanently consumes its id, which is
|
|
369
382
|
// fine: ordering is by id alone and the store is never made gapless.
|
|
370
383
|
// `data` is NULLABLE (a payload-less event is valid); `occurred_at` is
|
|
371
|
-
// informational domain time, never an ordering key.
|
|
384
|
+
// informational domain time, never an ordering key. `metadata` is the
|
|
385
|
+
// event's cleartext context bag (ADR-0017): `jsonb` only so a containment
|
|
386
|
+
// index stays possible without a rewrite, though nothing queries it and no
|
|
387
|
+
// index is built; `NOT NULL DEFAULT '{}'` gives an empty bag one shape.
|
|
372
388
|
`CREATE TABLE IF NOT EXISTS ${main} (
|
|
373
389
|
id bigserial,
|
|
374
390
|
type text NOT NULL,
|
|
375
391
|
data bytea,
|
|
376
392
|
tags text[] NOT NULL,
|
|
377
393
|
uuid text NOT NULL,
|
|
378
|
-
occurred_at timestamptz NOT NULL
|
|
394
|
+
occurred_at timestamptz NOT NULL,
|
|
395
|
+
metadata jsonb NOT NULL DEFAULT '{}'::jsonb
|
|
379
396
|
) WITH (${AUTOVACUUM_WITH});`,
|
|
380
397
|
// Covering unique index: uniqueness on id plus INCLUDE(type) so the read
|
|
381
398
|
// path's id→type lookups are index-only.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ddl.js","names":["allowedTypesPredicate","eventRecordsetColumns","qualifiedRows","taggedMatchChain","DEFAULT_SCHEMA","DEFAULT_TABLE_PREFIX","MAX_IDENTIFIER_BYTES","utf8ByteLength","value","TextEncoder","encode","length","derivedAtoms","config","schema","tablePrefix","replace","oversizedIdentifier","label","atom","bytes","undefined","resolveNames","problem","Error","mainTable","tagTable","mainIndex","tagIndex","appendFn","appendUnconditionalFn","channel","quoteQualified","name","split","map","join","quoteAtom","quoteLiteral","AUTOVACUUM_WITH","lockClause","names","insertFanoutNotify","options","main","tag","notify","conditionalAppendBody","lock","after","queryItemsExpr","afterExpr","unconditionalAppendBody","appendFunctionStatement","qualifiedName","argList","body","kind","ddlStatements","runDdl","sql","statement","unsafe"],"sources":["../../../src/internal/ddl.ts"],"sourcesContent":[null],"mappings":"AA4BA,SACEA,qBAAqB,EACrBC,qBAAqB,EACrBC,aAAa,EACbC,gBAAgB,QACX,eAAY;AAqCnB;AACA,OAAO,MAAMC,cAAc,GAAG,QAAQ;AACtC;AACA,OAAO,MAAMC,oBAAoB,GAAG,YAAY;AAEhD;;;;;;;AAOA,MAAMC,oBAAoB,GAAG,EAAE;AAE/B;AACA,MAAMC,cAAc,GAAIC,KAAa,IACnC,IAAIC,WAAW,EAAE,CAACC,MAAM,CAACF,KAAK,CAAC,CAACG,MAAM;AAExC;;;;;;AAMA,MAAMC,YAAY,GAA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3,"file":"ddl.js","names":["allowedTypesPredicate","eventColumnList","eventInsertSelectList","eventRecordsetColumns","qualifiedRows","RECORDSET_ALIAS","taggedMatchChain","DEFAULT_SCHEMA","DEFAULT_TABLE_PREFIX","MAX_IDENTIFIER_BYTES","utf8ByteLength","value","TextEncoder","encode","length","derivedAtoms","config","schema","tablePrefix","replace","oversizedIdentifier","label","atom","bytes","undefined","resolveNames","problem","Error","mainTable","tagTable","mainIndex","tagIndex","appendFn","appendUnconditionalFn","channel","quoteQualified","name","split","map","join","quoteAtom","quoteLiteral","AUTOVACUUM_WITH","lockClause","names","insertFanoutNotify","options","main","tag","notify","ne","conditionalAppendBody","lock","after","queryItemsExpr","afterExpr","unconditionalAppendBody","appendFunctionStatement","qualifiedName","argList","body","kind","ddlStatements","runDdl","sql","statement","unsafe"],"sources":["../../../src/internal/ddl.ts"],"sourcesContent":[null],"mappings":"AA4BA,SACEA,qBAAqB,EACrBC,eAAe,EACfC,qBAAqB,EACrBC,qBAAqB,EACrBC,aAAa,EACbC,eAAe,EACfC,gBAAgB,QACX,eAAY;AAqCnB;AACA,OAAO,MAAMC,cAAc,GAAG,QAAQ;AACtC;AACA,OAAO,MAAMC,oBAAoB,GAAG,YAAY;AAEhD;;;;;;;AAOA,MAAMC,oBAAoB,GAAG,EAAE;AAE/B;AACA,MAAMC,cAAc,GAAIC,KAAa,IACnC,IAAIC,WAAW,EAAE,CAACC,MAAM,CAACF,KAAK,CAAC,CAACG,MAAM;AAExC;;;;;;AAMA,MAAMC,YAAY,GAAIC,MAGrB,IAA8C;EAC7C,MAAMC,MAAM,GAAGD,MAAM,CAACC,MAAM,IAAIV,cAAc;EAC9C,MAAMW,WAAW,GAAGF,MAAM,CAACE,WAAW,IAAIV,oBAAoB;EAC9D,OAAO,CACL,CAAC,QAAQ,EAAES,MAAM,CAAC,EAClB,CAAC,cAAc,EAAEC,WAAW,CAAC,EAC7B,CAAC,WAAW,EAAE,GAAGA,WAAW,OAAO,CAAC,EACpC,CAAC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AK,CAACb,QAAQ,CAAC;EAE1C,OAAO;EACL;EACA;EACA,+BAA+Bd,MAAM,GAAG;EAExC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,8BAA8B8B,IAAI;;;;;;;;eAQvBL,eAAe,IAAI;EAE9B;EACA;EACA,qCAAqCZ,SAAS;YACtCiB,IAAI,uBAAuB;EAEnC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,8BAA8BC,GAAG;;mCAEFD,IAAI;eACxBL,eAAe,IAAI;EAE9B;EACA;EACA,8BAA8BX,QAAQ;YAC9BiB,GAAG,kBAAkB;EAE7B;EACAS,uBAAuB,CAACb,KAAK,EAAE;IAAEiB,IAAI,EAAE;EAAa,CAAE,CAAC;EAEvD;EACAJ,uBAAuB,CAACb,KAAK,EAAE;IAAEiB,IAAI,EAAE;EAAe,CAAE,CAAC,CAC1D;AACH,CAAC;AAED;;;;;;AAMA,OAAO,MAAME,MAAM,GAAGA,CACpBC,GAAwB,EACxBpB,KAAoB,KAEpBkB,aAAa,CAAClB,KAAK,CAAC,CAACN,GAAG,CAAE2B,SAAS,IAAKD,GAAG,CAACE,MAAM,CAACD,SAAS,CAAC,CAAC","ignoreList":[]}
|
|
@@ -32,10 +32,11 @@
|
|
|
32
32
|
* that union, so a column rename here is a COMPILE error there, not a silent drift
|
|
33
33
|
* the lax read boundary (ADR-0006) would only surface as corrupt data under test.
|
|
34
34
|
*/
|
|
35
|
-
const EVENT_COLUMNS = [['type', 'text'], ['data', 'text'], ['tags', 'text[]'], ['uuid', 'text'], ['occurred_at', 'timestamptz']];
|
|
35
|
+
const EVENT_COLUMNS = [['type', 'text'], ['data', 'text'], ['tags', 'text[]'], ['uuid', 'text'], ['occurred_at', 'timestamptz'], ['metadata', 'jsonb']];
|
|
36
36
|
/**
|
|
37
37
|
* The `jsonb_to_recordset(new_events) AS (…)` column definitions —
|
|
38
|
-
* `type text, data text, tags text[], uuid text, occurred_at timestamptz
|
|
38
|
+
* `type text, data text, tags text[], uuid text, occurred_at timestamptz,
|
|
39
|
+
* metadata jsonb`. The
|
|
39
40
|
* transport `data` is base64 TEXT (restored to `bytea` by `decode(…, 'base64')`
|
|
40
41
|
* at insert), which is why it is `text` here, not `bytea`.
|
|
41
42
|
*/
|
|
@@ -46,6 +47,47 @@ export const eventRecordsetColumns = /*#__PURE__*/EVENT_COLUMNS.map(([name, type
|
|
|
46
47
|
* cannot drift from the recordset.
|
|
47
48
|
*/
|
|
48
49
|
export const readColumns = (prefix = '') => [`${prefix}id`, ...EVENT_COLUMNS.map(([name]) => `${prefix}${name}`)].join(', ');
|
|
50
|
+
/**
|
|
51
|
+
* The alias the append body gives the expanded `new_events` recordset — the `ne`
|
|
52
|
+
* CTE in `ddl.ts` — which every expression in `EVENT_COLUMN_SOURCE` reads from.
|
|
53
|
+
* Exported so the CTE is declared under the one name its readers assume.
|
|
54
|
+
*/
|
|
55
|
+
export const RECORDSET_ALIAS = 'ne';
|
|
56
|
+
/**
|
|
57
|
+
* What the append body inserts into each event column, read from the recordset row.
|
|
58
|
+
*
|
|
59
|
+
* Typed as a `Record` over `EventColumnName` so that adding a column to
|
|
60
|
+
* `EVENT_COLUMNS` without saying where its value comes from is a COMPILE error,
|
|
61
|
+
* rather than a column the insert silently leaves at its default. Every column but
|
|
62
|
+
* `data` passes through as the recordset already types it — `text[]` tags,
|
|
63
|
+
* `timestamptz`, `jsonb` metadata — because `jsonb_to_recordset` did the casting;
|
|
64
|
+
* `data` crosses as base64 TEXT and is restored to `bytea` here, its one decode.
|
|
65
|
+
*/
|
|
66
|
+
const EVENT_COLUMN_SOURCE = {
|
|
67
|
+
type: `${RECORDSET_ALIAS}.type`,
|
|
68
|
+
data: `decode(${RECORDSET_ALIAS}.data, 'base64')`,
|
|
69
|
+
tags: `${RECORDSET_ALIAS}.tags`,
|
|
70
|
+
uuid: `${RECORDSET_ALIAS}.uuid`,
|
|
71
|
+
occurred_at: `${RECORDSET_ALIAS}.occurred_at`,
|
|
72
|
+
metadata: `${RECORDSET_ALIAS}.metadata`
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The bare event column names — `type, data, tags, uuid, occurred_at, metadata` —
|
|
76
|
+
* which the append body uses twice: as the recordset CTE's projection and as the
|
|
77
|
+
* `INSERT INTO t (…)` target list.
|
|
78
|
+
*/
|
|
79
|
+
export const eventColumnList = /*#__PURE__*/EVENT_COLUMNS.map(([name]) => name).join(', ');
|
|
80
|
+
/**
|
|
81
|
+
* The append insert's SELECT list: one source expression per column, in the SAME
|
|
82
|
+
* ORDER as `eventColumnList`.
|
|
83
|
+
*
|
|
84
|
+
* The two halves of `INSERT INTO t (a, b) SELECT x, y` agree POSITION BY POSITION
|
|
85
|
+
* and nothing checks that they do: two columns of compatible type swap without an
|
|
86
|
+
* error at any layer and the data lands in the wrong one. Both halves are therefore
|
|
87
|
+
* one walk of the one descriptor, so the append body has no second ordering to keep
|
|
88
|
+
* in step and no hand-written list for a new column to be missing from.
|
|
89
|
+
*/
|
|
90
|
+
export const eventInsertSelectList = /*#__PURE__*/EVENT_COLUMNS.map(([name]) => EVENT_COLUMN_SOURCE[name]).join(', ');
|
|
49
91
|
/**
|
|
50
92
|
* The allowed-types filter for a candidate row aliased `m`: an item with no
|
|
51
93
|
* types matches ANY type; otherwise the event's type must be one of them.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matchSql.js","names":["EVENT_COLUMNS","eventRecordsetColumns","map","name","type","join","readColumns","prefix","allowedTypesPredicate","typesCol","taggedMatchChain","tokens","queryItemsExpr","tag","afterExpr","qualifiedRows","main","wildcardReadSql","typeOnlyReadSql","taggedReadSql"],"sources":["../../../src/internal/matchSql.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;AAwBA;;;;;;;;;;;AAWA,MAAMA,aAAa,GAAG,CACpB,CAAC,MAAM,EAAE,MAAM,CAAC,EAChB,CAAC,MAAM,EAAE,MAAM,CAAC,EAChB,CAAC,MAAM,EAAE,QAAQ,CAAC,EAClB,CAAC,MAAM,EAAE,MAAM,CAAC,EAChB,CAAC,aAAa,EAAE,aAAa,CAAC,
|
|
1
|
+
{"version":3,"file":"matchSql.js","names":["EVENT_COLUMNS","eventRecordsetColumns","map","name","type","join","readColumns","prefix","RECORDSET_ALIAS","EVENT_COLUMN_SOURCE","data","tags","uuid","occurred_at","metadata","eventColumnList","eventInsertSelectList","allowedTypesPredicate","typesCol","taggedMatchChain","tokens","queryItemsExpr","tag","afterExpr","qualifiedRows","main","wildcardReadSql","typeOnlyReadSql","taggedReadSql"],"sources":["../../../src/internal/matchSql.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;AAwBA;;;;;;;;;;;AAWA,MAAMA,aAAa,GAAG,CACpB,CAAC,MAAM,EAAE,MAAM,CAAC,EAChB,CAAC,MAAM,EAAE,MAAM,CAAC,EAChB,CAAC,MAAM,EAAE,QAAQ,CAAC,EAClB,CAAC,MAAM,EAAE,MAAM,CAAC,EAChB,CAAC,aAAa,EAAE,aAAa,CAAC,EAC9B,CAAC,UAAU,EAAE,OAAO,CAAC,CAGtB;AAYD;;;;;;;AAOA,OAAO,MAAMC,qBAAqB,gBAAWD,aAAa,CAACE,GAAG,CAC5D,CAAC,CAACC,IAAI,EAAEC,IAAI,CAAC,KAAK,GAAGD,IAAI,IAAIC,IAAI,EAAE,CACpC,CAACC,IAAI,CAAC,IAAI,CAAC;AAEZ;;;;;AAKA,OAAO,MAAMC,WAAW,GAAGA,CAACC,MAAM,GAAG,EAAE,KACrC,CAAC,GAAGA,MAAM,IAAI,EAAE,GAAGP,aAAa,CAACE,GAAG,CAAC,CAAC,CAACC,IAAI,CAAC,KAAK,GAAGI,MAAM,GAAGJ,IAAI,EAAE,CAAC,CAAC,CAACE,IAAI,CACxE,IAAI,CACL;AAEH;;;;;AAKA,OAAO,MAAMG,eAAe,GAAG,IAAI;AAEnC;;;;;;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B,WAAW,EAAE;UACfmB,IAAI;;;YAGF;AAEZ;;;;;;AAMA,OAAO,MAAMG,aAAa,GAAGA,CAACH,IAAY,EAAEH,GAAW,KACrD,QAAQH,gBAAgB,CAAC;EAAEE,cAAc,EAAE,WAAW;EAAEE,SAAS,EAAE,IAAI;EAAEE,IAAI;EAAEH;AAAG,CAAE,CAAC;;;MAGjFE,aAAa,CAAC;EAAEC,IAAI;EAAEF,SAAS,EAAE;AAAI,CAAE,CAAC;;;;WAInCjB,WAAW,CAAC,IAAI,CAAC;SACnBmB,IAAI;;oBAEO","ignoreList":[]}
|
|
@@ -30,10 +30,14 @@ export const compileFetchPlan = (tables, query) => {
|
|
|
30
30
|
case 'typeOnly':
|
|
31
31
|
{
|
|
32
32
|
// node-postgres binds a JS array param as a Postgres array literal, so
|
|
33
|
-
// `type = ANY($2)` matches any of the item's types.
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
|
|
33
|
+
// `type = ANY($2)` matches any of the item's types. A branded `EventType` IS
|
|
34
|
+
// its string to the driver, so nothing has to be unwrapped — but the list is
|
|
35
|
+
// COPIED here, at compile time. The driver serialises the array afresh on
|
|
36
|
+
// every `bind`, and a subscription re-binds on every poll, so binding the
|
|
37
|
+
// caller's own array would let an edit the caller makes after subscribing
|
|
38
|
+
// reach the next fetch. The tagged arm is immune by construction, since it
|
|
39
|
+
// serialises once below; this copy is what makes the two arms agree.
|
|
40
|
+
const types = [...shape.types];
|
|
37
41
|
return {
|
|
38
42
|
text: typeOnlyReadSql(tables.main),
|
|
39
43
|
bind: (after, limit) => [after, types, limit]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readPlan.js","names":["classifyServableQuery","ORIGIN","taggedReadSql","typeOnlyReadSql","wildcardReadSql","jsonbText","toQueryItemsJson","compileFetchPlan","tables","query","shape","kind","text","main","bind","after","limit","types","itemsJson","tag","runFetch","sql","plan","unsafe"],"sources":["../../../src/internal/readPlan.ts"],"sourcesContent":[null],"mappings":"AAyBA,SAASA,qBAAqB,EAAEC,MAAM,QAAoB,iBAAiB;AAE3E,SAASC,aAAa,EAAEC,eAAe,EAAEC,eAAe,QAAQ,eAAY;AAC5E,SAAwBC,SAAS,EAAEC,gBAAgB,QAAQ,gBAAa;AA0BxE;;;;;;;;;;;;;;;;;;AAkBA,OAAO,MAAMC,gBAAgB,GAAGA,CAC9BC,MAAkB,EAClBC,KAAY,KACC;EACb,MAAMC,KAAK,GAAGV,qBAAqB,CAACS,KAAK,CAAC;EAC1C,QAAQC,KAAK,CAACC,IAAI;IAChB,KAAK,UAAU;MACb,OAAO;QACLC,IAAI,EAAER,eAAe,CAACI,MAAM,CAACK,IAAI,CAAC;QAClCC,IAAI,EAAEA,CAACC,KAAK,EAAEC,KAAK,KAAK,CAACD,KAAK,EAAEC,KAAK;OACtC;IACH,KAAK,UAAU;MAAE;QACf;QACA;QACA;QACA;QACA,MAAMC,KAAK,GAAGP,KAAK,CAACO,KAAK;
|
|
1
|
+
{"version":3,"file":"readPlan.js","names":["classifyServableQuery","ORIGIN","taggedReadSql","typeOnlyReadSql","wildcardReadSql","jsonbText","toQueryItemsJson","compileFetchPlan","tables","query","shape","kind","text","main","bind","after","limit","types","itemsJson","tag","runFetch","sql","plan","unsafe"],"sources":["../../../src/internal/readPlan.ts"],"sourcesContent":[null],"mappings":"AAyBA,SAASA,qBAAqB,EAAEC,MAAM,QAAoB,iBAAiB;AAE3E,SAASC,aAAa,EAAEC,eAAe,EAAEC,eAAe,QAAQ,eAAY;AAC5E,SAAwBC,SAAS,EAAEC,gBAAgB,QAAQ,gBAAa;AA0BxE;;;;;;;;;;;;;;;;;;AAkBA,OAAO,MAAMC,gBAAgB,GAAGA,CAC9BC,MAAkB,EAClBC,KAAY,KACC;EACb,MAAMC,KAAK,GAAGV,qBAAqB,CAACS,KAAK,CAAC;EAC1C,QAAQC,KAAK,CAACC,IAAI;IAChB,KAAK,UAAU;MACb,OAAO;QACLC,IAAI,EAAER,eAAe,CAACI,MAAM,CAACK,IAAI,CAAC;QAClCC,IAAI,EAAEA,CAACC,KAAK,EAAEC,KAAK,KAAK,CAACD,KAAK,EAAEC,KAAK;OACtC;IACH,KAAK,UAAU;MAAE;QACf;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA,MAAMC,KAAK,GAAG,CAAC,GAAGP,KAAK,CAACO,KAAK,CAAC;QAC9B,OAAO;UACLL,IAAI,EAAET,eAAe,CAACK,MAAM,CAACK,IAAI,CAAC;UAClCC,IAAI,EAAEA,CAACC,KAAK,EAAEC,KAAK,KAAK,CAACD,KAAK,EAAEE,KAAK,EAAED,KAAK;SAC7C;MACH;IACA,KAAK,QAAQ;MAAE;QACb;QACA;QACA,MAAME,SAAS,GAAGb,SAAS,CAACC,gBAAgB,CAACG,KAAK,CAAC,CAAC;QACpD,OAAO;UACLG,IAAI,EAAEV,aAAa,CAACM,MAAM,CAACK,IAAI,EAAEL,MAAM,CAACW,GAAG,CAAC;UAC5CL,IAAI,EAAEA,CAACC,KAAK,EAAEC,KAAK,KAAK,CAACE,SAAS,EAAEH,KAAK,EAAEC,KAAK;SACjD;MACH;EACF;AACF,CAAC;AAED;;;;AAIA,OAAO,MAAMI,QAAQ,GAAGA,CACtBC,GAAwB,EACxBC,IAAe,EACfP,KAA2B,EAC3BC,KAA4B,KAE5BK,GAAG,CAACE,MAAM,CAAWD,IAAI,CAACV,IAAI,EAAEU,IAAI,CAACR,IAAI,CAACC,KAAK,IAAId,MAAM,EAAEe,KAAK,IAAI,IAAI,CAAC,CAAC","ignoreList":[]}
|
|
@@ -26,12 +26,34 @@ export const toEventJson = event => ({
|
|
|
26
26
|
data: Encoding.encodeBase64(event.data),
|
|
27
27
|
tags: [...event.tags],
|
|
28
28
|
uuid: event.uuid,
|
|
29
|
-
occurred_at: DateTime.formatIso(event.occurredAt)
|
|
29
|
+
occurred_at: DateTime.formatIso(event.occurredAt),
|
|
30
|
+
metadata: event.metadata
|
|
30
31
|
});
|
|
32
|
+
/**
|
|
33
|
+
* The `metadata` column as the event's bag, checked to be a JSON OBJECT.
|
|
34
|
+
*
|
|
35
|
+
* The store only ever writes an object, but `jsonb` admits any JSON value — an
|
|
36
|
+
* array, a string, a number, a boolean, JSON `null` (which `NOT NULL` does not
|
|
37
|
+
* exclude) — so a hand-written row can hold one. Such a row is corrupt, and
|
|
38
|
+
* handing an event whose `metadata` is not a bag to every projection
|
|
39
|
+
* downstream is worse than stopping at the read. This is the CONTAINER check
|
|
40
|
+
* only; the shape rule itself has one home, core's `Metadata` doc. The VALUES
|
|
41
|
+
* are deliberately not re-checked as strings, because a value check on read
|
|
42
|
+
* alone would make an event the store accepted unreadable for ever.
|
|
43
|
+
*/
|
|
44
|
+
const metadataFrom = value => {
|
|
45
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
46
|
+
throw new Error(`@kairos-es/store-postgres: the \`metadata\` column did not hold a JSON object (got ${JSON.stringify(value)}).`);
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
};
|
|
31
50
|
/**
|
|
32
51
|
* Map a raw main-table row to a core `SequencedEvent`. `data` NULL → empty bytes
|
|
33
52
|
* (a payload-less event round-trips to a zero-length `Uint8Array`); `occurred_at`
|
|
34
|
-
* Date → `DateTime.Utc`; `id` string → branded `Position
|
|
53
|
+
* Date → `DateTime.Utc`; `id` string → branded `Position`; `metadata` through
|
|
54
|
+
* `metadataFrom`. The column is `NOT NULL DEFAULT '{}'`, so there is no absent
|
|
55
|
+
* case to reconcile: an event appended with an empty bag reads back as `{}` on
|
|
56
|
+
* this engine exactly as on the in-memory oracle.
|
|
35
57
|
*
|
|
36
58
|
* The branded scalars (`type`, `tags`, `uuid`, `position`) are minted by trusted
|
|
37
59
|
* `as`-cast, NOT by re-running `EventType.make` / `Tag.make` etc. This is
|
|
@@ -49,7 +71,8 @@ export const rowToSequenced = row => ({
|
|
|
49
71
|
data: row.data ?? new Uint8Array(0),
|
|
50
72
|
tags: row.tags,
|
|
51
73
|
uuid: row.uuid,
|
|
52
|
-
occurredAt: DateTime.unsafeFromDate(row.occurred_at)
|
|
74
|
+
occurredAt: DateTime.unsafeFromDate(row.occurred_at),
|
|
75
|
+
metadata: metadataFrom(row.metadata)
|
|
53
76
|
},
|
|
54
77
|
position: BigInt(row.id)
|
|
55
78
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transport.js","names":["DateTime","Encoding","jsonbText","value","JSON","stringify","toQueryItemsJson","query","items","map","item","types","tags","toEventJson","event","type","data","encodeBase64","uuid","occurred_at","formatIso","occurredAt","rowToSequenced","row","Uint8Array","unsafeFromDate","position","BigInt","id"],"sources":["../../../src/internal/transport.ts"],"sourcesContent":[null],"mappings":"
|
|
1
|
+
{"version":3,"file":"transport.js","names":["DateTime","Encoding","jsonbText","value","JSON","stringify","toQueryItemsJson","query","items","map","item","types","tags","toEventJson","event","type","data","encodeBase64","uuid","occurred_at","formatIso","occurredAt","metadata","metadataFrom","Array","isArray","Error","rowToSequenced","row","Uint8Array","unsafeFromDate","position","BigInt","id"],"sources":["../../../src/internal/transport.ts"],"sourcesContent":[null],"mappings":"AA4BA,SAASA,QAAQ,EAAEC,QAAQ,QAAQ,QAAQ;AAG3C;;;;;;;;;;;;;AAaA,OAAO,MAAMC,SAAS,GAAIC,KAAc,IAAaC,IAAI,CAACC,SAAS,CAACF,KAAK,CAAC;AAQ1E;AACA,OAAO,MAAMG,gBAAgB,GAAIC,KAAY,IAC3CA,KAAK,CAACC,KAAK,CAACC,GAAG,CAAEC,IAAI,KAAM;EACzBC,KAAK,EAAE,CAAC,GAAGD,IAAI,CAACC,KAAK,CAAC;EACtBC,IAAI,EAAE,CAAC,GAAGF,IAAI,CAACE,IAAI;CACpB,CAAC,CAAC;AA+BL;AACA,OAAO,MAAMC,WAAW,GAAIC,KAAe,KAAiB;EAC1DC,IAAI,EAAED,KAAK,CAACC,IAAI;EAChB;EACA;EACAC,IAAI,EAAEf,QAAQ,CAACgB,YAAY,CAACH,KAAK,CAACE,IAAI,CAAC;EACvCJ,IAAI,EAAE,CAAC,GAAGE,KAAK,CAACF,IAAI,CAAC;EACrBM,IAAI,EAAEJ,KAAK,CAACI,IAAI;EAChBC,WAAW,EAAEnB,QAAQ,CAACoB,SAAS,CAACN,KAAK,CAACO,UAAU,CAAC;EACjDC,QAAQ,EAAER,KAAK,CAACQ;CACjB,CAAC;AAgCF;;;;;;;;;;;;AAYA,MAAMC,YAAY,GAAIpB,KAAc,IAAc;EAChD,IAAI,OAAOA,KAAK,KAAK,QAAQ,IAAIA,KAAK,KAAK,IAAI,IAAIqB,KAAK,CAACC,OAAO,CAACtB,KAAK,CAAC,EAAE;IACvE,MAAM,IAAIuB,KAAK,CACb,sFAAsFtB,IAAI,CAACC,SAAS,CAACF,KAAK,CAAC,IAAI,CAChH;EACH;EACA,OAAOA,KAAiB;AAC1B,CAAC;AAED;;;;;;;;;;;;;;;;;;AAkBA,OAAO,MAAMwB,cAAc,GAAIC,GAAa,KAAsB;EAChEd,KAAK,EAAE;IACLC,IAAI,EAAEa,GAAG,CAACb,IAAwB;IAClCC,IAAI,EAAEY,GAAG,CAACZ,IAAI,IAAI,IAAIa,UAAU,CAAC,CAAC,CAAC;IACnCjB,IAAI,EAAEgB,GAAG,CAAChB,IAAmC;IAC7CM,IAAI,EAAEU,GAAG,CAACV,IAAwB;IAClCG,UAAU,EAAErB,QAAQ,CAAC8B,cAAc,CAACF,GAAG,CAACT,WAAW,CAAC;IACpDG,QAAQ,EAAEC,YAAY,CAACK,GAAG,CAACN,QAAQ;GACpC;EACDS,QAAQ,EAAEC,MAAM,CAACJ,GAAG,CAACK,EAAE;CACxB,CAAC","ignoreList":[]}
|
package/dist/esm/store.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.js","names":["AppendConditionFailed","DcbEventStore","DEFAULT_CATCH_UP_PAGE_SIZE","DEFAULT_POLL_INTERVAL_MILLIS","makeContractStore","ORIGIN","subscribeStream","Effect","Layer","Stream","PgClientDirect","PgClientPooled","decodePostgresStoreConfig","quoteQualified","resolveNames","compileFetchPlan","runFetch","jsonbText","rowToSequenced","toEventJson","toQueryItemsJson","setLocalLockTimeout","sql","lockTimeout","make","config","names","gen","pooled","direct","pollInterval","catchUpPageSize","tables","main","mainTable","tag","tagTable","globalHead","pipe","map","rows","max","undefined","BigInt","readRowsAndHead","query","options","plan","limit","after","lastRow","at","head","id","withTransaction","unsafe","read","events","fromIterable","callAppendFn","select","zipRight","append","condition","eventsJson","appendUnconditionalFn","headFromRows","queryItems","failIfEventsMatch","afterId","appendFn","length","Error","subscribe","fetch","from","listen","channel","layer","decoded","effect"],"sources":["../../src/store.ts"],"sourcesContent":[null],"mappings":"
|
|
1
|
+
{"version":3,"file":"store.js","names":["AppendConditionFailed","DcbEventStore","DEFAULT_CATCH_UP_PAGE_SIZE","DEFAULT_POLL_INTERVAL_MILLIS","makeContractStore","ORIGIN","subscribeStream","Effect","Layer","Stream","PgClientDirect","PgClientPooled","decodePostgresStoreConfig","quoteQualified","resolveNames","compileFetchPlan","runFetch","jsonbText","rowToSequenced","toEventJson","toQueryItemsJson","setLocalLockTimeout","sql","lockTimeout","make","config","names","gen","pooled","direct","pollInterval","catchUpPageSize","tables","main","mainTable","tag","tagTable","globalHead","pipe","map","rows","max","undefined","BigInt","readRowsAndHead","query","options","plan","limit","after","lastRow","at","head","id","withTransaction","unsafe","read","events","fromIterable","callAppendFn","select","zipRight","append","condition","eventsJson","appendUnconditionalFn","headFromRows","queryItems","failIfEventsMatch","afterId","appendFn","length","Error","subscribe","fetch","from","listen","channel","layer","decoded","effect"],"sources":["../../src/store.ts"],"sourcesContent":[null],"mappings":"AAkDA,SAEEA,qBAAqB,EACrBC,aAAa,EACbC,0BAA0B,EAC1BC,4BAA4B,EAC5BC,iBAAiB,EACjBC,MAAM,EAGNC,eAAe,QACV,iBAAiB;AACxB,SAA4BC,MAAM,EAAEC,KAAK,EAAcC,MAAM,QAAQ,QAAQ;AAC7E,SAASC,cAA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,OAAOjB,KAAK,CAACwE,MAAM,CACjB/E,aAAa,EACbM,MAAM,CAACgC,GAAG,CAACf,IAAI,CAACuD,OAAO,EAAEjE,YAAY,CAACiE,OAAO,CAAC,CAAC,EAAE3E,iBAAiB,CAAC,CACpE;AACH,CAAC","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kairos-es/store-postgres",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.4",
|
|
4
4
|
"description": "Postgres DcbEventStore backend for kairos-es",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
6
|
"repository": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"@effect/sql": "^0.52.0",
|
|
17
17
|
"@effect/sql-pg": "^0.53.0",
|
|
18
18
|
"effect": "^3.22.0",
|
|
19
|
-
"@kairos-es/core": "^1.0.0-alpha.
|
|
19
|
+
"@kairos-es/core": "^1.0.0-alpha.4"
|
|
20
20
|
},
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
22
22
|
"module": "./dist/esm/index.js",
|
package/src/ensureSchema.ts
CHANGED
|
@@ -18,7 +18,12 @@
|
|
|
18
18
|
* consumer, this package only provides the idempotent PAYLOAD.
|
|
19
19
|
*
|
|
20
20
|
* Idempotence: every statement is `CREATE … IF NOT EXISTS` or `CREATE OR REPLACE
|
|
21
|
-
* FUNCTION`, so re-running
|
|
21
|
+
* FUNCTION`, so re-running it is safe. It is NOT a no-op across versions: a
|
|
22
|
+
* re-run replaces the append functions with this version's, and does NOT alter
|
|
23
|
+
* an existing table, so a column a newer version adds must be added by hand
|
|
24
|
+
* before the re-run (the release notes give the statement). A store whose
|
|
25
|
+
* table gained a column but whose functions were never replaced keeps writing
|
|
26
|
+
* through the old ones, which do not know the column. Names come from
|
|
22
27
|
* `sql(identifier)` (via the pre-quoted builder in `internal/ddl.ts`); the DDL
|
|
23
28
|
* bodies carry no bound values (pure schema), and the append functions keep
|
|
24
29
|
* their runtime VALUES as bound parameters, never interpolated.
|
package/src/internal/ddl.ts
CHANGED
|
@@ -28,8 +28,11 @@
|
|
|
28
28
|
import type { SqlClient, Statement } from '@effect/sql'
|
|
29
29
|
import {
|
|
30
30
|
allowedTypesPredicate,
|
|
31
|
+
eventColumnList,
|
|
32
|
+
eventInsertSelectList,
|
|
31
33
|
eventRecordsetColumns,
|
|
32
34
|
qualifiedRows,
|
|
35
|
+
RECORDSET_ALIAS,
|
|
33
36
|
taggedMatchChain,
|
|
34
37
|
} from './matchSql'
|
|
35
38
|
|
|
@@ -250,6 +253,16 @@ export interface AppendBodyOptions {
|
|
|
250
253
|
* preserves batch order, and `RETURNING id, tags` is captured into a temp
|
|
251
254
|
* result set so the tag fan-out and the head can both read it. `data` arrives as
|
|
252
255
|
* a base64 text field and is `decode(…, 'base64')`-restored to `bytea`.
|
|
256
|
+
*
|
|
257
|
+
* The recordset definition, its projection, the `INSERT INTO (…)` target list and
|
|
258
|
+
* the `SELECT` list are all derived from the one `EVENT_COLUMNS` descriptor in
|
|
259
|
+
* `matchSql.ts`, never written out here. The last two must agree POSITIONALLY and a
|
|
260
|
+
* mismatch is not an error, so deriving both from one walk is what keeps them from
|
|
261
|
+
* falling out of step; a column added without a source is a compile error there.
|
|
262
|
+
* What the compiler does NOT reach is whether the source READS the right field, or
|
|
263
|
+
* whether the column comes back out on the read; the round-trip contract group run
|
|
264
|
+
* against this engine is what proves a new column, and only with a value distinct
|
|
265
|
+
* per column, which is what makes a swap visible.
|
|
253
266
|
*/
|
|
254
267
|
const insertFanoutNotify = (
|
|
255
268
|
names: ResolvedNames,
|
|
@@ -273,18 +286,18 @@ const insertFanoutNotify = (
|
|
|
273
286
|
PERFORM pg_notify(${channel}, '1');
|
|
274
287
|
END IF;`
|
|
275
288
|
: ''
|
|
276
|
-
|
|
277
|
-
|
|
289
|
+
const ne = RECORDSET_ALIAS
|
|
290
|
+
return ` WITH ${ne} AS (
|
|
291
|
+
SELECT ${eventColumnList}, ordinality
|
|
278
292
|
FROM ROWS FROM(
|
|
279
293
|
jsonb_to_recordset(new_events) AS (${eventRecordsetColumns})
|
|
280
294
|
) WITH ORDINALITY
|
|
281
295
|
),
|
|
282
296
|
inserted AS (
|
|
283
|
-
INSERT INTO ${main} (
|
|
284
|
-
SELECT
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
ORDER BY ne.ordinality
|
|
297
|
+
INSERT INTO ${main} (${eventColumnList})
|
|
298
|
+
SELECT ${eventInsertSelectList}
|
|
299
|
+
FROM ${ne}
|
|
300
|
+
ORDER BY ${ne}.ordinality
|
|
288
301
|
RETURNING id, tags
|
|
289
302
|
),
|
|
290
303
|
fanned AS (
|
|
@@ -337,9 +350,12 @@ const conditionalAppendBody = (
|
|
|
337
350
|
// its ITEMS — none at all, an item carrying no tags, an item carrying tags —
|
|
338
351
|
// which between them cover every `Query` value the grammar can express, so a new
|
|
339
352
|
// SHAPE changes which SQL the read compiles without changing what this body has
|
|
340
|
-
// to match. The exposure is the GRAMMAR itself: a
|
|
341
|
-
// meaning for an existing one, is something this body ignores rather than
|
|
342
|
-
// on, and no `switch` over the shape union would have caught that either.
|
|
353
|
+
// to match. The exposure is the GRAMMAR itself: a field ADDED to `QueryItem`, or a
|
|
354
|
+
// new meaning for an existing one, is something this body ignores rather than
|
|
355
|
+
// fails on, and no `switch` over the shape union would have caught that either. A
|
|
356
|
+
// key the grammar does NOT define never reaches this body: core's
|
|
357
|
+
// `servableQueryFault` refuses it at the contract boundary, which is what keeps a
|
|
358
|
+
// smuggled predicate from being dropped here in silence.
|
|
343
359
|
const after = 'COALESCE(after_id, 0)'
|
|
344
360
|
return `
|
|
345
361
|
DECLARE
|
|
@@ -537,14 +553,18 @@ export const ddlStatements = (names: ResolvedNames): ReadonlyArray<string> => {
|
|
|
537
553
|
// positions — a rolled-back insert permanently consumes its id, which is
|
|
538
554
|
// fine: ordering is by id alone and the store is never made gapless.
|
|
539
555
|
// `data` is NULLABLE (a payload-less event is valid); `occurred_at` is
|
|
540
|
-
// informational domain time, never an ordering key.
|
|
556
|
+
// informational domain time, never an ordering key. `metadata` is the
|
|
557
|
+
// event's cleartext context bag (ADR-0017): `jsonb` only so a containment
|
|
558
|
+
// index stays possible without a rewrite, though nothing queries it and no
|
|
559
|
+
// index is built; `NOT NULL DEFAULT '{}'` gives an empty bag one shape.
|
|
541
560
|
`CREATE TABLE IF NOT EXISTS ${main} (
|
|
542
561
|
id bigserial,
|
|
543
562
|
type text NOT NULL,
|
|
544
563
|
data bytea,
|
|
545
564
|
tags text[] NOT NULL,
|
|
546
565
|
uuid text NOT NULL,
|
|
547
|
-
occurred_at timestamptz NOT NULL
|
|
566
|
+
occurred_at timestamptz NOT NULL,
|
|
567
|
+
metadata jsonb NOT NULL DEFAULT '{}'::jsonb
|
|
548
568
|
) WITH (${AUTOVACUUM_WITH});`,
|
|
549
569
|
|
|
550
570
|
// Covering unique index: uniqueness on id plus INCLUDE(type) so the read
|
package/src/internal/matchSql.ts
CHANGED
|
@@ -39,6 +39,7 @@ const EVENT_COLUMNS = [
|
|
|
39
39
|
['tags', 'text[]'],
|
|
40
40
|
['uuid', 'text'],
|
|
41
41
|
['occurred_at', 'timestamptz'],
|
|
42
|
+
['metadata', 'jsonb'],
|
|
42
43
|
] as const satisfies ReadonlyArray<
|
|
43
44
|
readonly [name: string, recordsetType: string]
|
|
44
45
|
>
|
|
@@ -55,7 +56,8 @@ export type EventColumnName = (typeof EVENT_COLUMNS)[number][0]
|
|
|
55
56
|
|
|
56
57
|
/**
|
|
57
58
|
* The `jsonb_to_recordset(new_events) AS (…)` column definitions —
|
|
58
|
-
* `type text, data text, tags text[], uuid text, occurred_at timestamptz
|
|
59
|
+
* `type text, data text, tags text[], uuid text, occurred_at timestamptz,
|
|
60
|
+
* metadata jsonb`. The
|
|
59
61
|
* transport `data` is base64 TEXT (restored to `bytea` by `decode(…, 'base64')`
|
|
60
62
|
* at insert), which is why it is `text` here, not `bytea`.
|
|
61
63
|
*/
|
|
@@ -73,6 +75,55 @@ export const readColumns = (prefix = ''): string =>
|
|
|
73
75
|
', ',
|
|
74
76
|
)
|
|
75
77
|
|
|
78
|
+
/**
|
|
79
|
+
* The alias the append body gives the expanded `new_events` recordset — the `ne`
|
|
80
|
+
* CTE in `ddl.ts` — which every expression in `EVENT_COLUMN_SOURCE` reads from.
|
|
81
|
+
* Exported so the CTE is declared under the one name its readers assume.
|
|
82
|
+
*/
|
|
83
|
+
export const RECORDSET_ALIAS = 'ne'
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* What the append body inserts into each event column, read from the recordset row.
|
|
87
|
+
*
|
|
88
|
+
* Typed as a `Record` over `EventColumnName` so that adding a column to
|
|
89
|
+
* `EVENT_COLUMNS` without saying where its value comes from is a COMPILE error,
|
|
90
|
+
* rather than a column the insert silently leaves at its default. Every column but
|
|
91
|
+
* `data` passes through as the recordset already types it — `text[]` tags,
|
|
92
|
+
* `timestamptz`, `jsonb` metadata — because `jsonb_to_recordset` did the casting;
|
|
93
|
+
* `data` crosses as base64 TEXT and is restored to `bytea` here, its one decode.
|
|
94
|
+
*/
|
|
95
|
+
const EVENT_COLUMN_SOURCE: Record<EventColumnName, string> = {
|
|
96
|
+
type: `${RECORDSET_ALIAS}.type`,
|
|
97
|
+
data: `decode(${RECORDSET_ALIAS}.data, 'base64')`,
|
|
98
|
+
tags: `${RECORDSET_ALIAS}.tags`,
|
|
99
|
+
uuid: `${RECORDSET_ALIAS}.uuid`,
|
|
100
|
+
occurred_at: `${RECORDSET_ALIAS}.occurred_at`,
|
|
101
|
+
metadata: `${RECORDSET_ALIAS}.metadata`,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The bare event column names — `type, data, tags, uuid, occurred_at, metadata` —
|
|
106
|
+
* which the append body uses twice: as the recordset CTE's projection and as the
|
|
107
|
+
* `INSERT INTO t (…)` target list.
|
|
108
|
+
*/
|
|
109
|
+
export const eventColumnList: string = EVENT_COLUMNS.map(([name]) => name).join(
|
|
110
|
+
', ',
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The append insert's SELECT list: one source expression per column, in the SAME
|
|
115
|
+
* ORDER as `eventColumnList`.
|
|
116
|
+
*
|
|
117
|
+
* The two halves of `INSERT INTO t (a, b) SELECT x, y` agree POSITION BY POSITION
|
|
118
|
+
* and nothing checks that they do: two columns of compatible type swap without an
|
|
119
|
+
* error at any layer and the data lands in the wrong one. Both halves are therefore
|
|
120
|
+
* one walk of the one descriptor, so the append body has no second ordering to keep
|
|
121
|
+
* in step and no hand-written list for a new column to be missing from.
|
|
122
|
+
*/
|
|
123
|
+
export const eventInsertSelectList: string = EVENT_COLUMNS.map(
|
|
124
|
+
([name]) => EVENT_COLUMN_SOURCE[name],
|
|
125
|
+
).join(', ')
|
|
126
|
+
|
|
76
127
|
/**
|
|
77
128
|
* The allowed-types filter for a candidate row aliased `m`: an item with no
|
|
78
129
|
* types matches ANY type; otherwise the event's type must be one of them.
|
package/src/internal/readPlan.ts
CHANGED
|
@@ -83,10 +83,14 @@ export const compileFetchPlan = (
|
|
|
83
83
|
}
|
|
84
84
|
case 'typeOnly': {
|
|
85
85
|
// node-postgres binds a JS array param as a Postgres array literal, so
|
|
86
|
-
// `type = ANY($2)` matches any of the item's types.
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
|
|
86
|
+
// `type = ANY($2)` matches any of the item's types. A branded `EventType` IS
|
|
87
|
+
// its string to the driver, so nothing has to be unwrapped — but the list is
|
|
88
|
+
// COPIED here, at compile time. The driver serialises the array afresh on
|
|
89
|
+
// every `bind`, and a subscription re-binds on every poll, so binding the
|
|
90
|
+
// caller's own array would let an edit the caller makes after subscribing
|
|
91
|
+
// reach the next fetch. The tagged arm is immune by construction, since it
|
|
92
|
+
// serialises once below; this copy is what makes the two arms agree.
|
|
93
|
+
const types = [...shape.types]
|
|
90
94
|
return {
|
|
91
95
|
text: typeOnlyReadSql(tables.main),
|
|
92
96
|
bind: (after, limit) => [after, types, limit],
|