@objectstack/metadata 5.0.0 → 5.1.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/node.js CHANGED
@@ -755,7 +755,6 @@ var DatabaseLoader = class {
755
755
  /**
756
756
  * Create a history record for a metadata change.
757
757
  *
758
- * @param metadataId - The metadata record ID
759
758
  * @param type - Metadata type
760
759
  * @param name - Metadata name
761
760
  * @param version - Version number
@@ -765,7 +764,7 @@ var DatabaseLoader = class {
765
764
  * @param changeNote - Optional change description
766
765
  * @param recordedBy - Optional user who made the change
767
766
  */
768
- async createHistoryRecord(metadataId, type, name, version, metadata, operationType, previousChecksum, changeNote, recordedBy) {
767
+ async createHistoryRecord(type, name, version, metadata, operationType, previousChecksum, changeNote, recordedBy) {
769
768
  if (!this.trackHistory) return;
770
769
  await this.ensureHistorySchema();
771
770
  const now = (/* @__PURE__ */ new Date()).toISOString();
@@ -778,7 +777,6 @@ var DatabaseLoader = class {
778
777
  const eventSeq = await this.nextEventSeq();
779
778
  const historyRecord = {
780
779
  id: historyId,
781
- metadataId,
782
780
  name,
783
781
  type,
784
782
  version,
@@ -795,7 +793,6 @@ var DatabaseLoader = class {
795
793
  await this._create(this.historyTableName, {
796
794
  id: historyRecord.id,
797
795
  event_seq: eventSeq,
798
- metadata_id: historyRecord.metadataId,
799
796
  name: historyRecord.name,
800
797
  type: historyRecord.type,
801
798
  version: historyRecord.version,
@@ -983,12 +980,9 @@ var DatabaseLoader = class {
983
980
  async getHistoryRecord(type, name, version) {
984
981
  if (!this.trackHistory) return null;
985
982
  await this.ensureHistorySchema();
986
- const metadataRow = await this._findOne(this.tableName, {
987
- where: this.baseFilter(type, name)
988
- });
989
- if (!metadataRow) return null;
990
983
  const filter = {
991
- metadata_id: metadataRow.id,
984
+ type,
985
+ name,
992
986
  version
993
987
  };
994
988
  if (this.organizationId) {
@@ -1000,7 +994,6 @@ var DatabaseLoader = class {
1000
994
  if (!row) return null;
1001
995
  return {
1002
996
  id: row.id,
1003
- metadataId: row.metadata_id,
1004
997
  name: row.name,
1005
998
  type: row.type,
1006
999
  version: row.version,
@@ -1025,14 +1018,9 @@ var DatabaseLoader = class {
1025
1018
  }
1026
1019
  await this.ensureSchema();
1027
1020
  await this.ensureHistorySchema();
1028
- const filter = { type, name };
1029
- if (this.organizationId) filter.organization_id = this.organizationId;
1030
- const metadataRecord = await this._findOne(this.tableName, { where: filter });
1031
- if (!metadataRecord) {
1032
- return { records: [], total: 0, hasMore: false };
1033
- }
1034
1021
  const historyFilter = {
1035
- metadata_id: metadataRecord.id
1022
+ type,
1023
+ name
1036
1024
  };
1037
1025
  if (this.organizationId) historyFilter.organization_id = this.organizationId;
1038
1026
  if (options?.operationType) historyFilter.operation_type = options.operationType;
@@ -1063,7 +1051,6 @@ var DatabaseLoader = class {
1063
1051
  const parsedMetadata = typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata;
1064
1052
  return {
1065
1053
  id: row.id,
1066
- metadataId: row.metadata_id,
1067
1054
  name: row.name,
1068
1055
  type: row.type,
1069
1056
  version: row.version,
@@ -1108,7 +1095,6 @@ var DatabaseLoader = class {
1108
1095
  });
1109
1096
  this.invalidate(type, name);
1110
1097
  await this.createHistoryRecord(
1111
- existing.id,
1112
1098
  type,
1113
1099
  name,
1114
1100
  newVersion,
@@ -1150,7 +1136,6 @@ var DatabaseLoader = class {
1150
1136
  });
1151
1137
  this.invalidate(type, name);
1152
1138
  await this.createHistoryRecord(
1153
- existing.id,
1154
1139
  type,
1155
1140
  name,
1156
1141
  version,
@@ -1184,7 +1169,6 @@ var DatabaseLoader = class {
1184
1169
  });
1185
1170
  this.invalidate(type, name);
1186
1171
  await this.createHistoryRecord(
1187
- id,
1188
1172
  type,
1189
1173
  name,
1190
1174
  1,
@@ -3603,6 +3587,10 @@ function registerMetadataHistoryRoutes(app, metadataService) {
3603
3587
  }
3604
3588
 
3605
3589
  // src/utils/history-cleanup.ts
3590
+ import { DEFAULT_METADATA_TYPE_REGISTRY as DEFAULT_METADATA_TYPE_REGISTRY2 } from "@objectstack/spec/kernel";
3591
+ function executionPinnedTypes() {
3592
+ return DEFAULT_METADATA_TYPE_REGISTRY2.filter((entry) => entry.executionPinned).map((entry) => entry.type);
3593
+ }
3606
3594
  var HistoryCleanupManager = class {
3607
3595
  constructor(policy, dbLoader) {
3608
3596
  this.policy = policy;
@@ -3640,6 +3628,8 @@ var HistoryCleanupManager = class {
3640
3628
  const organizationId = this.dbLoader.organizationId;
3641
3629
  let deleted = 0;
3642
3630
  let errors = 0;
3631
+ const pinnedTypes = executionPinnedTypes();
3632
+ const isPinned = (t) => !!t && pinnedTypes.includes(t);
3643
3633
  try {
3644
3634
  if (this.policy.maxAgeDays) {
3645
3635
  const cutoffDate = /* @__PURE__ */ new Date();
@@ -3651,6 +3641,9 @@ var HistoryCleanupManager = class {
3651
3641
  if (organizationId) {
3652
3642
  filter.organization_id = organizationId;
3653
3643
  }
3644
+ if (pinnedTypes.length > 0) {
3645
+ filter.type = { $nin: pinnedTypes };
3646
+ }
3654
3647
  try {
3655
3648
  const result = await this.bulkDeleteByFilter(driver, historyTableName, filter);
3656
3649
  deleted += result.deleted;
@@ -3663,19 +3656,22 @@ var HistoryCleanupManager = class {
3663
3656
  try {
3664
3657
  const baseWhere = {};
3665
3658
  if (organizationId) baseWhere.organization_id = organizationId;
3666
- const metadataIds = await driver.find(historyTableName, {
3659
+ const metaItems = await driver.find(historyTableName, {
3667
3660
  object: historyTableName,
3668
3661
  where: baseWhere,
3669
- fields: ["metadata_id"]
3662
+ fields: ["type", "name"]
3670
3663
  });
3671
- const uniqueIds = /* @__PURE__ */ new Set();
3672
- for (const record of metadataIds) {
3673
- if (record.metadata_id) {
3674
- uniqueIds.add(record.metadata_id);
3664
+ const uniqueKeys = /* @__PURE__ */ new Set();
3665
+ for (const record of metaItems) {
3666
+ const t = record.type;
3667
+ const n = record.name;
3668
+ if (t && n && !isPinned(t)) {
3669
+ uniqueKeys.add(`${t}${n}`);
3675
3670
  }
3676
3671
  }
3677
- for (const metadataId of uniqueIds) {
3678
- const filter = { metadata_id: metadataId, ...baseWhere };
3672
+ for (const key of uniqueKeys) {
3673
+ const [type, name] = key.split("");
3674
+ const filter = { type, name, ...baseWhere };
3679
3675
  try {
3680
3676
  const historyRecords = await driver.find(historyTableName, {
3681
3677
  object: historyTableName,
@@ -3752,6 +3748,8 @@ var HistoryCleanupManager = class {
3752
3748
  const organizationId = this.dbLoader.organizationId;
3753
3749
  let recordsByAge = 0;
3754
3750
  let recordsByCount = 0;
3751
+ const pinnedTypes = executionPinnedTypes();
3752
+ const isPinned = (t) => !!t && pinnedTypes.includes(t);
3755
3753
  try {
3756
3754
  const baseWhere = {};
3757
3755
  if (organizationId) baseWhere.organization_id = organizationId;
@@ -3763,25 +3761,31 @@ var HistoryCleanupManager = class {
3763
3761
  recorded_at: { $lt: cutoffISO },
3764
3762
  ...baseWhere
3765
3763
  };
3764
+ if (pinnedTypes.length > 0) {
3765
+ filter.type = { $nin: pinnedTypes };
3766
+ }
3766
3767
  recordsByAge = await driver.count(historyTableName, {
3767
3768
  object: historyTableName,
3768
3769
  where: filter
3769
3770
  });
3770
3771
  }
3771
3772
  if (this.policy.maxVersions) {
3772
- const metadataIds = await driver.find(historyTableName, {
3773
+ const metaItems = await driver.find(historyTableName, {
3773
3774
  object: historyTableName,
3774
3775
  where: baseWhere,
3775
- fields: ["metadata_id"]
3776
+ fields: ["type", "name"]
3776
3777
  });
3777
- const uniqueIds = /* @__PURE__ */ new Set();
3778
- for (const record of metadataIds) {
3779
- if (record.metadata_id) {
3780
- uniqueIds.add(record.metadata_id);
3778
+ const uniqueKeys = /* @__PURE__ */ new Set();
3779
+ for (const record of metaItems) {
3780
+ const t = record.type;
3781
+ const n = record.name;
3782
+ if (t && n && !isPinned(t)) {
3783
+ uniqueKeys.add(`${t}${n}`);
3781
3784
  }
3782
3785
  }
3783
- for (const metadataId of uniqueIds) {
3784
- const filter = { metadata_id: metadataId, ...baseWhere };
3786
+ for (const key of uniqueKeys) {
3787
+ const [type, name] = key.split("");
3788
+ const filter = { type, name, ...baseWhere };
3785
3789
  const count = await driver.count(historyTableName, {
3786
3790
  object: historyTableName,
3787
3791
  where: filter