@arvo-tools/postgres 1.0.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 (37) hide show
  1. package/dist/index.d.ts +5 -0
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +10 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/memory/factory/defaults.d.ts +5 -0
  6. package/dist/memory/factory/defaults.d.ts.map +1 -0
  7. package/dist/memory/factory/defaults.js +9 -0
  8. package/dist/memory/factory/defaults.js.map +1 -0
  9. package/dist/memory/factory/index.d.ts +117 -0
  10. package/dist/memory/factory/index.d.ts.map +1 -0
  11. package/dist/memory/factory/index.js +202 -0
  12. package/dist/memory/factory/index.js.map +1 -0
  13. package/dist/memory/factory/type.d.ts +29 -0
  14. package/dist/memory/factory/type.d.ts.map +1 -0
  15. package/dist/memory/factory/type.js +3 -0
  16. package/dist/memory/factory/type.js.map +1 -0
  17. package/dist/memory/types.d.ts +49 -0
  18. package/dist/memory/types.d.ts.map +1 -0
  19. package/dist/memory/types.js +3 -0
  20. package/dist/memory/types.js.map +1 -0
  21. package/dist/memory/v1/helper.d.ts +9 -0
  22. package/dist/memory/v1/helper.d.ts.map +1 -0
  23. package/dist/memory/v1/helper.js +84 -0
  24. package/dist/memory/v1/helper.js.map +1 -0
  25. package/dist/memory/v1/index.d.ts +88 -0
  26. package/dist/memory/v1/index.d.ts.map +1 -0
  27. package/dist/memory/v1/index.js +667 -0
  28. package/dist/memory/v1/index.js.map +1 -0
  29. package/dist/memory/v1/schema.d.ts +334 -0
  30. package/dist/memory/v1/schema.d.ts.map +1 -0
  31. package/dist/memory/v1/schema.js +159 -0
  32. package/dist/memory/v1/schema.js.map +1 -0
  33. package/dist/memory/v1/types.d.ts +28 -0
  34. package/dist/memory/v1/types.d.ts.map +1 -0
  35. package/dist/memory/v1/types.js +3 -0
  36. package/dist/memory/v1/types.js.map +1 -0
  37. package/package.json +54 -0
@@ -0,0 +1,88 @@
1
+ import { type Span } from '@opentelemetry/api';
2
+ import { type IMachineMemory, type MachineMemoryMetadata } from 'arvo-event-handler';
3
+ import type { PostgressMachineMemoryV1Param } from './types';
4
+ type VersionedData<T extends Record<string, unknown>> = T & {
5
+ __postgres_version_counter_data_$$__: number;
6
+ };
7
+ /**
8
+ * PostgreSQL-backed implementation of IMachineMemory for distributed workflow state management.
9
+ *
10
+ * This class provides persistent storage for workflow instances using PostgreSQL with the following features:
11
+ * - Optimistic locking via version tracking to prevent concurrent state modifications
12
+ * - Distributed lock management with TTL-based expiration to prevent deadlocks
13
+ * - Hierarchical workflow tracking for parent-child relationship queries
14
+ * - Optional cleanup of completed workflows
15
+ * - Optional OpenTelemetry instrumentation for observability
16
+ *
17
+ * The implementation uses three database tables:
18
+ * - State table: Stores workflow data, versions, execution status, and metadata
19
+ * - Lock table: Manages distributed locks with automatic expiration
20
+ * - Hierarchy table: Tracks workflow parent-child relationships and root subjects
21
+ */
22
+ export declare class PostgressMachineMemoryV1<T extends Record<string, unknown>> implements IMachineMemory<T> {
23
+ private readonly tables;
24
+ private readonly lockConfig;
25
+ private readonly enableCleanup;
26
+ private readonly enableOtel;
27
+ private readonly pool;
28
+ constructor(param: PostgressMachineMemoryV1Param);
29
+ close(): Promise<void>;
30
+ private delay;
31
+ validateTableStructure(): Promise<void>;
32
+ otel<R>({ name, fn }: {
33
+ name: string;
34
+ fn: (span?: Span) => Promise<R>;
35
+ }): Promise<R>;
36
+ read(id: string): Promise<T | null>;
37
+ write(id: string, data: T, prevData: VersionedData<T> | null, { source, initiator }: MachineMemoryMetadata): Promise<void>;
38
+ lock(id: string): Promise<boolean>;
39
+ unlock(id: string): Promise<boolean>;
40
+ cleanup(id: string): Promise<void>;
41
+ /**
42
+ * Retrieves all child workflow subjects belonging to a specific root workflow.
43
+ *
44
+ * This method queries the hierarchy table to find all workflows that are descendants
45
+ * of the specified root workflow. The root subject itself is excluded from the results.
46
+ *
47
+ * @param rootSubject - The subject identifier of the root workflow
48
+ * @returns Array of subject identifiers for all child workflows (excluding the root itself)
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * const subject = 'some_string'
53
+ * const children = await memory.getSubjectsByRoot(subject);
54
+ * console.log(`Found ${children.length} child workflows subjects`);
55
+ * ```
56
+ */
57
+ getSubjectsByRoot(rootSubject: string): Promise<string[]>;
58
+ /**
59
+ * Retrieves the root workflow subject for a given workflow instance.
60
+ *
61
+ * This method queries the hierarchy table to find the root workflow subject
62
+ * associated with the specified workflow. Every workflow in the hierarchy has
63
+ * a root_subject field that points to the top-level workflow that initiated
64
+ * the entire workflow tree.
65
+ *
66
+ * @param subject - The subject identifier of the workflow to look up
67
+ * @returns The root subject identifier, or null if the subject is not found in the hierarchy table
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const subject = 'some_string'
72
+ * const root = await memory.getRootSubject(subject);
73
+ * if (root) {
74
+ * console.log(`Root workflow subject: ${root}`);
75
+ * if (root === subject) {
76
+ * console.log('This is a root workflow');
77
+ * } else {
78
+ * console.log('This is a child workflow');
79
+ * }
80
+ * } else {
81
+ * console.log('Workflow not found in hierarchy');
82
+ * }
83
+ * ```
84
+ */
85
+ getRootSubject(subject: string): Promise<string | null>;
86
+ }
87
+ export {};
88
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/memory/v1/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,EAAkB,MAAM,oBAAoB,CAAC;AAE/D,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,qBAAqB,EAAgB,MAAM,oBAAoB,CAAC;AAGnG,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,SAAS,CAAC;AAE7D,KAAK,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG;IAC1D,oCAAoC,EAAE,MAAM,CAAC;CAC9C,CAAC;AAEF;;;;;;;;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