@accelerate/messaging 0.0.0-stage → 0.1.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.
Files changed (48) hide show
  1. package/README.md +25 -2
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.js +1 -0
  4. package/dist/modules/events-core/dist/index.d.ts +14 -0
  5. package/dist/modules/events-core/dist/index.js +3 -0
  6. package/dist/modules/events-core/dist/journal.d.ts +57 -0
  7. package/dist/modules/events-core/dist/journal.js +124 -0
  8. package/dist/modules/events-memory/dist/index.d.ts +3 -0
  9. package/dist/modules/events-memory/dist/index.js +7 -0
  10. package/dist/modules/events-memory/dist/journal.d.ts +37 -0
  11. package/dist/modules/events-memory/dist/journal.js +42 -0
  12. package/dist/modules/events-postgres/dist/index.d.ts +8 -0
  13. package/dist/modules/events-postgres/dist/index.js +4 -0
  14. package/dist/modules/events-postgres/dist/journal.d.ts +21 -0
  15. package/dist/modules/events-postgres/dist/journal.js +72 -0
  16. package/dist/modules/events-postgres/dist/schema.d.ts +1 -0
  17. package/dist/modules/events-postgres/dist/schema.js +1 -0
  18. package/dist/modules/events-redis/dist/index.d.ts +15 -0
  19. package/dist/modules/events-redis/dist/index.js +15 -0
  20. package/dist/modules/events-redis/dist/journal.d.ts +25 -0
  21. package/dist/modules/events-redis/dist/journal.js +45 -0
  22. package/dist/modules/jobs-core/dist/index.d.ts +21 -0
  23. package/dist/modules/jobs-core/dist/index.js +2 -0
  24. package/dist/modules/jobs-postgres/dist/index.d.ts +20 -0
  25. package/dist/modules/jobs-postgres/dist/index.js +123 -0
  26. package/dist/modules/jobs-postgres/dist/testing.d.ts +23 -0
  27. package/dist/modules/jobs-postgres/dist/testing.js +62 -0
  28. package/dist/modules/outbox-core/dist/index.d.ts +32 -0
  29. package/dist/modules/outbox-core/dist/index.js +34 -0
  30. package/dist/modules/outbox-postgres/dist/index.d.ts +13 -0
  31. package/dist/modules/outbox-postgres/dist/index.js +104 -0
  32. package/dist/modules/scheduler-core/dist/durable.d.ts +52 -0
  33. package/dist/modules/scheduler-core/dist/durable.js +153 -0
  34. package/dist/modules/scheduler-core/dist/index.d.ts +18 -0
  35. package/dist/modules/scheduler-core/dist/index.js +29 -0
  36. package/dist/modules/scheduler-postgres/dist/index.d.ts +19 -0
  37. package/dist/modules/scheduler-postgres/dist/index.js +97 -0
  38. package/dist/modules/workflows-core/dist/definition.d.ts +58 -0
  39. package/dist/modules/workflows-core/dist/definition.js +105 -0
  40. package/dist/modules/workflows-core/dist/durable.d.ts +60 -0
  41. package/dist/modules/workflows-core/dist/durable.js +300 -0
  42. package/dist/modules/workflows-core/dist/index.d.ts +19 -0
  43. package/dist/modules/workflows-core/dist/index.js +13 -0
  44. package/dist/modules/workflows-memory/dist/index.d.ts +2 -0
  45. package/dist/modules/workflows-memory/dist/index.js +32 -0
  46. package/dist/modules/workflows-postgres/dist/index.d.ts +5 -0
  47. package/dist/modules/workflows-postgres/dist/index.js +49 -0
  48. package/package.json +116 -4
@@ -0,0 +1,32 @@
1
+ export function createMemoryWorkflowStore(clock = Date.now) {
2
+ const runs = new Map();
3
+ return {
4
+ async create(instance) {
5
+ if (runs.has(instance.id))
6
+ return false;
7
+ runs.set(instance.id, {
8
+ state: structuredClone(instance),
9
+ events: [{ at: clock(), type: "started" }],
10
+ });
11
+ return true;
12
+ },
13
+ async get(id) {
14
+ return structuredClone(runs.get(id)?.state);
15
+ },
16
+ async update(id, fn) {
17
+ const run = runs.get(id);
18
+ if (!run)
19
+ throw Error("Unknown workflow instance");
20
+ const state = structuredClone(run.state), events = [];
21
+ const result = fn(state, clock(), (event) => events.push(event));
22
+ run.state = structuredClone(state);
23
+ run.events.push(...structuredClone(events));
24
+ return result;
25
+ },
26
+ async history(id, after = 0, limit = 100) {
27
+ return structuredClone((runs.get(id)?.events ?? [])
28
+ .slice(after, after + limit)
29
+ .map((event, index) => ({ ...event, cursor: after + index + 1 })));
30
+ },
31
+ };
32
+ }
@@ -0,0 +1,5 @@
1
+ import type { Database } from "@accelerate/data/core";
2
+ import type { Migration } from "@accelerate/data/migrations";
3
+ import type { WorkflowStore } from "../../workflows-core/dist/index.js";
4
+ export declare const workflowMigration: Migration;
5
+ export declare function createPostgresWorkflowStore(db: Database): WorkflowStore;
@@ -0,0 +1,49 @@
1
+ export const workflowMigration = {
2
+ id: "accelerate-workflows/1",
3
+ statements: [
4
+ "CREATE TABLE accelerate_workflows (id text PRIMARY KEY, state jsonb NOT NULL)",
5
+ "CREATE TABLE accelerate_workflow_history (instance_id text NOT NULL REFERENCES accelerate_workflows(id), cursor bigint GENERATED ALWAYS AS IDENTITY, event jsonb NOT NULL, PRIMARY KEY(instance_id,cursor))",
6
+ ],
7
+ };
8
+ async function time(tx) {
9
+ const { rows } = await tx.query("SELECT floor(extract(epoch from clock_timestamp())*1000)::bigint AS ms");
10
+ return Number(rows[0].ms);
11
+ }
12
+ async function append(tx, id, events) {
13
+ if (events.length)
14
+ await tx.query("INSERT INTO accelerate_workflow_history(instance_id,event) SELECT $1,entry FROM jsonb_array_elements($2::jsonb) WITH ORDINALITY AS e(entry,pos) ORDER BY pos", [id, JSON.stringify(events)]);
15
+ }
16
+ export function createPostgresWorkflowStore(db) {
17
+ return {
18
+ create(instance) {
19
+ return db.transaction(async (tx) => {
20
+ const r = await tx.query("INSERT INTO accelerate_workflows VALUES ($1,$2::jsonb) ON CONFLICT DO NOTHING", [instance.id, JSON.stringify(instance)]);
21
+ if (!r.rowCount)
22
+ return false;
23
+ await append(tx, instance.id, [
24
+ { at: await time(tx), type: "started" },
25
+ ]);
26
+ return true;
27
+ });
28
+ },
29
+ async get(id) {
30
+ return (await db.query("SELECT state FROM accelerate_workflows WHERE id=$1", [id])).rows[0]?.state;
31
+ },
32
+ update(id, fn) {
33
+ return db.transaction(async (tx) => {
34
+ const { rows } = await tx.query("SELECT state FROM accelerate_workflows WHERE id=$1 FOR UPDATE", [id]);
35
+ if (!rows.length)
36
+ throw Error("Unknown workflow instance");
37
+ const state = rows[0].state, events = [];
38
+ const result = fn(state, await time(tx), (e) => events.push(e));
39
+ await tx.query("UPDATE accelerate_workflows SET state=$2::jsonb WHERE id=$1 AND state IS DISTINCT FROM $2::jsonb", [id, JSON.stringify(state)]);
40
+ await append(tx, id, events);
41
+ return result;
42
+ }, { isolationLevel: "read committed" });
43
+ },
44
+ async history(id, after = 0, limit = 100) {
45
+ const { rows } = await db.query("SELECT cursor,event FROM accelerate_workflow_history WHERE instance_id=$1 AND cursor>$2 ORDER BY cursor LIMIT $3", [id, after, limit]);
46
+ return rows.map((r) => ({ ...r.event, cursor: Number(r.cursor) }));
47
+ },
48
+ };
49
+ }
package/package.json CHANGED
@@ -1,6 +1,118 @@
1
1
  {
2
2
  "name": "@accelerate/messaging",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Accelerate — Events, jobs, workflows, scheduling and transactional outbox.",
5
+ "keywords": [
6
+ "accelerate",
7
+ "typescript",
8
+ "messaging",
9
+ "events",
10
+ "jobs",
11
+ "workflows"
12
+ ],
13
+ "type": "module",
14
+ "sideEffects": false,
15
+ "main": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "./events": {
23
+ "types": "./dist/modules/events-core/dist/index.d.ts",
24
+ "default": "./dist/modules/events-core/dist/index.js"
25
+ },
26
+ "./events/memory": {
27
+ "types": "./dist/modules/events-memory/dist/index.d.ts",
28
+ "default": "./dist/modules/events-memory/dist/index.js"
29
+ },
30
+ "./events/postgres": {
31
+ "types": "./dist/modules/events-postgres/dist/index.d.ts",
32
+ "default": "./dist/modules/events-postgres/dist/index.js"
33
+ },
34
+ "./events/redis": {
35
+ "types": "./dist/modules/events-redis/dist/index.d.ts",
36
+ "default": "./dist/modules/events-redis/dist/index.js"
37
+ },
38
+ "./jobs": {
39
+ "types": "./dist/modules/jobs-core/dist/index.d.ts",
40
+ "default": "./dist/modules/jobs-core/dist/index.js"
41
+ },
42
+ "./jobs/postgres": {
43
+ "types": "./dist/modules/jobs-postgres/dist/index.d.ts",
44
+ "default": "./dist/modules/jobs-postgres/dist/index.js"
45
+ },
46
+ "./jobs/postgres/testing": {
47
+ "types": "./dist/modules/jobs-postgres/dist/testing.d.ts",
48
+ "default": "./dist/modules/jobs-postgres/dist/testing.js"
49
+ },
50
+ "./outbox": {
51
+ "types": "./dist/modules/outbox-core/dist/index.d.ts",
52
+ "default": "./dist/modules/outbox-core/dist/index.js"
53
+ },
54
+ "./outbox/postgres": {
55
+ "types": "./dist/modules/outbox-postgres/dist/index.d.ts",
56
+ "default": "./dist/modules/outbox-postgres/dist/index.js"
57
+ },
58
+ "./scheduler": {
59
+ "types": "./dist/modules/scheduler-core/dist/index.d.ts",
60
+ "default": "./dist/modules/scheduler-core/dist/index.js"
61
+ },
62
+ "./scheduler/postgres": {
63
+ "types": "./dist/modules/scheduler-postgres/dist/index.d.ts",
64
+ "default": "./dist/modules/scheduler-postgres/dist/index.js"
65
+ },
66
+ "./workflows": {
67
+ "types": "./dist/modules/workflows-core/dist/index.d.ts",
68
+ "default": "./dist/modules/workflows-core/dist/index.js"
69
+ },
70
+ "./workflows/memory": {
71
+ "types": "./dist/modules/workflows-memory/dist/index.d.ts",
72
+ "default": "./dist/modules/workflows-memory/dist/index.js"
73
+ },
74
+ "./workflows/postgres": {
75
+ "types": "./dist/modules/workflows-postgres/dist/index.d.ts",
76
+ "default": "./dist/modules/workflows-postgres/dist/index.js"
77
+ }
78
+ },
79
+ "files": [
80
+ "dist"
81
+ ],
82
+ "scripts": {
83
+ "build": "node ../../tools/registry/consolidation.mjs build messaging",
84
+ "typecheck": "tsc --noEmit --skipLibCheck --module NodeNext --moduleResolution NodeNext dist/index.d.ts"
85
+ },
86
+ "repository": {
87
+ "type": "git",
88
+ "url": "git+https://github.com/archubbuck/accelerate.git",
89
+ "directory": "packages/messaging"
90
+ },
91
+ "homepage": "https://github.com/archubbuck/accelerate/blob/main/packages/messaging/README.md",
92
+ "bugs": "https://github.com/archubbuck/accelerate/issues",
93
+ "publishConfig": {
94
+ "access": "public",
95
+ "registry": "https://registry.npmjs.org/",
96
+ "tag": "latest"
97
+ },
98
+ "dependencies": {
99
+ "@accelerate/data": "0.1.0",
100
+ "cronosjs": "1.7.1",
101
+ "@js-temporal/polyfill": "^0.5.1"
102
+ },
103
+ "devDependencies": {
104
+ "@accelerate/events-core": "0.1.1",
105
+ "@accelerate/events-memory": "0.1.1",
106
+ "@accelerate/events-postgres": "0.1.2",
107
+ "@accelerate/events-redis": "0.1.1",
108
+ "@accelerate/jobs-core": "0.1.0",
109
+ "@accelerate/jobs-postgres": "0.1.1",
110
+ "@accelerate/outbox-core": "0.1.1",
111
+ "@accelerate/outbox-postgres": "0.1.1",
112
+ "@accelerate/scheduler-core": "0.2.0",
113
+ "@accelerate/scheduler-postgres": "0.1.0",
114
+ "@accelerate/workflows-core": "0.1.2",
115
+ "@accelerate/workflows-memory": "0.1.0",
116
+ "@accelerate/workflows-postgres": "0.1.0"
117
+ }
118
+ }