@workglow/postgres 0.2.28

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 (41) hide show
  1. package/dist/job-queue/PostgresQueueStorage.d.ts +155 -0
  2. package/dist/job-queue/PostgresQueueStorage.d.ts.map +1 -0
  3. package/dist/job-queue/PostgresRateLimiterStorage.d.ts +57 -0
  4. package/dist/job-queue/PostgresRateLimiterStorage.d.ts.map +1 -0
  5. package/dist/job-queue/browser.d.ts +7 -0
  6. package/dist/job-queue/browser.d.ts.map +1 -0
  7. package/dist/job-queue/browser.js +732 -0
  8. package/dist/job-queue/browser.js.map +11 -0
  9. package/dist/job-queue/bun.d.ts +7 -0
  10. package/dist/job-queue/bun.d.ts.map +1 -0
  11. package/dist/job-queue/common.d.ts +8 -0
  12. package/dist/job-queue/common.d.ts.map +1 -0
  13. package/dist/job-queue/node.d.ts +7 -0
  14. package/dist/job-queue/node.d.ts.map +1 -0
  15. package/dist/job-queue/node.js +732 -0
  16. package/dist/job-queue/node.js.map +11 -0
  17. package/dist/storage/PostgresKvStorage.d.ts +27 -0
  18. package/dist/storage/PostgresKvStorage.d.ts.map +1 -0
  19. package/dist/storage/PostgresTabularStorage.d.ts +194 -0
  20. package/dist/storage/PostgresTabularStorage.d.ts.map +1 -0
  21. package/dist/storage/PostgresVectorStorage.d.ts +39 -0
  22. package/dist/storage/PostgresVectorStorage.d.ts.map +1 -0
  23. package/dist/storage/_postgres/browser.d.ts +32 -0
  24. package/dist/storage/_postgres/browser.d.ts.map +1 -0
  25. package/dist/storage/_postgres/node-bun.d.ts +26 -0
  26. package/dist/storage/_postgres/node-bun.d.ts.map +1 -0
  27. package/dist/storage/_postgres/pglite-pool.d.ts +21 -0
  28. package/dist/storage/_postgres/pglite-pool.d.ts.map +1 -0
  29. package/dist/storage/browser.d.ts +10 -0
  30. package/dist/storage/browser.d.ts.map +1 -0
  31. package/dist/storage/browser.js +951 -0
  32. package/dist/storage/browser.js.map +14 -0
  33. package/dist/storage/bun.d.ts +7 -0
  34. package/dist/storage/bun.d.ts.map +1 -0
  35. package/dist/storage/common.d.ts +10 -0
  36. package/dist/storage/common.d.ts.map +1 -0
  37. package/dist/storage/node.d.ts +7 -0
  38. package/dist/storage/node.d.ts.map +1 -0
  39. package/dist/storage/node.js +842 -0
  40. package/dist/storage/node.js.map +13 -0
  41. package/package.json +78 -0
@@ -0,0 +1,155 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Steven Roussey <sroussey@gmail.com>
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import type { Pool } from "@workglow/postgres/storage";
7
+ import { JobStatus } from "@workglow/job-queue";
8
+ import type { IQueueStorage, JobStorageFormat, PrefixColumn, QueueChangePayload, QueueStorageOptions, QueueSubscribeOptions } from "@workglow/job-queue";
9
+ export declare const POSTGRES_QUEUE_STORAGE: import("@workglow/util").ServiceToken<IQueueStorage<any, any>>;
10
+ /**
11
+ * PostgreSQL implementation of a job queue.
12
+ * Provides storage and retrieval for job execution states using PostgreSQL.
13
+ */
14
+ export declare class PostgresQueueStorage<Input, Output> implements IQueueStorage<Input, Output> {
15
+ protected readonly db: Pool;
16
+ protected readonly queueName: string;
17
+ readonly scope: "cluster";
18
+ /** The prefix column definitions */
19
+ protected readonly prefixes: readonly PrefixColumn[];
20
+ /** The prefix values for filtering */
21
+ protected readonly prefixValues: Readonly<Record<string, string | number>>;
22
+ /** The table name for the job queue */
23
+ protected readonly tableName: string;
24
+ constructor(db: Pool, queueName: string, options?: QueueStorageOptions);
25
+ /**
26
+ * Gets the SQL column type for a prefix column
27
+ */
28
+ private getPrefixColumnType;
29
+ /**
30
+ * Builds the prefix columns SQL for CREATE TABLE
31
+ */
32
+ private buildPrefixColumnsSql;
33
+ /**
34
+ * Builds prefix column names for use in queries
35
+ */
36
+ private getPrefixColumnNames;
37
+ /**
38
+ * Builds WHERE clause conditions for prefix filtering
39
+ * @param startParam - The starting parameter number for parameterized queries
40
+ * @returns Object with conditions string and parameter values
41
+ */
42
+ private buildPrefixWhereClause;
43
+ /**
44
+ * Gets prefix values as an array in column order
45
+ */
46
+ private getPrefixParamValues;
47
+ setupDatabase(): Promise<void>;
48
+ /**
49
+ * Channel name for this storage's LISTEN/NOTIFY. Mirrors the trigger's
50
+ * computation so subscriber and notifier agree.
51
+ */
52
+ private notifyChannelName;
53
+ /**
54
+ * Adds a new job to the queue.
55
+ * @param job - The job to add
56
+ * @returns The ID of the added job
57
+ */
58
+ add(job: JobStorageFormat<Input, Output>): Promise<unknown>;
59
+ /**
60
+ * Retrieves a job by its ID.
61
+ * @param id - The ID of the job to retrieve
62
+ * @returns The job if found, undefined otherwise
63
+ */
64
+ get(id: unknown): Promise<JobStorageFormat<Input, Output> | undefined>;
65
+ /**
66
+ * Retrieves a slice of jobs from the queue.
67
+ * @param num - Maximum number of jobs to return
68
+ * @returns An array of jobs
69
+ */
70
+ peek(status?: JobStatus, num?: number): Promise<Array<JobStorageFormat<Input, Output>>>;
71
+ /**
72
+ * Retrieves the next available job that is ready to be processed.
73
+ * @param workerId - Worker ID to associate with the job (required)
74
+ * @returns The next job or undefined if no job is available
75
+ */
76
+ next(workerId: string): Promise<JobStorageFormat<Input, Output> | undefined>;
77
+ /**
78
+ * Retrieves the number of jobs in the queue with a specific status.
79
+ * @param status - The status of the jobs to count
80
+ * @returns The count of jobs with the specified status
81
+ */
82
+ size(status?: "PENDING"): Promise<number>;
83
+ /**
84
+ * Marks a job as complete with its output or error.
85
+ * Enhanced error handling:
86
+ * - For a retryable error, increments run_attempts and updates run_after.
87
+ * - Marks a job as FAILED immediately for permanent or generic errors.
88
+ */
89
+ complete(jobDetails: JobStorageFormat<Input, Output>): Promise<void>;
90
+ /**
91
+ * Clears all jobs from the queue.
92
+ */
93
+ deleteAll(): Promise<void>;
94
+ /**
95
+ * Looks up cached output for a given input
96
+ * Uses input fingerprinting for efficient matching
97
+ * @returns The cached output or null if not found
98
+ */
99
+ outputForInput(input: Input): Promise<Output | null>;
100
+ /**
101
+ * Aborts a job by setting its status to "ABORTING".
102
+ * This method will signal the corresponding AbortController so that
103
+ * the job's execute() method (if it supports an AbortSignal parameter)
104
+ * can clean up and exit.
105
+ */
106
+ abort(jobId: unknown): Promise<void>;
107
+ /**
108
+ * Releases a claimed job back to PENDING without incrementing run_attempts.
109
+ * @param jobId - The id of the claimed job to release.
110
+ */
111
+ release(jobId: unknown): Promise<void>;
112
+ /**
113
+ * Retrieves all jobs for a given job run ID.
114
+ * @param job_run_id - The ID of the job run to retrieve
115
+ * @returns An array of jobs
116
+ */
117
+ getByRunId(job_run_id: string): Promise<Array<JobStorageFormat<Input, Output>>>;
118
+ /**
119
+ * Implements the abstract saveProgress method from JobQueue
120
+ */
121
+ saveProgress(jobId: unknown, progress: number, message: string, details: Record<string, any>): Promise<void>;
122
+ /**
123
+ * Deletes a job by its ID
124
+ */
125
+ delete(jobId: unknown): Promise<void>;
126
+ /**
127
+ * Delete jobs with a specific status older than a cutoff date
128
+ * @param status - Status of jobs to delete
129
+ * @param olderThanMs - Delete jobs completed more than this many milliseconds ago
130
+ */
131
+ deleteJobsByStatusAndAge(status: JobStatus, olderThanMs: number): Promise<void>;
132
+ /**
133
+ * Subscribe to INSERT/UPDATE notifications via PostgreSQL LISTEN/NOTIFY.
134
+ * Replaces 100ms-poll fallback for cluster Postgres deployments — workers
135
+ * wake within network-latency of an actual change rather than on a timer.
136
+ *
137
+ * Acquires a dedicated client from the pool (LISTEN occupies a connection
138
+ * for its lifetime). On unsubscribe, runs UNLISTEN and releases the client.
139
+ * On connection loss, attempts to reconnect with bounded backoff and
140
+ * re-LISTEN. After every successful (re)connect — including the initial
141
+ * one — emits a synthetic `{ type: "RESYNC" }` event so subscribers can
142
+ * re-poll state and pick up any rows inserted during the disconnect window
143
+ * (or between subscribe and the first NOTIFY landing).
144
+ *
145
+ * Throws synchronously when the underlying pool lacks `connect()`
146
+ * (single-connection wrappers like PGLite) so the caller's try/catch can
147
+ * fall back to polling. JobQueueServer.start does this and logs at debug.
148
+ *
149
+ * `options.prefixFilter` follows {@link QueueSubscribeOptions.prefixFilter}:
150
+ * `undefined` means "use the storage instance's configured prefixValues",
151
+ * `{}` means "receive all changes regardless of prefix".
152
+ */
153
+ subscribeToChanges(callback: (change: QueueChangePayload<Input, Output>) => void, options?: QueueSubscribeOptions): () => void;
154
+ }
155
+ //# sourceMappingURL=PostgresQueueStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostgresQueueStorage.d.ts","sourceRoot":"","sources":["../../src/job-queue/PostgresQueueStorage.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,4BAA4B,CAAC;AAEvD,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,KAAK,EACV,aAAa,EACb,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,EAClB,mBAAmB,EACnB,qBAAqB,EACtB,MAAM,qBAAqB,CAAC;AAE7B,eAAO,MAAM,sBAAsB,gEAElC,CAAC;AAgBF;;;GAGG;AACH,qBAAa,oBAAoB,CAAC,KAAK,EAAE,MAAM,CAAE,YAAW,aAAa,CAAC,KAAK,EAAE,MAAM,CAAC;IAUpF,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,IAAI;IAC3B,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM;IAVtC,SAAgB,KAAK,EAAG,SAAS,CAAU;IAC3C,oCAAoC;IACpC,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,SAAS,YAAY,EAAE,CAAC;IACrD,sCAAsC;IACtC,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC;IAC3E,uCAAuC;IACvC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAErC,YACqB,EAAE,EAAE,IAAI,EACR,S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GAAG,IAAI,CAAC,CAYhE;IAED;;;;;OAKG;IACU,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAShD;IAED;;;OAGG;IACU,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAalD;IAED;;;;OAIG;IACU,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,gBAAgB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,CAS3F;IAED;;OAEG;IACU,YAAY,CACvB,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC3B,OAAO,CAAC,IAAI,CAAC,CAkBf;IAED;;OAEG;IACU,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAMjD;IAED;;;;OAIG;IACU,wBAAwB,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAW3F;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACI,kBAAkB,CACvB,QAAQ,EAAE,CAAC,MAAM,EAAE,kBAAkB,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,EAC7D,OAAO,CAAC,EAAE,qBAAqB,GAC9B,MAAM,IAAI,CA+JZ;CACF"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Steven Roussey <sroussey@gmail.com>
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import type { Pool } from "@workglow/postgres/storage";
7
+ import type { PrefixColumn } from "@workglow/job-queue";
8
+ import type { IRateLimiterStorage, RateLimiterStorageOptions, RateLimiterStorageScope } from "@workglow/job-queue";
9
+ export declare const POSTGRES_RATE_LIMITER_STORAGE: import("@workglow/util").ServiceToken<IRateLimiterStorage>;
10
+ /**
11
+ * PostgreSQL implementation of rate limiter storage.
12
+ * Manages execution records and next available times for rate limiting.
13
+ */
14
+ export declare class PostgresRateLimiterStorage implements IRateLimiterStorage {
15
+ protected readonly db: Pool;
16
+ readonly scope: RateLimiterStorageScope;
17
+ /** The prefix column definitions */
18
+ protected readonly prefixes: readonly PrefixColumn[];
19
+ /** The prefix values for filtering */
20
+ protected readonly prefixValues: Readonly<Record<string, string | number>>;
21
+ /** The table name for execution tracking */
22
+ protected readonly executionTableName: string;
23
+ /** The table name for next available times */
24
+ protected readonly nextAvailableTableName: string;
25
+ constructor(db: Pool, options?: RateLimiterStorageOptions);
26
+ /**
27
+ * Gets the SQL column type for a prefix column.
28
+ */
29
+ private getPrefixColumnType;
30
+ /**
31
+ * Builds the prefix columns SQL for CREATE TABLE.
32
+ */
33
+ private buildPrefixColumnsSql;
34
+ /**
35
+ * Builds prefix column names for use in queries.
36
+ */
37
+ private getPrefixColumnNames;
38
+ /**
39
+ * Builds WHERE clause conditions for prefix filtering.
40
+ * @param startParam - The starting parameter number for parameterized queries
41
+ */
42
+ private buildPrefixWhereClause;
43
+ /**
44
+ * Gets prefix values as an array in column order.
45
+ */
46
+ private getPrefixParamValues;
47
+ setupDatabase(): Promise<void>;
48
+ tryReserveExecution(queueName: string, maxExecutions: number, windowMs: number): Promise<unknown | null>;
49
+ releaseExecution(queueName: string, token: unknown): Promise<void>;
50
+ recordExecution(queueName: string): Promise<void>;
51
+ getExecutionCount(queueName: string, windowStartTime: string): Promise<number>;
52
+ getOldestExecutionAtOffset(queueName: string, offset: number): Promise<string | undefined>;
53
+ getNextAvailableTime(queueName: string): Promise<string | undefined>;
54
+ setNextAvailableTime(queueName: string, nextAvailableAt: string): Promise<void>;
55
+ clear(queueName: string): Promise<void>;
56
+ }
57
+ //# sourceMappingURL=PostgresRateLimiterStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostgresRateLimiterStorage.d.ts","sourceRoot":"","sources":["../../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AAO,GAAG,IAAI,CAAC,CAsIzB;IAEY,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAU9E;IAEY,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAkB7D;IAEY,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAa1F;IAEY,0BAA0B,CACrC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAiB7B;IAEY,oBAAoB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAehF;IAEY,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAwB3F;IAEY,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAWnD;CACF"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Steven Roussey <sroussey@gmail.com>
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ export * from "./common";
7
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../src/job-queue/browser.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,UAAU,CAAC"}