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