@kairos-es/store-postgres 1.0.0-alpha.4 → 1.0.0-alpha.6
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 +54 -29
- package/dist/cjs/clients.js +34 -24
- package/dist/cjs/clients.js.map +1 -1
- package/dist/cjs/config.js +9 -15
- package/dist/cjs/config.js.map +1 -1
- package/dist/cjs/ensureSchema.js +2 -2
- package/dist/cjs/ensureSchema.js.map +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/internal/matchSql.js +15 -4
- package/dist/cjs/internal/matchSql.js.map +1 -1
- package/dist/cjs/internal/readPlan.js +25 -1
- package/dist/cjs/internal/readPlan.js.map +1 -1
- package/dist/cjs/internal/transport.js +17 -13
- package/dist/cjs/internal/transport.js.map +1 -1
- package/dist/cjs/store.js +30 -25
- package/dist/cjs/store.js.map +1 -1
- package/dist/dts-cjs/clients.d.ts +66 -0
- package/dist/dts-cjs/clients.d.ts.map +1 -0
- package/dist/{dts → dts-cjs}/config.d.ts +9 -19
- package/dist/{dts → dts-cjs}/config.d.ts.map +1 -1
- package/dist/{dts → dts-cjs}/ensureSchema.d.ts +2 -2
- package/dist/dts-cjs/ensureSchema.d.ts.map +1 -0
- package/dist/{dts → dts-cjs}/index.d.ts +4 -3
- package/dist/dts-cjs/index.d.ts.map +1 -0
- package/dist/{dts → dts-cjs}/internal/ddl.d.ts +1 -1
- package/dist/{dts → dts-cjs}/internal/ddl.d.ts.map +1 -1
- package/dist/{dts → dts-cjs}/internal/matchSql.d.ts.map +1 -1
- package/dist/{dts → dts-cjs}/internal/readPlan.d.ts +1 -1
- package/dist/{dts → dts-cjs}/internal/readPlan.d.ts.map +1 -1
- package/dist/{dts → dts-cjs}/internal/transport.d.ts +20 -15
- package/dist/dts-cjs/internal/transport.d.ts.map +1 -0
- package/dist/dts-cjs/package.json +3 -0
- package/dist/dts-cjs/store.d.ts.map +1 -0
- package/dist/dts-esm/clients.d.ts +66 -0
- package/dist/dts-esm/clients.d.ts.map +1 -0
- package/dist/dts-esm/config.d.ts +48 -0
- package/dist/dts-esm/config.d.ts.map +1 -0
- package/dist/dts-esm/ensureSchema.d.ts +40 -0
- package/dist/dts-esm/ensureSchema.d.ts.map +1 -0
- package/dist/dts-esm/index.d.ts +42 -0
- package/dist/dts-esm/index.d.ts.map +1 -0
- package/dist/dts-esm/internal/ddl.d.ts +174 -0
- package/dist/dts-esm/internal/ddl.d.ts.map +1 -0
- package/dist/dts-esm/internal/matchSql.d.ts +164 -0
- package/dist/dts-esm/internal/matchSql.d.ts.map +1 -0
- package/dist/dts-esm/internal/readPlan.d.ts +72 -0
- package/dist/dts-esm/internal/readPlan.d.ts.map +1 -0
- package/dist/dts-esm/internal/transport.d.ts +131 -0
- package/dist/dts-esm/internal/transport.d.ts.map +1 -0
- package/dist/dts-esm/package.json +3 -0
- package/dist/dts-esm/store.d.ts +17 -0
- package/dist/dts-esm/store.d.ts.map +1 -0
- package/dist/esm/clients.js +34 -24
- package/dist/esm/clients.js.map +1 -1
- package/dist/esm/config.js +9 -15
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/ensureSchema.js +2 -2
- package/dist/esm/ensureSchema.js.map +1 -1
- package/dist/esm/index.js +4 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/matchSql.js +15 -4
- package/dist/esm/internal/matchSql.js.map +1 -1
- package/dist/esm/internal/readPlan.js +24 -1
- package/dist/esm/internal/readPlan.js.map +1 -1
- package/dist/esm/internal/transport.js +18 -14
- package/dist/esm/internal/transport.js.map +1 -1
- package/dist/esm/store.js +30 -25
- package/dist/esm/store.js.map +1 -1
- package/package.json +13 -11
- package/src/clients.ts +43 -31
- package/src/config.ts +12 -13
- package/src/ensureSchema.ts +2 -2
- package/src/index.ts +4 -3
- package/src/internal/ddl.ts +1 -1
- package/src/internal/matchSql.ts +15 -4
- package/src/internal/readPlan.ts +3 -2
- package/src/internal/transport.ts +28 -20
- package/src/store.ts +35 -28
- package/dist/dts/clients.d.ts +0 -34
- package/dist/dts/clients.d.ts.map +0 -1
- package/dist/dts/ensureSchema.d.ts.map +0 -1
- package/dist/dts/index.d.ts.map +0 -1
- package/dist/dts/internal/transport.d.ts.map +0 -1
- package/dist/dts/store.d.ts.map +0 -1
- /package/dist/{dts → dts-cjs}/internal/matchSql.d.ts +0 -0
- /package/dist/{dts → dts-cjs}/store.d.ts +0 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kairos-es/store-postgres — the Postgres `DcbEventStore` backend.
|
|
3
|
+
*
|
|
4
|
+
* A single store is one `{ schema, tablePrefix }` naming pair (`PostgresStoreConfig`);
|
|
5
|
+
* every object it owns is derived from that pair, so multiple isolated stores
|
|
6
|
+
* cohabit one database. The engine is the two-table B-tree + exclusive-lock
|
|
7
|
+
* conditional append of ADR-0002, with JSON transport and a two-client Layer to
|
|
8
|
+
* survive Neon's pooled LISTEN/NOTIFY drop.
|
|
9
|
+
*
|
|
10
|
+
* Wiring, end to end:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { PgClient } from '@effect/sql-pg'
|
|
14
|
+
* import {
|
|
15
|
+
* ensureSchema,
|
|
16
|
+
* layer,
|
|
17
|
+
* pooledLayer,
|
|
18
|
+
* directLayer,
|
|
19
|
+
* } from '@kairos-es/store-postgres'
|
|
20
|
+
*
|
|
21
|
+
* const config = { schema: 'public', tablePrefix: 'dcb_events' }
|
|
22
|
+
*
|
|
23
|
+
* // 1. Register `ensureSchema(config)` in YOUR migrator (or run it at boot).
|
|
24
|
+
* // 2. Provide the two clients (same URL off Neon; a direct endpoint for
|
|
25
|
+
* // `directLayer` on Neon), then the store layer over them.
|
|
26
|
+
* const clients = Layer.merge(
|
|
27
|
+
* pooledLayer({ url: Redacted.make(poolUrl) }),
|
|
28
|
+
* directLayer({ url: Redacted.make(directUrl) }),
|
|
29
|
+
* )
|
|
30
|
+
* const store = layer(config).pipe(Layer.provide(clients))
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* The store `layer` performs NO DDL: it depends only on the two client services
|
|
34
|
+
* and assumes `ensureSchema` has run. `ensureSchema` requires the generic
|
|
35
|
+
* `SqlClient` service so it slots into any migrator; the runtime uses the
|
|
36
|
+
* store-owned services.
|
|
37
|
+
*/
|
|
38
|
+
export { directLayer, PgClientDirect, PgClientPooled, pooledLayer, } from './clients.js';
|
|
39
|
+
export type { PostgresStoreConfig } from './config.js';
|
|
40
|
+
export { ensureSchema } from './ensureSchema.js';
|
|
41
|
+
export { layer } from './store.js';
|
|
42
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,OAAO,EACL,WAAW,EACX,cAAc,EACd,cAAc,EACd,WAAW,GACZ,MAAM,WAAW,CAAA;AAClB,YAAY,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAA;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal DDL and object-naming for the Postgres `DcbEventStore` backend.
|
|
3
|
+
*
|
|
4
|
+
* This module is the single source of truth for every object name a store owns
|
|
5
|
+
* and for the plpgsql function bodies. It is deliberately factored apart from
|
|
6
|
+
* the public engine so that:
|
|
7
|
+
*
|
|
8
|
+
* 1. names are derived ONCE, from `{ schema, tablePrefix }`, and reused by both
|
|
9
|
+
* `ensureSchema` (DDL) and the runtime (`append`/`read`/`subscribe`), so the
|
|
10
|
+
* migration and the queries can never disagree about what a table is called;
|
|
11
|
+
* 2. the append-function body is assembled from COMPOSABLE parts — in
|
|
12
|
+
* particular the `LOCK TABLE … IN EXCLUSIVE MODE` clause is an omittable
|
|
13
|
+
* fragment — so a test can `CREATE OR REPLACE` a deliberately-LOCKLESS twin of
|
|
14
|
+
* the append function to prove the shared contract suite actually detects a
|
|
15
|
+
* non-serialising store. The production Layer never exposes a lockless option;
|
|
16
|
+
* the seam lives here, in an internal module, reachable only from the test
|
|
17
|
+
* graph.
|
|
18
|
+
*
|
|
19
|
+
* WHY names are never string-concatenated: every identifier flows through
|
|
20
|
+
* `sql(identifier)`, which quotes and dot-splits safely, so a hostile or merely
|
|
21
|
+
* awkward `schema`/`tablePrefix` cannot inject SQL or collide with a reserved
|
|
22
|
+
* word. The plpgsql *bodies*, which must embed the resolved names as literal
|
|
23
|
+
* text inside a `CREATE FUNCTION … $$ … $$` string, use the SAME quoting via a
|
|
24
|
+
* small `quoteQualified` helper so the body text matches what `sql(identifier)`
|
|
25
|
+
* would emit; VALUES stay bound parameters (`sql.unsafe` with `params`) and are
|
|
26
|
+
* never interpolated.
|
|
27
|
+
*/
|
|
28
|
+
import type { SqlClient, Statement } from 'effect/sql';
|
|
29
|
+
/**
|
|
30
|
+
* A store is one physical `{ schema, tablePrefix }` pair. Every object it owns
|
|
31
|
+
* (both tables, both indexes, both functions, and the NOTIFY channel) is derived
|
|
32
|
+
* from this pair, so one prefix is one completely isolated store: its own
|
|
33
|
+
* bigserial sequence, its own exclusive-lock scope, its own wake channel.
|
|
34
|
+
*/
|
|
35
|
+
export interface ResolvedNames {
|
|
36
|
+
readonly schema: string;
|
|
37
|
+
readonly tablePrefix: string;
|
|
38
|
+
/** `<schema>.<prefix>` — the main event table. */
|
|
39
|
+
readonly mainTable: string;
|
|
40
|
+
/** `<schema>.<prefix>_tags` — the junction tag table. */
|
|
41
|
+
readonly tagTable: string;
|
|
42
|
+
/** `<prefix>_idx_id_type` — the covering unique index on the main table. */
|
|
43
|
+
readonly mainIndex: string;
|
|
44
|
+
/** `<prefix>_idx_tag_main_id` — the composite B-tree on the tag table. */
|
|
45
|
+
readonly tagIndex: string;
|
|
46
|
+
/** `<schema>.<prefix>_append` — the conditional (lock → check → insert → notify) function. */
|
|
47
|
+
readonly appendFn: string;
|
|
48
|
+
/**
|
|
49
|
+
* `<schema>.<prefix>_append_unconditional` — the check-free insert twin. It takes
|
|
50
|
+
* the SAME exclusive lock as the conditional one, which is what discharges the
|
|
51
|
+
* subscribe machine's ascending-commit-order precondition on this engine;
|
|
52
|
+
* `unconditionalAppendBody` owns why a lock-free twin would not.
|
|
53
|
+
*/
|
|
54
|
+
readonly appendUnconditionalFn: string;
|
|
55
|
+
/**
|
|
56
|
+
* The `LISTEN`/`NOTIFY` channel: the fully-qualified events-table name with
|
|
57
|
+
* dots replaced by `_`. A channel is a single unqualified identifier, so the
|
|
58
|
+
* schema is folded into the name to keep two stores in different schemas but
|
|
59
|
+
* with the same prefix from sharing a channel.
|
|
60
|
+
*/
|
|
61
|
+
readonly channel: string;
|
|
62
|
+
}
|
|
63
|
+
/** Default schema when the config omits one. */
|
|
64
|
+
export declare const DEFAULT_SCHEMA = "public";
|
|
65
|
+
/** Default table prefix when the config omits one. */
|
|
66
|
+
export declare const DEFAULT_TABLE_PREFIX = "dcb_events";
|
|
67
|
+
/**
|
|
68
|
+
* The first derived identifier that exceeds 63 bytes, as a human-readable
|
|
69
|
+
* message, or `undefined` when every name fits. Shared by `resolveNames` (which
|
|
70
|
+
* throws) and the `PostgresStoreConfig` schema filter (which fails validation),
|
|
71
|
+
* so the byte bound is enforced identically at both the name-derivation and the
|
|
72
|
+
* config-decode boundary.
|
|
73
|
+
*/
|
|
74
|
+
export declare const oversizedIdentifier: (config: {
|
|
75
|
+
readonly schema?: string;
|
|
76
|
+
readonly tablePrefix?: string;
|
|
77
|
+
}) => string | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Derive every owned object name from `{ schema, tablePrefix }`. The index and
|
|
80
|
+
* function *base* names are unqualified prefixes (`<prefix>_…`) because an index
|
|
81
|
+
* name is scoped to its table's schema and a function is created in `<schema>`;
|
|
82
|
+
* the tables and functions carry the schema explicitly so `sql(identifier)`
|
|
83
|
+
* dot-splits them.
|
|
84
|
+
*
|
|
85
|
+
* Throws at construction if any derived identifier would be silently truncated
|
|
86
|
+
* (see `oversizedIdentifier` / `MAX_IDENTIFIER_BYTES`) — a loud failure at the
|
|
87
|
+
* single choke point beats an invisible index-skip or channel collision later.
|
|
88
|
+
*/
|
|
89
|
+
export declare const resolveNames: (config: {
|
|
90
|
+
readonly schema?: string;
|
|
91
|
+
readonly tablePrefix?: string;
|
|
92
|
+
}) => ResolvedNames;
|
|
93
|
+
/**
|
|
94
|
+
* Quote a possibly-qualified identifier for embedding inside a plpgsql body as
|
|
95
|
+
* literal text. Splits on `.` and double-quotes each atom, mirroring what
|
|
96
|
+
* `sql(identifier)` emits, so the body's table references and the runtime's
|
|
97
|
+
* `sql(identifier)` references resolve to exactly the same object. Any embedded
|
|
98
|
+
* `"` is doubled per SQL identifier-quoting rules.
|
|
99
|
+
*/
|
|
100
|
+
export declare const quoteQualified: (name: string) => string;
|
|
101
|
+
/**
|
|
102
|
+
* The named body-fragment seams, replacing positional boolean flags — the old
|
|
103
|
+
* `unconditionalAppendBody(names, true, false)` was boolean-blind. Both knobs
|
|
104
|
+
* default to the production configuration (locked + notifying); a test twin
|
|
105
|
+
* overrides one field BY NAME. This is the union of both knobs; each body `Pick`s
|
|
106
|
+
* only the ones it actually varies — the conditional body always notifies, so it
|
|
107
|
+
* never took `notify`.
|
|
108
|
+
*
|
|
109
|
+
* - `lock` — include the `LOCK TABLE … IN EXCLUSIVE MODE` clause (default true).
|
|
110
|
+
* BOTH twins vary it: the test-only lockless twins pass `false` to prove the
|
|
111
|
+
* lock is load-bearing (the conditional path's lockless meta-test and the
|
|
112
|
+
* unconditional commit-order repro).
|
|
113
|
+
* - `notify` — include the `pg_notify` wake (default true). ONLY the unconditional
|
|
114
|
+
* twin varies it: the poll-only delivery test passes `false` to prove the
|
|
115
|
+
* periodic poll, not NOTIFY, is the delivery guarantee.
|
|
116
|
+
*/
|
|
117
|
+
export interface AppendBodyOptions {
|
|
118
|
+
readonly lock?: boolean;
|
|
119
|
+
readonly notify?: boolean;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Which append function to (re)create, plus the body-fragment seams. `kind`
|
|
123
|
+
* selects the conditional (guarded) or unconditional (check-free) twin — fixing
|
|
124
|
+
* the function NAME and SIGNATURE — and the seams default to the production
|
|
125
|
+
* configuration.
|
|
126
|
+
*
|
|
127
|
+
* A DISCRIMINATED union, not `extends AppendBodyOptions`, so the seams a `kind`
|
|
128
|
+
* cannot vary are un-expressible: the conditional twin exposes only `lock` (it
|
|
129
|
+
* always notifies), so `{ kind: 'conditional', notify: false }` is a compile error
|
|
130
|
+
* rather than a silently-ignored dead option.
|
|
131
|
+
*/
|
|
132
|
+
export type AppendFunctionOptions = ({
|
|
133
|
+
readonly kind: 'conditional';
|
|
134
|
+
} & Pick<AppendBodyOptions, 'lock'>) | ({
|
|
135
|
+
readonly kind: 'unconditional';
|
|
136
|
+
} & AppendBodyOptions);
|
|
137
|
+
/**
|
|
138
|
+
* Build ONE complete `CREATE OR REPLACE FUNCTION … RETURNS SETOF bigint LANGUAGE
|
|
139
|
+
* plpgsql AS $kairos$…$kairos$` statement for an append function. This is the
|
|
140
|
+
* single source of the wrapper — its qualified name, argument SIGNATURE, and
|
|
141
|
+
* `$kairos$`-delimited body — consumed by `ddlStatements` (production, both twins)
|
|
142
|
+
* AND by the test installers that `CREATE OR REPLACE` a lockless / notify-
|
|
143
|
+
* suppressed twin over their own prefix.
|
|
144
|
+
*
|
|
145
|
+
* WHY one builder: `ddlStatements` plus three test twins previously hand-restated
|
|
146
|
+
* this wrapper, several with string-INTERPOLATED identifiers this module's own
|
|
147
|
+
* doctrine forbids (`"${schema}"."${prefix}_append"` rather than
|
|
148
|
+
* `quoteQualified(names.appendFn)`). A drift in the SIGNATURE across those copies
|
|
149
|
+
* is a silent footgun: `CREATE OR REPLACE` with a changed argument list creates a
|
|
150
|
+
* new OVERLOAD instead of replacing, leaving the original production function live
|
|
151
|
+
* under test. Single-sourcing name + signature makes a twin unable to diverge —
|
|
152
|
+
* a twin `CREATE OR REPLACE`s exactly the function `ddlStatements` created.
|
|
153
|
+
*/
|
|
154
|
+
export declare const appendFunctionStatement: (names: ResolvedNames, options: AppendFunctionOptions) => string;
|
|
155
|
+
/**
|
|
156
|
+
* The full ordered list of idempotent DDL statements that create/refresh a
|
|
157
|
+
* store's objects. Each entry is a `sql.unsafe` statement string; VALUES are not
|
|
158
|
+
* involved (pure DDL), and every identifier is pre-quoted via `quoteQualified`
|
|
159
|
+
* to match the runtime's `sql(identifier)` resolution.
|
|
160
|
+
*
|
|
161
|
+
* Ordering matters: tables before their indexes and before the tag table's FK,
|
|
162
|
+
* functions last (they reference the tables). Everything is `IF NOT EXISTS` /
|
|
163
|
+
* `CREATE OR REPLACE`, so re-running is a no-op — the migration payload is safe
|
|
164
|
+
* to register in any migrator or to run directly.
|
|
165
|
+
*/
|
|
166
|
+
export declare const ddlStatements: (names: ResolvedNames) => ReadonlyArray<string>;
|
|
167
|
+
/**
|
|
168
|
+
* Run the ordered DDL against the generic `SqlClient`. Kept here (not in the
|
|
169
|
+
* public `ensureSchema`) so the same builder feeds both the public migration
|
|
170
|
+
* payload and any internal test setup. Each statement is a separate `sql.unsafe`
|
|
171
|
+
* call so a driver that rejects multi-statement strings still works.
|
|
172
|
+
*/
|
|
173
|
+
export declare const runDdl: (sql: SqlClient.SqlClient, names: ResolvedNames) => ReadonlyArray<Statement.Statement<unknown>>;
|
|
174
|
+
//# sourceMappingURL=ddl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ddl.d.ts","sourceRoot":"","sources":["../../../src/internal/ddl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAWtD;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,kDAAkD;IAClD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,yDAAyD;IACzD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,4EAA4E;IAC5E,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,0EAA0E;IAC1E,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,8FAA8F;IAC9F,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB;;;;;OAKG;IACH,QAAQ,CAAC,qBAAqB,EAAE,MAAM,CAAA;IACtC;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CACzB;AAED,gDAAgD;AAChD,eAAO,MAAM,cAAc,WAAW,CAAA;AACtC,sDAAsD;AACtD,eAAO,MAAM,oBAAoB,eAAe,CAAA;AAuChD;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,GAAI,QAAQ;IAC1C,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAC9B,KAAG,MAAM,GAAG,SAQZ,CAAA;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ;IACnC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAC9B,KAAG,aAkBH,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,KAAG,MAIhC,CAAA;AA2Cd;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAA;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAC1B;AAoOD;;;;;;;;;;GAUG;AACH,MAAM,MAAM,qBAAqB,GAC7B,CAAC;IAAE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAA;CAAE,GAAG,IAAI,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC,GACpE,CAAC;IAAE,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAA;CAAE,GAAG,iBAAiB,CAAC,CAAA;AAE5D;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,uBAAuB,GAClC,OAAO,aAAa,EACpB,SAAS,qBAAqB,KAC7B,MAqBF,CAAA;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,aAAa,GAAI,OAAO,aAAa,KAAG,aAAa,CAAC,MAAM,CAmExE,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,MAAM,GACjB,KAAK,SAAS,CAAC,SAAS,EACxB,OAAO,aAAa,KACnB,aAAa,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,CACmB,CAAA"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single source of truth for the tag-first match SQL.
|
|
3
|
+
*
|
|
4
|
+
* The servable-query matching semantics — the tagged AND-superset match with
|
|
5
|
+
* distinct-tag counting, the allowed-types filter, the exclusive `after` bound —
|
|
6
|
+
* were hand-duplicated between the TS read path (`store.ts`) and the plpgsql
|
|
7
|
+
* conflict check (`ddl.ts`): two SQL dialect-copies, in two files, that drift
|
|
8
|
+
* independently (the read's wildcard mis-routing of a degenerate empty item was
|
|
9
|
+
* one such drift). This module emits the shared fragments ONCE, parameterised by:
|
|
10
|
+
*
|
|
11
|
+
* - the PLACEHOLDER TOKENS each call site binds — plpgsql parameter names
|
|
12
|
+
* (`query_items`, `COALESCE(after_id, 0)`) for the conflict body vs bound
|
|
13
|
+
* `$n` placeholders for the runtime `sql.unsafe(text, params)` read; and
|
|
14
|
+
* - the PROJECTION — full rows (read) vs an `EXISTS`/`LIMIT 1` probe (conflict),
|
|
15
|
+
* which each call site wraps around the shared `qualifiedRows` source.
|
|
16
|
+
*
|
|
17
|
+
* The `jsonb_to_recordset` event column list also lives here as ONE descriptor,
|
|
18
|
+
* so the write recordset (`ddl.ts`) and the read projection cannot disagree.
|
|
19
|
+
*
|
|
20
|
+
* Table names arrive PRE-QUOTED (via `quoteQualified` in `ddl.ts`), so this
|
|
21
|
+
* module never quotes identifiers itself and stays free of the naming logic.
|
|
22
|
+
* Every fragment assumes the main events table is aliased `m`.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* The event payload columns shared by the write recordset and every read
|
|
26
|
+
* projection, as `[name, recordsetType]` pairs. `id` (the `bigserial` position)
|
|
27
|
+
* is prepended by the read projection and absent from the events recordset.
|
|
28
|
+
*
|
|
29
|
+
* `as const` freezes it into a tuple of literal names so `EventColumnName` (below)
|
|
30
|
+
* can derive the exact column-name union — the whole point of the "one descriptor"
|
|
31
|
+
* story: the TS transport shapes in `internal/transport.ts` are mapped types over
|
|
32
|
+
* that union, so a column rename here is a COMPILE error there, not a silent drift
|
|
33
|
+
* the lax read boundary (ADR-0006) would only surface as corrupt data under test.
|
|
34
|
+
*/
|
|
35
|
+
declare const EVENT_COLUMNS: readonly [readonly ["type", "text"], readonly ["data", "text"], readonly ["tags", "text[]"], readonly ["uuid", "text"], readonly ["occurred_at", "timestamptz"], readonly ["metadata", "jsonb"]];
|
|
36
|
+
/**
|
|
37
|
+
* The event payload column NAMES as a string-literal union, derived from the ONE
|
|
38
|
+
* `EVENT_COLUMNS` descriptor. `internal/transport.ts` builds its transport shapes
|
|
39
|
+
* (`EventJson`, the JSON sent to the append functions; `EventRow`, a raw driver
|
|
40
|
+
* row) as mapped types over this union, so the transport shapes cannot drift from
|
|
41
|
+
* the SQL recordset/projection — a rename in the descriptor removes the old key
|
|
42
|
+
* from the union and makes the mapped type fail to compile.
|
|
43
|
+
*/
|
|
44
|
+
export type EventColumnName = (typeof EVENT_COLUMNS)[number][0];
|
|
45
|
+
/**
|
|
46
|
+
* The `jsonb_to_recordset(new_events) AS (…)` column definitions —
|
|
47
|
+
* `type text, data text, tags text[], uuid text, occurred_at timestamptz,
|
|
48
|
+
* metadata jsonb`. The
|
|
49
|
+
* transport `data` is base64 TEXT (restored to `bytea` by `decode(…, 'base64')`
|
|
50
|
+
* at insert), which is why it is `text` here, not `bytea`.
|
|
51
|
+
*/
|
|
52
|
+
export declare const eventRecordsetColumns: string;
|
|
53
|
+
/**
|
|
54
|
+
* The read projection column list (`id` + the event columns), optionally
|
|
55
|
+
* prefixed with a table alias (`'m.'`). Sourced from the one descriptor so it
|
|
56
|
+
* cannot drift from the recordset.
|
|
57
|
+
*/
|
|
58
|
+
export declare const readColumns: (prefix?: string) => string;
|
|
59
|
+
/**
|
|
60
|
+
* The alias the append body gives the expanded `new_events` recordset — the `ne`
|
|
61
|
+
* CTE in `ddl.ts` — which every expression in `EVENT_COLUMN_SOURCE` reads from.
|
|
62
|
+
* Exported so the CTE is declared under the one name its readers assume.
|
|
63
|
+
*/
|
|
64
|
+
export declare const RECORDSET_ALIAS = "ne";
|
|
65
|
+
/**
|
|
66
|
+
* The bare event column names — `type, data, tags, uuid, occurred_at, metadata` —
|
|
67
|
+
* which the append body uses twice: as the recordset CTE's projection and as the
|
|
68
|
+
* `INSERT INTO t (…)` target list.
|
|
69
|
+
*/
|
|
70
|
+
export declare const eventColumnList: string;
|
|
71
|
+
/**
|
|
72
|
+
* The append insert's SELECT list: one source expression per column, in the SAME
|
|
73
|
+
* ORDER as `eventColumnList`.
|
|
74
|
+
*
|
|
75
|
+
* The two halves of `INSERT INTO t (a, b) SELECT x, y` agree POSITION BY POSITION
|
|
76
|
+
* and nothing checks that they do: two columns of compatible type swap without an
|
|
77
|
+
* error at any layer and the data lands in the wrong one. Both halves are therefore
|
|
78
|
+
* one walk of the one descriptor, so the append body has no second ordering to keep
|
|
79
|
+
* in step and no hand-written list for a new column to be missing from.
|
|
80
|
+
*/
|
|
81
|
+
export declare const eventInsertSelectList: string;
|
|
82
|
+
/**
|
|
83
|
+
* The allowed-types filter for a candidate row aliased `m`: an item with no
|
|
84
|
+
* types matches ANY type; otherwise the event's type must be one of them.
|
|
85
|
+
* `typesCol` is the `text[]` column holding the item's types — `q.allowed_types`
|
|
86
|
+
* for a tagged match, `qi.types` for a tagless one.
|
|
87
|
+
*/
|
|
88
|
+
export declare const allowedTypesPredicate: (typesCol: string) => string;
|
|
89
|
+
/** The placeholder tokens + pre-quoted table names one call site binds. */
|
|
90
|
+
export interface MatchTokens {
|
|
91
|
+
/** SQL yielding the query-items jsonb: `query_items` or `$1::jsonb`. */
|
|
92
|
+
readonly queryItemsExpr: string;
|
|
93
|
+
/** SQL for the exclusive lower bound: `COALESCE(after_id, 0)` or `$2`. */
|
|
94
|
+
readonly afterExpr: string;
|
|
95
|
+
/** Pre-quoted main events table. */
|
|
96
|
+
readonly main: string;
|
|
97
|
+
/** Pre-quoted tag junction table. */
|
|
98
|
+
readonly tag: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The tag-first match CTE chain shared by the read and conflict paths: expand
|
|
102
|
+
* the query items (`qi`), join the tag junction, count DISTINCT matched tags per
|
|
103
|
+
* (event, item), and keep events matching an item's FULL tag superset. Returns
|
|
104
|
+
* `qi, initial_matches, matched_groups, qualified_ids` — the caller follows it
|
|
105
|
+
* with a projection CTE selecting from `qualified_ids`.
|
|
106
|
+
*
|
|
107
|
+
* ## Both sides of the count are SET SIZES, and that is the whole invariant
|
|
108
|
+
*
|
|
109
|
+
* `qualified_ids` compares two numbers, and the ONE thing an edit here must
|
|
110
|
+
* preserve is that they count the same kind of thing: `matched_tag_count` is the
|
|
111
|
+
* number of DISTINCT tags the event carried out of the item's requirement, so
|
|
112
|
+
* `required_tag_count` is the size of that requirement AS A SET and never the
|
|
113
|
+
* length of the list it arrived as. A query item may list one tag twice — no
|
|
114
|
+
* constructor de-duplicates, `assertServableQuery` admits it and
|
|
115
|
+
* `classifyServableQuery` routes it here — and the in-memory oracle folds an
|
|
116
|
+
* item's tags into a superset test that a repeat cannot change. Comparing a
|
|
117
|
+
* de-duplicated 1 against a list length of 2 refused such a match: the READ
|
|
118
|
+
* returned nothing and, because the plpgsql conflict body composes this same
|
|
119
|
+
* chain, the GUARD found no conflict and the append it should have refused
|
|
120
|
+
* succeeded — an optimistic-concurrency check that escaped silently.
|
|
121
|
+
*
|
|
122
|
+
* The asymmetry has two repairs — de-duplicate both sides, or expand both — and
|
|
123
|
+
* only the first is available here. The matched side CANNOT become a plain row
|
|
124
|
+
* count, because this engine's junction holds one row per (tag, event) OCCURRENCE
|
|
125
|
+
* and not per pair, so an event whose own `tags` list repeats a tag has two rows
|
|
126
|
+
* there and a row count would refuse a single-tag item against it. `ddl.ts` states
|
|
127
|
+
* that property on the junction, and it is what `COUNT(DISTINCT tag)` is here to
|
|
128
|
+
* absorb; the requirement is de-duplicated to meet it.
|
|
129
|
+
*
|
|
130
|
+
* `required_tag_count` is therefore computed in `qi`, once per ITEM, rather than
|
|
131
|
+
* in the group that consumes it — the chain groups per (event, item), so a
|
|
132
|
+
* subquery in `matched_groups` would be re-evaluated per matched event. It is
|
|
133
|
+
* `count(*)` over a `SELECT DISTINCT` rather than `count(DISTINCT …)` because the
|
|
134
|
+
* latter drops a NULL element: a `[tag, NULL]` requirement would shrink to 1,
|
|
135
|
+
* meet a single matched tag and match, where the oracle's superset test cannot be
|
|
136
|
+
* satisfied by a NULL and refuses it.
|
|
137
|
+
*
|
|
138
|
+
* The `array_length(qi.tags, 1) >= 1` guard excludes tagless items from the
|
|
139
|
+
* tagged branch (load-bearing for the conflict body, which also has a separate
|
|
140
|
+
* tagless branch; a no-op for the read, whose tagged shape is all-tagged). It
|
|
141
|
+
* reads the item's own array because it is a test of the item's SHAPE — tagged or
|
|
142
|
+
* tagless — and not part of the match arithmetic above.
|
|
143
|
+
*/
|
|
144
|
+
export declare const taggedMatchChain: (tokens: MatchTokens) => string;
|
|
145
|
+
/**
|
|
146
|
+
* The shared qualified-rows source: join `qualified_ids` back to the main table
|
|
147
|
+
* (aliased `m`), re-apply the exclusive `after` bound, and filter by allowed
|
|
148
|
+
* types. The read wraps this in an ordered/limited `DISTINCT m.id` CTE; the
|
|
149
|
+
* conflict wraps it in a `SELECT 1 … LIMIT 1` existence probe.
|
|
150
|
+
*/
|
|
151
|
+
export declare const qualifiedRows: (tokens: Pick<MatchTokens, "main" | "afterExpr">) => string;
|
|
152
|
+
/** The wildcard read (no items): every event after the bound, ordered/limited. */
|
|
153
|
+
export declare const wildcardReadSql: (main: string) => string;
|
|
154
|
+
/** The type-only read (single tagless item): filter by `type = ANY($2)`. */
|
|
155
|
+
export declare const typeOnlyReadSql: (main: string) => string;
|
|
156
|
+
/**
|
|
157
|
+
* The tagged read: the shared match chain, then a `filtered_ids` CTE that orders
|
|
158
|
+
* and limits the qualified ids by position, then a full-row select. Bound params
|
|
159
|
+
* are `$1` = query-items jsonb, `$2` = exclusive `after`, `$3` = limit (`NULL` =
|
|
160
|
+
* no limit).
|
|
161
|
+
*/
|
|
162
|
+
export declare const taggedReadSql: (main: string, tag: string) => string;
|
|
163
|
+
export {};
|
|
164
|
+
//# sourceMappingURL=matchSql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matchSql.d.ts","sourceRoot":"","sources":["../../../src/internal/matchSql.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH;;;;;;;;;;GAUG;AACH,QAAA,MAAM,aAAa,kMASlB,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;AAE/D;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,EAAE,MAExB,CAAA;AAEZ;;;;GAIG;AACH,eAAO,MAAM,WAAW,GAAI,eAAW,KAAG,MAGvC,CAAA;AAEH;;;;GAIG;AACH,eAAO,MAAM,eAAe,OAAO,CAAA;AAgCnC;;;;GAIG;AACH,eAAO,MAAM,eAAe,EAAE,MAE7B,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,qBAAqB,EAAE,MAExB,CAAA;AAEZ;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,UAAU,MAAM,KAAG,MACkD,CAAA;AAE3G,2EAA2E;AAC3E,MAAM,WAAW,WAAW;IAC1B,wEAAwE;IACxE,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;IAC/B,0EAA0E;IAC1E,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,oCAAoC;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,qCAAqC;IACrC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,eAAO,MAAM,gBAAgB,GAAI,QAAQ,WAAW,KAAG,MA6BnD,CAAA;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GACxB,QAAQ,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,WAAW,CAAC,KAC9C,MAImD,CAAA;AAEtD,kFAAkF;AAClF,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,KAAG,MAKnC,CAAA;AAEZ,4EAA4E;AAC5E,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,KAAG,MAKnC,CAAA;AAEZ;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,EAAE,KAAK,MAAM,KAAG,MAWtC,CAAA"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The read-plan compiler for the Postgres engine, extracted from `store.ts` so it
|
|
3
|
+
* is a self-contained unit depending only on the PRE-QUOTED table names — never on
|
|
4
|
+
* the SQL clients. `store.ts`'s `read` and its `subscribe` — the latter feeding the
|
|
5
|
+
* plan to core's subscribe machine as that machine's `fetch` seam — both compile a
|
|
6
|
+
* plan here and run it against whichever client they hold.
|
|
7
|
+
*
|
|
8
|
+
* `compileFetchPlan` turns a servable `Query` into prepared SQL text plus a
|
|
9
|
+
* cursor-only `bind`, so a long-lived subscription re-binds only the cursor rather
|
|
10
|
+
* than re-dispatching the shape and re-serialising the query JSON on every poll.
|
|
11
|
+
* Which shape a query IS is not decided here: core's `classifyServableQuery` owns
|
|
12
|
+
* that dispatch beside the grammar it dispatches over (`Query.ts`), including the
|
|
13
|
+
* degenerate empty item's routing. The JSON/JSONB transport shapes and
|
|
14
|
+
* (de)serialisers — `EventRow`, `jsonbText`, `toQueryItemsJson`, `rowToSequenced`
|
|
15
|
+
* — live in `internal/transport.ts`, and this module only compiles plans.
|
|
16
|
+
*
|
|
17
|
+
* A plan compiled here is therefore the one place the Postgres engine dispatches
|
|
18
|
+
* a shape in TypeScript, and the exhaustiveness below protects BOTH verbs that
|
|
19
|
+
* compile one — `read`, and the `subscribe` poll sharing its plan. What it cannot
|
|
20
|
+
* reach is the append: its conflict guard covers the same three shapes inside its
|
|
21
|
+
* plpgsql body, where no compiler follows, and `ddl.ts` owns what that rests on
|
|
22
|
+
* instead.
|
|
23
|
+
*/
|
|
24
|
+
import type { Position, ReadLimit } from '@kairos-es/core';
|
|
25
|
+
import { type Query } from '@kairos-es/core';
|
|
26
|
+
import type { Effect } from 'effect';
|
|
27
|
+
import type { SqlClient, SqlError } from 'effect/sql';
|
|
28
|
+
import { type EventRow } from './transport.js';
|
|
29
|
+
/** The pre-quoted table names a plan reads from (see `quoteQualified`). */
|
|
30
|
+
export interface ReadTables {
|
|
31
|
+
/** Pre-quoted main events table. */
|
|
32
|
+
readonly main: string;
|
|
33
|
+
/** Pre-quoted tag junction table. */
|
|
34
|
+
readonly tag: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A COMPILED read plan: the prepared SQL text plus a `bind` that produces the
|
|
38
|
+
* positional params for a given `after`/`limit`. Everything
|
|
39
|
+
* query-dependent-but-cursor-independent — the shape dispatch, the SQL text, the
|
|
40
|
+
* query-items JSON serialisation — is done ONCE, so a long-lived subscription's
|
|
41
|
+
* poll loop re-binds only the cursor rather than re-classifying and re-JSON-
|
|
42
|
+
* serialising on every iteration.
|
|
43
|
+
*/
|
|
44
|
+
export interface FetchPlan {
|
|
45
|
+
readonly text: string;
|
|
46
|
+
readonly bind: (after: Position, limit: ReadLimit | null) => ReadonlyArray<unknown>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Compile a servable query into a `FetchPlan`. The SQL comes from the shared
|
|
50
|
+
* `matchSql` builders (the tagged path composes the same emitted match chain as the
|
|
51
|
+
* conflict check, which binds its own placeholder tokens and wraps it differently),
|
|
52
|
+
* with the pre-quoted names embedded and the VALUES bound as `$n` parameters
|
|
53
|
+
* (never interpolated).
|
|
54
|
+
*
|
|
55
|
+
* The `switch` is exhaustive over core's `ServableQueryShape` with NO `default`
|
|
56
|
+
* and no trailing fall-through arm, and what that buys is a COMPILE error where
|
|
57
|
+
* there would otherwise be a silent mis-serve. Core owns that union and this
|
|
58
|
+
* module only consumes it, so a fourth member added there is a change this file
|
|
59
|
+
* cannot see coming: a cascade ending in an unguarded tagged branch would have
|
|
60
|
+
* SERVED that shape as tagged, and both `read` and the `subscribe` poll sharing
|
|
61
|
+
* its plan would have returned the wrong rows — no error raised, nothing failing
|
|
62
|
+
* to typecheck. Every arm returns instead, so the added member leaves this
|
|
63
|
+
* function with a reachable end and a return type that does not admit
|
|
64
|
+
* `undefined`, and the module stops compiling.
|
|
65
|
+
*/
|
|
66
|
+
export declare const compileFetchPlan: (tables: ReadTables, query: Query) => FetchPlan;
|
|
67
|
+
/**
|
|
68
|
+
* Run a compiled plan for a given `after`/`limit`, honouring `after` (exclusive;
|
|
69
|
+
* absent → `0`) and `limit` (`NULL` = no limit), ordered by `id ASC`.
|
|
70
|
+
*/
|
|
71
|
+
export declare const runFetch: (sql: SqlClient.SqlClient, plan: FetchPlan, after: Position | undefined, limit: ReadLimit | undefined) => Effect.Effect<ReadonlyArray<EventRow>, SqlError.SqlError>;
|
|
72
|
+
//# sourceMappingURL=readPlan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readPlan.d.ts","sourceRoot":"","sources":["../../../src/internal/readPlan.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC1D,OAAO,EAAiC,KAAK,KAAK,EAAE,MAAM,iBAAiB,CAAA;AAC3E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAErD,OAAO,EAAE,KAAK,QAAQ,EAA+B,MAAM,aAAa,CAAA;AAExE,2EAA2E;AAC3E,MAAM,WAAW,UAAU;IACzB,oCAAoC;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,qCAAqC;IACrC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;CACrB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,CACb,KAAK,EAAE,QAAQ,EACf,KAAK,EAAE,SAAS,GAAG,IAAI,KACpB,aAAa,CAAC,OAAO,CAAC,CAAA;CAC5B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB,GAC3B,QAAQ,UAAU,EAClB,OAAO,KAAK,KACX,SAiCF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,QAAQ,GACnB,KAAK,SAAS,CAAC,SAAS,EACxB,MAAM,SAAS,EACf,OAAO,QAAQ,GAAG,SAAS,EAC3B,OAAO,SAAS,GAAG,SAAS,KAC3B,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC,QAAQ,CACiB,CAAA"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The JSON/JSONB transport for the Postgres engine: the wire shapes and the
|
|
3
|
+
* (de)serialisers that carry an event batch / query items ACROSS the boundary into
|
|
4
|
+
* the append functions, and map a raw driver row BACK to a core `SequencedEvent`.
|
|
5
|
+
* Extracted from `store.ts` (the write side) and `internal/readPlan.ts` (the read
|
|
6
|
+
* side) so the transport concept lives in ONE module rather than being split
|
|
7
|
+
* across the write path and the read-plan compiler — the module name now predicts
|
|
8
|
+
* where transport code lives, and the two per-column type maps (`EventJson`,
|
|
9
|
+
* `EventRow`) sit next to each other rather than in two files.
|
|
10
|
+
*
|
|
11
|
+
* The event column contract stays single-sourced on `EventColumnName`
|
|
12
|
+
* (`internal/matchSql.ts`), the one column-NAME descriptor: both type maps here
|
|
13
|
+
* are mapped types over it, so a column rename in `EVENT_COLUMNS` is a COMPILE
|
|
14
|
+
* error in this module, never a silent transport drift the lax read boundary
|
|
15
|
+
* (ADR-0006) would only surface as corrupt data under test.
|
|
16
|
+
*
|
|
17
|
+
* The read boundary (`rowToSequenced`) is the trusted, deliberately-lax
|
|
18
|
+
* deserialisation point (ADR-0006 store-laxness); the write boundary
|
|
19
|
+
* (`toEventJson`) serialises `data` as base64 text and domain time as an ISO
|
|
20
|
+
* string, both restored in SQL.
|
|
21
|
+
*/
|
|
22
|
+
import type { DcbEvent, Metadata, Query, SequencedEvent } from '@kairos-es/core';
|
|
23
|
+
import type { EventColumnName } from './matchSql.js';
|
|
24
|
+
/**
|
|
25
|
+
* Serialise a value to a JSON STRING for transport as a `::jsonb`-cast bound
|
|
26
|
+
* parameter.
|
|
27
|
+
*
|
|
28
|
+
* WHY a string and not `sql.json(value)`: the read plan runs through
|
|
29
|
+
* `sql.unsafe(text, params)` with POSITIONAL parameters, and a positional
|
|
30
|
+
* parameter must be a plain scalar or a flat array of scalars — `@effect/sql-pg`
|
|
31
|
+
* cannot infer a Postgres type for an object or an array of objects, and a
|
|
32
|
+
* `sql.json` fragment is not a value that list can carry. A pre-stringified
|
|
33
|
+
* JSON string binds untyped and the `::jsonb` cast has Postgres parse it, which
|
|
34
|
+
* round-trips reliably for both objects and arrays on the positional path and
|
|
35
|
+
* the tagged-template path alike. Kept as a named helper so every
|
|
36
|
+
* JSON-transport call site is consistent and the reasoning lives in one place.
|
|
37
|
+
*/
|
|
38
|
+
export declare const jsonbText: (value: unknown) => string;
|
|
39
|
+
/** The JSON shape of one query item: types OR-filter, tags AND-superset. */
|
|
40
|
+
export interface QueryItemJson {
|
|
41
|
+
readonly types: ReadonlyArray<string>;
|
|
42
|
+
readonly tags: ReadonlyArray<string>;
|
|
43
|
+
}
|
|
44
|
+
/** Serialise a `Query`'s items into their transport JSON shape. */
|
|
45
|
+
export declare const toQueryItemsJson: (query: Query) => ReadonlyArray<QueryItemJson>;
|
|
46
|
+
/**
|
|
47
|
+
* The per-column TS types of the transport JSON shape — one entry per
|
|
48
|
+
* `EVENT_COLUMNS` name. `data` is base64 TEXT and `occurred_at` an ISO string
|
|
49
|
+
* (both restored in SQL: `decode(…, 'base64')` / `timestamptz`); `metadata`
|
|
50
|
+
* travels as a nested JSON object, which `jsonb_to_recordset` hands to the
|
|
51
|
+
* insert as `jsonb` untouched; the rest map straight across. Kept as a plain
|
|
52
|
+
* map so `EventJson` below is a mapped type over the ONE column descriptor
|
|
53
|
+
* rather than a hand-restated column list.
|
|
54
|
+
*/
|
|
55
|
+
interface EventJsonColumns {
|
|
56
|
+
readonly type: string;
|
|
57
|
+
readonly data: string;
|
|
58
|
+
readonly tags: ReadonlyArray<string>;
|
|
59
|
+
readonly uuid: string;
|
|
60
|
+
readonly occurred_at: string;
|
|
61
|
+
readonly metadata: Metadata;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The JSON shape of one event as sent to the append functions, as a mapped type
|
|
65
|
+
* over the shared `EventColumnName` descriptor (`internal/matchSql.ts`). `data`
|
|
66
|
+
* is base64 text (restored to `bytea` by `decode(…, 'base64')` in plpgsql);
|
|
67
|
+
* `occurred_at` is an ISO string parsed by `timestamptz`. Because the keys come
|
|
68
|
+
* from the same descriptor that builds the `jsonb_to_recordset(new_events)`
|
|
69
|
+
* column list, the two cannot drift — rename a column in `EVENT_COLUMNS` and this
|
|
70
|
+
* mapped type stops compiling (the old key is gone from `EventJsonColumns`).
|
|
71
|
+
*/
|
|
72
|
+
export type EventJson = {
|
|
73
|
+
readonly [K in EventColumnName]: EventJsonColumns[K];
|
|
74
|
+
};
|
|
75
|
+
/** Serialise one core `DcbEvent` into its transport JSON shape. */
|
|
76
|
+
export declare const toEventJson: (event: DcbEvent) => EventJson;
|
|
77
|
+
/**
|
|
78
|
+
* The per-column TS types of a raw driver row — one entry per `EVENT_COLUMNS`
|
|
79
|
+
* name. These are the JS types `@effect/sql-pg`'s default binary codecs decode
|
|
80
|
+
* to: `data` a `Uint8Array` (or null for a payload-less event), `occurred_at` a
|
|
81
|
+
* JS `Date`, `tags` a `string[]`, `metadata` whatever the driver parses a
|
|
82
|
+
* `jsonb` value into — declared
|
|
83
|
+
* `unknown`, because `jsonb` is the one column here whose SQL type does not
|
|
84
|
+
* fix its JS shape, and `metadataFrom` below is where it is checked. `id`
|
|
85
|
+
* (the `bigserial` position) is NOT here — it is prepended by the read projection
|
|
86
|
+
* and absent from the events recordset, so it sits outside the shared descriptor.
|
|
87
|
+
*/
|
|
88
|
+
interface EventRowColumns {
|
|
89
|
+
readonly type: string;
|
|
90
|
+
readonly data: Uint8Array | null;
|
|
91
|
+
readonly tags: ReadonlyArray<string>;
|
|
92
|
+
readonly uuid: string;
|
|
93
|
+
readonly occurred_at: Date;
|
|
94
|
+
readonly metadata: unknown;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A raw row from the main table as the driver returns it, as `id` plus a
|
|
98
|
+
* mapped type over the shared `EventColumnName` descriptor. `id` is `int8`, which
|
|
99
|
+
* the driver's default codec hands back as a `bigint` (JS `number` is unsafe past
|
|
100
|
+
* 2^53). `rowToSequenced` still passes it through `BigInt(…)`, so a client
|
|
101
|
+
* configured with its own `int8` codec that yields the decimal string instead
|
|
102
|
+
* still mints a real `bigint` position.
|
|
103
|
+
* The payload columns cannot drift from the read projection: a rename in
|
|
104
|
+
* `EVENT_COLUMNS` breaks this mapped type.
|
|
105
|
+
*/
|
|
106
|
+
export type EventRow = {
|
|
107
|
+
readonly id: bigint | string;
|
|
108
|
+
} & {
|
|
109
|
+
readonly [K in EventColumnName]: EventRowColumns[K];
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Map a raw main-table row to a core `SequencedEvent`. `data` NULL → empty bytes
|
|
113
|
+
* (a payload-less event round-trips to a zero-length `Uint8Array`); `occurred_at`
|
|
114
|
+
* Date → `DateTime.Utc`; `id` → branded `Position`; `metadata` through
|
|
115
|
+
* `metadataFrom`. The column is `NOT NULL DEFAULT '{}'`, so there is no absent
|
|
116
|
+
* case to reconcile: an event appended with an empty bag reads back as `{}` on
|
|
117
|
+
* this engine exactly as on the in-memory oracle.
|
|
118
|
+
*
|
|
119
|
+
* The branded scalars (`type`, `tags`, `uuid`, `position`) are minted by trusted
|
|
120
|
+
* `as`-cast, NOT by re-running `EventType.make` / `Tag.make` etc. This is
|
|
121
|
+
* deliberate: these bytes were validated at write time, and the store schema is
|
|
122
|
+
* intentionally LAX on read (ADR-0006 store-laxness — hand-assembled or pre-ADR
|
|
123
|
+
* events must remain readable), so re-validating here could REJECT a legitimately
|
|
124
|
+
* stored value and break that contract. It mirrors the in-memory oracle, which
|
|
125
|
+
* likewise mints branded positions by cast at its trusted allocation boundary
|
|
126
|
+
* (`inMemory.ts`). The read boundary is the analogous trusted deserialisation
|
|
127
|
+
* point.
|
|
128
|
+
*/
|
|
129
|
+
export declare const rowToSequenced: (row: EventRow) => SequencedEvent;
|
|
130
|
+
export {};
|
|
131
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../../src/internal/transport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EAER,KAAK,EACL,cAAc,EACf,MAAM,iBAAiB,CAAA;AAGxB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEjD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,OAAO,KAAG,MAA+B,CAAA;AAE1E,4EAA4E;AAC5E,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;IACrC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;CACrC;AAED,mEAAmE;AACnE,eAAO,MAAM,gBAAgB,GAAI,OAAO,KAAK,KAAG,aAAa,CAAC,aAAa,CAItE,CAAA;AAEL;;;;;;;;GAQG;AACH,UAAU,gBAAgB;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAA;CAC5B;AAED;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG;IAAE,QAAQ,EAAE,CAAC,IAAI,eAAe,GAAG,gBAAgB,CAAC,CAAC,CAAC;CAAE,CAAA;AAEhF,mEAAmE;AACnE,eAAO,MAAM,WAAW,GAAI,OAAO,QAAQ,KAAG,SAW5C,CAAA;AAEF;;;;;;;;;;GAUG;AACH,UAAU,eAAe;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAAA;IAChC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAA;IAC1B,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;CAC3B;AAED;;;;;;;;;GASG;AACH,MAAM,MAAM,QAAQ,GAAG;IAAE,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG;IACxD,QAAQ,EAAE,CAAC,IAAI,eAAe,GAAG,eAAe,CAAC,CAAC,CAAC;CACpD,CAAA;AAuBD;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,cAAc,GAAI,KAAK,QAAQ,KAAG,cAU7C,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DcbEventStore } from '@kairos-es/core';
|
|
2
|
+
import { Layer } from 'effect';
|
|
3
|
+
import { PgClientDirect, PgClientPooled } from './clients.js';
|
|
4
|
+
import { type PostgresStoreConfig } from './config.js';
|
|
5
|
+
/**
|
|
6
|
+
* The Postgres `DcbEventStore` layer factory. Builds the engine over
|
|
7
|
+
* `PgClientPooled` (reads/append/notify) and `PgClientDirect` (the subscribe
|
|
8
|
+
* listener). The caller wires those two client layers — pointing `PgClientDirect`
|
|
9
|
+
* at a direct (non-pooler) endpoint on Neon, or at the same URL as `PgClientPooled`
|
|
10
|
+
* everywhere else.
|
|
11
|
+
*
|
|
12
|
+
* `ensureSchema(config)` must have been run (by the caller's migrator or at
|
|
13
|
+
* boot) before the layer is used; the layer itself performs no DDL, so it stays
|
|
14
|
+
* a pure runtime dependency requiring only the two clients.
|
|
15
|
+
*/
|
|
16
|
+
export declare const layer: (config?: PostgresStoreConfig) => Layer.Layer<DcbEventStore, never, PgClientPooled | PgClientDirect>;
|
|
17
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAiDA,OAAO,EAGL,aAAa,EAQd,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAA6B,KAAK,EAAsB,MAAM,QAAQ,CAAA;AAE7E,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAC1D,OAAO,EAA6B,KAAK,mBAAmB,EAAE,MAAM,UAAU,CAAA;AA2U9E;;;;;;;;;;GAUG;AACH,eAAO,MAAM,KAAK,GAChB,SAAQ,mBAAwB,KAC/B,KAAK,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,cAAc,GAAG,cAAc,CASnE,CAAA"}
|