@ddd-ts-kit/prisma 0.8.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
@@ -543,6 +543,26 @@ disappears with them if it fails.
543
543
 
544
544
  Delivery is at least once, unordered.
545
545
 
546
+ ## Event store
547
+
548
+ Copy the models of `prisma/event-store.postgresql.prisma` into the schema; `prisma migrate dev`
549
+ creates the tables. Keep their table and column names: the stores run raw statements on them.
550
+
551
+ ```ts
552
+ const { events, snapshots, checkpoints, subjectKeys } = createPrismaEventSourcing(unitOfWork);
553
+ ```
554
+
555
+ A context that keeps its event store in a schema of its own adds `@@schema("<context>")` to
556
+ the copies, gives them model names of their own, and passes `{ schema: '<context>' }`.
557
+
558
+ Each statement runs on the executor of the context: an append joins the transaction of the
559
+ unit of work, so the stream and the outbox records its events publish commit together
560
+ (`eventStoreUnitOfWorkContract`). Appends take the head of the store, a row locked until their
561
+ transaction ends: positions are given in commit order, without gaps, and two appends never run
562
+ side by side, whatever their streams. PostgreSQL only. The stores are the ones of the kernel
563
+ (`PostgresEventStore` and its companions), held to `eventStoreContract` here; how an
564
+ event-sourced aggregate uses them: the README of `@ddd-ts-kit/kernel`, section "Event sourcing".
565
+
546
566
  ## Dialect coverage
547
567
 
548
568
  What follows is about this package. The CLI generates for PostgreSQL only: `init --orm prisma`
@@ -0,0 +1,11 @@
1
+ import { type EventSourcingStores, type EventStoreTablesOptions } from '@ddd-ts-kit/kernel';
2
+ import type { PrismaRawQueries } from '../outbox/prisma-raw-queries.js';
3
+ import type { PrismaContext } from '../unit-of-work/prisma-context.js';
4
+ /**
5
+ * The event store, the snapshots, the checkpoints and the keys of the data subjects on
6
+ * PostgreSQL, through the raw queries of the Prisma client, over the models of
7
+ * `prisma/event-store.postgresql.prisma`. Every statement runs on the client of the context, so
8
+ * an append joins the transaction of the unit of work, with the outbox. Bind each one to its port.
9
+ */
10
+ export declare function createPrismaEventSourcing<Tx extends PrismaRawQueries>(context: PrismaContext<Tx>, options?: EventStoreTablesOptions): EventSourcingStores;
11
+ //# sourceMappingURL=create-prisma-event-sourcing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-prisma-event-sourcing.d.ts","sourceRoot":"","sources":["../../src/event-sourcing/create-prisma-event-sourcing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,KAAK,mBAAmB,EAAE,KAAK,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AACnH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACxE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAEvE;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,EAAE,SAAS,gBAAgB,EACnE,OAAO,EAAE,aAAa,CAAC,EAAE,CAAC,EAC1B,OAAO,GAAE,uBAA4B,GACpC,mBAAmB,CAQrB"}
@@ -0,0 +1,16 @@
1
+ import { postgresEventSourcing } from '@ddd-ts-kit/kernel';
2
+ /**
3
+ * The event store, the snapshots, the checkpoints and the keys of the data subjects on
4
+ * PostgreSQL, through the raw queries of the Prisma client, over the models of
5
+ * `prisma/event-store.postgresql.prisma`. Every statement runs on the client of the context, so
6
+ * an append joins the transaction of the unit of work, with the outbox. Bind each one to its port.
7
+ */
8
+ export function createPrismaEventSourcing(context, options = {}) {
9
+ return postgresEventSourcing(async (statement, parameters) => {
10
+ const rows = await context.client().$queryRawUnsafe(statement, ...parameters);
11
+ return Array.isArray(rows)
12
+ ? rows.filter((row) => typeof row === 'object' && row !== null)
13
+ : [];
14
+ }, options);
15
+ }
16
+ //# sourceMappingURL=create-prisma-event-sourcing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-prisma-event-sourcing.js","sourceRoot":"","sources":["../../src/event-sourcing/create-prisma-event-sourcing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAA0D,MAAM,oBAAoB,CAAC;AAInH;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CACvC,OAA0B,EAC1B,OAAO,GAA4B,EAAE;IAErC,OAAO,qBAAqB,CAAC,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE;QAC3D,MAAM,IAAI,GAAY,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,eAAe,CAAC,SAAS,EAAE,GAAG,UAAU,CAAC,CAAC;QAEvF,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;YACxB,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAA4C,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,CAAC;YACzG,CAAC,CAAC,EAAE,CAAC;IACT,CAAC,EAAE,OAAO,CAAC,CAAC;AACd,CAAC"}
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from './unit-of-work/prisma-transaction-host.js';
3
3
  export * from './unit-of-work/prisma-transaction-client.js';
4
4
  export * from './unit-of-work/prisma-context.js';
5
5
  export * from './unit-of-work/create-prisma-unit-of-work.js';
6
+ export * from './event-sourcing/create-prisma-event-sourcing.js';
6
7
  export * from './outbox/prisma-json-input.js';
7
8
  export * from './outbox/prisma-outbox-delegate.js';
8
9
  export * from './outbox/prisma-raw-queries.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,EAEhB,YAAY,EACZ,KAAK,WAAW,GACjB,MAAM,oBAAoB,CAAC;AAC5B,cAAc,2CAA2C,CAAC;AAC1D,cAAc,6CAA6C,CAAC;AAC5D,cAAc,kCAAkC,CAAC;AACjD,cAAc,8CAA8C,CAAC;AAC7D,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oCAAoC,CAAC;AACnD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,iCAAiC,CAAC;AAChD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,0CAA0C,CAAC;AACzD,cAAc,iCAAiC,CAAC;AAChD,cAAc,qCAAqC,CAAC;AACpD,cAAc,oCAAoC,CAAC;AACnD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yCAAyC,CAAC;AACxD,cAAc,yCAAyC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAEhB,YAAY,EACZ,KAAK,WAAW,GACjB,MAAM,oBAAoB,CAAC;AAC5B,cAAc,2CAA2C,CAAC;AAC1D,cAAc,6CAA6C,CAAC;AAC5D,cAAc,kCAAkC,CAAC;AACjD,cAAc,8CAA8C,CAAC;AAC7D,cAAc,kDAAkD,CAAC;AACjE,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oCAAoC,CAAC;AACnD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,iCAAiC,CAAC;AAChD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,0CAA0C,CAAC;AACzD,cAAc,iCAAiC,CAAC;AAChD,cAAc,qCAAqC,CAAC;AACpD,cAAc,oCAAoC,CAAC;AACnD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yCAAyC,CAAC;AACxD,cAAc,yCAAyC,CAAC"}
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export * from './unit-of-work/prisma-transaction-host.js';
5
5
  export * from './unit-of-work/prisma-transaction-client.js';
6
6
  export * from './unit-of-work/prisma-context.js';
7
7
  export * from './unit-of-work/create-prisma-unit-of-work.js';
8
+ export * from './event-sourcing/create-prisma-event-sourcing.js';
8
9
  export * from './outbox/prisma-json-input.js';
9
10
  export * from './outbox/prisma-outbox-delegate.js';
10
11
  export * from './outbox/prisma-raw-queries.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;AAChB,oFAAoF;AACpF,YAAY,GAEb,MAAM,oBAAoB,CAAC;AAC5B,cAAc,2CAA2C,CAAC;AAC1D,cAAc,6CAA6C,CAAC;AAC5D,cAAc,kCAAkC,CAAC;AACjD,cAAc,8CAA8C,CAAC;AAC7D,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oCAAoC,CAAC;AACnD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,iCAAiC,CAAC;AAChD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,0CAA0C,CAAC;AACzD,cAAc,iCAAiC,CAAC;AAChD,cAAc,qCAAqC,CAAC;AACpD,cAAc,oCAAoC,CAAC;AACnD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yCAAyC,CAAC;AACxD,cAAc,yCAAyC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB;AAChB,oFAAoF;AACpF,YAAY,GAEb,MAAM,oBAAoB,CAAC;AAC5B,cAAc,2CAA2C,CAAC;AAC1D,cAAc,6CAA6C,CAAC;AAC5D,cAAc,kCAAkC,CAAC;AACjD,cAAc,8CAA8C,CAAC;AAC7D,cAAc,kDAAkD,CAAC;AACjE,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oCAAoC,CAAC;AACnD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,iCAAiC,CAAC;AAChD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,0CAA0C,CAAC;AACzD,cAAc,iCAAiC,CAAC;AAChD,cAAc,qCAAqC,CAAC;AACpD,cAAc,oCAAoC,CAAC;AACnD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yCAAyC,CAAC;AACxD,cAAc,yCAAyC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ddd-ts-kit/prisma",
3
- "version": "0.8.0",
4
- "description": "Prisma adapters for the kernel ports: unit of work, transactional outbox store, processed-event store.",
3
+ "version": "0.10.0",
4
+ "description": "Prisma 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",
@@ -27,8 +27,8 @@
27
27
  "prisma"
28
28
  ],
29
29
  "dependencies": {
30
- "@ddd-ts-kit/kernel": "0.8.0",
31
- "@ddd-ts-kit/events": "0.8.0"
30
+ "@ddd-ts-kit/kernel": "0.10.0",
31
+ "@ddd-ts-kit/events": "0.10.0"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "@prisma/client": "^7.0.0"
@@ -37,7 +37,7 @@
37
37
  "@prisma/client": "^7.10.0",
38
38
  "prisma": "^7.10.0",
39
39
  "@prisma/adapter-pg": "^7.10.0",
40
- "@ddd-ts-kit/testing": "0.8.0",
40
+ "@ddd-ts-kit/testing": "0.10.0",
41
41
  "pg": "^8.23.1",
42
42
  "@types/pg": "^8.23.1",
43
43
  "@electric-sql/pglite": "0.5.9",
@@ -0,0 +1,60 @@
1
+ // Models of the event store of @ddd-ts-kit/prisma for PostgreSQL. Copy them into your
2
+ // schema.prisma; your own `prisma migrate dev` then creates the tables in a migration of your
3
+ // project. The stores run raw statements against these tables: keep the table and column names
4
+ // (`@@map`, `@map`). A context that keeps its own event store puts the models in its schema
5
+ // (`@@schema("<context>")`, with model names of its own) and gives `createPrismaEventSourcing`
6
+ // that `schema`.
7
+
8
+ /// The head of the store: the position the next append starts after. One row, id 1, locked by
9
+ /// each append until its transaction ends.
10
+ model StoredEventsHead {
11
+ id Int @id @db.SmallInt
12
+ position BigInt
13
+
14
+ @@map("stored_events_head")
15
+ }
16
+
17
+ /// The events of every stream, never changed once written.
18
+ model StoredEvent {
19
+ position BigInt @id
20
+ stream String
21
+ version Int
22
+ eventId String @unique @map("event_id")
23
+ type String
24
+ schemaVersion Int @map("schema_version")
25
+ data Json
26
+ metadata Json
27
+ occurredAt DateTime @map("occurred_at") @db.Timestamptz(3)
28
+ recordedAt DateTime @default(now()) @map("recorded_at") @db.Timestamptz(3)
29
+
30
+ @@unique([stream, version])
31
+ @@map("stored_events")
32
+ }
33
+
34
+ model StreamSnapshot {
35
+ stream String @id
36
+ version Int
37
+ schemaVersion Int @map("schema_version")
38
+ data Json
39
+ takenAt DateTime @default(now()) @map("taken_at") @db.Timestamptz(3)
40
+
41
+ @@map("stream_snapshots")
42
+ }
43
+
44
+ model SubscriptionCheckpoint {
45
+ subscriber String @id
46
+ position BigInt
47
+ updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamptz(3)
48
+
49
+ @@map("subscription_checkpoints")
50
+ }
51
+
52
+ /// One key per data subject: erasing the subject deletes its row.
53
+ model SubjectKey {
54
+ subject String @id
55
+ keyId String @unique @map("key_id")
56
+ material String
57
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
58
+
59
+ @@map("subject_keys")
60
+ }