@wataruoguchi/emmett-event-store-kysely 2.2.5 → 2.2.7
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 +0 -1
- package/dist/index.cjs +6 -8
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/projections/runner.d.ts +8 -7
- package/dist/projections/runner.d.ts.map +1 -1
- package/dist/projections/snapshot-projection.d.ts +7 -5
- package/dist/projections/snapshot-projection.d.ts.map +1 -1
- package/dist/projections/snapshot-projection.js +8 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -81,7 +81,6 @@ const registry = createSnapshotProjectionRegistry(
|
|
|
81
81
|
["CartCreated", "ItemAdded", "CartCheckedOut"],
|
|
82
82
|
{
|
|
83
83
|
tableName: "carts",
|
|
84
|
-
primaryKeys: ["tenant_id", "cart_id", "partition"],
|
|
85
84
|
extractKeys: (event, partition) => ({
|
|
86
85
|
tenant_id: event.data.eventMeta.tenantId,
|
|
87
86
|
cart_id: event.data.eventMeta.cartId,
|
package/dist/index.cjs
CHANGED
|
@@ -533,16 +533,14 @@ function createProjectionRunner({
|
|
|
533
533
|
|
|
534
534
|
// src/projections/snapshot-projection.ts
|
|
535
535
|
function createSnapshotProjection(config) {
|
|
536
|
-
const {
|
|
537
|
-
|
|
538
|
-
primaryKeys,
|
|
539
|
-
extractKeys,
|
|
540
|
-
evolve,
|
|
541
|
-
initialState,
|
|
542
|
-
mapToColumns
|
|
543
|
-
} = config;
|
|
536
|
+
const { tableName, extractKeys, evolve, initialState, mapToColumns } = config;
|
|
537
|
+
let inferredPrimaryKeys;
|
|
544
538
|
return async ({ db, partition }, event) => {
|
|
545
539
|
const keys = extractKeys(event, partition);
|
|
540
|
+
if (!inferredPrimaryKeys) {
|
|
541
|
+
inferredPrimaryKeys = Object.keys(keys);
|
|
542
|
+
}
|
|
543
|
+
const primaryKeys = inferredPrimaryKeys;
|
|
546
544
|
const existing = await db.selectFrom(tableName).select(["last_stream_position", "snapshot"]).where((eb) => {
|
|
547
545
|
const conditions = Object.entries(keys).map(
|
|
548
546
|
([key, value]) => eb(key, "=", value)
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type { KyselyEventStoreConsumer, KyselyEventStoreConsumerConfig, } from "
|
|
|
3
3
|
export { getKyselyEventStore } from "./event-store/kysely-event-store.js";
|
|
4
4
|
export type { KyselyEventStore, KyselyEventStoreOptions, ProjectionReadStreamOptions, } from "./event-store/kysely-event-store.js";
|
|
5
5
|
export { createProjectionRunner } from "./projections/runner.js";
|
|
6
|
+
export type { ProjectEvents } from "./projections/runner.js";
|
|
6
7
|
export { createSnapshotProjection, createSnapshotProjectionRegistry, } from "./projections/snapshot-projection.js";
|
|
7
8
|
export type { SnapshotProjectionConfig } from "./projections/snapshot-projection.js";
|
|
8
9
|
export { createProjectionRegistry } from "./types.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,8BAA8B,EAAE,MAAM,4BAA4B,CAAC;AAC5E,YAAY,EACV,wBAAwB,EACxB,8BAA8B,GAC/B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,YAAY,EACV,gBAAgB,EAChB,uBAAuB,EACvB,2BAA2B,GAC5B,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EACL,wBAAwB,EACxB,gCAAgC,GACjC,MAAM,sCAAsC,CAAC;AAC9C,YAAY,EAAE,wBAAwB,EAAE,MAAM,sCAAsC,CAAC;AACrF,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AACtD,YAAY,EACV,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,8BAA8B,EAAE,MAAM,4BAA4B,CAAC;AAC5E,YAAY,EACV,wBAAwB,EACxB,8BAA8B,GAC/B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,YAAY,EACV,gBAAgB,EAChB,uBAAuB,EACvB,2BAA2B,GAC5B,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACjE,YAAY,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EACL,wBAAwB,EACxB,gCAAgC,GACjC,MAAM,sCAAsC,CAAC;AAC9C,YAAY,EAAE,wBAAwB,EAAE,MAAM,sCAAsC,CAAC;AACrF,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AACtD,YAAY,EACV,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,YAAY,CAAC"}
|
|
@@ -15,13 +15,14 @@ export type ProjectionRunnerDeps = {
|
|
|
15
15
|
readStream: KyselyEventStore["readStream"];
|
|
16
16
|
registry: ProjectionRegistry;
|
|
17
17
|
};
|
|
18
|
+
export type ProjectEvents = (subscriptionId: string, streamId: string, opts?: {
|
|
19
|
+
partition?: string;
|
|
20
|
+
batchSize?: number;
|
|
21
|
+
}) => Promise<{
|
|
22
|
+
processed: number;
|
|
23
|
+
currentStreamVersion: bigint;
|
|
24
|
+
}>;
|
|
18
25
|
export declare function createProjectionRunner({ db, readStream, registry, }: ProjectionRunnerDeps): {
|
|
19
|
-
projectEvents:
|
|
20
|
-
partition?: string;
|
|
21
|
-
batchSize?: number;
|
|
22
|
-
}) => Promise<{
|
|
23
|
-
processed: number;
|
|
24
|
-
currentStreamVersion: bigint;
|
|
25
|
-
}>;
|
|
26
|
+
projectEvents: ProjectEvents;
|
|
26
27
|
};
|
|
27
28
|
//# sourceMappingURL=runner.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../src/projections/runner.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAqB,MAAM,QAAQ,CAAC;AAExD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,KAAK,EAAmB,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEvE,MAAM,MAAM,sBAAsB,GAAG;IACnC,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,qBAAqB,EAAE,MAAM,CAAC;CAC/B,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;IACtB,UAAU,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IAC3C,QAAQ,EAAE,kBAAkB,CAAC;CAC9B,CAAC;AAEF,
|
|
1
|
+
{"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../src/projections/runner.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAqB,MAAM,QAAQ,CAAC;AAExD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,KAAK,EAAmB,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEvE,MAAM,MAAM,sBAAsB,GAAG;IACnC,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,qBAAqB,EAAE,MAAM,CAAC;CAC/B,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;IACtB,UAAU,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IAC3C,QAAQ,EAAE,kBAAkB,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,CAC1B,cAAc,EAAE,MAAM,EACtB,QAAQ,EAAE,MAAM,EAChB,IAAI,CAAC,EAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,KAC9C,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,oBAAoB,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAElE,wBAAgB,sBAAsB,CAAC,EACrC,EAAE,EACF,UAAU,EACV,QAAQ,GACT,EAAE,oBAAoB,GAAG;IAAE,aAAa,EAAE,aAAa,CAAA;CAAE,CA+HzD"}
|
|
@@ -18,12 +18,16 @@ export type SnapshotProjectionConfig<TState, TTable extends string, E extends {
|
|
|
18
18
|
*/
|
|
19
19
|
tableName: TTable;
|
|
20
20
|
/**
|
|
21
|
-
* The primary key columns
|
|
21
|
+
* @deprecated The primary key columns are now automatically inferred from the keys returned by extractKeys.
|
|
22
|
+
* This field is optional and will be removed in a future version.
|
|
23
|
+
*
|
|
24
|
+
* If provided, it will be validated against the keys returned by extractKeys.
|
|
22
25
|
* e.g., ['tenant_id', 'cart_id', 'partition']
|
|
23
26
|
*/
|
|
24
|
-
primaryKeys
|
|
27
|
+
primaryKeys?: string[];
|
|
25
28
|
/**
|
|
26
|
-
* Extract primary key values from the event data
|
|
29
|
+
* Extract primary key values from the event data.
|
|
30
|
+
* The keys of the returned object will be used as the primary key columns for upsert operations.
|
|
27
31
|
*/
|
|
28
32
|
extractKeys: (event: ProjectionEvent<E>, partition: string) => Record<string, string>;
|
|
29
33
|
/**
|
|
@@ -63,7 +67,6 @@ export type SnapshotProjectionConfig<TState, TTable extends string, E extends {
|
|
|
63
67
|
* ```typescript
|
|
64
68
|
* const cartProjection = createSnapshotProjection({
|
|
65
69
|
* tableName: 'carts',
|
|
66
|
-
* primaryKeys: ['tenant_id', 'cart_id', 'partition'],
|
|
67
70
|
* extractKeys: (event, partition) => ({
|
|
68
71
|
* tenant_id: event.data.eventMeta.tenantId,
|
|
69
72
|
* cart_id: event.data.eventMeta.cartId,
|
|
@@ -98,7 +101,6 @@ export declare function createSnapshotProjection<TState, TTable extends string,
|
|
|
98
101
|
* ['CartCreated', 'ItemAddedToCart', 'ItemRemovedFromCart'],
|
|
99
102
|
* {
|
|
100
103
|
* tableName: 'carts',
|
|
101
|
-
* primaryKeys: ['tenant_id', 'cart_id', 'partition'],
|
|
102
104
|
* extractKeys: (event, partition) => ({
|
|
103
105
|
* tenant_id: event.data.eventMeta.tenantId,
|
|
104
106
|
* cart_id: event.data.eventMeta.cartId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapshot-projection.d.ts","sourceRoot":"","sources":["../../src/projections/snapshot-projection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,gBAAgB,EAEhB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAErB;;;;;;GAMG;AACH,MAAM,MAAM,wBAAwB,CAClC,MAAM,EACN,MAAM,SAAS,MAAM,EACrB,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,IACzE;IACF;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB
|
|
1
|
+
{"version":3,"file":"snapshot-projection.d.ts","sourceRoot":"","sources":["../../src/projections/snapshot-projection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,gBAAgB,EAEhB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAC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wBAAgB,gCAAgC,CAC9C,MAAM,EACN,MAAM,SAAS,MAAM,EACrB,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,EAE3E,UAAU,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,EACvB,MAAM,EAAE,wBAAwB,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,GAClD,kBAAkB,CAYpB"}
|
|
@@ -11,7 +11,6 @@
|
|
|
11
11
|
* ```typescript
|
|
12
12
|
* const cartProjection = createSnapshotProjection({
|
|
13
13
|
* tableName: 'carts',
|
|
14
|
-
* primaryKeys: ['tenant_id', 'cart_id', 'partition'],
|
|
15
14
|
* extractKeys: (event, partition) => ({
|
|
16
15
|
* tenant_id: event.data.eventMeta.tenantId,
|
|
17
16
|
* cart_id: event.data.eventMeta.cartId,
|
|
@@ -30,9 +29,16 @@
|
|
|
30
29
|
* ```
|
|
31
30
|
*/
|
|
32
31
|
export function createSnapshotProjection(config) {
|
|
33
|
-
const { tableName,
|
|
32
|
+
const { tableName, extractKeys, evolve, initialState, mapToColumns } = config;
|
|
33
|
+
// Cache the inferred primary keys after the first call
|
|
34
|
+
let inferredPrimaryKeys;
|
|
34
35
|
return async ({ db, partition }, event) => {
|
|
35
36
|
const keys = extractKeys(event, partition);
|
|
37
|
+
// Infer primary keys from extractKeys on first call
|
|
38
|
+
if (!inferredPrimaryKeys) {
|
|
39
|
+
inferredPrimaryKeys = Object.keys(keys);
|
|
40
|
+
}
|
|
41
|
+
const primaryKeys = inferredPrimaryKeys;
|
|
36
42
|
// Check if event is newer than what we've already processed
|
|
37
43
|
// Note: Casting to `any` is necessary because Kysely cannot infer types for dynamic table names.
|
|
38
44
|
// The table name is provided at runtime, so TypeScript cannot verify the table structure at compile time.
|
|
@@ -110,7 +116,6 @@ export function createSnapshotProjection(config) {
|
|
|
110
116
|
* ['CartCreated', 'ItemAddedToCart', 'ItemRemovedFromCart'],
|
|
111
117
|
* {
|
|
112
118
|
* tableName: 'carts',
|
|
113
|
-
* primaryKeys: ['tenant_id', 'cart_id', 'partition'],
|
|
114
119
|
* extractKeys: (event, partition) => ({
|
|
115
120
|
* tenant_id: event.data.eventMeta.tenantId,
|
|
116
121
|
* cart_id: event.data.eventMeta.cartId,
|