@ddd-ts-kit/knex 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -597,6 +597,28 @@ Criteria and read models were never run on MySQL or SQLite.
597
597
  `(consumer, event_id)`. Run the consumer inside the unit of work: the mark then commits with
598
598
  the effects of the handler, and disappears with them if it fails.
599
599
 
600
+ ## Event store
601
+
602
+ ```ts
603
+ // a migration of the project
604
+ export const up = (knex) => createEventStoreTables(knex, { schema: 'scheduling' });
605
+
606
+ // the composition root
607
+ const { events, snapshots, checkpoints, subjectKeys } = createKnexEventSourcing(unitOfWork, { schema: 'scheduling' });
608
+ ```
609
+
610
+ `createEventStoreTables` runs `eventStoreTablesSql` of the kernel: `stored_events`,
611
+ `stored_events_head`, `stream_snapshots`, `subscription_checkpoints` and `subject_keys`, in the
612
+ schema of the context. `createKnexEventSourcing` refuses a knex that does not speak PostgreSQL.
613
+
614
+ Each statement runs on the executor of the context: an append joins the transaction of the
615
+ unit of work, so the stream and the outbox records its events publish commit together
616
+ (`eventStoreUnitOfWorkContract`). Appends take the head of the store, a row locked until their
617
+ transaction ends: positions are given in commit order, without gaps, and two appends never run
618
+ side by side, whatever their streams. PostgreSQL only. The stores are the ones of the kernel
619
+ (`PostgresEventStore` and its companions), held to `eventStoreContract` here; how an
620
+ event-sourced aggregate uses them: the README of `@ddd-ts-kit/kernel`, section "Event sourcing".
621
+
600
622
  ## Dialect coverage
601
623
 
602
624
  What follows is about this package. The CLI generates for PostgreSQL only: `init --orm knex`
@@ -0,0 +1,9 @@
1
+ import type { Knex } from 'knex';
2
+ import { type EventStoreTablesOptions } from '@ddd-ts-kit/kernel';
3
+ /**
4
+ * Creates the tables of the event store. Call it from a migration of the consuming project
5
+ * (`export const up = (knex) => createEventStoreTables(knex, { schema: 'scheduling' })`), which
6
+ * then owns the tables like any other.
7
+ */
8
+ export declare function createEventStoreTables(knex: Knex, options?: EventStoreTablesOptions): Promise<void>;
9
+ //# sourceMappingURL=create-event-store-tables.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-event-store-tables.d.ts","sourceRoot":"","sources":["../../src/event-sourcing/create-event-store-tables.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAuB,KAAK,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAEvF;;;;GAIG;AACH,wBAAsB,sBAAsB,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,GAAE,uBAA4B,GAAG,OAAO,CAAC,IAAI,CAAC,CAI7G"}
@@ -0,0 +1,12 @@
1
+ import { eventStoreTablesSql } from '@ddd-ts-kit/kernel';
2
+ /**
3
+ * Creates the tables of the event store. Call it from a migration of the consuming project
4
+ * (`export const up = (knex) => createEventStoreTables(knex, { schema: 'scheduling' })`), which
5
+ * then owns the tables like any other.
6
+ */
7
+ export async function createEventStoreTables(knex, options = {}) {
8
+ for (const statement of eventStoreTablesSql(options)) {
9
+ await knex.raw(statement);
10
+ }
11
+ }
12
+ //# sourceMappingURL=create-event-store-tables.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-event-store-tables.js","sourceRoot":"","sources":["../../src/event-sourcing/create-event-store-tables.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAgC,MAAM,oBAAoB,CAAC;AAEvF;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,IAAU,EAAE,OAAO,GAA4B,EAAE;IAC5F,KAAK,MAAM,SAAS,IAAI,mBAAmB,CAAC,OAAO,CAAC,EAAE,CAAC;QACrD,MAAM,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC5B,CAAC;AACH,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { type EventSourcingStores, type EventStoreTablesOptions } from '@ddd-ts-kit/kernel';
2
+ import type { KnexContext } from '../unit-of-work/knex-context.js';
3
+ /**
4
+ * The event store, the snapshots, the checkpoints and the keys of the data subjects on
5
+ * PostgreSQL, through knex. Every statement runs on the executor of the context, so an append
6
+ * joins the transaction of the unit of work, with the outbox. Bind each one to its port.
7
+ */
8
+ export declare function createKnexEventSourcing(context: KnexContext, options?: EventStoreTablesOptions): EventSourcingStores;
9
+ //# sourceMappingURL=create-knex-event-sourcing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-knex-event-sourcing.d.ts","sourceRoot":"","sources":["../../src/event-sourcing/create-knex-event-sourcing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,KAAK,mBAAmB,EAAE,KAAK,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAEnH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAGnE;;;;GAIG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,WAAW,EACpB,OAAO,GAAE,uBAA4B,GACpC,mBAAmB,CAQrB"}
@@ -0,0 +1,16 @@
1
+ import { postgresEventSourcing } from '@ddd-ts-kit/kernel';
2
+ import { dialectOf } from '../sql/dialect-of.js';
3
+ import { knexSqlRun } from './knex-sql-run.js';
4
+ /**
5
+ * The event store, the snapshots, the checkpoints and the keys of the data subjects on
6
+ * PostgreSQL, through knex. Every statement runs on the executor of the context, so an append
7
+ * joins the transaction of the unit of work, with the outbox. Bind each one to its port.
8
+ */
9
+ export function createKnexEventSourcing(context, options = {}) {
10
+ const dialect = dialectOf(context.executor());
11
+ if (dialect !== 'postgres') {
12
+ throw new TypeError(`The event store of @ddd-ts-kit/knex runs on PostgreSQL, and this knex speaks ${dialect}`);
13
+ }
14
+ return postgresEventSourcing(knexSqlRun(context), options);
15
+ }
16
+ //# sourceMappingURL=create-knex-event-sourcing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-knex-event-sourcing.js","sourceRoot":"","sources":["../../src/event-sourcing/create-knex-event-sourcing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAA0D,MAAM,oBAAoB,CAAC;AACnH,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CACrC,OAAoB,EACpB,OAAO,GAA4B,EAAE;IAErC,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IAE9C,IAAI,OAAO,KAAK,UAAU,EAAE,CAAC;QAC3B,MAAM,IAAI,SAAS,CAAC,gFAAgF,OAAO,EAAE,CAAC,CAAC;IACjH,CAAC;IAED,OAAO,qBAAqB,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC;AAC7D,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { SqlRun } from '@ddd-ts-kit/kernel';
2
+ import type { KnexContext } from '../unit-of-work/knex-context.js';
3
+ /** Runs the statements of the shared PostgreSQL stores on the executor of the context: knex binds `?`, each `$n` is one. */
4
+ export declare function knexSqlRun(context: KnexContext): SqlRun;
5
+ //# sourceMappingURL=knex-sql-run.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"knex-sql-run.d.ts","sourceRoot":"","sources":["../../src/event-sourcing/knex-sql-run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAEnE,4HAA4H;AAC5H,wBAAgB,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAMvD"}
@@ -0,0 +1,9 @@
1
+ import { recordsOf } from '../read-model/records-of.js';
2
+ /** Runs the statements of the shared PostgreSQL stores on the executor of the context: knex binds `?`, each `$n` is one. */
3
+ export function knexSqlRun(context) {
4
+ return async (statement, parameters) => {
5
+ const result = await context.executor().raw(statement.replace(/\$\d+/g, '?'), [...parameters]);
6
+ return recordsOf(typeof result === 'object' && result !== null && 'rows' in result ? result.rows : []);
7
+ };
8
+ }
9
+ //# sourceMappingURL=knex-sql-run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"knex-sql-run.js","sourceRoot":"","sources":["../../src/event-sourcing/knex-sql-run.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAGxD,4HAA4H;AAC5H,MAAM,UAAU,UAAU,CAAC,OAAoB;IAC7C,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE;QACrC,MAAM,MAAM,GAAY,MAAM,OAAO,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;QAExG,OAAO,SAAS,CAAC,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IACzG,CAAC,CAAC;AACJ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export { CONCURRENCY_CONFLICT, ConcurrencyError, optimisticUpdate, schemaNameOf, type VersionStep, } from '@ddd-ts-kit/kernel';
2
2
  export * from './unit-of-work/knex-context.js';
3
3
  export * from './unit-of-work/create-knex-unit-of-work.js';
4
+ export * from './event-sourcing/create-event-store-tables.js';
5
+ export * from './event-sourcing/create-knex-event-sourcing.js';
4
6
  export * from './outbox/table-names.js';
5
7
  export * from './outbox/outbox-tables-options.js';
6
8
  export * from './outbox/create-outbox-tables.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ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 @@
1
1
  export { CONCURRENCY_CONFLICT, ConcurrencyError, optimisticUpdate, schemaNameOf, } from '@ddd-ts-kit/kernel';
2
2
  export * from './unit-of-work/knex-context.js';
3
3
  export * from './unit-of-work/create-knex-unit-of-work.js';
4
+ export * from './event-sourcing/create-event-store-tables.js';
5
+ export * from './event-sourcing/create-knex-event-sourcing.js';
4
6
  export * from './outbox/table-names.js';
5
7
  export * from './outbox/outbox-tables-options.js';
6
8
  export * from './outbox/create-outbox-tables.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,GAEb,MAAM,oBAAoB,CAAC;AAC5B,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4CAA4C,CAAC;AAC3D,cAAc,yBAAyB,CAAC;AACxC,cAAc,mCAAmC,CAAC;AAClD,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wCAAwC,CAAC;AACvD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,mCAAmC,CAAC;AAClD,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAyC,CAAC;AACxD,cAAc,uCAAuC,CAAC;AACtD,cAAc,uCAAuC,CAAC;AACtD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,0CAA0C,CAAC;AACzD,cAAc,sCAAsC,CAAC;AACrD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,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Ac,sCAAsC,CAAC;AACrD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,8CAA8C,CAAC;AAC7D,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mCAAmC,CAAC;AAClD,cAAc,kCAAkC,CAAC;AACjD,cAAc,iCAAiC,CAAC;AAChD,cAAc,kCAAkC,CAAC;AACjD,cAAc,oCAAoC,CAAC;AACnD,cAAc,uCAAuC,CAAC;AACtD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,8CAA8C,CAAC;AAC7D,cAAc,mDAAmD,CAAC;AAClE,cAAc,gDAAgD,CAAC;AAC/D,cAAc,mCAAmC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ddd-ts-kit/knex",
3
- "version": "0.9.0",
4
- "description": "Knex adapters for the kernel ports: unit of work, transactional outbox store, processed-event store.",
3
+ "version": "0.10.0",
4
+ "description": "Knex adapters for the kernel ports: unit of work, transactional outbox store, processed-event store, PostgreSQL event store.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -26,15 +26,15 @@
26
26
  "dist"
27
27
  ],
28
28
  "dependencies": {
29
- "@ddd-ts-kit/kernel": "0.9.0",
30
- "@ddd-ts-kit/events": "0.9.0"
29
+ "@ddd-ts-kit/kernel": "0.10.0",
30
+ "@ddd-ts-kit/events": "0.10.0"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "knex": "^3.0.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "knex": "^3.3.0",
37
- "@ddd-ts-kit/testing": "0.9.0",
37
+ "@ddd-ts-kit/testing": "0.10.0",
38
38
  "pg": "^8.23.1",
39
39
  "@types/pg": "^8.23.1",
40
40
  "@electric-sql/pglite": "0.5.9",