@l-etabli/events 0.6.0 → 0.6.1
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/dist/adapters/kysely/KyselyEventQueries.cjs +27 -24
- package/dist/adapters/kysely/KyselyEventQueries.cjs.map +1 -1
- package/dist/adapters/kysely/KyselyEventQueries.d.cts +1 -1
- package/dist/adapters/kysely/KyselyEventQueries.d.ts +1 -1
- package/dist/adapters/kysely/KyselyEventQueries.mjs +27 -24
- package/dist/adapters/kysely/KyselyEventQueries.mjs.map +1 -1
- package/dist/adapters/kysely/KyselyEventRepository.cjs +42 -39
- package/dist/adapters/kysely/KyselyEventRepository.cjs.map +1 -1
- package/dist/adapters/kysely/KyselyEventRepository.d.cts +1 -1
- package/dist/adapters/kysely/KyselyEventRepository.d.ts +1 -1
- package/dist/adapters/kysely/KyselyEventRepository.mjs +42 -39
- package/dist/adapters/kysely/KyselyEventRepository.mjs.map +1 -1
- package/package.json +2 -2
- package/src/adapters/kysely/KyselyEventQueries.ts +35 -31
- package/src/adapters/kysely/KyselyEventRepository.ts +64 -60
|
@@ -22,32 +22,35 @@ __export(KyselyEventQueries_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(KyselyEventQueries_exports);
|
|
24
24
|
var import_kysely = require("kysely");
|
|
25
|
-
const createKyselyEventQueries = (db) =>
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
const createKyselyEventQueries = (db) => {
|
|
26
|
+
const eventsDb = db;
|
|
27
|
+
return {
|
|
28
|
+
getEvents: async ({ filters, limit }) => {
|
|
29
|
+
let query = eventsDb.selectFrom("events").selectAll().where("status", "in", filters.statuses).limit(limit);
|
|
30
|
+
if (filters.context) {
|
|
31
|
+
for (const [key, value] of Object.entries(filters.context)) {
|
|
32
|
+
query = query.where(import_kysely.sql`context->>${key} = ${value}`);
|
|
33
|
+
}
|
|
31
34
|
}
|
|
35
|
+
if (filters.occurredAt?.from) {
|
|
36
|
+
query = query.where("occurredAt", ">=", filters.occurredAt.from);
|
|
37
|
+
}
|
|
38
|
+
if (filters.occurredAt?.to) {
|
|
39
|
+
query = query.where("occurredAt", "<=", filters.occurredAt.to);
|
|
40
|
+
}
|
|
41
|
+
const rows = await query.execute();
|
|
42
|
+
return rows.map(
|
|
43
|
+
(row) => ({
|
|
44
|
+
...row,
|
|
45
|
+
context: row.context ?? void 0,
|
|
46
|
+
flowId: row.flowId ?? void 0,
|
|
47
|
+
causedByEventId: row.causedByEventId ?? void 0,
|
|
48
|
+
priority: row.priority ?? void 0
|
|
49
|
+
})
|
|
50
|
+
);
|
|
32
51
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
if (filters.occurredAt?.to) {
|
|
37
|
-
query = query.where("occurredAt", "<=", filters.occurredAt.to);
|
|
38
|
-
}
|
|
39
|
-
const rows = await query.execute();
|
|
40
|
-
return rows.map(
|
|
41
|
-
(row) => ({
|
|
42
|
-
...row,
|
|
43
|
-
context: row.context ?? void 0,
|
|
44
|
-
flowId: row.flowId ?? void 0,
|
|
45
|
-
causedByEventId: row.causedByEventId ?? void 0,
|
|
46
|
-
priority: row.priority ?? void 0
|
|
47
|
-
})
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
52
|
+
};
|
|
53
|
+
};
|
|
51
54
|
// Annotate the CommonJS export names for ESM import in node:
|
|
52
55
|
0 && (module.exports = {
|
|
53
56
|
createKyselyEventQueries
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/adapters/kysely/KyselyEventQueries.ts"],"sourcesContent":["import type { Kysely, SqlBool } from \"kysely\";\nimport { sql } from \"kysely\";\nimport type { EventQueries } from \"../../ports/EventQueries.ts\";\nimport type { DefaultContext, GenericEvent } from \"../../types.ts\";\nimport type { EventsTable } from \"./types.ts\";\n\nexport const createKyselyEventQueries = <\n Event extends GenericEvent<string, unknown, DefaultContext>,\n>(\n db: Kysely<
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/kysely/KyselyEventQueries.ts"],"sourcesContent":["import type { Kysely, SqlBool } from \"kysely\";\nimport { sql } from \"kysely\";\nimport type { EventQueries } from \"../../ports/EventQueries.ts\";\nimport type { DefaultContext, GenericEvent } from \"../../types.ts\";\nimport type { EventsTable } from \"./types.ts\";\n\nexport const createKyselyEventQueries = <\n Event extends GenericEvent<string, unknown, DefaultContext>,\n DB extends EventsTable = EventsTable,\n>(\n db: Kysely<DB>,\n): EventQueries<Event> => {\n const eventsDb = db as unknown as Kysely<EventsTable>;\n return {\n getEvents: async ({ filters, limit }) => {\n let query = eventsDb\n .selectFrom(\"events\")\n .selectAll()\n .where(\"status\", \"in\", filters.statuses)\n .limit(limit);\n\n if (filters.context) {\n for (const [key, value] of Object.entries(filters.context)) {\n query = query.where(sql<SqlBool>`context->>${key} = ${value}`);\n }\n }\n\n if (filters.occurredAt?.from) {\n query = query.where(\"occurredAt\", \">=\", filters.occurredAt.from);\n }\n\n if (filters.occurredAt?.to) {\n query = query.where(\"occurredAt\", \"<=\", filters.occurredAt.to);\n }\n\n const rows = await query.execute();\n return rows.map(\n (row: EventsTable[\"events\"]) =>\n ({\n ...row,\n context: row.context ?? undefined,\n flowId: row.flowId ?? undefined,\n causedByEventId: row.causedByEventId ?? undefined,\n priority: row.priority ?? undefined,\n }) as Event,\n );\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAoB;AAKb,MAAM,2BAA2B,CAItC,OACwB;AACxB,QAAM,WAAW;AACjB,SAAO;AAAA,IACL,WAAW,OAAO,EAAE,SAAS,MAAM,MAAM;AACvC,UAAI,QAAQ,SACT,WAAW,QAAQ,EACnB,UAAU,EACV,MAAM,UAAU,MAAM,QAAQ,QAAQ,EACtC,MAAM,KAAK;AAEd,UAAI,QAAQ,SAAS;AACnB,mBAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,QAAQ,OAAO,GAAG;AAC1D,kBAAQ,MAAM,MAAM,8BAAyB,GAAG,MAAM,KAAK,EAAE;AAAA,QAC/D;AAAA,MACF;AAEA,UAAI,QAAQ,YAAY,MAAM;AAC5B,gBAAQ,MAAM,MAAM,cAAc,MAAM,QAAQ,WAAW,IAAI;AAAA,MACjE;AAEA,UAAI,QAAQ,YAAY,IAAI;AAC1B,gBAAQ,MAAM,MAAM,cAAc,MAAM,QAAQ,WAAW,EAAE;AAAA,MAC/D;AAEA,YAAM,OAAO,MAAM,MAAM,QAAQ;AACjC,aAAO,KAAK;AAAA,QACV,CAAC,SACE;AAAA,UACC,GAAG;AAAA,UACH,SAAS,IAAI,WAAW;AAAA,UACxB,QAAQ,IAAI,UAAU;AAAA,UACtB,iBAAiB,IAAI,mBAAmB;AAAA,UACxC,UAAU,IAAI,YAAY;AAAA,QAC5B;AAAA,MACJ;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
@@ -3,6 +3,6 @@ import { EventQueries } from '../../ports/EventQueries.cjs';
|
|
|
3
3
|
import { GenericEvent, DefaultContext } from '../../types.cjs';
|
|
4
4
|
import { EventsTable } from './types.cjs';
|
|
5
5
|
|
|
6
|
-
declare const createKyselyEventQueries: <Event extends GenericEvent<string, unknown, DefaultContext
|
|
6
|
+
declare const createKyselyEventQueries: <Event extends GenericEvent<string, unknown, DefaultContext>, DB extends EventsTable = EventsTable>(db: Kysely<DB>) => EventQueries<Event>;
|
|
7
7
|
|
|
8
8
|
export { createKyselyEventQueries };
|
|
@@ -3,6 +3,6 @@ import { EventQueries } from '../../ports/EventQueries.js';
|
|
|
3
3
|
import { GenericEvent, DefaultContext } from '../../types.js';
|
|
4
4
|
import { EventsTable } from './types.js';
|
|
5
5
|
|
|
6
|
-
declare const createKyselyEventQueries: <Event extends GenericEvent<string, unknown, DefaultContext
|
|
6
|
+
declare const createKyselyEventQueries: <Event extends GenericEvent<string, unknown, DefaultContext>, DB extends EventsTable = EventsTable>(db: Kysely<DB>) => EventQueries<Event>;
|
|
7
7
|
|
|
8
8
|
export { createKyselyEventQueries };
|
|
@@ -1,30 +1,33 @@
|
|
|
1
1
|
import { sql } from "kysely";
|
|
2
|
-
const createKyselyEventQueries = (db) =>
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
const createKyselyEventQueries = (db) => {
|
|
3
|
+
const eventsDb = db;
|
|
4
|
+
return {
|
|
5
|
+
getEvents: async ({ filters, limit }) => {
|
|
6
|
+
let query = eventsDb.selectFrom("events").selectAll().where("status", "in", filters.statuses).limit(limit);
|
|
7
|
+
if (filters.context) {
|
|
8
|
+
for (const [key, value] of Object.entries(filters.context)) {
|
|
9
|
+
query = query.where(sql`context->>${key} = ${value}`);
|
|
10
|
+
}
|
|
8
11
|
}
|
|
12
|
+
if (filters.occurredAt?.from) {
|
|
13
|
+
query = query.where("occurredAt", ">=", filters.occurredAt.from);
|
|
14
|
+
}
|
|
15
|
+
if (filters.occurredAt?.to) {
|
|
16
|
+
query = query.where("occurredAt", "<=", filters.occurredAt.to);
|
|
17
|
+
}
|
|
18
|
+
const rows = await query.execute();
|
|
19
|
+
return rows.map(
|
|
20
|
+
(row) => ({
|
|
21
|
+
...row,
|
|
22
|
+
context: row.context ?? void 0,
|
|
23
|
+
flowId: row.flowId ?? void 0,
|
|
24
|
+
causedByEventId: row.causedByEventId ?? void 0,
|
|
25
|
+
priority: row.priority ?? void 0
|
|
26
|
+
})
|
|
27
|
+
);
|
|
9
28
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
if (filters.occurredAt?.to) {
|
|
14
|
-
query = query.where("occurredAt", "<=", filters.occurredAt.to);
|
|
15
|
-
}
|
|
16
|
-
const rows = await query.execute();
|
|
17
|
-
return rows.map(
|
|
18
|
-
(row) => ({
|
|
19
|
-
...row,
|
|
20
|
-
context: row.context ?? void 0,
|
|
21
|
-
flowId: row.flowId ?? void 0,
|
|
22
|
-
causedByEventId: row.causedByEventId ?? void 0,
|
|
23
|
-
priority: row.priority ?? void 0
|
|
24
|
-
})
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
29
|
+
};
|
|
30
|
+
};
|
|
28
31
|
export {
|
|
29
32
|
createKyselyEventQueries
|
|
30
33
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/adapters/kysely/KyselyEventQueries.ts"],"sourcesContent":["import type { Kysely, SqlBool } from \"kysely\";\nimport { sql } from \"kysely\";\nimport type { EventQueries } from '../../ports/EventQueries.mjs';\nimport type { DefaultContext, GenericEvent } from '../../types.mjs';\nimport type { EventsTable } from './types.mjs';\n\nexport const createKyselyEventQueries = <\n Event extends GenericEvent<string, unknown, DefaultContext>,\n>(\n db: Kysely<
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/kysely/KyselyEventQueries.ts"],"sourcesContent":["import type { Kysely, SqlBool } from \"kysely\";\nimport { sql } from \"kysely\";\nimport type { EventQueries } from '../../ports/EventQueries.mjs';\nimport type { DefaultContext, GenericEvent } from '../../types.mjs';\nimport type { EventsTable } from './types.mjs';\n\nexport const createKyselyEventQueries = <\n Event extends GenericEvent<string, unknown, DefaultContext>,\n DB extends EventsTable = EventsTable,\n>(\n db: Kysely<DB>,\n): EventQueries<Event> => {\n const eventsDb = db as unknown as Kysely<EventsTable>;\n return {\n getEvents: async ({ filters, limit }) => {\n let query = eventsDb\n .selectFrom(\"events\")\n .selectAll()\n .where(\"status\", \"in\", filters.statuses)\n .limit(limit);\n\n if (filters.context) {\n for (const [key, value] of Object.entries(filters.context)) {\n query = query.where(sql<SqlBool>`context->>${key} = ${value}`);\n }\n }\n\n if (filters.occurredAt?.from) {\n query = query.where(\"occurredAt\", \">=\", filters.occurredAt.from);\n }\n\n if (filters.occurredAt?.to) {\n query = query.where(\"occurredAt\", \"<=\", filters.occurredAt.to);\n }\n\n const rows = await query.execute();\n return rows.map(\n (row: EventsTable[\"events\"]) =>\n ({\n ...row,\n context: row.context ?? undefined,\n flowId: row.flowId ?? undefined,\n causedByEventId: row.causedByEventId ?? undefined,\n priority: row.priority ?? undefined,\n }) as Event,\n );\n },\n };\n};\n"],"mappings":"AACA,SAAS,WAAW;AAKb,MAAM,2BAA2B,CAItC,OACwB;AACxB,QAAM,WAAW;AACjB,SAAO;AAAA,IACL,WAAW,OAAO,EAAE,SAAS,MAAM,MAAM;AACvC,UAAI,QAAQ,SACT,WAAW,QAAQ,EACnB,UAAU,EACV,MAAM,UAAU,MAAM,QAAQ,QAAQ,EACtC,MAAM,KAAK;AAEd,UAAI,QAAQ,SAAS;AACnB,mBAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,QAAQ,OAAO,GAAG;AAC1D,kBAAQ,MAAM,MAAM,gBAAyB,GAAG,MAAM,KAAK,EAAE;AAAA,QAC/D;AAAA,MACF;AAEA,UAAI,QAAQ,YAAY,MAAM;AAC5B,gBAAQ,MAAM,MAAM,cAAc,MAAM,QAAQ,WAAW,IAAI;AAAA,MACjE;AAEA,UAAI,QAAQ,YAAY,IAAI;AAC1B,gBAAQ,MAAM,MAAM,cAAc,MAAM,QAAQ,WAAW,EAAE;AAAA,MAC/D;AAEA,YAAM,OAAO,MAAM,MAAM,QAAQ;AACjC,aAAO,KAAK;AAAA,QACV,CAAC,SACE;AAAA,UACC,GAAG;AAAA,UACH,SAAS,IAAI,WAAW;AAAA,UACxB,QAAQ,IAAI,UAAU;AAAA,UACtB,iBAAiB,IAAI,mBAAmB;AAAA,UACxC,UAAU,IAAI,YAAY;AAAA,QAC5B;AAAA,MACJ;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
@@ -23,50 +23,53 @@ __export(KyselyEventRepository_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(KyselyEventRepository_exports);
|
|
24
24
|
var import_kysely = require("kysely");
|
|
25
25
|
const jsonb = (value) => import_kysely.sql`${JSON.stringify(value)}::jsonb`;
|
|
26
|
-
const createKyselyEventRepository = (db) =>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
triggeredByActor: jsonb(event.triggeredByActor),
|
|
32
|
-
context: jsonb(event.context),
|
|
33
|
-
publications: jsonb(event.publications)
|
|
34
|
-
}).onConflict(
|
|
35
|
-
(oc) => oc.column("id").doUpdateSet({
|
|
36
|
-
topic: event.topic,
|
|
37
|
-
payload: jsonb(event.payload),
|
|
38
|
-
triggeredByActor: jsonb(event.triggeredByActor),
|
|
39
|
-
context: jsonb(event.context),
|
|
40
|
-
status: event.status,
|
|
41
|
-
flowId: event.flowId,
|
|
42
|
-
causedByEventId: event.causedByEventId,
|
|
43
|
-
occurredAt: event.occurredAt,
|
|
44
|
-
publications: jsonb(event.publications),
|
|
45
|
-
priority: event.priority
|
|
46
|
-
})
|
|
47
|
-
).execute();
|
|
48
|
-
},
|
|
49
|
-
saveNewEventsBatch: async (events) => {
|
|
50
|
-
if (events.length === 0) return;
|
|
51
|
-
await db.insertInto("events").values(
|
|
52
|
-
events.map((event) => ({
|
|
26
|
+
const createKyselyEventRepository = (db) => {
|
|
27
|
+
const eventsDb = db;
|
|
28
|
+
return {
|
|
29
|
+
save: async (event) => {
|
|
30
|
+
await eventsDb.insertInto("events").values({
|
|
53
31
|
...event,
|
|
54
32
|
payload: jsonb(event.payload),
|
|
55
33
|
triggeredByActor: jsonb(event.triggeredByActor),
|
|
56
34
|
context: jsonb(event.context),
|
|
57
35
|
publications: jsonb(event.publications)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
36
|
+
}).onConflict(
|
|
37
|
+
(oc) => oc.column("id").doUpdateSet({
|
|
38
|
+
topic: event.topic,
|
|
39
|
+
payload: jsonb(event.payload),
|
|
40
|
+
triggeredByActor: jsonb(event.triggeredByActor),
|
|
41
|
+
context: jsonb(event.context),
|
|
42
|
+
status: event.status,
|
|
43
|
+
flowId: event.flowId,
|
|
44
|
+
causedByEventId: event.causedByEventId,
|
|
45
|
+
occurredAt: event.occurredAt,
|
|
46
|
+
publications: jsonb(event.publications),
|
|
47
|
+
priority: event.priority
|
|
48
|
+
})
|
|
49
|
+
).execute();
|
|
50
|
+
},
|
|
51
|
+
saveNewEventsBatch: async (events) => {
|
|
52
|
+
if (events.length === 0) return;
|
|
53
|
+
await eventsDb.insertInto("events").values(
|
|
54
|
+
events.map((event) => ({
|
|
55
|
+
...event,
|
|
56
|
+
payload: jsonb(event.payload),
|
|
57
|
+
triggeredByActor: jsonb(event.triggeredByActor),
|
|
58
|
+
context: jsonb(event.context),
|
|
59
|
+
publications: jsonb(event.publications)
|
|
60
|
+
}))
|
|
61
|
+
).execute();
|
|
62
|
+
},
|
|
63
|
+
markEventsAsInProcess: async (events) => {
|
|
64
|
+
if (events.length === 0) return;
|
|
65
|
+
const ids = events.map((e) => e.id);
|
|
66
|
+
const lockedRows = await eventsDb.selectFrom("events").select("id").where("id", "in", ids).forUpdate().skipLocked().execute();
|
|
67
|
+
if (lockedRows.length === 0) return;
|
|
68
|
+
const lockedIds = lockedRows.map((r) => r.id);
|
|
69
|
+
await eventsDb.updateTable("events").set({ status: "in-process" }).where("id", "in", lockedIds).execute();
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
70
73
|
// Annotate the CommonJS export names for ESM import in node:
|
|
71
74
|
0 && (module.exports = {
|
|
72
75
|
createKyselyEventRepository
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/adapters/kysely/KyselyEventRepository.ts"],"sourcesContent":["import { type Kysely, type RawBuilder, sql } from \"kysely\";\nimport type { EventRepository } from \"../../ports/EventRepository.ts\";\nimport type { DefaultContext, GenericEvent } from \"../../types.ts\";\nimport type { EventsTable } from \"./types.ts\";\n\nconst jsonb = <T>(value: T): RawBuilder<T> =>\n sql`${JSON.stringify(value)}::jsonb`;\n\nexport const createKyselyEventRepository = <\n Event extends GenericEvent<string, unknown, DefaultContext>,\n>(\n db: Kysely<
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/kysely/KyselyEventRepository.ts"],"sourcesContent":["import { type Kysely, type RawBuilder, sql } from \"kysely\";\nimport type { EventRepository } from \"../../ports/EventRepository.ts\";\nimport type { DefaultContext, GenericEvent } from \"../../types.ts\";\nimport type { EventsTable } from \"./types.ts\";\n\nconst jsonb = <T>(value: T): RawBuilder<T> =>\n sql`${JSON.stringify(value)}::jsonb`;\n\nexport const createKyselyEventRepository = <\n Event extends GenericEvent<string, unknown, DefaultContext>,\n DB extends EventsTable = EventsTable,\n>(\n db: Kysely<DB>,\n): EventRepository<Event> => {\n const eventsDb = db as unknown as Kysely<EventsTable>;\n return {\n save: async (event) => {\n await eventsDb\n .insertInto(\"events\")\n .values({\n ...event,\n payload: jsonb(event.payload),\n triggeredByActor: jsonb(event.triggeredByActor),\n context: jsonb(event.context),\n publications: jsonb(event.publications),\n })\n .onConflict((oc) =>\n oc.column(\"id\").doUpdateSet({\n topic: event.topic,\n payload: jsonb(event.payload),\n triggeredByActor: jsonb(event.triggeredByActor),\n context: jsonb(event.context),\n status: event.status,\n flowId: event.flowId,\n causedByEventId: event.causedByEventId,\n occurredAt: event.occurredAt,\n publications: jsonb(event.publications),\n priority: event.priority,\n }),\n )\n .execute();\n },\n\n saveNewEventsBatch: async (events) => {\n if (events.length === 0) return;\n await eventsDb\n .insertInto(\"events\")\n .values(\n events.map((event) => ({\n ...event,\n payload: jsonb(event.payload),\n triggeredByActor: jsonb(event.triggeredByActor),\n context: jsonb(event.context),\n publications: jsonb(event.publications),\n })),\n )\n .execute();\n },\n\n markEventsAsInProcess: async (events) => {\n if (events.length === 0) return;\n const ids = events.map((e) => e.id);\n\n // Lock the rows to prevent concurrent processing\n const lockedRows = await eventsDb\n .selectFrom(\"events\")\n .select(\"id\")\n .where(\"id\", \"in\", ids)\n .forUpdate()\n .skipLocked()\n .execute();\n\n if (lockedRows.length === 0) return;\n const lockedIds = lockedRows.map((r) => r.id);\n\n // Update status to in-process (only for locked rows)\n await eventsDb\n .updateTable(\"events\")\n .set({ status: \"in-process\" })\n .where(\"id\", \"in\", lockedIds)\n .execute();\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAkD;AAKlD,MAAM,QAAQ,CAAI,UAChB,oBAAM,KAAK,UAAU,KAAK,CAAC;AAEtB,MAAM,8BAA8B,CAIzC,OAC2B;AAC3B,QAAM,WAAW;AACjB,SAAO;AAAA,IACL,MAAM,OAAO,UAAU;AACrB,YAAM,SACH,WAAW,QAAQ,EACnB,OAAO;AAAA,QACN,GAAG;AAAA,QACH,SAAS,MAAM,MAAM,OAAO;AAAA,QAC5B,kBAAkB,MAAM,MAAM,gBAAgB;AAAA,QAC9C,SAAS,MAAM,MAAM,OAAO;AAAA,QAC5B,cAAc,MAAM,MAAM,YAAY;AAAA,MACxC,CAAC,EACA;AAAA,QAAW,CAAC,OACX,GAAG,OAAO,IAAI,EAAE,YAAY;AAAA,UAC1B,OAAO,MAAM;AAAA,UACb,SAAS,MAAM,MAAM,OAAO;AAAA,UAC5B,kBAAkB,MAAM,MAAM,gBAAgB;AAAA,UAC9C,SAAS,MAAM,MAAM,OAAO;AAAA,UAC5B,QAAQ,MAAM;AAAA,UACd,QAAQ,MAAM;AAAA,UACd,iBAAiB,MAAM;AAAA,UACvB,YAAY,MAAM;AAAA,UAClB,cAAc,MAAM,MAAM,YAAY;AAAA,UACtC,UAAU,MAAM;AAAA,QAClB,CAAC;AAAA,MACH,EACC,QAAQ;AAAA,IACb;AAAA,IAEA,oBAAoB,OAAO,WAAW;AACpC,UAAI,OAAO,WAAW,EAAG;AACzB,YAAM,SACH,WAAW,QAAQ,EACnB;AAAA,QACC,OAAO,IAAI,CAAC,WAAW;AAAA,UACrB,GAAG;AAAA,UACH,SAAS,MAAM,MAAM,OAAO;AAAA,UAC5B,kBAAkB,MAAM,MAAM,gBAAgB;AAAA,UAC9C,SAAS,MAAM,MAAM,OAAO;AAAA,UAC5B,cAAc,MAAM,MAAM,YAAY;AAAA,QACxC,EAAE;AAAA,MACJ,EACC,QAAQ;AAAA,IACb;AAAA,IAEA,uBAAuB,OAAO,WAAW;AACvC,UAAI,OAAO,WAAW,EAAG;AACzB,YAAM,MAAM,OAAO,IAAI,CAAC,MAAM,EAAE,EAAE;AAGlC,YAAM,aAAa,MAAM,SACtB,WAAW,QAAQ,EACnB,OAAO,IAAI,EACX,MAAM,MAAM,MAAM,GAAG,EACrB,UAAU,EACV,WAAW,EACX,QAAQ;AAEX,UAAI,WAAW,WAAW,EAAG;AAC7B,YAAM,YAAY,WAAW,IAAI,CAAC,MAAM,EAAE,EAAE;AAG5C,YAAM,SACH,YAAY,QAAQ,EACpB,IAAI,EAAE,QAAQ,aAAa,CAAC,EAC5B,MAAM,MAAM,MAAM,SAAS,EAC3B,QAAQ;AAAA,IACb;AAAA,EACF;AACF;","names":[]}
|
|
@@ -3,6 +3,6 @@ import { EventRepository } from '../../ports/EventRepository.cjs';
|
|
|
3
3
|
import { GenericEvent, DefaultContext } from '../../types.cjs';
|
|
4
4
|
import { EventsTable } from './types.cjs';
|
|
5
5
|
|
|
6
|
-
declare const createKyselyEventRepository: <Event extends GenericEvent<string, unknown, DefaultContext
|
|
6
|
+
declare const createKyselyEventRepository: <Event extends GenericEvent<string, unknown, DefaultContext>, DB extends EventsTable = EventsTable>(db: Kysely<DB>) => EventRepository<Event>;
|
|
7
7
|
|
|
8
8
|
export { createKyselyEventRepository };
|
|
@@ -3,6 +3,6 @@ import { EventRepository } from '../../ports/EventRepository.js';
|
|
|
3
3
|
import { GenericEvent, DefaultContext } from '../../types.js';
|
|
4
4
|
import { EventsTable } from './types.js';
|
|
5
5
|
|
|
6
|
-
declare const createKyselyEventRepository: <Event extends GenericEvent<string, unknown, DefaultContext
|
|
6
|
+
declare const createKyselyEventRepository: <Event extends GenericEvent<string, unknown, DefaultContext>, DB extends EventsTable = EventsTable>(db: Kysely<DB>) => EventRepository<Event>;
|
|
7
7
|
|
|
8
8
|
export { createKyselyEventRepository };
|
|
@@ -1,49 +1,52 @@
|
|
|
1
1
|
import { sql } from "kysely";
|
|
2
2
|
const jsonb = (value) => sql`${JSON.stringify(value)}::jsonb`;
|
|
3
|
-
const createKyselyEventRepository = (db) =>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
triggeredByActor: jsonb(event.triggeredByActor),
|
|
9
|
-
context: jsonb(event.context),
|
|
10
|
-
publications: jsonb(event.publications)
|
|
11
|
-
}).onConflict(
|
|
12
|
-
(oc) => oc.column("id").doUpdateSet({
|
|
13
|
-
topic: event.topic,
|
|
14
|
-
payload: jsonb(event.payload),
|
|
15
|
-
triggeredByActor: jsonb(event.triggeredByActor),
|
|
16
|
-
context: jsonb(event.context),
|
|
17
|
-
status: event.status,
|
|
18
|
-
flowId: event.flowId,
|
|
19
|
-
causedByEventId: event.causedByEventId,
|
|
20
|
-
occurredAt: event.occurredAt,
|
|
21
|
-
publications: jsonb(event.publications),
|
|
22
|
-
priority: event.priority
|
|
23
|
-
})
|
|
24
|
-
).execute();
|
|
25
|
-
},
|
|
26
|
-
saveNewEventsBatch: async (events) => {
|
|
27
|
-
if (events.length === 0) return;
|
|
28
|
-
await db.insertInto("events").values(
|
|
29
|
-
events.map((event) => ({
|
|
3
|
+
const createKyselyEventRepository = (db) => {
|
|
4
|
+
const eventsDb = db;
|
|
5
|
+
return {
|
|
6
|
+
save: async (event) => {
|
|
7
|
+
await eventsDb.insertInto("events").values({
|
|
30
8
|
...event,
|
|
31
9
|
payload: jsonb(event.payload),
|
|
32
10
|
triggeredByActor: jsonb(event.triggeredByActor),
|
|
33
11
|
context: jsonb(event.context),
|
|
34
12
|
publications: jsonb(event.publications)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
13
|
+
}).onConflict(
|
|
14
|
+
(oc) => oc.column("id").doUpdateSet({
|
|
15
|
+
topic: event.topic,
|
|
16
|
+
payload: jsonb(event.payload),
|
|
17
|
+
triggeredByActor: jsonb(event.triggeredByActor),
|
|
18
|
+
context: jsonb(event.context),
|
|
19
|
+
status: event.status,
|
|
20
|
+
flowId: event.flowId,
|
|
21
|
+
causedByEventId: event.causedByEventId,
|
|
22
|
+
occurredAt: event.occurredAt,
|
|
23
|
+
publications: jsonb(event.publications),
|
|
24
|
+
priority: event.priority
|
|
25
|
+
})
|
|
26
|
+
).execute();
|
|
27
|
+
},
|
|
28
|
+
saveNewEventsBatch: async (events) => {
|
|
29
|
+
if (events.length === 0) return;
|
|
30
|
+
await eventsDb.insertInto("events").values(
|
|
31
|
+
events.map((event) => ({
|
|
32
|
+
...event,
|
|
33
|
+
payload: jsonb(event.payload),
|
|
34
|
+
triggeredByActor: jsonb(event.triggeredByActor),
|
|
35
|
+
context: jsonb(event.context),
|
|
36
|
+
publications: jsonb(event.publications)
|
|
37
|
+
}))
|
|
38
|
+
).execute();
|
|
39
|
+
},
|
|
40
|
+
markEventsAsInProcess: async (events) => {
|
|
41
|
+
if (events.length === 0) return;
|
|
42
|
+
const ids = events.map((e) => e.id);
|
|
43
|
+
const lockedRows = await eventsDb.selectFrom("events").select("id").where("id", "in", ids).forUpdate().skipLocked().execute();
|
|
44
|
+
if (lockedRows.length === 0) return;
|
|
45
|
+
const lockedIds = lockedRows.map((r) => r.id);
|
|
46
|
+
await eventsDb.updateTable("events").set({ status: "in-process" }).where("id", "in", lockedIds).execute();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
};
|
|
47
50
|
export {
|
|
48
51
|
createKyselyEventRepository
|
|
49
52
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/adapters/kysely/KyselyEventRepository.ts"],"sourcesContent":["import { type Kysely, type RawBuilder, sql } from \"kysely\";\nimport type { EventRepository } from '../../ports/EventRepository.mjs';\nimport type { DefaultContext, GenericEvent } from '../../types.mjs';\nimport type { EventsTable } from './types.mjs';\n\nconst jsonb = <T>(value: T): RawBuilder<T> =>\n sql`${JSON.stringify(value)}::jsonb`;\n\nexport const createKyselyEventRepository = <\n Event extends GenericEvent<string, unknown, DefaultContext>,\n>(\n db: Kysely<
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/kysely/KyselyEventRepository.ts"],"sourcesContent":["import { type Kysely, type RawBuilder, sql } from \"kysely\";\nimport type { EventRepository } from '../../ports/EventRepository.mjs';\nimport type { DefaultContext, GenericEvent } from '../../types.mjs';\nimport type { EventsTable } from './types.mjs';\n\nconst jsonb = <T>(value: T): RawBuilder<T> =>\n sql`${JSON.stringify(value)}::jsonb`;\n\nexport const createKyselyEventRepository = <\n Event extends GenericEvent<string, unknown, DefaultContext>,\n DB extends EventsTable = EventsTable,\n>(\n db: Kysely<DB>,\n): EventRepository<Event> => {\n const eventsDb = db as unknown as Kysely<EventsTable>;\n return {\n save: async (event) => {\n await eventsDb\n .insertInto(\"events\")\n .values({\n ...event,\n payload: jsonb(event.payload),\n triggeredByActor: jsonb(event.triggeredByActor),\n context: jsonb(event.context),\n publications: jsonb(event.publications),\n })\n .onConflict((oc) =>\n oc.column(\"id\").doUpdateSet({\n topic: event.topic,\n payload: jsonb(event.payload),\n triggeredByActor: jsonb(event.triggeredByActor),\n context: jsonb(event.context),\n status: event.status,\n flowId: event.flowId,\n causedByEventId: event.causedByEventId,\n occurredAt: event.occurredAt,\n publications: jsonb(event.publications),\n priority: event.priority,\n }),\n )\n .execute();\n },\n\n saveNewEventsBatch: async (events) => {\n if (events.length === 0) return;\n await eventsDb\n .insertInto(\"events\")\n .values(\n events.map((event) => ({\n ...event,\n payload: jsonb(event.payload),\n triggeredByActor: jsonb(event.triggeredByActor),\n context: jsonb(event.context),\n publications: jsonb(event.publications),\n })),\n )\n .execute();\n },\n\n markEventsAsInProcess: async (events) => {\n if (events.length === 0) return;\n const ids = events.map((e) => e.id);\n\n // Lock the rows to prevent concurrent processing\n const lockedRows = await eventsDb\n .selectFrom(\"events\")\n .select(\"id\")\n .where(\"id\", \"in\", ids)\n .forUpdate()\n .skipLocked()\n .execute();\n\n if (lockedRows.length === 0) return;\n const lockedIds = lockedRows.map((r) => r.id);\n\n // Update status to in-process (only for locked rows)\n await eventsDb\n .updateTable(\"events\")\n .set({ status: \"in-process\" })\n .where(\"id\", \"in\", lockedIds)\n .execute();\n },\n };\n};\n"],"mappings":"AAAA,SAAuC,WAAW;AAKlD,MAAM,QAAQ,CAAI,UAChB,MAAM,KAAK,UAAU,KAAK,CAAC;AAEtB,MAAM,8BAA8B,CAIzC,OAC2B;AAC3B,QAAM,WAAW;AACjB,SAAO;AAAA,IACL,MAAM,OAAO,UAAU;AACrB,YAAM,SACH,WAAW,QAAQ,EACnB,OAAO;AAAA,QACN,GAAG;AAAA,QACH,SAAS,MAAM,MAAM,OAAO;AAAA,QAC5B,kBAAkB,MAAM,MAAM,gBAAgB;AAAA,QAC9C,SAAS,MAAM,MAAM,OAAO;AAAA,QAC5B,cAAc,MAAM,MAAM,YAAY;AAAA,MACxC,CAAC,EACA;AAAA,QAAW,CAAC,OACX,GAAG,OAAO,IAAI,EAAE,YAAY;AAAA,UAC1B,OAAO,MAAM;AAAA,UACb,SAAS,MAAM,MAAM,OAAO;AAAA,UAC5B,kBAAkB,MAAM,MAAM,gBAAgB;AAAA,UAC9C,SAAS,MAAM,MAAM,OAAO;AAAA,UAC5B,QAAQ,MAAM;AAAA,UACd,QAAQ,MAAM;AAAA,UACd,iBAAiB,MAAM;AAAA,UACvB,YAAY,MAAM;AAAA,UAClB,cAAc,MAAM,MAAM,YAAY;AAAA,UACtC,UAAU,MAAM;AAAA,QAClB,CAAC;AAAA,MACH,EACC,QAAQ;AAAA,IACb;AAAA,IAEA,oBAAoB,OAAO,WAAW;AACpC,UAAI,OAAO,WAAW,EAAG;AACzB,YAAM,SACH,WAAW,QAAQ,EACnB;AAAA,QACC,OAAO,IAAI,CAAC,WAAW;AAAA,UACrB,GAAG;AAAA,UACH,SAAS,MAAM,MAAM,OAAO;AAAA,UAC5B,kBAAkB,MAAM,MAAM,gBAAgB;AAAA,UAC9C,SAAS,MAAM,MAAM,OAAO;AAAA,UAC5B,cAAc,MAAM,MAAM,YAAY;AAAA,QACxC,EAAE;AAAA,MACJ,EACC,QAAQ;AAAA,IACb;AAAA,IAEA,uBAAuB,OAAO,WAAW;AACvC,UAAI,OAAO,WAAW,EAAG;AACzB,YAAM,MAAM,OAAO,IAAI,CAAC,MAAM,EAAE,EAAE;AAGlC,YAAM,aAAa,MAAM,SACtB,WAAW,QAAQ,EACnB,OAAO,IAAI,EACX,MAAM,MAAM,MAAM,GAAG,EACrB,UAAU,EACV,WAAW,EACX,QAAQ;AAEX,UAAI,WAAW,WAAW,EAAG;AAC7B,YAAM,YAAY,WAAW,IAAI,CAAC,MAAM,EAAE,EAAE;AAG5C,YAAM,SACH,YAAY,QAAQ,EACpB,IAAI,EAAE,QAAQ,aAAa,CAAC,EAC5B,MAAM,MAAM,MAAM,SAAS,EAC3B,QAAQ;AAAA,IACb;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "The purpose of this repository is to make it easy to setup event driven architecture using outbox pattern",
|
|
4
4
|
"module": "src/index.ts",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"version": "0.6.
|
|
6
|
+
"version": "0.6.1",
|
|
7
7
|
"main": "./dist/index.mjs",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"kysely": "^0.28.2",
|
|
43
43
|
"lefthook": "^1.13.6",
|
|
44
44
|
"pg": "^8.16.3",
|
|
45
|
-
"semantic-release": "^
|
|
45
|
+
"semantic-release": "^25.0.3",
|
|
46
46
|
"tsup": "^8.5.0",
|
|
47
47
|
"typescript": "^5.9.3"
|
|
48
48
|
},
|
|
@@ -6,40 +6,44 @@ import type { EventsTable } from "./types.ts";
|
|
|
6
6
|
|
|
7
7
|
export const createKyselyEventQueries = <
|
|
8
8
|
Event extends GenericEvent<string, unknown, DefaultContext>,
|
|
9
|
+
DB extends EventsTable = EventsTable,
|
|
9
10
|
>(
|
|
10
|
-
db: Kysely<
|
|
11
|
-
): EventQueries<Event> =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
db: Kysely<DB>,
|
|
12
|
+
): EventQueries<Event> => {
|
|
13
|
+
const eventsDb = db as unknown as Kysely<EventsTable>;
|
|
14
|
+
return {
|
|
15
|
+
getEvents: async ({ filters, limit }) => {
|
|
16
|
+
let query = eventsDb
|
|
17
|
+
.selectFrom("events")
|
|
18
|
+
.selectAll()
|
|
19
|
+
.where("status", "in", filters.statuses)
|
|
20
|
+
.limit(limit);
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
if (filters.context) {
|
|
23
|
+
for (const [key, value] of Object.entries(filters.context)) {
|
|
24
|
+
query = query.where(sql<SqlBool>`context->>${key} = ${value}`);
|
|
25
|
+
}
|
|
22
26
|
}
|
|
23
|
-
}
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
if (filters.occurredAt?.from) {
|
|
29
|
+
query = query.where("occurredAt", ">=", filters.occurredAt.from);
|
|
30
|
+
}
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
if (filters.occurredAt?.to) {
|
|
33
|
+
query = query.where("occurredAt", "<=", filters.occurredAt.to);
|
|
34
|
+
}
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
36
|
+
const rows = await query.execute();
|
|
37
|
+
return rows.map(
|
|
38
|
+
(row: EventsTable["events"]) =>
|
|
39
|
+
({
|
|
40
|
+
...row,
|
|
41
|
+
context: row.context ?? undefined,
|
|
42
|
+
flowId: row.flowId ?? undefined,
|
|
43
|
+
causedByEventId: row.causedByEventId ?? undefined,
|
|
44
|
+
priority: row.priority ?? undefined,
|
|
45
|
+
}) as Event,
|
|
46
|
+
);
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
};
|
|
@@ -8,73 +8,77 @@ const jsonb = <T>(value: T): RawBuilder<T> =>
|
|
|
8
8
|
|
|
9
9
|
export const createKyselyEventRepository = <
|
|
10
10
|
Event extends GenericEvent<string, unknown, DefaultContext>,
|
|
11
|
+
DB extends EventsTable = EventsTable,
|
|
11
12
|
>(
|
|
12
|
-
db: Kysely<
|
|
13
|
-
): EventRepository<Event> =>
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
triggeredByActor: jsonb(event.triggeredByActor),
|
|
21
|
-
context: jsonb(event.context),
|
|
22
|
-
publications: jsonb(event.publications),
|
|
23
|
-
})
|
|
24
|
-
.onConflict((oc) =>
|
|
25
|
-
oc.column("id").doUpdateSet({
|
|
26
|
-
topic: event.topic,
|
|
27
|
-
payload: jsonb(event.payload),
|
|
28
|
-
triggeredByActor: jsonb(event.triggeredByActor),
|
|
29
|
-
context: jsonb(event.context),
|
|
30
|
-
status: event.status,
|
|
31
|
-
flowId: event.flowId,
|
|
32
|
-
causedByEventId: event.causedByEventId,
|
|
33
|
-
occurredAt: event.occurredAt,
|
|
34
|
-
publications: jsonb(event.publications),
|
|
35
|
-
priority: event.priority,
|
|
36
|
-
}),
|
|
37
|
-
)
|
|
38
|
-
.execute();
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
saveNewEventsBatch: async (events) => {
|
|
42
|
-
if (events.length === 0) return;
|
|
43
|
-
await db
|
|
44
|
-
.insertInto("events")
|
|
45
|
-
.values(
|
|
46
|
-
events.map((event) => ({
|
|
13
|
+
db: Kysely<DB>,
|
|
14
|
+
): EventRepository<Event> => {
|
|
15
|
+
const eventsDb = db as unknown as Kysely<EventsTable>;
|
|
16
|
+
return {
|
|
17
|
+
save: async (event) => {
|
|
18
|
+
await eventsDb
|
|
19
|
+
.insertInto("events")
|
|
20
|
+
.values({
|
|
47
21
|
...event,
|
|
48
22
|
payload: jsonb(event.payload),
|
|
49
23
|
triggeredByActor: jsonb(event.triggeredByActor),
|
|
50
24
|
context: jsonb(event.context),
|
|
51
25
|
publications: jsonb(event.publications),
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
26
|
+
})
|
|
27
|
+
.onConflict((oc) =>
|
|
28
|
+
oc.column("id").doUpdateSet({
|
|
29
|
+
topic: event.topic,
|
|
30
|
+
payload: jsonb(event.payload),
|
|
31
|
+
triggeredByActor: jsonb(event.triggeredByActor),
|
|
32
|
+
context: jsonb(event.context),
|
|
33
|
+
status: event.status,
|
|
34
|
+
flowId: event.flowId,
|
|
35
|
+
causedByEventId: event.causedByEventId,
|
|
36
|
+
occurredAt: event.occurredAt,
|
|
37
|
+
publications: jsonb(event.publications),
|
|
38
|
+
priority: event.priority,
|
|
39
|
+
}),
|
|
40
|
+
)
|
|
41
|
+
.execute();
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
saveNewEventsBatch: async (events) => {
|
|
45
|
+
if (events.length === 0) return;
|
|
46
|
+
await eventsDb
|
|
47
|
+
.insertInto("events")
|
|
48
|
+
.values(
|
|
49
|
+
events.map((event) => ({
|
|
50
|
+
...event,
|
|
51
|
+
payload: jsonb(event.payload),
|
|
52
|
+
triggeredByActor: jsonb(event.triggeredByActor),
|
|
53
|
+
context: jsonb(event.context),
|
|
54
|
+
publications: jsonb(event.publications),
|
|
55
|
+
})),
|
|
56
|
+
)
|
|
57
|
+
.execute();
|
|
58
|
+
},
|
|
56
59
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
markEventsAsInProcess: async (events) => {
|
|
61
|
+
if (events.length === 0) return;
|
|
62
|
+
const ids = events.map((e) => e.id);
|
|
60
63
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
// Lock the rows to prevent concurrent processing
|
|
65
|
+
const lockedRows = await eventsDb
|
|
66
|
+
.selectFrom("events")
|
|
67
|
+
.select("id")
|
|
68
|
+
.where("id", "in", ids)
|
|
69
|
+
.forUpdate()
|
|
70
|
+
.skipLocked()
|
|
71
|
+
.execute();
|
|
69
72
|
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
if (lockedRows.length === 0) return;
|
|
74
|
+
const lockedIds = lockedRows.map((r) => r.id);
|
|
72
75
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
76
|
+
// Update status to in-process (only for locked rows)
|
|
77
|
+
await eventsDb
|
|
78
|
+
.updateTable("events")
|
|
79
|
+
.set({ status: "in-process" })
|
|
80
|
+
.where("id", "in", lockedIds)
|
|
81
|
+
.execute();
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
};
|