@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/index.cjs CHANGED
@@ -799,7 +799,6 @@ var DatabaseLoader = class {
799
799
  /**
800
800
  * Create a history record for a metadata change.
801
801
  *
802
- * @param metadataId - The metadata record ID
803
802
  * @param type - Metadata type
804
803
  * @param name - Metadata name
805
804
  * @param version - Version number
@@ -809,7 +808,7 @@ var DatabaseLoader = class {
809
808
  * @param changeNote - Optional change description
810
809
  * @param recordedBy - Optional user who made the change
811
810
  */
812
- async createHistoryRecord(metadataId, type, name, version, metadata, operationType, previousChecksum, changeNote, recordedBy) {
811
+ async createHistoryRecord(type, name, version, metadata, operationType, previousChecksum, changeNote, recordedBy) {
813
812
  if (!this.trackHistory) return;
814
813
  await this.ensureHistorySchema();
815
814
  const now = (/* @__PURE__ */ new Date()).toISOString();
@@ -822,7 +821,6 @@ var DatabaseLoader = class {
822
821
  const eventSeq = await this.nextEventSeq();
823
822
  const historyRecord = {
824
823
  id: historyId,
825
- metadataId,
826
824
  name,
827
825
  type,
828
826
  version,
@@ -839,7 +837,6 @@ var DatabaseLoader = class {
839
837
  await this._create(this.historyTableName, {
840
838
  id: historyRecord.id,
841
839
  event_seq: eventSeq,
842
- metadata_id: historyRecord.metadataId,
843
840
  name: historyRecord.name,
844
841
  type: historyRecord.type,
845
842
  version: historyRecord.version,
@@ -1027,12 +1024,9 @@ var DatabaseLoader = class {
1027
1024
  async getHistoryRecord(type, name, version) {
1028
1025
  if (!this.trackHistory) return null;
1029
1026
  await this.ensureHistorySchema();
1030
- const metadataRow = await this._findOne(this.tableName, {
1031
- where: this.baseFilter(type, name)
1032
- });
1033
- if (!metadataRow) return null;
1034
1027
  const filter = {
1035
- metadata_id: metadataRow.id,
1028
+ type,
1029
+ name,
1036
1030
  version
1037
1031
  };
1038
1032
  if (this.organizationId) {
@@ -1044,7 +1038,6 @@ var DatabaseLoader = class {
1044
1038
  if (!row) return null;
1045
1039
  return {
1046
1040
  id: row.id,
1047
- metadataId: row.metadata_id,
1048
1041
  name: row.name,
1049
1042
  type: row.type,
1050
1043
  version: row.version,
@@ -1069,14 +1062,9 @@ var DatabaseLoader = class {
1069
1062
  }
1070
1063
  await this.ensureSchema();
1071
1064
  await this.ensureHistorySchema();
1072
- const filter = { type, name };
1073
- if (this.organizationId) filter.organization_id = this.organizationId;
1074
- const metadataRecord = await this._findOne(this.tableName, { where: filter });
1075
- if (!metadataRecord) {
1076
- return { records: [], total: 0, hasMore: false };
1077
- }
1078
1065
  const historyFilter = {
1079
- metadata_id: metadataRecord.id
1066
+ type,
1067
+ name
1080
1068
  };
1081
1069
  if (this.organizationId) historyFilter.organization_id = this.organizationId;
1082
1070
  if (options?.operationType) historyFilter.operation_type = options.operationType;
@@ -1107,7 +1095,6 @@ var DatabaseLoader = class {
1107
1095
  const parsedMetadata = typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata;
1108
1096
  return {
1109
1097
  id: row.id,
1110
- metadataId: row.metadata_id,
1111
1098
  name: row.name,
1112
1099
  type: row.type,
1113
1100
  version: row.version,
@@ -1152,7 +1139,6 @@ var DatabaseLoader = class {
1152
1139
  });
1153
1140
  this.invalidate(type, name);
1154
1141
  await this.createHistoryRecord(
1155
- existing.id,
1156
1142
  type,
1157
1143
  name,
1158
1144
  newVersion,
@@ -1194,7 +1180,6 @@ var DatabaseLoader = class {
1194
1180
  });
1195
1181
  this.invalidate(type, name);
1196
1182
  await this.createHistoryRecord(
1197
- existing.id,
1198
1183
  type,
1199
1184
  name,
1200
1185
  version,
@@ -1228,7 +1213,6 @@ var DatabaseLoader = class {
1228
1213
  });
1229
1214
  this.invalidate(type, name);
1230
1215
  await this.createHistoryRecord(
1231
- id,
1232
1216
  type,
1233
1217
  name,
1234
1218
  1,
@@ -3644,6 +3628,10 @@ function registerMetadataHistoryRoutes(app, metadataService) {
3644
3628
  }
3645
3629
 
3646
3630
  // src/utils/history-cleanup.ts
3631
+ var import_kernel2 = require("@objectstack/spec/kernel");
3632
+ function executionPinnedTypes() {
3633
+ return import_kernel2.DEFAULT_METADATA_TYPE_REGISTRY.filter((entry) => entry.executionPinned).map((entry) => entry.type);
3634
+ }
3647
3635
  var HistoryCleanupManager = class {
3648
3636
  constructor(policy, dbLoader) {
3649
3637
  this.policy = policy;
@@ -3681,6 +3669,8 @@ var HistoryCleanupManager = class {
3681
3669
  const organizationId = this.dbLoader.organizationId;
3682
3670
  let deleted = 0;
3683
3671
  let errors = 0;
3672
+ const pinnedTypes = executionPinnedTypes();
3673
+ const isPinned = (t) => !!t && pinnedTypes.includes(t);
3684
3674
  try {
3685
3675
  if (this.policy.maxAgeDays) {
3686
3676
  const cutoffDate = /* @__PURE__ */ new Date();
@@ -3692,6 +3682,9 @@ var HistoryCleanupManager = class {
3692
3682
  if (organizationId) {
3693
3683
  filter.organization_id = organizationId;
3694
3684
  }
3685
+ if (pinnedTypes.length > 0) {
3686
+ filter.type = { $nin: pinnedTypes };
3687
+ }
3695
3688
  try {
3696
3689
  const result = await this.bulkDeleteByFilter(driver, historyTableName, filter);
3697
3690
  deleted += result.deleted;
@@ -3704,19 +3697,22 @@ var HistoryCleanupManager = class {
3704
3697
  try {
3705
3698
  const baseWhere = {};
3706
3699
  if (organizationId) baseWhere.organization_id = organizationId;
3707
- const metadataIds = await driver.find(historyTableName, {
3700
+ const metaItems = await driver.find(historyTableName, {
3708
3701
  object: historyTableName,
3709
3702
  where: baseWhere,
3710
- fields: ["metadata_id"]
3703
+ fields: ["type", "name"]
3711
3704
  });
3712
- const uniqueIds = /* @__PURE__ */ new Set();
3713
- for (const record of metadataIds) {
3714
- if (record.metadata_id) {
3715
- uniqueIds.add(record.metadata_id);
3705
+ const uniqueKeys = /* @__PURE__ */ new Set();
3706
+ for (const record of metaItems) {
3707
+ const t = record.type;
3708
+ const n = record.name;
3709
+ if (t && n && !isPinned(t)) {
3710
+ uniqueKeys.add(`${t}${n}`);
3716
3711
  }
3717
3712
  }
3718
- for (const metadataId of uniqueIds) {
3719
- const filter = { metadata_id: metadataId, ...baseWhere };
3713
+ for (const key of uniqueKeys) {
3714
+ const [type, name] = key.split("");
3715
+ const filter = { type, name, ...baseWhere };
3720
3716
  try {
3721
3717
  const historyRecords = await driver.find(historyTableName, {
3722
3718
  object: historyTableName,
@@ -3793,6 +3789,8 @@ var HistoryCleanupManager = class {
3793
3789
  const organizationId = this.dbLoader.organizationId;
3794
3790
  let recordsByAge = 0;
3795
3791
  let recordsByCount = 0;
3792
+ const pinnedTypes = executionPinnedTypes();
3793
+ const isPinned = (t) => !!t && pinnedTypes.includes(t);
3796
3794
  try {
3797
3795
  const baseWhere = {};
3798
3796
  if (organizationId) baseWhere.organization_id = organizationId;
@@ -3804,25 +3802,31 @@ var HistoryCleanupManager = class {
3804
3802
  recorded_at: { $lt: cutoffISO },
3805
3803
  ...baseWhere
3806
3804
  };
3805
+ if (pinnedTypes.length > 0) {
3806
+ filter.type = { $nin: pinnedTypes };
3807
+ }
3807
3808
  recordsByAge = await driver.count(historyTableName, {
3808
3809
  object: historyTableName,
3809
3810
  where: filter
3810
3811
  });
3811
3812
  }
3812
3813
  if (this.policy.maxVersions) {
3813
- const metadataIds = await driver.find(historyTableName, {
3814
+ const metaItems = await driver.find(historyTableName, {
3814
3815
  object: historyTableName,
3815
3816
  where: baseWhere,
3816
- fields: ["metadata_id"]
3817
+ fields: ["type", "name"]
3817
3818
  });
3818
- const uniqueIds = /* @__PURE__ */ new Set();
3819
- for (const record of metadataIds) {
3820
- if (record.metadata_id) {
3821
- uniqueIds.add(record.metadata_id);
3819
+ const uniqueKeys = /* @__PURE__ */ new Set();
3820
+ for (const record of metaItems) {
3821
+ const t = record.type;
3822
+ const n = record.name;
3823
+ if (t && n && !isPinned(t)) {
3824
+ uniqueKeys.add(`${t}${n}`);
3822
3825
  }
3823
3826
  }
3824
- for (const metadataId of uniqueIds) {
3825
- const filter = { metadata_id: metadataId, ...baseWhere };
3827
+ for (const key of uniqueKeys) {
3828
+ const [type, name] = key.split("");
3829
+ const filter = { type, name, ...baseWhere };
3826
3830
  const count = await driver.count(historyTableName, {
3827
3831
  object: historyTableName,
3828
3832
  where: filter