@dudousxd/nestjs-durable-store-typeorm 0.30.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
@@ -674,6 +674,26 @@ function durableEntities(options = {}) {
674
674
  __name(durableEntities, "durableEntities");
675
675
  var ENTITIES = durableEntities();
676
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
+
677
697
  // src/schema.ts
678
698
  var JSON_BLOB_COLUMNS = {
679
699
  durable_workflow_runs: [
@@ -1184,6 +1204,19 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
1184
1204
  } catch {
1185
1205
  }
1186
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
+ }
1187
1220
  } finally {
1188
1221
  await runner.release();
1189
1222
  }
@@ -1908,7 +1941,8 @@ var TypeOrmStateStore = class {
1908
1941
  if (query.statuses) qb.andWhere(query.statuses.length ? "r.status IN (:...statuses)" : "1 = 0", query.statuses.length ? {
1909
1942
  statuses: query.statuses
1910
1943
  } : {});
1911
- 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", {
1912
1946
  tagPattern: `%"${query.tag}"%`
1913
1947
  });
1914
1948
  if (query.tags) {