@dudousxd/nestjs-durable-store-typeorm 0.25.1 → 0.25.3

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
@@ -23,8 +23,10 @@ var index_exports = {};
23
23
  __export(index_exports, {
24
24
  BufferedEventEntity: () => BufferedEventEntity,
25
25
  BufferedSignalEntity: () => BufferedSignalEntity,
26
+ DEFAULT_BLOCKING_ALTER_ROW_LIMIT: () => DEFAULT_BLOCKING_ALTER_ROW_LIMIT,
26
27
  ENTITIES: () => ENTITIES,
27
28
  JSON_BLOB_COLUMNS: () => JSON_BLOB_COLUMNS,
29
+ MYSQL_DATETIME_TYPE: () => MYSQL_DATETIME_TYPE,
28
30
  RunAttributeEntity: () => RunAttributeEntity,
29
31
  ScheduleEntity: () => ScheduleEntity,
30
32
  SignalWaiterEntity: () => SignalWaiterEntity,
@@ -39,6 +41,7 @@ __export(index_exports, {
39
41
  ensureTypeOrmDurableSchema: () => ensureTypeOrmDurableSchema,
40
42
  jsonBlobColumnType: () => jsonBlobColumnType,
41
43
  parseJsonColumn: () => parseJsonColumn,
44
+ typeOrmDeferredAlterWarning: () => typeOrmDeferredAlterWarning,
42
45
  typeOrmSchemaRepairWarning: () => typeOrmSchemaRepairWarning
43
46
  });
44
47
  module.exports = __toCommonJS(index_exports);
@@ -602,6 +605,29 @@ var JSON_BLOB_COLUMNS = {
602
605
  "events"
603
606
  ]
604
607
  };
608
+ var MYSQL_DATETIME_TYPE = "datetime(6)";
609
+ var DATETIME_COLUMNS = {
610
+ durable_workflow_runs: [
611
+ "wakeAt",
612
+ "lockedUntil",
613
+ "createdAt",
614
+ "updatedAt"
615
+ ],
616
+ durable_step_checkpoints: [
617
+ "wakeAt",
618
+ "enqueuedAt",
619
+ "startedAt",
620
+ "finishedAt"
621
+ ],
622
+ durable_buffered_events: [
623
+ "publishedAt"
624
+ ],
625
+ durable_schedules: [
626
+ "createdAt",
627
+ "updatedAt"
628
+ ]
629
+ };
630
+ var DEFAULT_BLOCKING_ALTER_ROW_LIMIT = 1e5;
605
631
  var TABLE_TO_ENTITY = {
606
632
  durable_workflow_runs: WorkflowRunEntity,
607
633
  durable_step_checkpoints: StepCheckpointEntity,
@@ -643,6 +669,16 @@ function buildWidenStatements(isMysql, quote, col) {
643
669
  return out;
644
670
  }
645
671
  __name(buildWidenStatements, "buildWidenStatements");
672
+ function typeOrmDeferredAlterWarning(deferred, limit) {
673
+ return [
674
+ `[nestjs-durable] store-typeorm did NOT apply ${deferred.length} durable-schema column type change(s) at boot.`,
675
+ `MySQL rebuilds a table to change a column type and blocks writes to it until the copy ends, and these tables hold more than blockingAlterRowLimit (${limit}) rows:`,
676
+ ...deferred.map((d) => ` -- ${d.table}, ~${d.rows} rows
677
+ ${d.statement};`),
678
+ "Run them in a maintenance window or with an online schema change tool (gh-ost, pt-online-schema-change), or raise blockingAlterRowLimit to let a boot run them. Until then the store keeps working on the old column types, and this warning repeats on every boot."
679
+ ].join("\n");
680
+ }
681
+ __name(typeOrmDeferredAlterWarning, "typeOrmDeferredAlterWarning");
646
682
  function typeOrmSchemaRepairWarning(repairs) {
647
683
  return [
648
684
  `[nestjs-durable] store-typeorm repaired the durable schema at boot: ${repairs.join("; ")}.`,
@@ -665,7 +701,7 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
665
701
  const str = "varchar(191)";
666
702
  const txt = jsonBlobColumnType(isMysql);
667
703
  const int = isMysql ? "int" : "integer";
668
- const ts = isPg ? "timestamptz" : "datetime";
704
+ const ts = isPg ? "timestamptz" : isMysql ? MYSQL_DATETIME_TYPE : "datetime";
669
705
  const nsCol = `${str} NOT NULL DEFAULT 'default'`;
670
706
  const runs = q("durable_workflow_runs");
671
707
  const checkpoints = q("durable_step_checkpoints");
@@ -848,6 +884,8 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
848
884
  ]
849
885
  };
850
886
  const repairs = [];
887
+ const deferred = [];
888
+ const rowLimit = options.blockingAlterRowLimit ?? DEFAULT_BLOCKING_ALTER_ROW_LIMIT;
851
889
  const created = /* @__PURE__ */ new Set();
852
890
  const runner = dataSource.createQueryRunner();
853
891
  try {
@@ -890,6 +928,29 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
890
928
  }
891
929
  }
892
930
  }
931
+ if (isMysql) {
932
+ for (const [table, props] of Object.entries(DATETIME_COLUMNS)) {
933
+ if (created.has(table)) continue;
934
+ const columns = await mysqlDatetimeColumns(runner, table);
935
+ const behind = props.map((prop) => resolve(table, prop)).filter((column) => {
936
+ const current = columns.get(column);
937
+ return current !== void 0 && current.precision < 6;
938
+ });
939
+ if (behind.length === 0) continue;
940
+ const statement = `ALTER TABLE ${q(table)} ${behind.map((column) => `MODIFY COLUMN ${q(column)} ${MYSQL_DATETIME_TYPE} ${columns.get(column)?.nullable ? "NULL" : "NOT NULL"}`).join(", ")}`;
941
+ const rows = await mysqlEstimatedRows(runner, table);
942
+ if (rows > rowLimit) {
943
+ deferred.push({
944
+ table,
945
+ rows,
946
+ statement
947
+ });
948
+ continue;
949
+ }
950
+ await runner.query(statement);
951
+ repairs.push(`raised ${behind.map((column) => `${table}.${column}`).join(", ")} to ${MYSQL_DATETIME_TYPE}`);
952
+ }
953
+ }
893
954
  const indexes = [
894
955
  [
895
956
  "durable_runs_status_idx",
@@ -968,8 +1029,29 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
968
1029
  }
969
1030
  const freshInstall = created.size === tables.length;
970
1031
  if (repairs.length > 0 && !freshInstall) logger.warn(typeOrmSchemaRepairWarning(repairs));
1032
+ if (deferred.length > 0) logger.warn(typeOrmDeferredAlterWarning(deferred, rowLimit));
971
1033
  }
972
1034
  __name(ensureTypeOrmDurableSchema, "ensureTypeOrmDurableSchema");
1035
+ async function mysqlDatetimeColumns(runner, table) {
1036
+ 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'", [
1037
+ table
1038
+ ]);
1039
+ return new Map(rows.map((r) => [
1040
+ r.name,
1041
+ {
1042
+ precision: Number(r.precision ?? 0),
1043
+ nullable: String(r.nullable).toUpperCase() === "YES"
1044
+ }
1045
+ ]));
1046
+ }
1047
+ __name(mysqlDatetimeColumns, "mysqlDatetimeColumns");
1048
+ async function mysqlEstimatedRows(runner, table) {
1049
+ const rows = await runner.query("SELECT table_rows AS n FROM information_schema.tables WHERE table_name = ? AND TABLE_SCHEMA = DATABASE() LIMIT 1", [
1050
+ table
1051
+ ]);
1052
+ return Number(rows[0]?.n ?? 0);
1053
+ }
1054
+ __name(mysqlEstimatedRows, "mysqlEstimatedRows");
973
1055
  async function existingTables(runner, d) {
974
1056
  const names = durableManagedTables();
975
1057
  if (d.isSqlite) {
@@ -1022,7 +1104,8 @@ var TypeOrmStateStore = class {
1022
1104
  }
1023
1105
  async ensureSchema() {
1024
1106
  await ensureTypeOrmDurableSchema(this.dataSource, {
1025
- logger: this.options.schemaLogger
1107
+ logger: this.options.schemaLogger,
1108
+ blockingAlterRowLimit: this.options.schemaBlockingAlterRowLimit
1026
1109
  });
1027
1110
  }
1028
1111
  runs() {
@@ -1805,6 +1888,31 @@ var TypeOrmStateStore = class {
1805
1888
  payload: e.payload ?? void 0
1806
1889
  };
1807
1890
  }
1891
+ async listBufferedSignals(tokenPrefix, afterId, limit) {
1892
+ const rows = await this.buffered().find({
1893
+ where: {
1894
+ token: (0, import_typeorm2.Like)(`${tokenPrefix}%`),
1895
+ ...afterId !== void 0 ? {
1896
+ id: (0, import_typeorm2.MoreThan)(Number(afterId))
1897
+ } : {}
1898
+ },
1899
+ order: {
1900
+ id: "ASC"
1901
+ },
1902
+ take: limit
1903
+ });
1904
+ return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
1905
+ id: String(r.id),
1906
+ token: r.token,
1907
+ payload: r.payload ?? void 0
1908
+ }));
1909
+ }
1910
+ async deleteBufferedSignals(ids) {
1911
+ if (ids.length === 0) return;
1912
+ await this.buffered().delete({
1913
+ id: (0, import_typeorm2.In)(ids.map(Number))
1914
+ });
1915
+ }
1808
1916
  async bufferEvent(input) {
1809
1917
  await this.bufferedEvents().save({
1810
1918
  id: input.id,
@@ -2022,8 +2130,10 @@ __name(updateIfPending, "updateIfPending");
2022
2130
  0 && (module.exports = {
2023
2131
  BufferedEventEntity,
2024
2132
  BufferedSignalEntity,
2133
+ DEFAULT_BLOCKING_ALTER_ROW_LIMIT,
2025
2134
  ENTITIES,
2026
2135
  JSON_BLOB_COLUMNS,
2136
+ MYSQL_DATETIME_TYPE,
2027
2137
  RunAttributeEntity,
2028
2138
  ScheduleEntity,
2029
2139
  SignalWaiterEntity,
@@ -2038,6 +2148,7 @@ __name(updateIfPending, "updateIfPending");
2038
2148
  ensureTypeOrmDurableSchema,
2039
2149
  jsonBlobColumnType,
2040
2150
  parseJsonColumn,
2151
+ typeOrmDeferredAlterWarning,
2041
2152
  typeOrmSchemaRepairWarning
2042
2153
  });
2043
2154
  //# sourceMappingURL=index.cjs.map