openworkflow 0.4.1 → 0.6.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 (220) hide show
  1. package/README.md +43 -345
  2. package/dist/backend-test/backend.testsuite.d.ts +20 -0
  3. package/dist/backend-test/backend.testsuite.d.ts.map +1 -0
  4. package/dist/{core → backend-test}/backend.testsuite.js +191 -59
  5. package/dist/backend-test/index.d.ts +2 -0
  6. package/dist/backend-test/index.d.ts.map +1 -0
  7. package/dist/backend-test/index.js +1 -0
  8. package/dist/{core/backend.d.ts → backend.d.ts} +7 -5
  9. package/dist/backend.d.ts.map +1 -0
  10. package/dist/{core/backend.js → backend.js} +0 -1
  11. package/dist/backend.testsuite.d.ts +20 -0
  12. package/dist/backend.testsuite.d.ts.map +1 -0
  13. package/dist/{core/backend-test-suite.js → backend.testsuite.js} +301 -171
  14. package/dist/bin/openworkflow.d.ts +3 -0
  15. package/dist/bin/openworkflow.d.ts.map +1 -0
  16. package/dist/bin/openworkflow.js +43 -0
  17. package/dist/chaos.test.d.ts +2 -0
  18. package/dist/chaos.test.d.ts.map +1 -0
  19. package/dist/chaos.test.js +88 -0
  20. package/dist/client.d.ts +141 -0
  21. package/dist/client.d.ts.map +1 -0
  22. package/dist/{sdk/sdk.js → client.js} +43 -71
  23. package/dist/client.test.d.ts +2 -0
  24. package/dist/client.test.d.ts.map +1 -0
  25. package/dist/{sdk/sdk.test.js → client.test.js} +130 -14
  26. package/dist/core/duration.d.ts +4 -2
  27. package/dist/core/duration.d.ts.map +1 -1
  28. package/dist/core/duration.js +3 -2
  29. package/dist/core/duration.test.js +0 -1
  30. package/dist/core/error.d.ts +14 -0
  31. package/dist/core/error.d.ts.map +1 -0
  32. package/dist/core/error.js +17 -0
  33. package/dist/core/error.test.d.ts +2 -0
  34. package/dist/core/error.test.d.ts.map +1 -0
  35. package/dist/core/error.test.js +60 -0
  36. package/dist/core/json.js +0 -1
  37. package/dist/core/result.d.ts +14 -4
  38. package/dist/core/result.d.ts.map +1 -1
  39. package/dist/core/result.js +10 -1
  40. package/dist/core/result.test.js +2 -2
  41. package/dist/core/retry.d.ts +0 -9
  42. package/dist/core/retry.d.ts.map +1 -1
  43. package/dist/core/retry.js +0 -15
  44. package/dist/core/schema.js +0 -1
  45. package/dist/core/step.d.ts +1 -32
  46. package/dist/core/step.d.ts.map +1 -1
  47. package/dist/core/step.js +0 -36
  48. package/dist/core/step.test.js +1 -75
  49. package/dist/core/workflow.d.ts +2 -47
  50. package/dist/core/workflow.d.ts.map +1 -1
  51. package/dist/core/workflow.js +0 -45
  52. package/dist/core/workflow.test.js +1 -104
  53. package/dist/driver.d.ts +116 -0
  54. package/dist/driver.d.ts.map +1 -0
  55. package/dist/driver.js +1 -0
  56. package/dist/{execution/execution.d.ts → execution.d.ts} +4 -26
  57. package/dist/execution.d.ts.map +1 -0
  58. package/dist/{execution/execution.js → execution.js} +4 -5
  59. package/dist/execution.test.d.ts.map +1 -0
  60. package/dist/{execution/execution.test.js → execution.test.js} +4 -5
  61. package/dist/factory.d.ts +74 -0
  62. package/dist/factory.d.ts.map +1 -0
  63. package/dist/factory.js +72 -0
  64. package/dist/index.d.ts +6 -9
  65. package/dist/index.d.ts.map +1 -1
  66. package/dist/index.js +4 -5
  67. package/dist/internal.d.ts +7 -0
  68. package/dist/internal.d.ts.map +1 -0
  69. package/dist/internal.js +2 -0
  70. package/dist/node-sqlite/backend.d.ts +52 -0
  71. package/dist/node-sqlite/backend.d.ts.map +1 -0
  72. package/dist/node-sqlite/backend.js +673 -0
  73. package/dist/node-sqlite/index.d.ts +11 -0
  74. package/dist/node-sqlite/index.d.ts.map +1 -0
  75. package/dist/node-sqlite/index.js +7 -0
  76. package/dist/node-sqlite/sqlite.d.ts +60 -0
  77. package/dist/node-sqlite/sqlite.d.ts.map +1 -0
  78. package/dist/{backend-sqlite → node-sqlite}/sqlite.js +20 -3
  79. package/dist/postgres/backend.d.ts +44 -0
  80. package/dist/postgres/backend.d.ts.map +1 -0
  81. package/dist/postgres/backend.js +534 -0
  82. package/dist/postgres/backend.test.d.ts +2 -0
  83. package/dist/postgres/backend.test.d.ts.map +1 -0
  84. package/dist/postgres/backend.test.js +19 -0
  85. package/dist/postgres/driver.d.ts +81 -0
  86. package/dist/postgres/driver.d.ts.map +1 -0
  87. package/dist/postgres/driver.js +63 -0
  88. package/dist/postgres/index.d.ts +11 -0
  89. package/dist/postgres/index.d.ts.map +1 -0
  90. package/dist/postgres/index.js +7 -0
  91. package/dist/postgres/internal.d.ts +2 -0
  92. package/dist/postgres/internal.d.ts.map +1 -0
  93. package/dist/postgres/internal.js +1 -0
  94. package/dist/postgres/postgres.d.ts +42 -0
  95. package/dist/postgres/postgres.d.ts.map +1 -0
  96. package/dist/postgres/postgres.js +233 -0
  97. package/dist/postgres/postgres.test.d.ts +2 -0
  98. package/dist/postgres/postgres.test.d.ts.map +1 -0
  99. package/dist/postgres/postgres.test.js +45 -0
  100. package/dist/postgres/scripts/db-migrate.d.ts +2 -0
  101. package/dist/postgres/scripts/db-migrate.d.ts.map +1 -0
  102. package/dist/postgres/scripts/db-migrate.js +4 -0
  103. package/dist/postgres/scripts/db-reset.d.ts +2 -0
  104. package/dist/postgres/scripts/db-reset.d.ts.map +1 -0
  105. package/dist/postgres/scripts/db-reset.js +5 -0
  106. package/dist/postgres/scripts/squawk.d.ts +2 -0
  107. package/dist/postgres/scripts/squawk.d.ts.map +1 -0
  108. package/dist/postgres/scripts/squawk.js +16 -0
  109. package/dist/postgres/vitest.global-setup.d.ts +3 -0
  110. package/dist/postgres/vitest.global-setup.d.ts.map +1 -0
  111. package/dist/postgres/vitest.global-setup.js +7 -0
  112. package/dist/postgres.d.ts +2 -0
  113. package/dist/postgres.d.ts.map +1 -0
  114. package/dist/postgres.js +1 -0
  115. package/dist/registry.d.ts +27 -0
  116. package/dist/registry.d.ts.map +1 -0
  117. package/dist/registry.js +48 -0
  118. package/dist/registry.test.d.ts +2 -0
  119. package/dist/registry.test.d.ts.map +1 -0
  120. package/dist/registry.test.js +109 -0
  121. package/dist/{backend-sqlite → sqlite}/backend.d.ts +8 -4
  122. package/dist/sqlite/backend.d.ts.map +1 -0
  123. package/dist/{backend-sqlite → sqlite}/backend.js +35 -9
  124. package/dist/sqlite/backend.test.d.ts +2 -0
  125. package/dist/sqlite/backend.test.d.ts.map +1 -0
  126. package/dist/sqlite/backend.test.js +50 -0
  127. package/dist/sqlite/driver.d.ts +79 -0
  128. package/dist/sqlite/driver.d.ts.map +1 -0
  129. package/dist/sqlite/driver.js +62 -0
  130. package/dist/sqlite/index.d.ts +13 -0
  131. package/dist/sqlite/index.d.ts.map +1 -0
  132. package/dist/sqlite/index.js +11 -0
  133. package/dist/sqlite/internal.d.ts +2 -0
  134. package/dist/sqlite/internal.d.ts.map +1 -0
  135. package/dist/sqlite/internal.js +1 -0
  136. package/dist/{backend-sqlite → sqlite}/sqlite.d.ts +18 -2
  137. package/dist/sqlite/sqlite.d.ts.map +1 -0
  138. package/dist/sqlite/sqlite.js +246 -0
  139. package/dist/sqlite/sqlite.test.d.ts +2 -0
  140. package/dist/sqlite/sqlite.test.d.ts.map +1 -0
  141. package/dist/sqlite/sqlite.test.js +171 -0
  142. package/dist/sqlite.d.ts +2 -0
  143. package/dist/sqlite.d.ts.map +1 -0
  144. package/dist/sqlite.js +1 -0
  145. package/dist/tsconfig.tsbuildinfo +1 -1
  146. package/dist/{worker/worker.d.ts → worker.d.ts} +11 -4
  147. package/dist/worker.d.ts.map +1 -0
  148. package/dist/{worker/worker.js → worker.js} +20 -11
  149. package/dist/{worker/worker.test.d.ts.map → worker.test.d.ts.map} +1 -1
  150. package/dist/{worker/worker.test.js → worker.test.js} +136 -22
  151. package/dist/workflow.d.ts +60 -0
  152. package/dist/workflow.d.ts.map +1 -0
  153. package/dist/workflow.js +48 -0
  154. package/dist/workflow.test.d.ts +2 -0
  155. package/dist/workflow.test.d.ts.map +1 -0
  156. package/dist/workflow.test.js +84 -0
  157. package/package.json +28 -4
  158. package/dist/backend-sqlite/backend.d.ts.map +0 -1
  159. package/dist/backend-sqlite/backend.js.map +0 -1
  160. package/dist/backend-sqlite/index.d.ts +0 -2
  161. package/dist/backend-sqlite/index.d.ts.map +0 -1
  162. package/dist/backend-sqlite/index.js +0 -2
  163. package/dist/backend-sqlite/index.js.map +0 -1
  164. package/dist/backend-sqlite/sqlite.d.ts.map +0 -1
  165. package/dist/backend-sqlite/sqlite.js.map +0 -1
  166. package/dist/config/config.d.ts +0 -102
  167. package/dist/config/config.d.ts.map +0 -1
  168. package/dist/config/config.js +0 -29
  169. package/dist/config/config.js.map +0 -1
  170. package/dist/config/index.d.ts +0 -3
  171. package/dist/config/index.d.ts.map +0 -1
  172. package/dist/config/index.js +0 -2
  173. package/dist/config/index.js.map +0 -1
  174. package/dist/config.d.ts +0 -28
  175. package/dist/config.d.ts.map +0 -1
  176. package/dist/config.js +0 -41
  177. package/dist/config.js.map +0 -1
  178. package/dist/core/backend-test-suite.d.ts +0 -22
  179. package/dist/core/backend-test-suite.d.ts.map +0 -1
  180. package/dist/core/backend-test-suite.js.map +0 -1
  181. package/dist/core/backend.d.ts.map +0 -1
  182. package/dist/core/backend.js.map +0 -1
  183. package/dist/core/backend.testsuite.d.ts +0 -21
  184. package/dist/core/backend.testsuite.d.ts.map +0 -1
  185. package/dist/core/backend.testsuite.js.map +0 -1
  186. package/dist/core/duration.js.map +0 -1
  187. package/dist/core/duration.test.js.map +0 -1
  188. package/dist/core/json.js.map +0 -1
  189. package/dist/core/result.js.map +0 -1
  190. package/dist/core/result.test.js.map +0 -1
  191. package/dist/core/retry.js.map +0 -1
  192. package/dist/core/retry.test.d.ts +0 -2
  193. package/dist/core/retry.test.d.ts.map +0 -1
  194. package/dist/core/retry.test.js +0 -36
  195. package/dist/core/retry.test.js.map +0 -1
  196. package/dist/core/schema.js.map +0 -1
  197. package/dist/core/step.js.map +0 -1
  198. package/dist/core/step.test.js.map +0 -1
  199. package/dist/core/workflow.js.map +0 -1
  200. package/dist/core/workflow.test.js.map +0 -1
  201. package/dist/execution/execution.d.ts.map +0 -1
  202. package/dist/execution/execution.js.map +0 -1
  203. package/dist/execution/execution.test.d.ts.map +0 -1
  204. package/dist/execution/execution.test.js.map +0 -1
  205. package/dist/global.d.ts +0 -62
  206. package/dist/global.d.ts.map +0 -1
  207. package/dist/global.js +0 -78
  208. package/dist/global.js.map +0 -1
  209. package/dist/index.js.map +0 -1
  210. package/dist/sdk/sdk.d.ts +0 -182
  211. package/dist/sdk/sdk.d.ts.map +0 -1
  212. package/dist/sdk/sdk.js.map +0 -1
  213. package/dist/sdk/sdk.test.d.ts +0 -2
  214. package/dist/sdk/sdk.test.d.ts.map +0 -1
  215. package/dist/sdk/sdk.test.js.map +0 -1
  216. package/dist/worker/worker.d.ts.map +0 -1
  217. package/dist/worker/worker.js.map +0 -1
  218. package/dist/worker/worker.test.js.map +0 -1
  219. /package/dist/{execution/execution.test.d.ts → execution.test.d.ts} +0 -0
  220. /package/dist/{worker/worker.test.d.ts → worker.test.d.ts} +0 -0
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Creates a PostgreSQL driver for OpenWorkflow.
3
+ *
4
+ * This function follows the Drizzle ORM pattern and supports multiple
5
+ * calling conventions:
6
+ * @param config - Connection string or configuration object
7
+ * @returns A PostgresDriver instance
8
+ * @example
9
+ * ```ts
10
+ * // With connection string
11
+ * postgres(process.env.DATABASE_URL)
12
+ *
13
+ * // With config object
14
+ * postgres({
15
+ * connection: process.env.DATABASE_URL,
16
+ * namespace: 'my-app',
17
+ * })
18
+ *
19
+ * // With existing client
20
+ * postgres({ client: existingPostgresClient })
21
+ * ```
22
+ */
23
+ export function postgres(config) {
24
+ // Normalize config
25
+ const normalizedConfig = typeof config === "string" ? { connection: config } : config;
26
+ // Store the client reference (will be set on createBackend)
27
+ let clientRef = normalizedConfig.client;
28
+ return {
29
+ dialect: "postgres",
30
+ get $client() {
31
+ return clientRef;
32
+ },
33
+ async createBackend() {
34
+ // Dynamically import BackendPostgres to avoid requiring the postgres
35
+ // package as a direct dependency of the openworkflow package
36
+ const { BackendPostgres } = await import("@openworkflow/backend-postgres");
37
+ // Determine the connection URL
38
+ const connectionUrl = normalizedConfig.connection;
39
+ if (!connectionUrl && !normalizedConfig.client) {
40
+ throw new Error("PostgreSQL driver requires either a connection string or an existing client. " +
41
+ "Provide `connection` URL or `client` in the config.");
42
+ }
43
+ // If we have an existing client, we need to handle it differently
44
+ // For now, we use the BackendPostgres.connect which creates its own client
45
+ if (!connectionUrl) {
46
+ throw new Error("Using an existing postgres.js client is not yet supported. " +
47
+ "Please provide a connection string via the `connection` option.");
48
+ }
49
+ const options = {
50
+ runMigrations: normalizedConfig.runMigrations ?? true,
51
+ };
52
+ if (normalizedConfig.namespace) {
53
+ options.namespaceId = normalizedConfig.namespace;
54
+ }
55
+ const backend = await BackendPostgres.connect(connectionUrl, options);
56
+ // Store client reference for $client getter
57
+ // Note: BackendPostgres doesn't expose the client, so this remains undefined
58
+ // In the future, we could extend BackendPostgres to expose the client
59
+ clientRef = undefined;
60
+ return backend;
61
+ },
62
+ };
63
+ }
@@ -0,0 +1,11 @@
1
+ import { BackendPostgres } from "./backend.js";
2
+ import type { Postgres } from "./postgres.js";
3
+ /**
4
+ * Create a Postgres-backed OpenWorkflow backend using postgres.js.
5
+ * @param client - postgres.js client or connection URL
6
+ * @param options - Backend options
7
+ * @returns A connected backend instance
8
+ */
9
+ export declare function openworkflow(client: Postgres, options?: Parameters<typeof BackendPostgres.fromClient>[1]): Promise<BackendPostgres>;
10
+ export declare function openworkflow(url: string, options?: Parameters<typeof BackendPostgres.connect>[1]): Promise<BackendPostgres>;
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../postgres/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;;GAKG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,QAAQ,EAChB,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GACzD,OAAO,CAAC,eAAe,CAAC,CAAC;AAC5B,wBAAsB,YAAY,CAChC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GACtD,OAAO,CAAC,eAAe,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { BackendPostgres } from "./backend.js";
2
+ export async function openworkflow(clientOrUrl, options) {
3
+ if (typeof clientOrUrl === "string") {
4
+ return BackendPostgres.connect(clientOrUrl, options);
5
+ }
6
+ return BackendPostgres.fromClient(clientOrUrl, options);
7
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./postgres.js";
2
+ //# sourceMappingURL=internal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../../postgres/internal.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./postgres.js";
@@ -0,0 +1,42 @@
1
+ import postgres from "postgres";
2
+ export declare const DEFAULT_POSTGRES_URL = "postgresql://postgres:postgres@localhost:5432/postgres";
3
+ export declare const DEFAULT_SCHEMA = "openworkflow";
4
+ export type Postgres = ReturnType<typeof postgres>;
5
+ export type PostgresOptions = Parameters<typeof postgres>[1];
6
+ /**
7
+ * newPostgres creates a new Postgres client.
8
+ * @param url - Database connection URL
9
+ * @param options - Postgres client options
10
+ * @returns A Postgres client
11
+ */
12
+ export declare function newPostgres(url: string, options?: PostgresOptions): postgres.Sql<{}>;
13
+ /**
14
+ * newPostgresMaxOne creates a new Postgres client with a maximum pool size of
15
+ * one, which is useful for migrations.
16
+ * @param url - Database connection URL
17
+ * @param options - Postgres client options
18
+ * @returns A Postgres client
19
+ */
20
+ export declare function newPostgresMaxOne(url: string, options?: PostgresOptions): postgres.Sql<{}>;
21
+ /**
22
+ * migrations returns the list of migration SQL statements.
23
+ * @param schema - Schema name
24
+ * @returns Migration SQL statements
25
+ */
26
+ export declare function migrations(schema: string): string[];
27
+ /**
28
+ * migrate applies pending migrations to the database. Does nothing if the
29
+ * database is already up to date.
30
+ * @param pg - Postgres client
31
+ * @param schema - Schema name
32
+ * @returns Promise resolved when migrations complete
33
+ */
34
+ export declare function migrate(pg: Postgres, schema: string): Promise<void>;
35
+ /**
36
+ * dropSchema drops the specified schema from the database.
37
+ * @param pg - Postgres client
38
+ * @param schema - Schema name
39
+ * @returns Promise resolved when the schema is dropped
40
+ */
41
+ export declare function dropSchema(pg: Postgres, schema: string): Promise<void>;
42
+ //# sourceMappingURL=postgres.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../postgres/postgres.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAEhC,eAAO,MAAM,oBAAoB,2DACyB,CAAC;AAO3D,eAAO,MAAM,cAAc,iBAAiB,CAAC;AAE7C,MAAM,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC;AACnD,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AAE7D;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,oBAEjE;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,oBAEvE;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CA+JnD;AAED;;;;;;GAMG;AACH,wBAAsB,OAAO,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,iBAQzD;AAED;;;;;GAKG;AACH,wBAAsB,UAAU,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,iBAE5D"}
@@ -0,0 +1,233 @@
1
+ import postgres from "postgres";
2
+ export const DEFAULT_POSTGRES_URL = "postgresql://postgres:postgres@localhost:5432/postgres";
3
+ // The default schema to use for OpenWorkflow data. This type is more for
4
+ // documentation than for practical use. The only time we allow schema
5
+ // customization is during testing, specifically for testing migrations.
6
+ // Everywhere else uses the "openworkflow" schema directly for prepared
7
+ // statements.
8
+ export const DEFAULT_SCHEMA = "openworkflow";
9
+ /**
10
+ * newPostgres creates a new Postgres client.
11
+ * @param url - Database connection URL
12
+ * @param options - Postgres client options
13
+ * @returns A Postgres client
14
+ */
15
+ export function newPostgres(url, options) {
16
+ return postgres(url, { ...options, transform: postgres.toCamel });
17
+ }
18
+ /**
19
+ * newPostgresMaxOne creates a new Postgres client with a maximum pool size of
20
+ * one, which is useful for migrations.
21
+ * @param url - Database connection URL
22
+ * @param options - Postgres client options
23
+ * @returns A Postgres client
24
+ */
25
+ export function newPostgresMaxOne(url, options) {
26
+ return newPostgres(url, { ...options, max: 1 });
27
+ }
28
+ /**
29
+ * migrations returns the list of migration SQL statements.
30
+ * @param schema - Schema name
31
+ * @returns Migration SQL statements
32
+ */
33
+ export function migrations(schema) {
34
+ return [
35
+ // 0 - init
36
+ `BEGIN;
37
+
38
+ CREATE SCHEMA IF NOT EXISTS ${schema};
39
+
40
+ CREATE TABLE IF NOT EXISTS "${schema}"."openworkflow_migrations" (
41
+ "version" BIGINT NOT NULL PRIMARY KEY
42
+ );
43
+
44
+ INSERT INTO "${schema}"."openworkflow_migrations" ("version")
45
+ VALUES (0)
46
+ ON CONFLICT DO NOTHING;
47
+
48
+ COMMIT;`,
49
+ // 1 - add workflow_runs and step_attempts tables
50
+ `BEGIN;
51
+
52
+ CREATE TABLE IF NOT EXISTS "${schema}"."workflow_runs" (
53
+ "namespace_id" TEXT NOT NULL,
54
+ "id" TEXT NOT NULL,
55
+ --
56
+ "workflow_name" TEXT NOT NULL,
57
+ "version" TEXT,
58
+ "status" TEXT NOT NULL,
59
+ "idempotency_key" TEXT,
60
+ "config" JSONB NOT NULL,
61
+ "context" JSONB,
62
+ "input" JSONB,
63
+ "output" JSONB,
64
+ "error" JSONB,
65
+ "attempts" INTEGER NOT NULL,
66
+ "parent_step_attempt_namespace_id" TEXT,
67
+ "parent_step_attempt_id" TEXT,
68
+ "worker_id" TEXT,
69
+ "available_at" TIMESTAMPTZ,
70
+ "deadline_at" TIMESTAMPTZ,
71
+ "started_at" TIMESTAMPTZ,
72
+ "finished_at" TIMESTAMPTZ,
73
+ "created_at" TIMESTAMPTZ NOT NULL,
74
+ "updated_at" TIMESTAMPTZ NOT NULL,
75
+ PRIMARY KEY ("namespace_id", "id")
76
+ );
77
+
78
+ CREATE TABLE IF NOT EXISTS "${schema}"."step_attempts" (
79
+ "namespace_id" TEXT NOT NULL,
80
+ "id" TEXT NOT NULL,
81
+ --
82
+ "workflow_run_id" TEXT NOT NULL,
83
+ "step_name" TEXT NOT NULL,
84
+ "kind" TEXT NOT NULL,
85
+ "status" TEXT NOT NULL,
86
+ "config" JSONB NOT NULL,
87
+ "context" JSONB,
88
+ "output" JSONB,
89
+ "error" JSONB,
90
+ "child_workflow_run_namespace_id" TEXT,
91
+ "child_workflow_run_id" TEXT,
92
+ "started_at" TIMESTAMPTZ,
93
+ "finished_at" TIMESTAMPTZ,
94
+ "created_at" TIMESTAMPTZ NOT NULL,
95
+ "updated_at" TIMESTAMPTZ NOT NULL,
96
+ PRIMARY KEY ("namespace_id", "id")
97
+ );
98
+
99
+ INSERT INTO "${schema}"."openworkflow_migrations" ("version")
100
+ VALUES (1)
101
+ ON CONFLICT DO NOTHING;
102
+
103
+ COMMIT;`,
104
+ // 2 - foreign keys
105
+ `BEGIN;
106
+
107
+ ALTER TABLE "${schema}"."step_attempts"
108
+ ADD CONSTRAINT "step_attempts_workflow_run_fk"
109
+ FOREIGN KEY ("namespace_id", "workflow_run_id")
110
+ REFERENCES "${schema}"."workflow_runs" ("namespace_id", "id")
111
+ ON DELETE CASCADE
112
+ NOT VALID;
113
+
114
+ ALTER TABLE "${schema}"."workflow_runs"
115
+ ADD CONSTRAINT "workflow_runs_parent_step_attempt_fk"
116
+ FOREIGN KEY ("parent_step_attempt_namespace_id", "parent_step_attempt_id")
117
+ REFERENCES "${schema}"."step_attempts" ("namespace_id", "id")
118
+ ON DELETE SET NULL
119
+ NOT VALID;
120
+
121
+ ALTER TABLE "${schema}"."step_attempts"
122
+ ADD CONSTRAINT "step_attempts_child_workflow_run_fk"
123
+ FOREIGN KEY ("child_workflow_run_namespace_id", "child_workflow_run_id")
124
+ REFERENCES "${schema}"."workflow_runs" ("namespace_id", "id")
125
+ ON DELETE SET NULL
126
+ NOT VALID;
127
+
128
+ INSERT INTO "${schema}"."openworkflow_migrations" ("version")
129
+ VALUES (2)
130
+ ON CONFLICT DO NOTHING;
131
+
132
+ COMMIT;`,
133
+ // 3 - validate foreign keys
134
+ `BEGIN;
135
+
136
+ ALTER TABLE "${schema}"."step_attempts"
137
+ VALIDATE CONSTRAINT "step_attempts_workflow_run_fk";
138
+
139
+ ALTER TABLE "${schema}"."workflow_runs" VALIDATE CONSTRAINT
140
+ "workflow_runs_parent_step_attempt_fk";
141
+
142
+ ALTER TABLE "${schema}"."step_attempts"
143
+ VALIDATE CONSTRAINT "step_attempts_child_workflow_run_fk";
144
+
145
+ INSERT INTO "${schema}"."openworkflow_migrations" ("version")
146
+ VALUES (3)
147
+ ON CONFLICT DO NOTHING;
148
+
149
+ COMMIT;`,
150
+ // 4 - indexes
151
+ `BEGIN;
152
+
153
+ CREATE INDEX IF NOT EXISTS "workflow_runs_status_available_at_created_at_idx"
154
+ ON "${schema}"."workflow_runs" ("namespace_id", "status", "available_at", "created_at");
155
+
156
+ CREATE INDEX IF NOT EXISTS "workflow_runs_workflow_name_idempotency_key_created_at_idx"
157
+ ON "${schema}"."workflow_runs" ("namespace_id", "workflow_name", "idempotency_key", "created_at");
158
+
159
+ CREATE INDEX IF NOT EXISTS "workflow_runs_parent_step_idx"
160
+ ON "${schema}"."workflow_runs" ("parent_step_attempt_namespace_id", "parent_step_attempt_id")
161
+ WHERE parent_step_attempt_namespace_id IS NOT NULL AND parent_step_attempt_id IS NOT NULL;
162
+
163
+ CREATE INDEX IF NOT EXISTS "workflow_runs_created_at_desc_idx"
164
+ ON "${schema}"."workflow_runs" ("namespace_id", "created_at" DESC);
165
+
166
+ CREATE INDEX IF NOT EXISTS "workflow_runs_status_created_at_desc_idx"
167
+ ON "${schema}"."workflow_runs" ("namespace_id", "status", "created_at" DESC);
168
+
169
+ CREATE INDEX IF NOT EXISTS "workflow_runs_workflow_name_status_created_at_desc_idx"
170
+ ON "${schema}"."workflow_runs" ("namespace_id", "workflow_name", "status", "created_at" DESC);
171
+
172
+ CREATE INDEX IF NOT EXISTS "step_attempts_workflow_run_created_at_idx"
173
+ ON "${schema}"."step_attempts" ("namespace_id", "workflow_run_id", "created_at");
174
+
175
+ CREATE INDEX IF NOT EXISTS "step_attempts_workflow_run_step_name_created_at_idx"
176
+ ON "${schema}"."step_attempts" ("namespace_id", "workflow_run_id", "step_name", "created_at");
177
+
178
+ CREATE INDEX IF NOT EXISTS "step_attempts_child_workflow_run_idx"
179
+ ON "${schema}"."step_attempts" ("child_workflow_run_namespace_id", "child_workflow_run_id")
180
+ WHERE child_workflow_run_namespace_id IS NOT NULL AND child_workflow_run_id IS NOT NULL;
181
+
182
+ INSERT INTO "${schema}"."openworkflow_migrations"("version")
183
+ VALUES (4)
184
+ ON CONFLICT DO NOTHING;
185
+
186
+ COMMIT;`,
187
+ ];
188
+ }
189
+ /**
190
+ * migrate applies pending migrations to the database. Does nothing if the
191
+ * database is already up to date.
192
+ * @param pg - Postgres client
193
+ * @param schema - Schema name
194
+ * @returns Promise resolved when migrations complete
195
+ */
196
+ export async function migrate(pg, schema) {
197
+ const currentMigrationVersion = await getCurrentMigrationVersion(pg, schema);
198
+ for (const [i, migrationSql] of migrations(schema).entries()) {
199
+ if (i <= currentMigrationVersion)
200
+ continue; // already applied
201
+ await pg.unsafe(migrationSql);
202
+ }
203
+ }
204
+ /**
205
+ * dropSchema drops the specified schema from the database.
206
+ * @param pg - Postgres client
207
+ * @param schema - Schema name
208
+ * @returns Promise resolved when the schema is dropped
209
+ */
210
+ export async function dropSchema(pg, schema) {
211
+ await pg.unsafe(`DROP SCHEMA IF EXISTS ${schema} CASCADE;`);
212
+ }
213
+ /**
214
+ * getCurrentVersion returns the current migration version of the database.
215
+ * @param pg - Postgres client
216
+ * @param schema - Schema name
217
+ * @returns Current migration version
218
+ */
219
+ async function getCurrentMigrationVersion(pg, schema) {
220
+ // check if migrations table exists
221
+ const existsRes = await pg.unsafe(`
222
+ SELECT EXISTS (
223
+ SELECT 1
224
+ FROM information_schema.tables
225
+ WHERE table_schema = '${schema}'
226
+ AND table_name = 'openworkflow_migrations'
227
+ )`);
228
+ if (!existsRes[0]?.exists)
229
+ return -1;
230
+ // get current version
231
+ const currentVersionRes = await pg.unsafe(`SELECT MAX("version") AS "version" FROM "${schema}"."openworkflow_migrations";`);
232
+ return currentVersionRes[0]?.version ?? -1;
233
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=postgres.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres.test.d.ts","sourceRoot":"","sources":["../../postgres/postgres.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,45 @@
1
+ import { DEFAULT_POSTGRES_URL, DEFAULT_SCHEMA, newPostgresMaxOne, migrations, migrate, dropSchema, } from "./postgres.js";
2
+ import { afterAll, beforeAll, describe, expect, test } from "vitest";
3
+ describe("postgres", () => {
4
+ let pg;
5
+ beforeAll(() => {
6
+ // maxOne since we use SQL-based transactions instead of the postgres
7
+ // driver's built-in transactions
8
+ pg = newPostgresMaxOne(DEFAULT_POSTGRES_URL);
9
+ });
10
+ afterAll(async () => {
11
+ await pg.end();
12
+ });
13
+ describe("migrations()", () => {
14
+ test("returns migrations in 'openworkflow' schema when no schema is specified", () => {
15
+ const migs = migrations(DEFAULT_SCHEMA);
16
+ for (const mig of migs) {
17
+ expect(mig).toContain(`"openworkflow"`);
18
+ }
19
+ });
20
+ test("returns migration in the specified schema when one is specified", () => {
21
+ const schema = "test_custom_schema";
22
+ const migs = migrations(schema);
23
+ for (const mig of migs) {
24
+ expect(mig).toContain(`"${schema}"`);
25
+ expect(mig).not.toContain(`"openworkflow"`);
26
+ }
27
+ });
28
+ });
29
+ describe("migrate()", () => {
30
+ test("runs database migrations idempotently", async () => {
31
+ const schema = "test_migrate_idempotent";
32
+ await dropSchema(pg, schema);
33
+ await migrate(pg, schema);
34
+ await migrate(pg, schema);
35
+ });
36
+ });
37
+ describe("dropSchema()", () => {
38
+ test("drops the schema idempotently", async () => {
39
+ const testSchema = "test_drop_schema_idempotent";
40
+ await migrate(pg, testSchema);
41
+ await dropSchema(pg, testSchema);
42
+ await dropSchema(pg, testSchema);
43
+ });
44
+ });
45
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=db-migrate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db-migrate.d.ts","sourceRoot":"","sources":["../../../postgres/scripts/db-migrate.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import { DEFAULT_POSTGRES_URL, DEFAULT_SCHEMA, newPostgresMaxOne, migrate, } from "../postgres.js";
2
+ const pg = newPostgresMaxOne(DEFAULT_POSTGRES_URL);
3
+ await migrate(pg, DEFAULT_SCHEMA);
4
+ await pg.end();
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=db-reset.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db-reset.d.ts","sourceRoot":"","sources":["../../../postgres/scripts/db-reset.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ import { DEFAULT_POSTGRES_URL, DEFAULT_SCHEMA, newPostgresMaxOne, dropSchema, migrate, } from "../postgres.js";
2
+ const pg = newPostgresMaxOne(DEFAULT_POSTGRES_URL);
3
+ await dropSchema(pg, DEFAULT_SCHEMA);
4
+ await migrate(pg, DEFAULT_SCHEMA);
5
+ await pg.end();
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=squawk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"squawk.d.ts","sourceRoot":"","sources":["../../../postgres/scripts/squawk.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ import { DEFAULT_SCHEMA, migrations } from "../postgres.js";
2
+ import { execSync } from "node:child_process";
3
+ import { unlinkSync, writeFileSync } from "node:fs";
4
+ const sql = migrations(DEFAULT_SCHEMA).join("\n\n");
5
+ writeFileSync("squawk.sql", sql);
6
+ try {
7
+ // eslint-disable-next-line sonarjs/no-os-command-from-path
8
+ execSync("npx squawk squawk.sql", { stdio: "inherit" });
9
+ }
10
+ catch {
11
+ // ignore - squawk will produce its own error output
12
+ }
13
+ finally {
14
+ unlinkSync("squawk.sql");
15
+ console.log("");
16
+ }
@@ -0,0 +1,3 @@
1
+ /** Run database migrations once before Postgres backend tests. */
2
+ export declare function setup(): Promise<void>;
3
+ //# sourceMappingURL=vitest.global-setup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vitest.global-setup.d.ts","sourceRoot":"","sources":["../../postgres/vitest.global-setup.ts"],"names":[],"mappings":"AAOA,kEAAkE;AAClE,wBAAsB,KAAK,kBAI1B"}
@@ -0,0 +1,7 @@
1
+ import { migrate, newPostgresMaxOne, DEFAULT_SCHEMA, DEFAULT_POSTGRES_URL, } from "./postgres.js";
2
+ /** Run database migrations once before Postgres backend tests. */
3
+ export async function setup() {
4
+ const pg = newPostgresMaxOne(DEFAULT_POSTGRES_URL);
5
+ await migrate(pg, DEFAULT_SCHEMA);
6
+ await pg.end();
7
+ }
@@ -0,0 +1,2 @@
1
+ export { BackendPostgres } from "./postgres/backend.js";
2
+ //# sourceMappingURL=postgres.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../postgres.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1 @@
1
+ export { BackendPostgres } from "./postgres/backend.js";
@@ -0,0 +1,27 @@
1
+ import type { Workflow } from "./workflow.js";
2
+ /**
3
+ * A registry for storing and retrieving workflows by name and version.
4
+ * Provides a centralized way to manage workflow registrations.
5
+ */
6
+ export declare class WorkflowRegistry {
7
+ private readonly workflows;
8
+ /**
9
+ * Register a workflow in the registry.
10
+ * @param workflow - The workflow to register
11
+ * @throws {Error} If a workflow with the same name and version is already registered
12
+ */
13
+ register(workflow: Workflow<unknown, unknown, unknown>): void;
14
+ /**
15
+ * Get a workflow from the registry by name and version.
16
+ * @param name - The workflow name
17
+ * @param version - The workflow version (null for unversioned)
18
+ * @returns The workflow if found, undefined otherwise
19
+ */
20
+ get(name: string, version: string | null): Workflow<unknown, unknown, unknown> | undefined;
21
+ /**
22
+ * Get all registered workflows.
23
+ * @returns Array of all registered workflows
24
+ */
25
+ getAll(): Workflow<unknown, unknown, unknown>[];
26
+ }
27
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C;;;GAGG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAGtB;IAEJ;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,IAAI;IAW7D;;;;;OAKG;IACH,GAAG,CACD,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,GAAG,IAAI,GACrB,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,SAAS;IAKlD;;;OAGG;IACH,MAAM,IAAI,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE;CAGhD"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * A registry for storing and retrieving workflows by name and version.
3
+ * Provides a centralized way to manage workflow registrations.
4
+ */
5
+ export class WorkflowRegistry {
6
+ workflows = new Map();
7
+ /**
8
+ * Register a workflow in the registry.
9
+ * @param workflow - The workflow to register
10
+ * @throws {Error} If a workflow with the same name and version is already registered
11
+ */
12
+ register(workflow) {
13
+ const name = workflow.spec.name;
14
+ const version = workflow.spec.version ?? null;
15
+ const key = registryKey(name, version);
16
+ if (this.workflows.has(key)) {
17
+ const versionStr = version ? ` (version: ${version})` : "";
18
+ throw new Error(`Workflow "${name}"${versionStr} is already registered`);
19
+ }
20
+ this.workflows.set(key, workflow);
21
+ }
22
+ /**
23
+ * Get a workflow from the registry by name and version.
24
+ * @param name - The workflow name
25
+ * @param version - The workflow version (null for unversioned)
26
+ * @returns The workflow if found, undefined otherwise
27
+ */
28
+ get(name, version) {
29
+ const key = registryKey(name, version);
30
+ return this.workflows.get(key);
31
+ }
32
+ /**
33
+ * Get all registered workflows.
34
+ * @returns Array of all registered workflows
35
+ */
36
+ getAll() {
37
+ return [...this.workflows.values()];
38
+ }
39
+ }
40
+ /**
41
+ * Build a registry key from name and version.
42
+ * @param name - Workflow name
43
+ * @param version - Workflow version (or null)
44
+ * @returns Registry key
45
+ */
46
+ function registryKey(name, version) {
47
+ return version ? `${name}@${version}` : name;
48
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=registry.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.test.d.ts","sourceRoot":"","sources":["../registry.test.ts"],"names":[],"mappings":""}