@kairos-es/read-postgres 1.0.0-alpha.4 → 1.0.0-alpha.5
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 +35 -29
- package/dist/cjs/config.js +9 -13
- package/dist/cjs/config.js.map +1 -1
- package/dist/cjs/ensureCheckpointSchema.js +2 -2
- package/dist/cjs/ensureCheckpointSchema.js.map +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/internal/ddl.js +2 -2
- package/dist/cjs/projectionStore.js +62 -12
- package/dist/cjs/projectionStore.js.map +1 -1
- package/dist/{dts → dts-cjs}/config.d.ts +7 -11
- package/dist/{dts → dts-cjs}/config.d.ts.map +1 -1
- package/dist/{dts → dts-cjs}/ensureCheckpointSchema.d.ts +2 -2
- package/dist/{dts → dts-cjs}/ensureCheckpointSchema.d.ts.map +1 -1
- package/dist/{dts → dts-cjs}/index.d.ts +6 -4
- package/dist/{dts → dts-cjs}/index.d.ts.map +1 -1
- package/dist/{dts → dts-cjs}/internal/ddl.d.ts +4 -4
- package/dist/{dts → dts-cjs}/internal/ddl.d.ts.map +1 -1
- package/dist/dts-cjs/package.json +3 -0
- package/dist/dts-cjs/projectionStore.d.ts +77 -0
- package/dist/dts-cjs/projectionStore.d.ts.map +1 -0
- package/dist/dts-esm/config.d.ts +61 -0
- package/dist/dts-esm/config.d.ts.map +1 -0
- package/dist/dts-esm/ensureCheckpointSchema.d.ts +39 -0
- package/dist/dts-esm/ensureCheckpointSchema.d.ts.map +1 -0
- package/dist/dts-esm/index.d.ts +92 -0
- package/dist/dts-esm/index.d.ts.map +1 -0
- package/dist/dts-esm/internal/ddl.d.ts +146 -0
- package/dist/dts-esm/internal/ddl.d.ts.map +1 -0
- package/dist/dts-esm/internal/statements.d.ts +100 -0
- package/dist/dts-esm/internal/statements.d.ts.map +1 -0
- package/dist/dts-esm/package.json +3 -0
- package/dist/dts-esm/projectionStore.d.ts +77 -0
- package/dist/dts-esm/projectionStore.d.ts.map +1 -0
- package/dist/esm/config.js +9 -13
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/ensureCheckpointSchema.js +2 -2
- package/dist/esm/ensureCheckpointSchema.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/ddl.js +2 -2
- package/dist/esm/projectionStore.js +61 -12
- package/dist/esm/projectionStore.js.map +1 -1
- package/package.json +13 -9
- package/src/config.ts +11 -11
- package/src/ensureCheckpointSchema.ts +3 -2
- package/src/index.ts +6 -4
- package/src/internal/ddl.ts +4 -4
- package/src/projectionStore.ts +28 -20
- package/dist/dts/projectionStore.d.ts +0 -30
- package/dist/dts/projectionStore.d.ts.map +0 -1
- /package/dist/{dts → dts-cjs}/internal/statements.d.ts +0 -0
- /package/dist/{dts → dts-cjs}/internal/statements.d.ts.map +0 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public configuration for the Postgres `ProjectionStore`.
|
|
3
|
+
*
|
|
4
|
+
* A read-side store is defined entirely by its `{ schema, tablePrefix }` naming
|
|
5
|
+
* pair: the checkpoint table name is DERIVED from it (see `internal/ddl.ts`), so
|
|
6
|
+
* one config value is one completely isolated checkpoint namespace, cohabiting
|
|
7
|
+
* per prefix exactly as the event store's objects do (ADR-0002).
|
|
8
|
+
*
|
|
9
|
+
* It is a `Schema`, not a bare interface, so malformed input fails LOUDLY at
|
|
10
|
+
* construction (`makeSqlProjectionStore`/`ensureCheckpointSchema` decode through
|
|
11
|
+
* it) rather than late and obscurely on the first commit — the same
|
|
12
|
+
* fail-at-the-boundary discipline core applies to `Tag`/`ReadLimit`. It rejects an
|
|
13
|
+
* empty or dotted `schema`/`tablePrefix` atom (a dot would break the
|
|
14
|
+
* qualified-name dot-split) and — via `oversizedIdentifier` — any pair whose
|
|
15
|
+
* derived table name would exceed Postgres's 63-byte identifier limit and be
|
|
16
|
+
* silently truncated.
|
|
17
|
+
*
|
|
18
|
+
* **WHY this config is DUPLICATED rather than imported from
|
|
19
|
+
* `@kairos-es/store-postgres`.** Importing it would mean peering that package,
|
|
20
|
+
* and peering an event-store BACKEND from the read side is precisely what the
|
|
21
|
+
* per-materialisation view-store design exists to avoid: a durable view must be
|
|
22
|
+
* free to sit over any log satisfying `DcbEventStore` — the in-memory oracle,
|
|
23
|
+
* Postgres, a future backend — so this package depends on the PORT
|
|
24
|
+
* (`@kairos-es/read`) and on `effect/sql`, never on a sibling backend. The
|
|
25
|
+
* duplication also keeps the two guards honestly independent: each package
|
|
26
|
+
* measures only the identifiers IT materialises, so neither inherits a byte budget
|
|
27
|
+
* from names it never creates.
|
|
28
|
+
*
|
|
29
|
+
* The shape is intentionally the event store's MINUS everything read-side wiring
|
|
30
|
+
* has no business choosing: no `lockTimeout` (the checkpoint's guarded
|
|
31
|
+
* compare-and-set takes only ordinary row locks, and a blocked writer is a
|
|
32
|
+
* competing runner whose loss is a `CheckpointSuperseded` the supervisor already
|
|
33
|
+
* handles), no `pollInterval`/`catchUpPageSize` (those belong to `subscribe`, on
|
|
34
|
+
* the log side). What remains is naming, and only naming.
|
|
35
|
+
*/
|
|
36
|
+
import { Schema } from 'effect';
|
|
37
|
+
/**
|
|
38
|
+
* Configuration for a read-side checkpoint store.
|
|
39
|
+
*
|
|
40
|
+
* - `schema` — the Postgres schema that owns the checkpoint table (default
|
|
41
|
+
* `public`). `ensureCheckpointSchema` creates it defensively.
|
|
42
|
+
* - `tablePrefix` — the base name the checkpoint table derives from (default
|
|
43
|
+
* `dcb_events`, giving `dcb_events_checkpoints`). Pass the SAME pair the event
|
|
44
|
+
* store is configured with and the checkpoint table lands beside that store's
|
|
45
|
+
* objects, one isolated store per prefix.
|
|
46
|
+
*/
|
|
47
|
+
export declare const ReadPostgresConfig: Schema.Struct<{
|
|
48
|
+
readonly schema: Schema.optionalKey<Schema.String>;
|
|
49
|
+
readonly tablePrefix: Schema.optionalKey<Schema.String>;
|
|
50
|
+
}>;
|
|
51
|
+
export type ReadPostgresConfig = typeof ReadPostgresConfig.Type;
|
|
52
|
+
/**
|
|
53
|
+
* Decode an unknown value into a validated `ReadPostgresConfig`, throwing a
|
|
54
|
+
* `SchemaError` on any violation. Used by `makeSqlProjectionStore` and
|
|
55
|
+
* `ensureCheckpointSchema` so a bad config fails at construction time.
|
|
56
|
+
*/
|
|
57
|
+
export declare const decodeReadPostgresConfig: (input: unknown, options?: import("effect/SchemaAST").ParseOptions) => {
|
|
58
|
+
readonly schema?: string;
|
|
59
|
+
readonly tablePrefix?: string;
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAiB/B;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB;;;EAW9B,CAAA;AACD,MAAM,MAAM,kBAAkB,GAAG,OAAO,kBAAkB,CAAC,IAAI,CAAA;AAE/D;;;;GAIG;AACH,eAAO,MAAM,wBAAwB;;;CACS,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ensureCheckpointSchema` — the idempotent checkpoint DDL, as a building block
|
|
3
|
+
* rather than a migrator (ADR-0007, mirroring the event store's `ensureSchema`).
|
|
4
|
+
*
|
|
5
|
+
* It is a single `Effect<void, SqlError, SqlClient>` that creates (or refreshes)
|
|
6
|
+
* the one object this package owns: the checkpoint table. It requires the GENERIC
|
|
7
|
+
* `SqlClient` service — not a package-owned client service — precisely so a consumer can
|
|
8
|
+
* drop it straight into THEIR OWN migrator, e.g.:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* Migrator.fromRecord({
|
|
12
|
+
* '0002_kairos_checkpoints': ensureCheckpointSchema(config),
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* or simply run it once at boot, beside `ensureSchema` from the event-store
|
|
17
|
+
* backend. We ship no `FileSystem`/`Path`/`ChildProcess` dependency and no
|
|
18
|
+
* migrator: migration ORCHESTRATION belongs to the consumer, this package only
|
|
19
|
+
* provides the idempotent PAYLOAD. That is also why schema ownership never
|
|
20
|
+
* silently moves — a read model cannot conjure its own table at runtime.
|
|
21
|
+
*
|
|
22
|
+
* Idempotence: every statement is `CREATE … IF NOT EXISTS`, so re-running against
|
|
23
|
+
* an existing checkpoint table is a no-op. Names come pre-quoted from
|
|
24
|
+
* `internal/ddl.ts`; the DDL carries no bound values (pure schema).
|
|
25
|
+
*
|
|
26
|
+
* Unlike the runtime store, this path needs no `withoutTransforms()` guard: it
|
|
27
|
+
* reads no rows and passes no identifier through `sql(identifier)`, so a
|
|
28
|
+
* consumer's name transforms have nothing here to rewrite.
|
|
29
|
+
*/
|
|
30
|
+
import { Effect } from 'effect';
|
|
31
|
+
import { SqlClient, type SqlError } from 'effect/sql';
|
|
32
|
+
import { type ReadPostgresConfig } from './config.js';
|
|
33
|
+
/**
|
|
34
|
+
* Produce the checkpoint migration payload for `config`. Resolves the table name
|
|
35
|
+
* once and runs the ordered idempotent DDL against whatever `SqlClient` the
|
|
36
|
+
* caller's migrator (or boot code) provides.
|
|
37
|
+
*/
|
|
38
|
+
export declare const ensureCheckpointSchema: (config: ReadPostgresConfig) => Effect.Effect<void, SqlError.SqlError, SqlClient.SqlClient>;
|
|
39
|
+
//# sourceMappingURL=ensureCheckpointSchema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ensureCheckpointSchema.d.ts","sourceRoot":"","sources":["../../src/ensureCheckpointSchema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAE,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,EAA4B,KAAK,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAG5E;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GACjC,QAAQ,kBAAkB,KACzB,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC,SAAS,CASzD,CAAA"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kairos-es/read-postgres — the SQL `ProjectionStore`: a durable checkpoint
|
|
3
|
+
* table plus the guarded compare-and-set that brackets a read model's own view
|
|
4
|
+
* writes in one ordinary transaction (ADR-0007).
|
|
5
|
+
*
|
|
6
|
+
* Needs only the GENERIC `SqlClient` from `effect/sql`, deliberately not
|
|
7
|
+
* `@effect/sql-pg` and deliberately no reserved connection: the event store owns
|
|
8
|
+
* the LISTEN, so the read side needs no dedicated connection of its own, and
|
|
9
|
+
* requiring the generic client is what lets `ensureCheckpointSchema` drop into a
|
|
10
|
+
* consumer's existing migrator instead of shipping a migrator here.
|
|
11
|
+
*
|
|
12
|
+
* A store is one `{ schema, tablePrefix }` naming pair (`ReadPostgresConfig`) —
|
|
13
|
+
* the same pair the event store is configured with — so the checkpoint table
|
|
14
|
+
* cohabits PER PREFIX and ADR-0002's "each prefix is a complete, isolated store"
|
|
15
|
+
* keeps holding across the read side.
|
|
16
|
+
*
|
|
17
|
+
* Wiring, end to end:
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* import {
|
|
21
|
+
* checkpointKey,
|
|
22
|
+
* DurableEventLog,
|
|
23
|
+
* forKey,
|
|
24
|
+
* ProjectionId,
|
|
25
|
+
* projectionLayer,
|
|
26
|
+
* } from '@kairos-es/read'
|
|
27
|
+
* import {
|
|
28
|
+
* ensureCheckpointSchema,
|
|
29
|
+
* makeSqlProjectionStore,
|
|
30
|
+
* } from '@kairos-es/read-postgres'
|
|
31
|
+
*
|
|
32
|
+
* const config = { schema: 'public', tablePrefix: 'dcb_events' }
|
|
33
|
+
*
|
|
34
|
+
* // 1. Register `ensureCheckpointSchema(config)` in YOUR migrator (or run it at
|
|
35
|
+
* // boot), beside the event-store backend's own `ensureSchema(config)`.
|
|
36
|
+
* // 2. Build the store where the read model is wired, so `commit` closes over the
|
|
37
|
+
* // acquired client and the read model itself stays free of `SqlClient`.
|
|
38
|
+
* const courseView = Layer.unwrapEffect(
|
|
39
|
+
* Effect.gen(function* () {
|
|
40
|
+
* const store = yield* makeSqlProjectionStore(config)
|
|
41
|
+
* return projectionLayer({
|
|
42
|
+
* // One store, many read models: `forKey` binds it to THIS one's checkpoint,
|
|
43
|
+
* // and from there no operation takes a key to get wrong.
|
|
44
|
+
* store: forKey(store, checkpointKey(ProjectionId.make('course-subs'))),
|
|
45
|
+
* slices,
|
|
46
|
+
* apply,
|
|
47
|
+
* })
|
|
48
|
+
* }),
|
|
49
|
+
* )
|
|
50
|
+
*
|
|
51
|
+
* // 3. Declare what the LOG is worth ONCE, beside the layer that chose it —
|
|
52
|
+
* // `DcbEventStore` exposes no durability, so the application is the only
|
|
53
|
+
* // honest source, and R2 (a durable view needs a durable log) is checked
|
|
54
|
+
* // against this. It is a required input of every projection layer, and one
|
|
55
|
+
* // declaration satisfies every one of them.
|
|
56
|
+
* const main = Layer.provide(
|
|
57
|
+
* Layer.mergeAll(courseView, studentView),
|
|
58
|
+
* Layer.mergeAll(eventStoreLayer, DurableEventLog, SerializerDefault),
|
|
59
|
+
* )
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* The resulting layer requires the `SqlClient` the checkpoint (and typically the
|
|
63
|
+
* view) is written through, the `EventLogDurability` declaration above, plus
|
|
64
|
+
* whatever the read model's own `apply` needs. The checkpoint table is assumed to
|
|
65
|
+
* already exist, exactly as the event store's `layer` assumes its objects do: this
|
|
66
|
+
* package performs no DDL at runtime.
|
|
67
|
+
*
|
|
68
|
+
* `commit` is `sql.withTransaction(viewWrites *> guardedAdvance)`. The author's own
|
|
69
|
+
* view writes join that transaction only when issued through the SAME `SqlClient`
|
|
70
|
+
* the store was built from; a write through any other `SqlClient` instance is NOT
|
|
71
|
+
* bracketed and is not rolled back on a lost guard. `withTransaction` is
|
|
72
|
+
* savepoint-aware, so a projection already inside a
|
|
73
|
+
* transaction composes without a second physical one. A guarded advance touching
|
|
74
|
+
* zero rows raises `CheckpointSuperseded`, whose FAILURE aborts the transaction and
|
|
75
|
+
* rolls the view writes back with it — the abort that makes the projection
|
|
76
|
+
* exactly-once. An infrastructure fault surfaces as `ProjectionStoreError` on the
|
|
77
|
+
* ERROR CHANNEL rather than as a defect, inverting the event store's rule
|
|
78
|
+
* (ADR-0002) because the read side has a supervisor above it that must distinguish
|
|
79
|
+
* retry-with-backoff from give-up.
|
|
80
|
+
*
|
|
81
|
+
* The config and the identifier guards are duplicated here rather than imported
|
|
82
|
+
* from `@kairos-es/store-postgres`, and that duplication is deliberate: peering
|
|
83
|
+
* the event-store backend would couple the read side to one specific event-store
|
|
84
|
+
* implementation, which is precisely what per-materialisation view-store selection
|
|
85
|
+
* exists to avoid. A durable view must be free to sit over any log satisfying
|
|
86
|
+
* `DcbEventStore`, so this package depends on the port (`@kairos-es/read`) and
|
|
87
|
+
* on `effect/sql`, never on a sibling backend.
|
|
88
|
+
*/
|
|
89
|
+
export type { ReadPostgresConfig } from './config.js';
|
|
90
|
+
export { ensureCheckpointSchema } from './ensureCheckpointSchema.js';
|
|
91
|
+
export { makeSqlProjectionStore } from './projectionStore.js';
|
|
92
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuFG;AACH,YAAY,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAClD,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAA;AACjE,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal object-naming and DDL for the Postgres `ProjectionStore`.
|
|
3
|
+
*
|
|
4
|
+
* This module is the single source of truth for the ONE object this package owns
|
|
5
|
+
* — the checkpoint table — and for the idempotent DDL that creates it. It is
|
|
6
|
+
* factored apart from the public surface for the same reason the event store's
|
|
7
|
+
* `internal/ddl.ts` is: the name is derived ONCE, from `{ schema, tablePrefix }`,
|
|
8
|
+
* and reused by both `ensureCheckpointSchema` (the migration payload) and the
|
|
9
|
+
* runtime statements in `projectionStore.ts`, so a migration and a query can
|
|
10
|
+
* never disagree about what the table is called.
|
|
11
|
+
*
|
|
12
|
+
* The checkpoint table derives from the SAME `{ schema, tablePrefix }` pair the
|
|
13
|
+
* event store uses, so a checkpoint table cohabits PER PREFIX: two prefixes are
|
|
14
|
+
* two complete, isolated stores (ADR-0002) with two independent checkpoint
|
|
15
|
+
* namespaces, and that isolation keeps holding once the read side is added rather
|
|
16
|
+
* than being punctured by one shared checkpoint table.
|
|
17
|
+
*
|
|
18
|
+
* WHY names are never string-concatenated into a query: every identifier is
|
|
19
|
+
* pre-quoted here (`quoteQualified` for the dotted table, `quoteAtom` for a bare
|
|
20
|
+
* atom) and embedded as literal text into a `sql.unsafe(text, params)` statement,
|
|
21
|
+
* exactly as the event store's read plans are; VALUES are always bound `$n`
|
|
22
|
+
* parameters and never interpolated. A hostile or merely awkward
|
|
23
|
+
* `schema`/`tablePrefix` therefore cannot inject SQL or collide with a reserved
|
|
24
|
+
* word.
|
|
25
|
+
*
|
|
26
|
+
* The runtime deliberately does NOT route these names through `sql(identifier)`.
|
|
27
|
+
* This package takes the CONSUMER's generic `SqlClient`, which may carry a
|
|
28
|
+
* query-name transform (`effect/sql` applies it to every `sql(identifier)`), so
|
|
29
|
+
* a camel-casing consumer would silently rewrite `last_position` to
|
|
30
|
+
* `lastPosition` and address a column that does not exist. Pre-quoted text inside
|
|
31
|
+
* `sql.unsafe` is transform-proof; the result side is handled by
|
|
32
|
+
* `withoutTransforms()` in `projectionStore.ts`.
|
|
33
|
+
*/
|
|
34
|
+
import type { SqlClient, Statement } from 'effect/sql';
|
|
35
|
+
/**
|
|
36
|
+
* The one object a read-side config owns. An interface rather than a bare string
|
|
37
|
+
* so the `{ schema, tablePrefix }` atoms stay available to error messages and so
|
|
38
|
+
* a second derived object (should one ever be needed) is an additive change here
|
|
39
|
+
* rather than a new parameter at every call site.
|
|
40
|
+
*/
|
|
41
|
+
export interface ResolvedCheckpointNames {
|
|
42
|
+
readonly schema: string;
|
|
43
|
+
readonly tablePrefix: string;
|
|
44
|
+
/** `<schema>.<prefix>_checkpoints` — the scalar checkpoint table. */
|
|
45
|
+
readonly checkpointTable: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Default schema when the config omits one. Identical to the event store's
|
|
49
|
+
* default, on purpose: the common wiring is one `{ schema, tablePrefix }` pair
|
|
50
|
+
* used for both sides, so both defaults must agree or a caller omitting the
|
|
51
|
+
* config would silently split the log and its checkpoints across two schemas.
|
|
52
|
+
*/
|
|
53
|
+
export declare const DEFAULT_SCHEMA = "public";
|
|
54
|
+
/** Default table prefix when the config omits one — again the event store's. */
|
|
55
|
+
export declare const DEFAULT_TABLE_PREFIX = "dcb_events";
|
|
56
|
+
/**
|
|
57
|
+
* The first derived identifier that exceeds 63 bytes, as a human-readable
|
|
58
|
+
* message, or `undefined` when every name fits. Shared by
|
|
59
|
+
* `resolveCheckpointNames` (which throws) and the `ReadPostgresConfig` schema
|
|
60
|
+
* filter (which fails validation), so the byte bound is enforced identically at
|
|
61
|
+
* both the name-derivation and the config-decode boundary.
|
|
62
|
+
*
|
|
63
|
+
* The bound is checked INDEPENDENTLY of the event store's: this package measures
|
|
64
|
+
* only the names IT materialises, so a prefix accepted there may be rejected here
|
|
65
|
+
* (and vice versa). Each package guards its own names, which is what lets the
|
|
66
|
+
* read side stay uncoupled from any particular event-store backend.
|
|
67
|
+
*/
|
|
68
|
+
export declare const oversizedIdentifier: (config: {
|
|
69
|
+
readonly schema?: string;
|
|
70
|
+
readonly tablePrefix?: string;
|
|
71
|
+
}) => string | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Derive the checkpoint table name from `{ schema, tablePrefix }`. The table
|
|
74
|
+
* carries its schema explicitly so `quoteQualified` dot-splits it into the two
|
|
75
|
+
* atoms Postgres expects.
|
|
76
|
+
*
|
|
77
|
+
* Throws at construction if a derived identifier would be silently truncated (see
|
|
78
|
+
* `oversizedIdentifier`) — a loud failure at the single choke point beats an
|
|
79
|
+
* invisible cross-prefix checkpoint collision later.
|
|
80
|
+
*/
|
|
81
|
+
export declare const resolveCheckpointNames: (config: {
|
|
82
|
+
readonly schema?: string;
|
|
83
|
+
readonly tablePrefix?: string;
|
|
84
|
+
}) => ResolvedCheckpointNames;
|
|
85
|
+
/**
|
|
86
|
+
* Quote a possibly-qualified identifier for embedding as literal SQL text.
|
|
87
|
+
* Splits on `.` and double-quotes each atom, mirroring what `sql(identifier)`
|
|
88
|
+
* emits, so a pre-quoted name and an `sql(identifier)` reference resolve to
|
|
89
|
+
* exactly the same object. Any embedded `"` is doubled per SQL
|
|
90
|
+
* identifier-quoting rules. (The dot-split is why `ReadPostgresConfig` bars a dot
|
|
91
|
+
* inside either atom: an embedded dot would silently re-partition the name into
|
|
92
|
+
* the wrong schema.)
|
|
93
|
+
*/
|
|
94
|
+
export declare const quoteQualified: (name: string) => string;
|
|
95
|
+
/**
|
|
96
|
+
* The checkpoint table's columns, PRE-QUOTED, as one shared descriptor.
|
|
97
|
+
*
|
|
98
|
+
* Shared by the DDL here and by every runtime statement in `projectionStore.ts`,
|
|
99
|
+
* so a column rename cannot half-land: the table it creates and the statements
|
|
100
|
+
* that address it read the same constant. Pre-quoted because `partition` is
|
|
101
|
+
* Postgres's own partitioning vocabulary — it parses unquoted today, but quoting
|
|
102
|
+
* removes the question, and quoting all three keeps the emitted SQL uniform.
|
|
103
|
+
*
|
|
104
|
+
* These names are FIXED and owned by this package (never config-derived), which
|
|
105
|
+
* is why they are plain constants rather than something the caller can influence.
|
|
106
|
+
*/
|
|
107
|
+
export declare const CHECKPOINT_COLUMNS: {
|
|
108
|
+
/** The read model's `ProjectionId`. */
|
|
109
|
+
readonly projection: string;
|
|
110
|
+
/** The read model's `PartitionId` (`default` while sharding is cut). */
|
|
111
|
+
readonly partition: string;
|
|
112
|
+
/**
|
|
113
|
+
* The scalar checkpoint: `bigint`, because a `Position` is 64-bit and exceeds
|
|
114
|
+
* JS `number` safety at 2^53. The driver reads it back as a `bigint`, and it
|
|
115
|
+
* is never coerced through `number`.
|
|
116
|
+
*/
|
|
117
|
+
readonly lastPosition: string;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* The ordered, idempotent DDL that creates (or refreshes) the checkpoint table.
|
|
121
|
+
* Each entry is a `sql.unsafe` statement string; no VALUES are involved (pure
|
|
122
|
+
* DDL), and every identifier is pre-quoted to match the runtime's resolution.
|
|
123
|
+
*
|
|
124
|
+
* Notes on the shape, both load-bearing:
|
|
125
|
+
*
|
|
126
|
+
* - `PRIMARY KEY (projection, partition)` is what makes `readCheckpoint` an index
|
|
127
|
+
* lookup AND supplies the arbiter index the first-commit upsert's
|
|
128
|
+
* `ON CONFLICT (projection, partition)` names. Without it that upsert would not
|
|
129
|
+
* compile, so the key is not merely hygiene.
|
|
130
|
+
* - `last_position bigint NOT NULL` with NO row for a never-committed key: a key
|
|
131
|
+
* reads back as `ORIGIN` by ABSENCE, so "never committed" and "reset" are one
|
|
132
|
+
* state (`resetCheckpoint` deletes the row rather than zeroing it), exactly as
|
|
133
|
+
* the in-memory store's map deletes its entry.
|
|
134
|
+
* - No autovacuum storage parameters, unlike the event store's tables. Those are
|
|
135
|
+
* tuned for an append-heavy, effectively-immutable log; this table is a handful
|
|
136
|
+
* of hot-UPDATEd rows, where the stock thresholds already fire aggressively.
|
|
137
|
+
*/
|
|
138
|
+
export declare const checkpointDdlStatements: (names: ResolvedCheckpointNames) => ReadonlyArray<string>;
|
|
139
|
+
/**
|
|
140
|
+
* Run the ordered DDL against the generic `SqlClient`. Kept here (not in the
|
|
141
|
+
* public `ensureCheckpointSchema`) so the same builder feeds both the public
|
|
142
|
+
* migration payload and any internal test setup. Each statement is a separate
|
|
143
|
+
* `sql.unsafe` call so a driver that rejects multi-statement strings still works.
|
|
144
|
+
*/
|
|
145
|
+
export declare const runCheckpointDdl: (sql: SqlClient.SqlClient, names: ResolvedCheckpointNames) => ReadonlyArray<Statement.Statement<unknown>>;
|
|
146
|
+
//# sourceMappingURL=ddl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ddl.d.ts","sourceRoot":"","sources":["../../../src/internal/ddl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAEtD;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,qEAAqE;IACrE,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAA;CACjC;AAED;;;;;GAKG;AACH,eAAO,MAAM,cAAc,WAAW,CAAA;AACtC,gFAAgF;AAChF,eAAO,MAAM,oBAAoB,eAAe,CAAA;AA0ChD;;;;;;;;;;;GAWG;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;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,GAAI,QAAQ;IAC7C,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAC9B,KAAG,uBAYH,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,KAAG,MAIhC,CAAA;AAQd;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,kBAAkB;IAC7B,uCAAuC;;IAEvC,wEAAwE;;IAExE;;;;OAIG;;CAEK,CAAA;AAEV;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,uBAAuB,GAClC,OAAO,uBAAuB,KAC7B,aAAa,CAAC,MAAM,CAiBtB,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAC3B,KAAK,SAAS,CAAC,SAAS,EACxB,OAAO,uBAAuB,KAC7B,aAAa,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,CAC6B,CAAA"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The four runtime statement texts the Postgres `ProjectionStore` issues, and the
|
|
3
|
+
* TWO parameter lists that feed them.
|
|
4
|
+
*
|
|
5
|
+
* The texts and their binding live in ONE module deliberately. `$3` and `$4` of
|
|
6
|
+
* the guarded advance are `next` and `expected` — both a `Position`, so both a
|
|
7
|
+
* `bigint` — and the whole exactly-once property of the read side rests on which
|
|
8
|
+
* of them the `SET` writes and which the `WHERE` compares. While the texts sat
|
|
9
|
+
* beside the store's Effect wiring and the binding sat inside its closure, that
|
|
10
|
+
* correspondence was asserted in prose in one place and implemented 180 lines away
|
|
11
|
+
* in another, and prose is exactly what drifted: a comment claimed a `bind` helper
|
|
12
|
+
* made a transposed argument list inexpressible, while the call site spread two
|
|
13
|
+
* interchangeable `Position`s positionally. Here the claim is checkable by reading
|
|
14
|
+
* one file, and `test/guardParameters.test.ts` pins it without a database.
|
|
15
|
+
*
|
|
16
|
+
* Everything else follows `internal/ddl.ts`: identifiers are pre-quoted literal
|
|
17
|
+
* text (`quoteQualified` / `CHECKPOINT_COLUMNS`) embedded into `sql.unsafe`, and
|
|
18
|
+
* every VALUE is a bound `$n` parameter, never interpolated.
|
|
19
|
+
*/
|
|
20
|
+
import type { Position } from '@kairos-es/core';
|
|
21
|
+
import type { CheckpointKey } from '@kairos-es/read';
|
|
22
|
+
import { type ResolvedCheckpointNames } from './ddl.js';
|
|
23
|
+
/**
|
|
24
|
+
* The four statement texts a store issues, compiled ONCE per store from the
|
|
25
|
+
* resolved names — the same compile-once discipline as the event store's read
|
|
26
|
+
* plans, so a long-lived projection re-binds only its parameters rather than
|
|
27
|
+
* re-assembling SQL per batch.
|
|
28
|
+
*
|
|
29
|
+
* All four take their parameters in ONE positional order — `$1` projection,
|
|
30
|
+
* `$2` partition, `$3` next, `$4` expected — and no consumer writes that order
|
|
31
|
+
* out: `keyParams` binds the two the `select` and the `reset` take, and
|
|
32
|
+
* `advanceParams` binds all four for the two guard shapes, reading each from a
|
|
33
|
+
* NAMED field. That naming is the point rather than a nicety, because the type
|
|
34
|
+
* system cannot help here: `next` and `expected` are the same type, so a
|
|
35
|
+
* positional list makes `[..., expected, next]` typecheck, and a transposed guard
|
|
36
|
+
* would compare the stored cursor against the position it was about to write —
|
|
37
|
+
* inverting the compare-and-set that makes a projection exactly-once. Reading the
|
|
38
|
+
* fields by name does not make the mistake impossible, but it makes it
|
|
39
|
+
* unwritable without naming the wrong field, which review and a diff can see.
|
|
40
|
+
*/
|
|
41
|
+
export interface CheckpointStatements {
|
|
42
|
+
readonly select: string;
|
|
43
|
+
readonly upsertFromOrigin: string;
|
|
44
|
+
readonly advance: string;
|
|
45
|
+
readonly reset: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Build the statement texts for a resolved name set.
|
|
49
|
+
*
|
|
50
|
+
* **The two guard shapes, and why one cannot serve both.** The guarded advance is
|
|
51
|
+
* a compare-and-set on `last_position`, and its correct SQL depends on whether the
|
|
52
|
+
* expected value is `ORIGIN`:
|
|
53
|
+
*
|
|
54
|
+
* - `expected === ORIGIN` needs `upsertFromOrigin`. A never-committed key has NO
|
|
55
|
+
* ROW (a key reads back as `ORIGIN` by absence, so "fresh" and "reset" are one
|
|
56
|
+
* state), so a plain `UPDATE` would match zero rows and report
|
|
57
|
+
* `CheckpointSuperseded` — a fresh read model could never make its FIRST commit.
|
|
58
|
+
* The `ON CONFLICT … DO UPDATE … WHERE chk.last_position = $4` arm keeps the
|
|
59
|
+
* upsert a real guard rather than a blind write: two racers both starting from
|
|
60
|
+
* `ORIGIN` cannot both win, because the second one's arm blocks on the first's
|
|
61
|
+
* uncommitted row, then re-reads it and finds a position that is no longer
|
|
62
|
+
* `ORIGIN`, matching zero rows. That arm also makes "row present at `0`"
|
|
63
|
+
* (an operator zeroing the column by hand instead of deleting the row) behave
|
|
64
|
+
* identically to "row absent", so the two ways of expressing a reset are
|
|
65
|
+
* indistinguishable to a first commit.
|
|
66
|
+
* - `expected > ORIGIN` needs `advance`, the plain `UPDATE`. The upsert would be
|
|
67
|
+
* WRONG here: `ON CONFLICT` guards only the branch where the row exists, so an
|
|
68
|
+
* absent row would be INSERTED unconditionally — a `resetCheckpoint` racing an
|
|
69
|
+
* in-flight commit would be silently undone, the key resurrected at `next`, and
|
|
70
|
+
* the entire backlog the reset existed to replay skipped with no error at all.
|
|
71
|
+
* The `UPDATE`'s zero rows for an absent row is the CORRECT outcome: somebody
|
|
72
|
+
* reset it, so this commit must lose and the runner must re-read.
|
|
73
|
+
*
|
|
74
|
+
* Both shapes `RETURNING` a column so the outcome is a ROW COUNT: zero rows means
|
|
75
|
+
* the guard lost. That mirrors the event store's append, where a conflict is the
|
|
76
|
+
* function's zero-row result and never a caught SQL error — no unique-violation
|
|
77
|
+
* or serialisation-failure path to classify, so a lost guard can never arrive
|
|
78
|
+
* mistagged as an infrastructure fault.
|
|
79
|
+
*
|
|
80
|
+
* The `AS chk` alias exists so the `DO UPDATE` guard can reference the EXISTING
|
|
81
|
+
* row unambiguously: the insert target is schema-qualified, and without an alias
|
|
82
|
+
* the guard would need a three-part column reference.
|
|
83
|
+
*/
|
|
84
|
+
export declare const compileStatements: (names: ResolvedCheckpointNames) => CheckpointStatements;
|
|
85
|
+
/** The `$1`/`$2` key parameters the `select` and the `reset` take. */
|
|
86
|
+
export declare const keyParams: (key: CheckpointKey) => ReadonlyArray<unknown>;
|
|
87
|
+
/**
|
|
88
|
+
* The `$1`-`$4` parameters both guard shapes take, read from named fields.
|
|
89
|
+
*
|
|
90
|
+
* The argument is an object rather than three positional parameters for the reason
|
|
91
|
+
* `CheckpointStatements` gives: `next` and `expected` are interchangeable to the
|
|
92
|
+
* type checker and catastrophic to swap, so the ONE place their order is written
|
|
93
|
+
* out reads each of them by name.
|
|
94
|
+
*/
|
|
95
|
+
export declare const advanceParams: (args: {
|
|
96
|
+
readonly key: CheckpointKey;
|
|
97
|
+
readonly expected: Position;
|
|
98
|
+
readonly next: Position;
|
|
99
|
+
}) => ReadonlyArray<unknown>;
|
|
100
|
+
//# sourceMappingURL=statements.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"statements.d.ts","sourceRoot":"","sources":["../../../src/internal/statements.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACpD,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,OAAO,CAAA;AAEd;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAA;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CACvB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,eAAO,MAAM,iBAAiB,GAC5B,OAAO,uBAAuB,KAC7B,oBA2BF,CAAA;AAED,sEAAsE;AACtE,eAAO,MAAM,SAAS,GAAI,KAAK,aAAa,KAAG,aAAa,CAAC,OAAO,CAGnE,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM;IAClC,QAAQ,CAAC,GAAG,EAAE,aAAa,CAAA;IAC3B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAA;IAC3B,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAA;CACxB,KAAG,aAAa,CAAC,OAAO,CAKxB,CAAA"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SQL `ProjectionStore`: a durable scalar checkpoint plus the guarded
|
|
3
|
+
* compare-and-set that brackets a read model's own view writes in ONE ordinary
|
|
4
|
+
* transaction (ADR-0007).
|
|
5
|
+
*
|
|
6
|
+
* This is the durable half of the parity pair. `@kairos-es/read`'s in-memory store
|
|
7
|
+
* is the oracle; this backend must produce identical OBSERVABLE behaviour, and the
|
|
8
|
+
* shared `projection-store-contract-tests` suite runs unchanged against both. The
|
|
9
|
+
* one place the two genuinely differ is HOW the abort is achieved:
|
|
10
|
+
*
|
|
11
|
+
* - in memory, the guard is checked FIRST and a loss means `viewWrites` never ran
|
|
12
|
+
* (abort by omission — there is nothing to undo);
|
|
13
|
+
* - here, `viewWrites` runs first and a lost guard FAILS, so the transaction rolls
|
|
14
|
+
* the view writes back with it (abort by undo).
|
|
15
|
+
*
|
|
16
|
+
* Both satisfy the port's promise that a lost guard leaves no observable view
|
|
17
|
+
* effect, which is why ordering inside the transaction is a performance choice
|
|
18
|
+
* rather than a correctness one: running the guard last keeps the checkpoint row
|
|
19
|
+
* lock held only for the advance instead of for the whole batch's worth of view
|
|
20
|
+
* writes.
|
|
21
|
+
*
|
|
22
|
+
* Two contract inversions worth naming, both deliberate:
|
|
23
|
+
*
|
|
24
|
+
* 1. A `SqlError` becomes a `ProjectionStoreError` on the ERROR CHANNEL — the
|
|
25
|
+
* opposite of the event store, where an infrastructure fault is a DEFECT
|
|
26
|
+
* (`Effect.die`, ADR-0002). The read side has a supervisor above it whose whole
|
|
27
|
+
* job is telling "retry the subscription with backoff" from "give up and report
|
|
28
|
+
* a stalled projection", and a defect carries no such distinction. On the event
|
|
29
|
+
* store there is no supervisor: a caller cannot meaningfully retry a broken
|
|
30
|
+
* connection, so dying is right there and wrong here.
|
|
31
|
+
* 2. A lost guard is a tagged failure (`CheckpointSuperseded`), never a returned
|
|
32
|
+
* outcome value, and that failure is exactly what aborts the transaction. A
|
|
33
|
+
* returned value would only roll back if the caller remembered to convert it
|
|
34
|
+
* into a failure, and forgetting is silent double-application.
|
|
35
|
+
*
|
|
36
|
+
* The store closes over the `SqlClient` it acquired, so the returned
|
|
37
|
+
* `ProjectionStore.commit` adds NO `SqlClient` to the caller's `R` — a read model
|
|
38
|
+
* stays free of the view store's requirements, which is what lets one runner drive
|
|
39
|
+
* read models over different backends.
|
|
40
|
+
*
|
|
41
|
+
* What this module owns is the Effect wiring: the client, the transaction bracket,
|
|
42
|
+
* the error classification. The SQL itself and the parameter lists that feed it are
|
|
43
|
+
* `internal/statements.ts`, which owns the argument for why the two live together.
|
|
44
|
+
*/
|
|
45
|
+
import { type ProjectionStore } from '@kairos-es/read';
|
|
46
|
+
import { Effect } from 'effect';
|
|
47
|
+
import { SqlClient } from 'effect/sql';
|
|
48
|
+
import { type ReadPostgresConfig } from './config.js';
|
|
49
|
+
/**
|
|
50
|
+
* Build a durable `ProjectionStore` over the ambient `SqlClient`.
|
|
51
|
+
*
|
|
52
|
+
* A factory `Effect` rather than a `Layer`, matching the in-memory store: the view
|
|
53
|
+
* store is selected PER MATERIALISATION, so it is a value handed to a read model, not
|
|
54
|
+
* a context service one store could claim process-wide. Per materialisation rather than
|
|
55
|
+
* per read model, because one read model may be materialised several times at once —
|
|
56
|
+
* a store from here beside an in-memory one, wired by `@kairos-es/read`'s
|
|
57
|
+
* `runProjections` — which is a shape a process-wide service could not express at all. It
|
|
58
|
+
* acquires the client ONCE and closes over it, which is what keeps `commit`'s `R`
|
|
59
|
+
* down to the caller's own.
|
|
60
|
+
*
|
|
61
|
+
* **The atomic bracket covers writes issued through THIS client.** The caller's
|
|
62
|
+
* `viewWrites` join the transaction only when they are issued through the same
|
|
63
|
+
* `SqlClient` the store was built from: `effect/sql` carries the transaction's
|
|
64
|
+
* connection in the Effect CONTEXT under a key that belongs to that one client
|
|
65
|
+
* instance, so a statement run inside `commit` finds the open transaction only if
|
|
66
|
+
* its client owns that key. The store's own `withoutTransforms()` copy shares the
|
|
67
|
+
* key, which is why the guarded advance and the caller's view writes land in one
|
|
68
|
+
* transaction; a write through any OTHER `SqlClient` instance — even one over the
|
|
69
|
+
* same database — or to a different database is NOT bracketed, is not rolled back
|
|
70
|
+
* on a lost guard, and the exactly-once property does not extend to it.
|
|
71
|
+
*
|
|
72
|
+
* `ensureCheckpointSchema(config)` must have run (in the caller's migrator or at
|
|
73
|
+
* boot) before the store is used; this factory performs no DDL, so it stays a pure
|
|
74
|
+
* runtime dependency on the client alone.
|
|
75
|
+
*/
|
|
76
|
+
export declare const makeSqlProjectionStore: (config?: ReadPostgresConfig) => Effect.Effect<ProjectionStore, never, SqlClient.SqlClient>;
|
|
77
|
+
//# sourceMappingURL=projectionStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"projectionStore.d.ts","sourceRoot":"","sources":["../../src/projectionStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAGH,OAAO,EAGL,KAAK,eAAe,EAErB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAA4B,KAAK,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAmF5E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,sBAAsB,GACjC,SAAQ,kBAAuB,KAC9B,MAAM,CAAC,MAAM,CAAC,eAAe,EAAE,KAAK,EAAE,SAAS,CAAC,SAAS,CA4IxD,CAAA"}
|
package/dist/esm/config.js
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
* per-materialisation view-store design exists to avoid: a durable view must be
|
|
22
22
|
* free to sit over any log satisfying `DcbEventStore` — the in-memory oracle,
|
|
23
23
|
* Postgres, a future backend — so this package depends on the PORT
|
|
24
|
-
* (`@kairos-es/read`) and on
|
|
24
|
+
* (`@kairos-es/read`) and on `effect/sql`, never on a sibling backend. The
|
|
25
25
|
* duplication also keeps the two guards honestly independent: each package
|
|
26
26
|
* measures only the identifiers IT materialises, so neither inherits a byte budget
|
|
27
27
|
* from names it never creates.
|
|
@@ -41,7 +41,7 @@ import { oversizedIdentifier } from "./internal/ddl.js";
|
|
|
41
41
|
* dot-split when quoted, so an embedded dot would silently re-partition the name
|
|
42
42
|
* into the wrong schema/object.
|
|
43
43
|
*/
|
|
44
|
-
const IdentifierAtom = /*#__PURE__*/Schema.String.
|
|
44
|
+
const IdentifierAtom = /*#__PURE__*/Schema.String.check(/*#__PURE__*/Schema.isNonEmpty(), /*#__PURE__*/Schema.isPattern(/^[^.]+$/, {
|
|
45
45
|
identifier: 'PostgresIdentifierAtom',
|
|
46
46
|
description: 'a non-empty identifier atom containing no "."'
|
|
47
47
|
}));
|
|
@@ -56,24 +56,20 @@ const IdentifierAtom = /*#__PURE__*/Schema.String.pipe(/*#__PURE__*/Schema.nonEm
|
|
|
56
56
|
* objects, one isolated store per prefix.
|
|
57
57
|
*/
|
|
58
58
|
export const ReadPostgresConfig = /*#__PURE__*/Schema.Struct({
|
|
59
|
-
// `
|
|
59
|
+
// `optionalKey` keeps the decoded type at `key?: T` (never `T | undefined`),
|
|
60
60
|
// matching the repo's `exactOptionalPropertyTypes` discipline — an absent key,
|
|
61
61
|
// not an explicit `undefined`.
|
|
62
|
-
schema: Schema.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
tablePrefix: Schema.optionalWith(IdentifierAtom, {
|
|
66
|
-
exact: true
|
|
67
|
-
})
|
|
68
|
-
}).pipe(
|
|
62
|
+
schema: Schema.optionalKey(IdentifierAtom),
|
|
63
|
+
tablePrefix: Schema.optionalKey(IdentifierAtom)
|
|
64
|
+
}).check(
|
|
69
65
|
/*#__PURE__*/
|
|
70
66
|
// Cross-field guard: the derived table name must fit Postgres's 63-byte
|
|
71
67
|
// identifier limit. Returning the message string fails the decode with it;
|
|
72
|
-
// `undefined
|
|
73
|
-
Schema.
|
|
68
|
+
// `undefined` passes.
|
|
69
|
+
Schema.makeFilter(config => oversizedIdentifier(config)));
|
|
74
70
|
/**
|
|
75
71
|
* Decode an unknown value into a validated `ReadPostgresConfig`, throwing a
|
|
76
|
-
* `
|
|
72
|
+
* `SchemaError` on any violation. Used by `makeSqlProjectionStore` and
|
|
77
73
|
* `ensureCheckpointSchema` so a bad config fails at construction time.
|
|
78
74
|
*/
|
|
79
75
|
export const decodeReadPostgresConfig = /*#__PURE__*/Schema.decodeUnknownSync(ReadPostgresConfig);
|
package/dist/esm/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","names":["Schema","oversizedIdentifier","IdentifierAtom","String","
|
|
1
|
+
{"version":3,"file":"config.js","names":["Schema","oversizedIdentifier","IdentifierAtom","String","check","isNonEmpty","isPattern","identifier","description","ReadPostgresConfig","Struct","schema","optionalKey","tablePrefix","makeFilter","config","decodeReadPostgresConfig","decodeUnknownSync"],"sources":["../../src/config.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAASA,MAAM,QAAQ,QAAQ;AAC/B,SAASC,mBAAmB,QAAQ,mBAAgB;AAEpD;;;;;;AAMA,MAAMC,cAAc,gBAAGF,MAAM,CAACG,MAAM,CAACC,KAAK,cACxCJ,MAAM,CAACK,UAAU,EAAE,eACnBL,MAAM,CAACM,SAAS,CAAC,SAAS,EAAE;EAC1BC,UAAU,EAAE,wBAAwB;EACpCC,WAAW,EAAE;CACd,CAAC,CACH;AAED;;;;;;;;;;AAUA,OAAO,MAAMC,kBAAkB,gBAAGT,MAAM,CAACU,MAAM,CAAC;EAC9C;EACA;EACA;EACAC,MAAM,EAAEX,MAAM,CAACY,WAAW,CAACV,cAAc,CAAC;EAC1CW,WAAW,EAAEb,MAAM,CAACY,WAAW,CAACV,cAAc;CAC/C,CAAC,CAACE,KAAK;AAAA;AACN;AACA;AACA;AACAJ,MAAM,CAACc,UAAU,CAAEC,MAAM,IAAKd,mBAAmB,CAACc,MAAM,CAAC,CAAC,CAC3D;AAGD;;;;;AAKA,OAAO,MAAMC,wBAAwB,gBACnChB,MAAM,CAACiB,iBAAiB,CAACR,kBAAkB,CAAC","ignoreList":[]}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* It is a single `Effect<void, SqlError, SqlClient>` that creates (or refreshes)
|
|
6
6
|
* the one object this package owns: the checkpoint table. It requires the GENERIC
|
|
7
|
-
* `SqlClient`
|
|
7
|
+
* `SqlClient` service — not a package-owned client service — precisely so a consumer can
|
|
8
8
|
* drop it straight into THEIR OWN migrator, e.g.:
|
|
9
9
|
*
|
|
10
10
|
* ```ts
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
* reads no rows and passes no identifier through `sql(identifier)`, so a
|
|
28
28
|
* consumer's name transforms have nothing here to rewrite.
|
|
29
29
|
*/
|
|
30
|
-
import { SqlClient } from '@effect/sql';
|
|
31
30
|
import { Effect } from 'effect';
|
|
31
|
+
import { SqlClient } from 'effect/sql';
|
|
32
32
|
import { decodeReadPostgresConfig } from "./config.js";
|
|
33
33
|
import { resolveCheckpointNames, runCheckpointDdl } from "./internal/ddl.js";
|
|
34
34
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ensureCheckpointSchema.js","names":["
|
|
1
|
+
{"version":3,"file":"ensureCheckpointSchema.js","names":["Effect","SqlClient","decodeReadPostgresConfig","resolveCheckpointNames","runCheckpointDdl","ensureCheckpointSchema","config","gen","sql","names","all","discard"],"sources":["../../src/ensureCheckpointSchema.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,SAASA,MAAM,QAAQ,QAAQ;AAC/B,SAASC,SAAS,QAAuB,YAAY;AACrD,SAASC,wBAAwB,QAAiC,aAAU;AAC5E,SAASC,sBAAsB,EAAEC,gBAAgB,QAAQ,mBAAgB;AAEzE;;;;;AAKA,OAAO,MAAMC,sBAAsB,GACjCC,MAA0B,IAE1BN,MAAM,CAACO,GAAG,CAAC,aAAS;EAClB,MAAMC,GAAG,GAAG,OAAOP,SAAS,CAACA,SAAS;EACtC;EACA;EACA;EACA,MAAMQ,KAAK,GAAGN,sBAAsB,CAACD,wBAAwB,CAACI,MAAM,CAAC,CAAC;EACtE;EACA,OAAON,MAAM,CAACU,GAAG,CAACN,gBAAgB,CAACI,GAAG,EAAEC,KAAK,CAAC,EAAE;IAAEE,OAAO,EAAE;EAAI,CAAE,CAAC;AACpE,CAAC,CAAC","ignoreList":[]}
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["ensureCheckpointSchema","makeSqlProjectionStore"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":["ensureCheckpointSchema","makeSqlProjectionStore"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAyFA,SAASA,sBAAsB,QAAQ,6BAA0B;AACjE,SAASC,sBAAsB,QAAQ,sBAAmB","ignoreList":[]}
|