@dudousxd/nestjs-durable-store-typeorm 0.29.0 → 0.31.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.
package/dist/index.cjs CHANGED
@@ -39,6 +39,8 @@ __export(index_exports, {
39
39
  durableColumnResolver: () => durableColumnResolver,
40
40
  durableEntities: () => durableEntities,
41
41
  durableManagedTables: () => durableManagedTables,
42
+ durableSchemaName: () => durableSchemaName,
43
+ durableTableSchema: () => durableTableSchema,
42
44
  ensureTypeOrmDurableSchema: () => ensureTypeOrmDurableSchema,
43
45
  jsonBlobColumnType: () => jsonBlobColumnType,
44
46
  parseJsonColumn: () => parseJsonColumn,
@@ -188,12 +190,27 @@ var ScheduleEntity = class {
188
190
  createdAt;
189
191
  updatedAt;
190
192
  };
193
+ var SCHEMA_NAME = /^[A-Za-z_][A-Za-z0-9_$]*$/;
194
+ function durableSchemaName(schema) {
195
+ if (schema === void 0) return void 0;
196
+ if (!SCHEMA_NAME.test(schema) || schema.length > 63) {
197
+ throw new Error(`[nestjs-durable] invalid schema ${JSON.stringify(schema)}: use letters, digits, _ or $ (not first), at most 63 characters.`);
198
+ }
199
+ return schema;
200
+ }
201
+ __name(durableSchemaName, "durableSchemaName");
191
202
  function durableEntities(options = {}) {
192
203
  const col = columnNamer(options.naming ?? "snake_case");
204
+ const schema = durableSchemaName(options.schema);
205
+ const where = schema === void 0 ? {} : {
206
+ schema,
207
+ database: schema
208
+ };
193
209
  const workflowRuns = new import_typeorm.EntitySchema({
194
210
  name: "WorkflowRunEntity",
195
211
  target: WorkflowRunEntity,
196
212
  tableName: "durable_workflow_runs",
213
+ ...where,
197
214
  columns: {
198
215
  id: {
199
216
  type: "text",
@@ -328,6 +345,7 @@ function durableEntities(options = {}) {
328
345
  name: "StepCheckpointEntity",
329
346
  target: StepCheckpointEntity,
330
347
  tableName: "durable_step_checkpoints",
348
+ ...where,
331
349
  // A run's fan, in member order: `checkpointStats`/`queryCheckpoints` narrow by group and page by
332
350
  // seq. The DDL that actually creates it lives in `ensureTypeOrmDurableSchema`.
333
351
  indices: [
@@ -431,6 +449,7 @@ function durableEntities(options = {}) {
431
449
  name: "RunAttributeEntity",
432
450
  target: RunAttributeEntity,
433
451
  tableName: "durable_run_attributes",
452
+ ...where,
434
453
  columns: {
435
454
  runId: {
436
455
  type: "text",
@@ -459,6 +478,7 @@ function durableEntities(options = {}) {
459
478
  name: "SignalWaiterEntity",
460
479
  target: SignalWaiterEntity,
461
480
  tableName: "durable_signal_waiters",
481
+ ...where,
462
482
  indices: [
463
483
  {
464
484
  name: "durable_signal_waiters_run_id_idx",
@@ -495,6 +515,7 @@ function durableEntities(options = {}) {
495
515
  name: "BufferedSignalEntity",
496
516
  target: BufferedSignalEntity,
497
517
  tableName: "durable_buffered_signals",
518
+ ...where,
498
519
  columns: {
499
520
  // Auto-increment PK for FIFO ordering. Left as TypeORM's default increment generation so it
500
521
  // maps to the driver-native auto-increment column (INTEGER on SQLite, BIGINT/SERIAL elsewhere).
@@ -520,6 +541,7 @@ function durableEntities(options = {}) {
520
541
  name: "BufferedEventEntity",
521
542
  target: BufferedEventEntity,
522
543
  tableName: "durable_buffered_events",
544
+ ...where,
523
545
  // No `indices` here (unlike MikroORM's EntitySchema, which does declare one): this store manages
524
546
  // its DDL — including the `(name, publishedAt)` index — via raw SQL in `schema.ts`'s
525
547
  // `ensureTypeOrmDurableSchema`, the same as every other durable_* table's indexes.
@@ -551,6 +573,7 @@ function durableEntities(options = {}) {
551
573
  name: "SignalIdEntity",
552
574
  target: SignalIdEntity,
553
575
  tableName: "durable_signal_ids",
576
+ ...where,
554
577
  // Its `(token, signal_id)` key and `expires_at` index are raw DDL in `schema.ts`, like every
555
578
  // other durable table's.
556
579
  columns: {
@@ -574,6 +597,7 @@ function durableEntities(options = {}) {
574
597
  name: "ScheduleEntity",
575
598
  target: ScheduleEntity,
576
599
  tableName: "durable_schedules",
600
+ ...where,
577
601
  columns: {
578
602
  id: {
579
603
  type: "text",
@@ -650,6 +674,26 @@ function durableEntities(options = {}) {
650
674
  __name(durableEntities, "durableEntities");
651
675
  var ENTITIES = durableEntities();
652
676
 
677
+ // src/lifecycle-index.ts
678
+ var LIFECYCLE_PENDING_TAG = "durable:lifecycle:pending";
679
+ var LIFECYCLE_PENDING_LIKE = `%"${LIFECYCLE_PENDING_TAG}"%`;
680
+ var LIFECYCLE_PENDING_INDEX = "durable_runs_lifecycle_pending_idx";
681
+ async function ensurePgLifecyclePendingIndex(query, opts) {
682
+ const literal = /* @__PURE__ */ __name((value) => `'${value.replace(/'/g, "''")}'`, "literal");
683
+ const scope = opts.schema === void 0 ? "current_schema()" : literal(opts.schema);
684
+ const [state] = await query(`SELECT i.indisvalid AS valid, EXISTS (SELECT 1 FROM pg_stat_progress_create_index p WHERE p.index_relid = c.oid) AS building
685
+ FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace JOIN pg_index i ON i.indexrelid = c.oid
686
+ WHERE c.relname = ${literal(LIFECYCLE_PENDING_INDEX)} AND n.nspname = ${scope}`) ?? [];
687
+ if (state?.valid || state?.building) return false;
688
+ const [tx] = await query("SELECT now() = statement_timestamp() AS autocommit") ?? [];
689
+ const concurrently = tx?.autocommit === false ? "" : "CONCURRENTLY ";
690
+ const qualified = opts.schema === void 0 ? `"${LIFECYCLE_PENDING_INDEX}"` : `"${opts.schema.replace(/"/g, '""')}"."${LIFECYCLE_PENDING_INDEX}"`;
691
+ if (state) await query(`DROP INDEX ${concurrently}IF EXISTS ${qualified}`);
692
+ await query(`CREATE INDEX ${concurrently}IF NOT EXISTS "${LIFECYCLE_PENDING_INDEX}" ON ${opts.table} (${opts.createdAt}) WHERE ${opts.predicate}`);
693
+ return true;
694
+ }
695
+ __name(ensurePgLifecyclePendingIndex, "ensurePgLifecyclePendingIndex");
696
+
653
697
  // src/schema.ts
654
698
  var JSON_BLOB_COLUMNS = {
655
699
  durable_workflow_runs: [
@@ -716,6 +760,22 @@ function durableColumnResolver(dataSource) {
716
760
  };
717
761
  }
718
762
  __name(durableColumnResolver, "durableColumnResolver");
763
+ function durableTableSchema(dataSource, configured) {
764
+ const wanted = durableSchemaName(configured);
765
+ const args = dataSource.getMetadata(WorkflowRunEntity).tableMetadataArgs;
766
+ const registered = args.database ?? args.schema;
767
+ const type = String(dataSource.options.type);
768
+ const isSqlite = type === "sqlite" || type === "better-sqlite3" || type === "expo";
769
+ const schema = wanted ?? registered;
770
+ if (schema !== void 0 && isSqlite) {
771
+ throw new Error(`[nestjs-durable] store-typeorm: the schema option (${JSON.stringify(schema)}) is not supported on SQLite, which has no schemas. Remove it from the store options and from durableEntities().`);
772
+ }
773
+ if (wanted !== void 0 && wanted !== registered) {
774
+ throw new Error(`[nestjs-durable] store-typeorm: the store's schema is ${JSON.stringify(wanted)}, but the durable entities were registered ${registered === void 0 ? "without one" : `with ${JSON.stringify(registered)}`}. Register durableEntities({ schema: ${JSON.stringify(wanted)} }) in your DataSource.`);
775
+ }
776
+ return schema;
777
+ }
778
+ __name(durableTableSchema, "durableTableSchema");
719
779
  function jsonBlobColumnType(isMysql) {
720
780
  return isMysql ? "longtext" : "text";
721
781
  }
@@ -755,6 +815,8 @@ __name(typeOrmSchemaRepairWarning, "typeOrmSchemaRepairWarning");
755
815
  async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
756
816
  const logger = options.logger ?? console;
757
817
  const q = /* @__PURE__ */ __name((id) => dataSource.driver.escape(id), "q");
818
+ const schema = durableTableSchema(dataSource, options.schema);
819
+ const qt = /* @__PURE__ */ __name((table) => schema === void 0 ? q(table) : `${q(schema)}.${q(table)}`, "qt");
758
820
  const resolve = durableColumnResolver(dataSource);
759
821
  const type = String(dataSource.options.type);
760
822
  const isPg = type === "postgres" || type === "aurora-postgres";
@@ -765,21 +827,21 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
765
827
  const int = isMysql ? "int" : "integer";
766
828
  const ts = isPg ? "timestamptz" : isMysql ? MYSQL_DATETIME_TYPE : "datetime";
767
829
  const nsCol = `${str} NOT NULL DEFAULT 'default'`;
768
- const runs = q("durable_workflow_runs");
769
- const checkpoints = q("durable_step_checkpoints");
770
- const runAttributes = q("durable_run_attributes");
771
- const waiters = q("durable_signal_waiters");
772
- const buffered = q("durable_buffered_signals");
773
- const bufferedEvents = q("durable_buffered_events");
830
+ const runs = qt("durable_workflow_runs");
831
+ const checkpoints = qt("durable_step_checkpoints");
832
+ const runAttributes = qt("durable_run_attributes");
833
+ const waiters = qt("durable_signal_waiters");
834
+ const buffered = qt("durable_buffered_signals");
835
+ const bufferedEvents = qt("durable_buffered_events");
774
836
  const runsCol = /* @__PURE__ */ __name((property) => q(resolve("durable_workflow_runs", property)), "runsCol");
775
837
  const cpCol = /* @__PURE__ */ __name((property) => q(resolve("durable_step_checkpoints", property)), "cpCol");
776
838
  const attrCol = /* @__PURE__ */ __name((property) => q(resolve("durable_run_attributes", property)), "attrCol");
777
839
  const waiterCol = /* @__PURE__ */ __name((property) => q(resolve("durable_signal_waiters", property)), "waiterCol");
778
840
  const bufCol = /* @__PURE__ */ __name((property) => q(resolve("durable_buffered_signals", property)), "bufCol");
779
841
  const bevCol = /* @__PURE__ */ __name((property) => q(resolve("durable_buffered_events", property)), "bevCol");
780
- const schedules = q("durable_schedules");
842
+ const schedules = qt("durable_schedules");
781
843
  const schCol = /* @__PURE__ */ __name((property) => q(resolve("durable_schedules", property)), "schCol");
782
- const signalIds = q("durable_signal_ids");
844
+ const signalIds = qt("durable_signal_ids");
783
845
  const sidCol = /* @__PURE__ */ __name((property) => q(resolve("durable_signal_ids", property)), "sidCol");
784
846
  const num = isPg ? "double precision" : isMysql ? "double" : "real";
785
847
  const bufferedId = isSqlite ? `${bufCol("id")} integer PRIMARY KEY AUTOINCREMENT` : isMysql ? `${bufCol("id")} bigint NOT NULL AUTO_INCREMENT PRIMARY KEY` : isPg ? `${bufCol("id")} bigserial PRIMARY KEY` : `${bufCol("id")} bigint NOT NULL AUTO_INCREMENT PRIMARY KEY`;
@@ -984,9 +1046,14 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
984
1046
  const created = /* @__PURE__ */ new Set();
985
1047
  const runner = dataSource.createQueryRunner();
986
1048
  try {
1049
+ if (schema !== void 0) await createSchemaIfMissing(runner, schema, {
1050
+ isMysql,
1051
+ q
1052
+ });
987
1053
  const before = await existingTables(runner, {
988
1054
  isSqlite,
989
- isMysql
1055
+ isMysql,
1056
+ schema
990
1057
  });
991
1058
  for (const [table, sql] of tables) {
992
1059
  await runner.query(sql);
@@ -998,25 +1065,26 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
998
1065
  for (const [table, props] of Object.entries(additive)) {
999
1066
  const have = await existingColumns(runner, table, {
1000
1067
  isSqlite,
1001
- isMysql
1068
+ isMysql,
1069
+ schema
1002
1070
  });
1003
1071
  for (const [property, colType] of props) {
1004
1072
  const column = resolve(table, property);
1005
1073
  if (!have.has(column)) {
1006
- await runner.query(`ALTER TABLE ${q(table)} ADD COLUMN ${q(column)} ${colType}`);
1074
+ await runner.query(`ALTER TABLE ${qt(table)} ADD COLUMN ${q(column)} ${colType}`);
1007
1075
  repairs.push(`added column ${table}.${column}`);
1008
1076
  }
1009
1077
  }
1010
1078
  }
1011
1079
  if (isMysql) {
1012
1080
  for (const [table, props] of Object.entries(JSON_BLOB_COLUMNS)) {
1013
- const types = await mysqlColumnTypes(runner, table);
1081
+ const types = await mysqlColumnTypes(runner, table, schema);
1014
1082
  for (const prop of props) {
1015
1083
  const column = resolve(table, prop);
1016
1084
  const current = types.get(column);
1017
1085
  if (current === void 0 || current === "longtext") continue;
1018
1086
  try {
1019
- await runner.query(`ALTER TABLE ${q(table)} MODIFY COLUMN ${q(column)} ${jsonBlobColumnType(true)}`);
1087
+ await runner.query(`ALTER TABLE ${qt(table)} MODIFY COLUMN ${q(column)} ${jsonBlobColumnType(true)}`);
1020
1088
  repairs.push(`widened ${table}.${column} from ${current} to longtext`);
1021
1089
  } catch {
1022
1090
  }
@@ -1026,14 +1094,14 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
1026
1094
  if (isMysql) {
1027
1095
  for (const [table, props] of Object.entries(DATETIME_COLUMNS)) {
1028
1096
  if (created.has(table)) continue;
1029
- const columns = await mysqlDatetimeColumns(runner, table);
1097
+ const columns = await mysqlDatetimeColumns(runner, table, schema);
1030
1098
  const behind = props.map((prop) => resolve(table, prop)).filter((column) => {
1031
1099
  const current = columns.get(column);
1032
1100
  return current !== void 0 && current.precision < 6;
1033
1101
  });
1034
1102
  if (behind.length === 0) continue;
1035
- const statement = `ALTER TABLE ${q(table)} ${behind.map((column) => `MODIFY COLUMN ${q(column)} ${MYSQL_DATETIME_TYPE} ${columns.get(column)?.nullable ? "NULL" : "NOT NULL"}`).join(", ")}`;
1036
- const rows = await mysqlEstimatedRows(runner, table);
1103
+ const statement = `ALTER TABLE ${qt(table)} ${behind.map((column) => `MODIFY COLUMN ${q(column)} ${MYSQL_DATETIME_TYPE} ${columns.get(column)?.nullable ? "NULL" : "NOT NULL"}`).join(", ")}`;
1104
+ const rows = await mysqlEstimatedRows(runner, table, schema);
1037
1105
  if (rows > rowLimit) {
1038
1106
  deferred.push({
1039
1107
  table,
@@ -1136,6 +1204,19 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
1136
1204
  } catch {
1137
1205
  }
1138
1206
  }
1207
+ if (isPg) {
1208
+ try {
1209
+ const built = await ensurePgLifecyclePendingIndex((sql) => runner.query(sql), {
1210
+ schema,
1211
+ table: runs,
1212
+ createdAt: runsCol("createdAt"),
1213
+ predicate: `${runsCol("tags")} LIKE '${LIFECYCLE_PENDING_LIKE}'`
1214
+ });
1215
+ if (built && !created.has("durable_workflow_runs")) repairs.push(`created index ${LIFECYCLE_PENDING_INDEX} on durable_workflow_runs`);
1216
+ } catch (error) {
1217
+ logger.warn(`[nestjs-durable] store-typeorm could not create the partial index ${LIFECYCLE_PENDING_INDEX} (the owed-lifecycle listing scans the runs table until it exists); retrying on the next boot: ${error.message}`);
1218
+ }
1219
+ }
1139
1220
  } finally {
1140
1221
  await runner.release();
1141
1222
  }
@@ -1144,10 +1225,10 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
1144
1225
  if (deferred.length > 0) logger.warn(typeOrmDeferredAlterWarning(deferred, rowLimit));
1145
1226
  }
1146
1227
  __name(ensureTypeOrmDurableSchema, "ensureTypeOrmDurableSchema");
1147
- async function mysqlDatetimeColumns(runner, table) {
1148
- const rows = await runner.query("SELECT column_name AS name, datetime_precision AS `precision`, is_nullable AS nullable FROM information_schema.columns WHERE table_name = ? AND TABLE_SCHEMA = DATABASE() AND data_type = 'datetime'", [
1228
+ async function mysqlDatetimeColumns(runner, table, schema) {
1229
+ const rows = await runner.query(`SELECT column_name AS name, datetime_precision AS \`precision\`, is_nullable AS nullable FROM information_schema.columns WHERE table_name = ? AND ${mysqlScope(schema)} AND data_type = 'datetime'`, mysqlParams([
1149
1230
  table
1150
- ]);
1231
+ ], schema));
1151
1232
  return new Map(rows.map((r) => [
1152
1233
  r.name,
1153
1234
  {
@@ -1157,10 +1238,10 @@ async function mysqlDatetimeColumns(runner, table) {
1157
1238
  ]));
1158
1239
  }
1159
1240
  __name(mysqlDatetimeColumns, "mysqlDatetimeColumns");
1160
- async function mysqlEstimatedRows(runner, table) {
1161
- const rows = await runner.query("SELECT table_rows AS n FROM information_schema.tables WHERE table_name = ? AND TABLE_SCHEMA = DATABASE() LIMIT 1", [
1241
+ async function mysqlEstimatedRows(runner, table, schema) {
1242
+ const rows = await runner.query(`SELECT table_rows AS n FROM information_schema.tables WHERE table_name = ? AND ${mysqlScope(schema)} LIMIT 1`, mysqlParams([
1162
1243
  table
1163
- ]);
1244
+ ], schema));
1164
1245
  return Number(rows[0]?.n ?? 0);
1165
1246
  }
1166
1247
  __name(mysqlEstimatedRows, "mysqlEstimatedRows");
@@ -1170,15 +1251,17 @@ async function existingTables(runner, d) {
1170
1251
  const rows2 = await runner.query("SELECT name FROM sqlite_master WHERE type = 'table'");
1171
1252
  return new Set(rows2.map((r) => r.name).filter((name) => names.includes(name)));
1172
1253
  }
1173
- const scope = d.isMysql ? "TABLE_SCHEMA = DATABASE()" : "table_schema = current_schema()";
1174
- const rows = await runner.query(`SELECT table_name AS name FROM information_schema.tables WHERE ${scope}`);
1254
+ const scope = d.isMysql ? mysqlScope(d.schema) : pgScope(d.schema, 1);
1255
+ const rows = await runner.query(`SELECT table_name AS name FROM information_schema.tables WHERE ${scope}`, d.schema === void 0 ? void 0 : [
1256
+ d.schema
1257
+ ]);
1175
1258
  return new Set(rows.map((r) => r.name ?? r.TABLE_NAME).filter((name) => names.includes(name)));
1176
1259
  }
1177
1260
  __name(existingTables, "existingTables");
1178
- async function mysqlColumnTypes(runner, table) {
1179
- const rows = await runner.query("SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = ? AND TABLE_SCHEMA = DATABASE()", [
1261
+ async function mysqlColumnTypes(runner, table, schema) {
1262
+ const rows = await runner.query(`SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = ? AND ${mysqlScope(schema)}`, mysqlParams([
1180
1263
  table
1181
- ]);
1264
+ ], schema));
1182
1265
  return new Map(rows.map((r) => [
1183
1266
  r.name ?? r.COLUMN_NAME,
1184
1267
  String(r.type ?? r.DATA_TYPE).toLowerCase()
@@ -1190,14 +1273,46 @@ async function existingColumns(runner, table, d) {
1190
1273
  const rows2 = await runner.query(`PRAGMA table_info("${table}")`);
1191
1274
  return new Set(rows2.map((r) => r.name));
1192
1275
  }
1193
- const scope = d.isMysql ? "TABLE_SCHEMA = DATABASE()" : "table_schema = current_schema()";
1276
+ const scope = d.isMysql ? mysqlScope(d.schema) : pgScope(d.schema, 2);
1194
1277
  const placeholder = d.isMysql ? "?" : "$1";
1195
- const rows = await runner.query(`SELECT column_name AS name FROM information_schema.columns WHERE table_name = ${placeholder} AND ${scope}`, [
1278
+ const rows = await runner.query(`SELECT column_name AS name FROM information_schema.columns WHERE table_name = ${placeholder} AND ${scope}`, d.schema === void 0 ? [
1196
1279
  table
1280
+ ] : [
1281
+ table,
1282
+ d.schema
1197
1283
  ]);
1198
1284
  return new Set(rows.map((r) => r.name ?? r.COLUMN_NAME));
1199
1285
  }
1200
1286
  __name(existingColumns, "existingColumns");
1287
+ function mysqlScope(schema) {
1288
+ return schema === void 0 ? "TABLE_SCHEMA = DATABASE()" : "TABLE_SCHEMA = ?";
1289
+ }
1290
+ __name(mysqlScope, "mysqlScope");
1291
+ function mysqlParams(params, schema) {
1292
+ return schema === void 0 ? params : [
1293
+ ...params,
1294
+ schema
1295
+ ];
1296
+ }
1297
+ __name(mysqlParams, "mysqlParams");
1298
+ function pgScope(schema, n) {
1299
+ return schema === void 0 ? "table_schema = current_schema()" : `table_schema = $${n}`;
1300
+ }
1301
+ __name(pgScope, "pgScope");
1302
+ async function createSchemaIfMissing(runner, schema, d) {
1303
+ const rows = await runner.query(d.isMysql ? "SELECT 1 AS found FROM information_schema.schemata WHERE schema_name = ?" : "SELECT 1 AS found FROM pg_namespace WHERE nspname = $1", [
1304
+ schema
1305
+ ]);
1306
+ if (rows.length > 0) return;
1307
+ try {
1308
+ await runner.query(`${d.isMysql ? "CREATE DATABASE" : "CREATE SCHEMA"} IF NOT EXISTS ${d.q(schema)}`);
1309
+ } catch (err) {
1310
+ throw new Error(`[nestjs-durable] store-typeorm could not create the ${d.isMysql ? "database" : "schema"} "${schema}" for the durable tables. Create it ahead of time, or grant this role ${d.isMysql ? "CREATE" : "CREATE on the database (GRANT CREATE ON DATABASE <db> TO <role>)"}. Cause: ${err.message}`, {
1311
+ cause: err
1312
+ });
1313
+ }
1314
+ }
1315
+ __name(createSchemaIfMissing, "createSchemaIfMissing");
1201
1316
 
1202
1317
  // src/typeorm-state-store.ts
1203
1318
  var import_nestjs_durable_core = require("@dudousxd/nestjs-durable-core");
@@ -1222,10 +1337,11 @@ var TypeOrmStateStore = class {
1222
1337
  constructor(dataSource, options = {}) {
1223
1338
  this.dataSource = dataSource;
1224
1339
  this.options = options;
1340
+ if (dataSource.isInitialized) this.tableSchema();
1225
1341
  const type = String(dataSource.options.type);
1226
1342
  const dialect = type === "postgres" ? "postgres" : type === "mysql" ? "mysql" : void 0;
1227
1343
  if (!dialect) return;
1228
- const fast = new TypeOrmRoundTrips(dataSource, dialect, this);
1344
+ const fast = new TypeOrmRoundTrips(dataSource, dialect, this, () => this.tableSchema());
1229
1345
  this.insertCheckpointIfAbsent = (cp) => fast.insertCheckpointIfAbsent(cp);
1230
1346
  this.settleResultsIfPending = (results) => fast.settleResultsIfPending(results);
1231
1347
  this.claimPendingRuns = (limit, owner, until, now, ns) => fast.claimPendingRuns(limit, owner, until, now, ns);
@@ -1236,9 +1352,18 @@ var TypeOrmStateStore = class {
1236
1352
  async ensureSchema() {
1237
1353
  await ensureTypeOrmDurableSchema(this.dataSource, {
1238
1354
  logger: this.options.schemaLogger,
1239
- blockingAlterRowLimit: this.options.schemaBlockingAlterRowLimit
1355
+ blockingAlterRowLimit: this.options.schemaBlockingAlterRowLimit,
1356
+ schema: this.options.schema
1240
1357
  });
1241
1358
  }
1359
+ resolvedSchema;
1360
+ /** The durable tables' schema (see {@link TypeOrmStateStoreOptions.schema}), resolved once. */
1361
+ tableSchema() {
1362
+ this.resolvedSchema ??= {
1363
+ schema: durableTableSchema(this.dataSource, this.options.schema)
1364
+ };
1365
+ return this.resolvedSchema.schema;
1366
+ }
1242
1367
  runs() {
1243
1368
  return this.dataSource.getRepository(WorkflowRunEntity);
1244
1369
  }
@@ -1816,7 +1941,8 @@ var TypeOrmStateStore = class {
1816
1941
  if (query.statuses) qb.andWhere(query.statuses.length ? "r.status IN (:...statuses)" : "1 = 0", query.statuses.length ? {
1817
1942
  statuses: query.statuses
1818
1943
  } : {});
1819
- if (query.tag) qb.andWhere("r.tags LIKE :tagPattern", {
1944
+ if (query.tag === LIFECYCLE_PENDING_TAG) qb.andWhere(`r.tags LIKE '${LIFECYCLE_PENDING_LIKE}'`);
1945
+ else if (query.tag) qb.andWhere("r.tags LIKE :tagPattern", {
1820
1946
  tagPattern: `%"${query.tag}"%`
1821
1947
  });
1822
1948
  if (query.tags) {
@@ -1881,7 +2007,8 @@ var TypeOrmStateStore = class {
1881
2007
  const runIdCol = wrap(resolve("durable_run_attributes", "runId"));
1882
2008
  const keyCol = wrap(resolve("durable_run_attributes", "key"));
1883
2009
  const runPkCol = wrap(resolve("durable_workflow_runs", "id"));
1884
- const table = wrap("durable_run_attributes");
2010
+ const schema = this.tableSchema();
2011
+ const table = schema === void 0 ? wrap("durable_run_attributes") : `${wrap(schema)}.${wrap("durable_run_attributes")}`;
1885
2012
  const a = `a${i}`;
1886
2013
  const params = {
1887
2014
  [keyParam]: f.key
@@ -2665,12 +2792,14 @@ var TypeOrmRoundTrips = class TypeOrmRoundTrips2 {
2665
2792
  }
2666
2793
  ds;
2667
2794
  store;
2795
+ schema;
2668
2796
  pg;
2669
2797
  /** The checkpoint columns' actual Postgres types (what the schema created), read once. */
2670
2798
  cpTypes;
2671
- constructor(ds, dialect, store) {
2799
+ constructor(ds, dialect, store, schema) {
2672
2800
  this.ds = ds;
2673
2801
  this.store = store;
2802
+ this.schema = schema;
2674
2803
  this.pg = dialect === "postgres";
2675
2804
  }
2676
2805
  get runs() {
@@ -2685,8 +2814,11 @@ var TypeOrmRoundTrips = class TypeOrmRoundTrips2 {
2685
2814
  * `synchronize`), so the value converts exactly as the repository's own writes convert it.
2686
2815
  */
2687
2816
  async cpType(property) {
2688
- this.cpTypes ??= this.ds.query("SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = $1 AND table_schema = current_schema()", [
2817
+ this.cpTypes ??= this.ds.query(this.schema() === void 0 ? "SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = $1 AND table_schema = current_schema()" : "SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = $1 AND table_schema = $2", this.schema() === void 0 ? [
2689
2818
  this.cps.tableName
2819
+ ] : [
2820
+ this.cps.tableName,
2821
+ this.schema()
2690
2822
  ]).then((rows) => new Map(rows.map((r) => [
2691
2823
  r.name,
2692
2824
  r.type
@@ -2709,7 +2841,8 @@ var TypeOrmRoundTrips = class TypeOrmRoundTrips2 {
2709
2841
  return this.q(this.col(meta, property).databaseName);
2710
2842
  }
2711
2843
  table(meta) {
2712
- return this.q(meta.tableName);
2844
+ const schema = this.schema();
2845
+ return schema === void 0 ? this.q(meta.tableName) : `${this.q(schema)}.${this.q(meta.tableName)}`;
2713
2846
  }
2714
2847
  /** `value` as the driver binds it for `property` (transformers and type conversion included). */
2715
2848
  bind(meta, property, value) {
@@ -2917,6 +3050,8 @@ var TypeOrmRoundTrips = class TypeOrmRoundTrips2 {
2917
3050
  durableColumnResolver,
2918
3051
  durableEntities,
2919
3052
  durableManagedTables,
3053
+ durableSchemaName,
3054
+ durableTableSchema,
2920
3055
  ensureTypeOrmDurableSchema,
2921
3056
  jsonBlobColumnType,
2922
3057
  parseJsonColumn,