@memberjunction/archiving-engine 6.1.4 → 6.2.0-edge.1
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/ArchiveEngine.d.ts +7 -7
- package/dist/ArchiveEngine.js +18 -18
- package/dist/ArchiveProcessor.d.ts +10 -10
- package/dist/ArchiveProcessor.js +20 -20
- package/dist/ArchiveRecovery.d.ts +4 -4
- package/dist/ArchiveRecovery.js +8 -8
- package/dist/DefaultArchiveDriver.d.ts +13 -13
- package/dist/DefaultArchiveDriver.js +27 -27
- package/package.json +5 -5
package/dist/ArchiveEngine.d.ts
CHANGED
|
@@ -49,31 +49,31 @@ export declare class ArchiveEngine extends BaseSingleton<ArchiveEngine> {
|
|
|
49
49
|
/**
|
|
50
50
|
* Loads the ArchiveConfiguration record by ID.
|
|
51
51
|
*/
|
|
52
|
-
private
|
|
52
|
+
private loadConfiguration;
|
|
53
53
|
/**
|
|
54
54
|
* Loads all ArchiveConfigurationEntity records for the given configuration,
|
|
55
55
|
* ordered by Priority for controlled execution sequence.
|
|
56
56
|
*/
|
|
57
|
-
private
|
|
57
|
+
private loadConfigurationEntities;
|
|
58
58
|
/**
|
|
59
59
|
* Creates a new ArchiveRun record to track this execution.
|
|
60
60
|
*/
|
|
61
|
-
private
|
|
61
|
+
private createArchiveRun;
|
|
62
62
|
/**
|
|
63
63
|
* Updates the ArchiveRun record with final totals and completion status.
|
|
64
64
|
*/
|
|
65
|
-
private
|
|
65
|
+
private finalizeArchiveRun;
|
|
66
66
|
/**
|
|
67
67
|
* Initializes the ArchiveStorageManager from the configuration's storage account.
|
|
68
68
|
*/
|
|
69
|
-
private
|
|
69
|
+
private initializeStorage;
|
|
70
70
|
/**
|
|
71
71
|
* Processes all configured entities sequentially (order matters for dependency safety).
|
|
72
72
|
*/
|
|
73
|
-
private
|
|
73
|
+
private processAllEntities;
|
|
74
74
|
/**
|
|
75
75
|
* Builds a failure ArchiveRunResult with zero counts.
|
|
76
76
|
*/
|
|
77
|
-
private
|
|
77
|
+
private buildFailureResult;
|
|
78
78
|
}
|
|
79
79
|
//# sourceMappingURL=ArchiveEngine.d.ts.map
|
package/dist/ArchiveEngine.js
CHANGED
|
@@ -60,27 +60,27 @@ export class ArchiveEngine extends BaseSingleton {
|
|
|
60
60
|
*/
|
|
61
61
|
async RunArchive(configId, contextUser) {
|
|
62
62
|
try {
|
|
63
|
-
const config = await this.
|
|
63
|
+
const config = await this.loadConfiguration(configId, contextUser);
|
|
64
64
|
if (!config) {
|
|
65
|
-
return this.
|
|
65
|
+
return this.buildFailureResult('', `ArchiveConfiguration not found: ${configId}`);
|
|
66
66
|
}
|
|
67
67
|
const isActive = config.Get('IsActive');
|
|
68
68
|
if (!isActive) {
|
|
69
|
-
return this.
|
|
69
|
+
return this.buildFailureResult('', `ArchiveConfiguration "${config.Get('Name')}" is not active. Set IsActive to true before running.`);
|
|
70
70
|
}
|
|
71
71
|
const status = config.Get('Status');
|
|
72
72
|
if (status === 'Running') {
|
|
73
|
-
return this.
|
|
73
|
+
return this.buildFailureResult('', `ArchiveConfiguration "${config.Get('Name')}" is already running. Wait for the current run to complete.`);
|
|
74
74
|
}
|
|
75
|
-
const configEntities = await this.
|
|
75
|
+
const configEntities = await this.loadConfigurationEntities(configId, contextUser);
|
|
76
76
|
if (configEntities.length === 0) {
|
|
77
|
-
return this.
|
|
77
|
+
return this.buildFailureResult('', 'No entity configurations found for this archive configuration');
|
|
78
78
|
}
|
|
79
|
-
const archiveRun = await this.
|
|
80
|
-
const storageManager = await this.
|
|
79
|
+
const archiveRun = await this.createArchiveRun(config, contextUser);
|
|
80
|
+
const storageManager = await this.initializeStorage(config, contextUser);
|
|
81
81
|
LogStatus(`ArchiveEngine: Starting run ${archiveRun.Get('ID')} with ${configEntities.length} entities`);
|
|
82
|
-
const totals = await this.
|
|
83
|
-
await this.
|
|
82
|
+
const totals = await this.processAllEntities(configEntities, config, archiveRun, storageManager, contextUser);
|
|
83
|
+
await this.finalizeArchiveRun(archiveRun, totals, contextUser);
|
|
84
84
|
return {
|
|
85
85
|
Success: totals.Failed === 0,
|
|
86
86
|
ArchiveRunId: archiveRun.Get('ID'),
|
|
@@ -94,7 +94,7 @@ export class ArchiveEngine extends BaseSingleton {
|
|
|
94
94
|
catch (error) {
|
|
95
95
|
const message = error instanceof Error ? error.message : String(error);
|
|
96
96
|
LogError(`ArchiveEngine.RunArchive failed: ${message}`);
|
|
97
|
-
return this.
|
|
97
|
+
return this.buildFailureResult('', message);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
// ========================================
|
|
@@ -103,7 +103,7 @@ export class ArchiveEngine extends BaseSingleton {
|
|
|
103
103
|
/**
|
|
104
104
|
* Loads the ArchiveConfiguration record by ID.
|
|
105
105
|
*/
|
|
106
|
-
async
|
|
106
|
+
async loadConfiguration(configId, contextUser) {
|
|
107
107
|
const md = this.Provider;
|
|
108
108
|
const config = await md.GetEntityObject('MJ: Archive Configurations', contextUser);
|
|
109
109
|
const loaded = await config.InnerLoad(CompositeKey.FromID(configId));
|
|
@@ -113,7 +113,7 @@ export class ArchiveEngine extends BaseSingleton {
|
|
|
113
113
|
* Loads all ArchiveConfigurationEntity records for the given configuration,
|
|
114
114
|
* ordered by Priority for controlled execution sequence.
|
|
115
115
|
*/
|
|
116
|
-
async
|
|
116
|
+
async loadConfigurationEntities(configId, contextUser) {
|
|
117
117
|
const rv = new RunView();
|
|
118
118
|
const result = await rv.RunView({
|
|
119
119
|
EntityName: 'MJ: Archive Configuration Entities',
|
|
@@ -132,7 +132,7 @@ export class ArchiveEngine extends BaseSingleton {
|
|
|
132
132
|
/**
|
|
133
133
|
* Creates a new ArchiveRun record to track this execution.
|
|
134
134
|
*/
|
|
135
|
-
async
|
|
135
|
+
async createArchiveRun(config, contextUser) {
|
|
136
136
|
const md = this.Provider;
|
|
137
137
|
const run = await md.GetEntityObject('MJ: Archive Runs', contextUser);
|
|
138
138
|
run.Set('ArchiveConfigurationID', config.Get('ID'));
|
|
@@ -149,7 +149,7 @@ export class ArchiveEngine extends BaseSingleton {
|
|
|
149
149
|
/**
|
|
150
150
|
* Updates the ArchiveRun record with final totals and completion status.
|
|
151
151
|
*/
|
|
152
|
-
async
|
|
152
|
+
async finalizeArchiveRun(archiveRun, totals, contextUser) {
|
|
153
153
|
archiveRun.Set('Status', totals.Failed > 0 ? 'PartialSuccess' : 'Complete');
|
|
154
154
|
archiveRun.Set('CompletedAt', new Date().toISOString());
|
|
155
155
|
archiveRun.Set('TotalRecords', totals.Archived + totals.Failed + totals.Skipped);
|
|
@@ -168,7 +168,7 @@ export class ArchiveEngine extends BaseSingleton {
|
|
|
168
168
|
/**
|
|
169
169
|
* Initializes the ArchiveStorageManager from the configuration's storage account.
|
|
170
170
|
*/
|
|
171
|
-
async
|
|
171
|
+
async initializeStorage(config, contextUser) {
|
|
172
172
|
const storageAccountId = config.Get('StorageAccountID');
|
|
173
173
|
if (!storageAccountId) {
|
|
174
174
|
throw new Error('ArchiveConfiguration has no StorageAccountID configured');
|
|
@@ -183,7 +183,7 @@ export class ArchiveEngine extends BaseSingleton {
|
|
|
183
183
|
/**
|
|
184
184
|
* Processes all configured entities sequentially (order matters for dependency safety).
|
|
185
185
|
*/
|
|
186
|
-
async
|
|
186
|
+
async processAllEntities(configEntities, config, archiveRun, storageManager, contextUser) {
|
|
187
187
|
const totals = { Archived: 0, Failed: 0, Skipped: 0, Bytes: 0 };
|
|
188
188
|
for (const configEntity of configEntities) {
|
|
189
189
|
const processor = new ArchiveProcessor();
|
|
@@ -205,7 +205,7 @@ export class ArchiveEngine extends BaseSingleton {
|
|
|
205
205
|
/**
|
|
206
206
|
* Builds a failure ArchiveRunResult with zero counts.
|
|
207
207
|
*/
|
|
208
|
-
|
|
208
|
+
buildFailureResult(archiveRunId, errorMessage) {
|
|
209
209
|
return {
|
|
210
210
|
Success: false,
|
|
211
211
|
ArchiveRunId: archiveRunId,
|
|
@@ -38,45 +38,45 @@ export declare class ArchiveProcessor {
|
|
|
38
38
|
* Resolves the archive driver by class name via ClassFactory, falling back
|
|
39
39
|
* to DefaultArchiveDriver if no class name is specified or resolution fails.
|
|
40
40
|
*/
|
|
41
|
-
private
|
|
41
|
+
private resolveDriver;
|
|
42
42
|
/**
|
|
43
43
|
* Parses the FieldConfiguration JSON column from the ArchiveConfigurationEntity record.
|
|
44
44
|
* The FieldConfiguration column is NVARCHAR(MAX) storing JSON conforming to ArchiveFieldConfiguration.
|
|
45
45
|
*/
|
|
46
|
-
private
|
|
46
|
+
private parseFieldConfiguration;
|
|
47
47
|
/**
|
|
48
48
|
* Validates the parsed field configuration has the required structure.
|
|
49
49
|
*/
|
|
50
|
-
private
|
|
50
|
+
private validateFieldConfiguration;
|
|
51
51
|
/**
|
|
52
52
|
* Builds the SQL filter expression for querying eligible records.
|
|
53
53
|
* Combines the retention date filter with any custom filter expression.
|
|
54
54
|
*/
|
|
55
|
-
private
|
|
55
|
+
private buildRecordFilter;
|
|
56
56
|
/**
|
|
57
57
|
* Loads records matching the archive filter from the database.
|
|
58
58
|
*/
|
|
59
|
-
private
|
|
59
|
+
private loadEligibleRecords;
|
|
60
60
|
/**
|
|
61
61
|
* Determines the batch size from the entity config or parent config defaults.
|
|
62
62
|
*/
|
|
63
|
-
private
|
|
63
|
+
private getBatchSize;
|
|
64
64
|
/**
|
|
65
65
|
* Processes records in batches, calling the driver for each record and logging results.
|
|
66
66
|
*/
|
|
67
|
-
private
|
|
67
|
+
private processRecordBatches;
|
|
68
68
|
/**
|
|
69
69
|
* Processes a single batch of records.
|
|
70
70
|
*/
|
|
71
|
-
private
|
|
71
|
+
private processSingleBatch;
|
|
72
72
|
/**
|
|
73
73
|
* Processes a single record: checks eligibility, archives, and logs the detail.
|
|
74
74
|
* Returns 'skipped', 'failed', or the number of bytes archived.
|
|
75
75
|
*/
|
|
76
|
-
private
|
|
76
|
+
private processSingleRecord;
|
|
77
77
|
/**
|
|
78
78
|
* Creates an ArchiveRunDetail record to log the result of archiving a single record.
|
|
79
79
|
*/
|
|
80
|
-
private
|
|
80
|
+
private logArchiveRunDetail;
|
|
81
81
|
}
|
|
82
82
|
//# sourceMappingURL=ArchiveProcessor.d.ts.map
|
package/dist/ArchiveProcessor.js
CHANGED
|
@@ -27,15 +27,15 @@ export class ArchiveProcessor {
|
|
|
27
27
|
async ProcessEntity(configEntity, config, archiveRun, storageManager, contextUser) {
|
|
28
28
|
const entityName = configEntity.Get('Entity');
|
|
29
29
|
const driverClassName = configEntity.Get('DriverClass');
|
|
30
|
-
const batchSize = this.
|
|
30
|
+
const batchSize = this.getBatchSize(configEntity, config);
|
|
31
31
|
const basePath = config.Get('RootPath') ?? '';
|
|
32
32
|
LogStatus(`ArchiveProcessor: Starting processing for entity "${entityName}" (batch size: ${batchSize})`);
|
|
33
|
-
const driver = this.
|
|
34
|
-
const fieldConfig = this.
|
|
35
|
-
const filter = this.
|
|
36
|
-
const records = await this.
|
|
33
|
+
const driver = this.resolveDriver(driverClassName);
|
|
34
|
+
const fieldConfig = this.parseFieldConfiguration(configEntity);
|
|
35
|
+
const filter = this.buildRecordFilter(configEntity);
|
|
36
|
+
const records = await this.loadEligibleRecords(entityName, filter, contextUser);
|
|
37
37
|
LogStatus(`ArchiveProcessor: Found ${records.length} eligible records for "${entityName}"`);
|
|
38
|
-
return this.
|
|
38
|
+
return this.processRecordBatches(records, driver, fieldConfig, configEntity, config, archiveRun, storageManager, basePath, batchSize, contextUser);
|
|
39
39
|
}
|
|
40
40
|
// ========================================
|
|
41
41
|
// Driver Resolution
|
|
@@ -44,7 +44,7 @@ export class ArchiveProcessor {
|
|
|
44
44
|
* Resolves the archive driver by class name via ClassFactory, falling back
|
|
45
45
|
* to DefaultArchiveDriver if no class name is specified or resolution fails.
|
|
46
46
|
*/
|
|
47
|
-
|
|
47
|
+
resolveDriver(driverClassName) {
|
|
48
48
|
if (driverClassName) {
|
|
49
49
|
const driver = MJGlobal.Instance.ClassFactory.CreateInstance(BaseArchiveDriver, driverClassName);
|
|
50
50
|
if (driver) {
|
|
@@ -61,14 +61,14 @@ export class ArchiveProcessor {
|
|
|
61
61
|
* Parses the FieldConfiguration JSON column from the ArchiveConfigurationEntity record.
|
|
62
62
|
* The FieldConfiguration column is NVARCHAR(MAX) storing JSON conforming to ArchiveFieldConfiguration.
|
|
63
63
|
*/
|
|
64
|
-
|
|
64
|
+
parseFieldConfiguration(configEntity) {
|
|
65
65
|
const fieldConfigJson = configEntity.Get('FieldConfiguration');
|
|
66
66
|
if (!fieldConfigJson) {
|
|
67
67
|
throw new Error(`ArchiveConfigurationEntity ${configEntity.Get('ID')} has no FieldConfiguration`);
|
|
68
68
|
}
|
|
69
69
|
try {
|
|
70
70
|
const parsed = JSON.parse(fieldConfigJson);
|
|
71
|
-
this.
|
|
71
|
+
this.validateFieldConfiguration(parsed);
|
|
72
72
|
return parsed;
|
|
73
73
|
}
|
|
74
74
|
catch (error) {
|
|
@@ -79,7 +79,7 @@ export class ArchiveProcessor {
|
|
|
79
79
|
/**
|
|
80
80
|
* Validates the parsed field configuration has the required structure.
|
|
81
81
|
*/
|
|
82
|
-
|
|
82
|
+
validateFieldConfiguration(config) {
|
|
83
83
|
if (!config.Fields || !Array.isArray(config.Fields)) {
|
|
84
84
|
throw new Error('FieldConfiguration must have a Fields array');
|
|
85
85
|
}
|
|
@@ -100,7 +100,7 @@ export class ArchiveProcessor {
|
|
|
100
100
|
* Builds the SQL filter expression for querying eligible records.
|
|
101
101
|
* Combines the retention date filter with any custom filter expression.
|
|
102
102
|
*/
|
|
103
|
-
|
|
103
|
+
buildRecordFilter(configEntity) {
|
|
104
104
|
const retentionDays = configEntity.Get('RetentionDays');
|
|
105
105
|
const dateField = configEntity.Get('DateField');
|
|
106
106
|
const customFilter = configEntity.Get('FilterExpression');
|
|
@@ -119,7 +119,7 @@ export class ArchiveProcessor {
|
|
|
119
119
|
/**
|
|
120
120
|
* Loads records matching the archive filter from the database.
|
|
121
121
|
*/
|
|
122
|
-
async
|
|
122
|
+
async loadEligibleRecords(entityName, filter, contextUser) {
|
|
123
123
|
const rv = new RunView();
|
|
124
124
|
const result = await rv.RunView({
|
|
125
125
|
EntityName: entityName,
|
|
@@ -137,7 +137,7 @@ export class ArchiveProcessor {
|
|
|
137
137
|
/**
|
|
138
138
|
* Determines the batch size from the entity config or parent config defaults.
|
|
139
139
|
*/
|
|
140
|
-
|
|
140
|
+
getBatchSize(configEntity, config) {
|
|
141
141
|
const entityBatchSize = configEntity.Get('BatchSize');
|
|
142
142
|
const defaultBatchSize = config.Get('DefaultBatchSize');
|
|
143
143
|
return entityBatchSize ?? defaultBatchSize ?? 100;
|
|
@@ -145,11 +145,11 @@ export class ArchiveProcessor {
|
|
|
145
145
|
/**
|
|
146
146
|
* Processes records in batches, calling the driver for each record and logging results.
|
|
147
147
|
*/
|
|
148
|
-
async
|
|
148
|
+
async processRecordBatches(records, driver, fieldConfig, configEntity, config, archiveRun, storageManager, basePath, batchSize, contextUser) {
|
|
149
149
|
const totals = { Archived: 0, Failed: 0, Skipped: 0, Bytes: 0 };
|
|
150
150
|
for (let offset = 0; offset < records.length; offset += batchSize) {
|
|
151
151
|
const batch = records.slice(offset, offset + batchSize);
|
|
152
|
-
const batchResult = await this.
|
|
152
|
+
const batchResult = await this.processSingleBatch(batch, driver, fieldConfig, configEntity, config, archiveRun, storageManager, basePath, contextUser);
|
|
153
153
|
totals.Archived += batchResult.Archived;
|
|
154
154
|
totals.Failed += batchResult.Failed;
|
|
155
155
|
totals.Skipped += batchResult.Skipped;
|
|
@@ -160,10 +160,10 @@ export class ArchiveProcessor {
|
|
|
160
160
|
/**
|
|
161
161
|
* Processes a single batch of records.
|
|
162
162
|
*/
|
|
163
|
-
async
|
|
163
|
+
async processSingleBatch(batch, driver, fieldConfig, configEntity, config, archiveRun, storageManager, basePath, contextUser) {
|
|
164
164
|
const result = { Archived: 0, Failed: 0, Skipped: 0, Bytes: 0 };
|
|
165
165
|
for (const record of batch) {
|
|
166
|
-
const recordResult = await this.
|
|
166
|
+
const recordResult = await this.processSingleRecord(record, driver, fieldConfig, configEntity, config, archiveRun, storageManager, basePath, contextUser);
|
|
167
167
|
if (recordResult === 'skipped') {
|
|
168
168
|
result.Skipped++;
|
|
169
169
|
}
|
|
@@ -181,7 +181,7 @@ export class ArchiveProcessor {
|
|
|
181
181
|
* Processes a single record: checks eligibility, archives, and logs the detail.
|
|
182
182
|
* Returns 'skipped', 'failed', or the number of bytes archived.
|
|
183
183
|
*/
|
|
184
|
-
async
|
|
184
|
+
async processSingleRecord(record, driver, fieldConfig, configEntity, config, archiveRun, storageManager, basePath, contextUser) {
|
|
185
185
|
const context = {
|
|
186
186
|
Record: record,
|
|
187
187
|
FieldConfig: fieldConfig,
|
|
@@ -196,7 +196,7 @@ export class ArchiveProcessor {
|
|
|
196
196
|
return 'skipped';
|
|
197
197
|
}
|
|
198
198
|
const archiveResult = await driver.ArchiveRecord(context);
|
|
199
|
-
await this.
|
|
199
|
+
await this.logArchiveRunDetail(archiveRun, record, archiveResult, contextUser);
|
|
200
200
|
if (!archiveResult.Success) {
|
|
201
201
|
LogError(`Failed to archive record ${record.PrimaryKey.Values()} of "${record.EntityInfo.Name}": ${archiveResult.ErrorMessage}`);
|
|
202
202
|
return 'failed';
|
|
@@ -206,7 +206,7 @@ export class ArchiveProcessor {
|
|
|
206
206
|
/**
|
|
207
207
|
* Creates an ArchiveRunDetail record to log the result of archiving a single record.
|
|
208
208
|
*/
|
|
209
|
-
async
|
|
209
|
+
async logArchiveRunDetail(archiveRun, record, archiveResult, contextUser) {
|
|
210
210
|
try {
|
|
211
211
|
const md = this.Provider;
|
|
212
212
|
const detail = await md.GetEntityObject('MJ: Archive Run Details', contextUser);
|
|
@@ -32,20 +32,20 @@ export declare class ArchiveRecovery {
|
|
|
32
32
|
/**
|
|
33
33
|
* Loads a single ArchiveRunDetail record by ID.
|
|
34
34
|
*/
|
|
35
|
-
private
|
|
35
|
+
private loadArchiveRunDetail;
|
|
36
36
|
/**
|
|
37
37
|
* Loads a single ArchiveRun record by ID.
|
|
38
38
|
*/
|
|
39
|
-
private
|
|
39
|
+
private loadArchiveRun;
|
|
40
40
|
/**
|
|
41
41
|
* Initializes a storage driver from the ArchiveRun's associated configuration.
|
|
42
42
|
*/
|
|
43
|
-
private
|
|
43
|
+
private initializeStorageDriver;
|
|
44
44
|
/**
|
|
45
45
|
* Returns the DefaultArchiveDriver for restore operations.
|
|
46
46
|
* ArchiveRunDetail does not store the driver class used during archiving;
|
|
47
47
|
* the default driver handles the standard JSON-based restore path.
|
|
48
48
|
*/
|
|
49
|
-
private
|
|
49
|
+
private resolveDriver;
|
|
50
50
|
}
|
|
51
51
|
//# sourceMappingURL=ArchiveRecovery.d.ts.map
|
package/dist/ArchiveRecovery.js
CHANGED
|
@@ -47,16 +47,16 @@ export class ArchiveRecovery {
|
|
|
47
47
|
*/
|
|
48
48
|
async RestoreVersion(archiveRunDetailId, contextUser) {
|
|
49
49
|
try {
|
|
50
|
-
const detail = await this.
|
|
50
|
+
const detail = await this.loadArchiveRunDetail(archiveRunDetailId, contextUser);
|
|
51
51
|
if (!detail) {
|
|
52
52
|
return { Success: false, ErrorMessage: `ArchiveRunDetail not found: ${archiveRunDetailId}`, RestoredFields: [] };
|
|
53
53
|
}
|
|
54
|
-
const archiveRun = await this.
|
|
54
|
+
const archiveRun = await this.loadArchiveRun(detail.Get('ArchiveRunID'), contextUser);
|
|
55
55
|
if (!archiveRun) {
|
|
56
56
|
return { Success: false, ErrorMessage: 'Failed to load parent ArchiveRun', RestoredFields: [] };
|
|
57
57
|
}
|
|
58
|
-
const storageDriver = await this.
|
|
59
|
-
const driver = this.
|
|
58
|
+
const storageDriver = await this.initializeStorageDriver(archiveRun, contextUser);
|
|
59
|
+
const driver = this.resolveDriver();
|
|
60
60
|
const restoreContext = {
|
|
61
61
|
ArchiveRunDetail: detail,
|
|
62
62
|
StorageDriver: storageDriver,
|
|
@@ -78,7 +78,7 @@ export class ArchiveRecovery {
|
|
|
78
78
|
/**
|
|
79
79
|
* Loads a single ArchiveRunDetail record by ID.
|
|
80
80
|
*/
|
|
81
|
-
async
|
|
81
|
+
async loadArchiveRunDetail(detailId, contextUser) {
|
|
82
82
|
const md = this.Provider;
|
|
83
83
|
const detail = await md.GetEntityObject('MJ: Archive Run Details', contextUser);
|
|
84
84
|
const loaded = await detail.InnerLoad(CompositeKey.FromID(detailId));
|
|
@@ -87,7 +87,7 @@ export class ArchiveRecovery {
|
|
|
87
87
|
/**
|
|
88
88
|
* Loads a single ArchiveRun record by ID.
|
|
89
89
|
*/
|
|
90
|
-
async
|
|
90
|
+
async loadArchiveRun(archiveRunId, contextUser) {
|
|
91
91
|
const md = this.Provider;
|
|
92
92
|
const run = await md.GetEntityObject('MJ: Archive Runs', contextUser);
|
|
93
93
|
const loaded = await run.InnerLoad(CompositeKey.FromID(archiveRunId));
|
|
@@ -96,7 +96,7 @@ export class ArchiveRecovery {
|
|
|
96
96
|
/**
|
|
97
97
|
* Initializes a storage driver from the ArchiveRun's associated configuration.
|
|
98
98
|
*/
|
|
99
|
-
async
|
|
99
|
+
async initializeStorageDriver(archiveRun, contextUser) {
|
|
100
100
|
const configId = archiveRun.Get('ArchiveConfigurationID');
|
|
101
101
|
const md = this.Provider;
|
|
102
102
|
const config = await md.GetEntityObject('MJ: Archive Configurations', contextUser);
|
|
@@ -124,7 +124,7 @@ export class ArchiveRecovery {
|
|
|
124
124
|
* ArchiveRunDetail does not store the driver class used during archiving;
|
|
125
125
|
* the default driver handles the standard JSON-based restore path.
|
|
126
126
|
*/
|
|
127
|
-
|
|
127
|
+
resolveDriver() {
|
|
128
128
|
return new DefaultArchiveDriver();
|
|
129
129
|
}
|
|
130
130
|
}
|
|
@@ -33,66 +33,66 @@ export declare class DefaultArchiveDriver extends BaseArchiveDriver {
|
|
|
33
33
|
* Determines which fields to check for the "should archive" decision.
|
|
34
34
|
* Uses SkipIfAllNullFields if configured, otherwise all active field names.
|
|
35
35
|
*/
|
|
36
|
-
private
|
|
36
|
+
private getFieldsToCheck;
|
|
37
37
|
/**
|
|
38
38
|
* Serializes the archive document and writes it to storage.
|
|
39
39
|
*/
|
|
40
|
-
private
|
|
40
|
+
private writeDocumentToStorage;
|
|
41
41
|
/**
|
|
42
42
|
* Resolves the effective archive mode from the entity config or parent config.
|
|
43
43
|
*/
|
|
44
|
-
private
|
|
44
|
+
private resolveMode;
|
|
45
45
|
/**
|
|
46
46
|
* Applies the appropriate post-archive action based on the resolved mode.
|
|
47
47
|
*/
|
|
48
|
-
private
|
|
48
|
+
private applyPostArchiveAction;
|
|
49
49
|
/**
|
|
50
50
|
* Sets all configured archive fields to their empty value on the source record and saves it.
|
|
51
51
|
* Uses null for nullable columns and empty string for NOT NULL string columns.
|
|
52
52
|
*/
|
|
53
|
-
private
|
|
53
|
+
private nullifyArchivedFields;
|
|
54
54
|
/**
|
|
55
55
|
* Returns the appropriate empty value for a field based on its nullability.
|
|
56
56
|
* NOT NULL string fields get empty string; nullable fields get null.
|
|
57
57
|
*/
|
|
58
|
-
private
|
|
58
|
+
private getEmptyValueForField;
|
|
59
59
|
/**
|
|
60
60
|
* Deletes the source record from the database after successful archival.
|
|
61
61
|
* Automatically cascades to dependent (child) records via FK relationships
|
|
62
62
|
* discovered from entity metadata, archiving each child to storage before deleting it.
|
|
63
63
|
*/
|
|
64
|
-
private
|
|
64
|
+
private hardDeleteRecord;
|
|
65
65
|
/**
|
|
66
66
|
* Collects all dependent records depth-first, writes one batch archive file
|
|
67
67
|
* per entity to storage, then deletes all records leaf-first. This batched
|
|
68
68
|
* approach dramatically reduces storage API calls compared to per-record writes.
|
|
69
69
|
*/
|
|
70
|
-
private
|
|
70
|
+
private archiveAndDeleteDependentRecords;
|
|
71
71
|
/**
|
|
72
72
|
* Recursively collects all dependent records depth-first. Leaves (deepest
|
|
73
73
|
* children) are added to the array first, so deleting in array order
|
|
74
74
|
* respects FK constraints. Uses a visited set to prevent infinite loops
|
|
75
75
|
* from circular references.
|
|
76
76
|
*/
|
|
77
|
-
private
|
|
77
|
+
private collectDependentsDepthFirst;
|
|
78
78
|
/**
|
|
79
79
|
* Groups collected records by entity name and writes one batch archive
|
|
80
80
|
* document per entity to storage. A batch document contains all records
|
|
81
81
|
* of that entity type in a single JSON array, dramatically reducing
|
|
82
82
|
* the number of storage API calls.
|
|
83
83
|
*/
|
|
84
|
-
private
|
|
84
|
+
private batchArchiveDependents;
|
|
85
85
|
/**
|
|
86
86
|
* Loads all records from a child entity that reference a parent record via a join field.
|
|
87
87
|
*/
|
|
88
|
-
private
|
|
88
|
+
private loadChildRecords;
|
|
89
89
|
/**
|
|
90
90
|
* Reads and parses an archive document from storage.
|
|
91
91
|
*/
|
|
92
|
-
private
|
|
92
|
+
private readDocumentFromStorage;
|
|
93
93
|
/**
|
|
94
94
|
* Applies the archived field values from the document back onto the entity record and saves.
|
|
95
95
|
*/
|
|
96
|
-
private
|
|
96
|
+
private applyArchivedFieldsToRecord;
|
|
97
97
|
}
|
|
98
98
|
//# sourceMappingURL=DefaultArchiveDriver.d.ts.map
|
|
@@ -21,7 +21,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
21
21
|
* always archives — the intent is a full record snapshot, not field stripping.
|
|
22
22
|
*/
|
|
23
23
|
ShouldArchiveRecord(context) {
|
|
24
|
-
const fieldsToCheck = this.
|
|
24
|
+
const fieldsToCheck = this.getFieldsToCheck(context);
|
|
25
25
|
// If no specific fields are configured but ArchiveFullRecord is true,
|
|
26
26
|
// always archive — the intent is a full record snapshot, not field stripping
|
|
27
27
|
if (fieldsToCheck.length === 0) {
|
|
@@ -46,11 +46,11 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
46
46
|
const versionStamp = new Date();
|
|
47
47
|
const document = this.BuildArchiveDocument(context, versionStamp);
|
|
48
48
|
const storagePath = this.BuildStoragePath(context.BasePath, context.Record.EntityInfo.Name, document.recordId, versionStamp);
|
|
49
|
-
const writeResult = await this.
|
|
49
|
+
const writeResult = await this.writeDocumentToStorage(context, storagePath, document);
|
|
50
50
|
if (!writeResult.Success) {
|
|
51
51
|
return writeResult;
|
|
52
52
|
}
|
|
53
|
-
const postArchiveResult = await this.
|
|
53
|
+
const postArchiveResult = await this.applyPostArchiveAction(context);
|
|
54
54
|
if (!postArchiveResult.Success) {
|
|
55
55
|
return postArchiveResult;
|
|
56
56
|
}
|
|
@@ -81,8 +81,8 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
81
81
|
if (!storagePath) {
|
|
82
82
|
return { Success: false, ErrorMessage: 'No StoragePath on ArchiveRunDetail', RestoredFields: [] };
|
|
83
83
|
}
|
|
84
|
-
const document = await this.
|
|
85
|
-
const restoredFields = await this.
|
|
84
|
+
const document = await this.readDocumentFromStorage(context, storagePath);
|
|
85
|
+
const restoredFields = await this.applyArchivedFieldsToRecord(context, document);
|
|
86
86
|
return {
|
|
87
87
|
Success: true,
|
|
88
88
|
RestoredFields: restoredFields,
|
|
@@ -105,7 +105,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
105
105
|
* Determines which fields to check for the "should archive" decision.
|
|
106
106
|
* Uses SkipIfAllNullFields if configured, otherwise all active field names.
|
|
107
107
|
*/
|
|
108
|
-
|
|
108
|
+
getFieldsToCheck(context) {
|
|
109
109
|
if (context.FieldConfig.SkipIfAllNullFields && context.FieldConfig.SkipIfAllNullFields.length > 0) {
|
|
110
110
|
return context.FieldConfig.SkipIfAllNullFields;
|
|
111
111
|
}
|
|
@@ -119,7 +119,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
119
119
|
/**
|
|
120
120
|
* Serializes the archive document and writes it to storage.
|
|
121
121
|
*/
|
|
122
|
-
async
|
|
122
|
+
async writeDocumentToStorage(context, storagePath, document) {
|
|
123
123
|
const jsonContent = JSON.stringify(document, null, 2);
|
|
124
124
|
const buffer = Buffer.from(jsonContent, 'utf8');
|
|
125
125
|
const success = await context.StorageDriver.PutObject(storagePath, buffer, 'application/json');
|
|
@@ -140,7 +140,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
140
140
|
/**
|
|
141
141
|
* Resolves the effective archive mode from the entity config or parent config.
|
|
142
142
|
*/
|
|
143
|
-
|
|
143
|
+
resolveMode(context) {
|
|
144
144
|
return context.ConfigEntity.Get('Mode')
|
|
145
145
|
?? context.Config.Get('DefaultMode')
|
|
146
146
|
?? 'StripFields';
|
|
@@ -148,13 +148,13 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
148
148
|
/**
|
|
149
149
|
* Applies the appropriate post-archive action based on the resolved mode.
|
|
150
150
|
*/
|
|
151
|
-
async
|
|
152
|
-
const mode = this.
|
|
151
|
+
async applyPostArchiveAction(context) {
|
|
152
|
+
const mode = this.resolveMode(context);
|
|
153
153
|
switch (mode) {
|
|
154
154
|
case 'StripFields':
|
|
155
|
-
return this.
|
|
155
|
+
return this.nullifyArchivedFields(context);
|
|
156
156
|
case 'HardDelete':
|
|
157
|
-
return this.
|
|
157
|
+
return this.hardDeleteRecord(context);
|
|
158
158
|
case 'ArchiveOnly':
|
|
159
159
|
return { Success: true, StoragePath: null, BytesArchived: 0 };
|
|
160
160
|
default:
|
|
@@ -173,10 +173,10 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
173
173
|
* Sets all configured archive fields to their empty value on the source record and saves it.
|
|
174
174
|
* Uses null for nullable columns and empty string for NOT NULL string columns.
|
|
175
175
|
*/
|
|
176
|
-
async
|
|
176
|
+
async nullifyArchivedFields(context) {
|
|
177
177
|
for (const fieldConfig of context.FieldConfig.Fields) {
|
|
178
178
|
if (fieldConfig.IsActive !== false) {
|
|
179
|
-
const emptyValue = this.
|
|
179
|
+
const emptyValue = this.getEmptyValueForField(context, fieldConfig.FieldName);
|
|
180
180
|
context.Record.Set(fieldConfig.FieldName, emptyValue);
|
|
181
181
|
}
|
|
182
182
|
}
|
|
@@ -195,7 +195,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
195
195
|
* Returns the appropriate empty value for a field based on its nullability.
|
|
196
196
|
* NOT NULL string fields get empty string; nullable fields get null.
|
|
197
197
|
*/
|
|
198
|
-
|
|
198
|
+
getEmptyValueForField(context, fieldName) {
|
|
199
199
|
const fieldInfo = context.Record.EntityInfo.FieldByName(fieldName);
|
|
200
200
|
if (fieldInfo && !fieldInfo.AllowsNull) {
|
|
201
201
|
return '';
|
|
@@ -210,8 +210,8 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
210
210
|
* Automatically cascades to dependent (child) records via FK relationships
|
|
211
211
|
* discovered from entity metadata, archiving each child to storage before deleting it.
|
|
212
212
|
*/
|
|
213
|
-
async
|
|
214
|
-
const cascadeResult = await this.
|
|
213
|
+
async hardDeleteRecord(context) {
|
|
214
|
+
const cascadeResult = await this.archiveAndDeleteDependentRecords(context);
|
|
215
215
|
if (!cascadeResult.Success) {
|
|
216
216
|
return cascadeResult;
|
|
217
217
|
}
|
|
@@ -231,16 +231,16 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
231
231
|
* per entity to storage, then deletes all records leaf-first. This batched
|
|
232
232
|
* approach dramatically reduces storage API calls compared to per-record writes.
|
|
233
233
|
*/
|
|
234
|
-
async
|
|
234
|
+
async archiveAndDeleteDependentRecords(context) {
|
|
235
235
|
// 1. Collect all dependent records depth-first (leaves first in the resulting array)
|
|
236
236
|
const collectedRecords = [];
|
|
237
|
-
await this.
|
|
237
|
+
await this.collectDependentsDepthFirst(context.Record, context.ContextUser, collectedRecords, new Set());
|
|
238
238
|
if (collectedRecords.length === 0) {
|
|
239
239
|
return { Success: true, StoragePath: null, BytesArchived: 0 };
|
|
240
240
|
}
|
|
241
241
|
LogStatus(`HardDelete cascade: collected ${collectedRecords.length} dependent record(s) across ${new Set(collectedRecords.map(r => r.EntityInfo.Name)).size} entity type(s)`);
|
|
242
242
|
// 2. Batch-archive: group records by entity name and write one file per entity
|
|
243
|
-
const archiveResult = await this.
|
|
243
|
+
const archiveResult = await this.batchArchiveDependents(collectedRecords, context);
|
|
244
244
|
if (!archiveResult.Success) {
|
|
245
245
|
return archiveResult;
|
|
246
246
|
}
|
|
@@ -264,7 +264,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
264
264
|
* respects FK constraints. Uses a visited set to prevent infinite loops
|
|
265
265
|
* from circular references.
|
|
266
266
|
*/
|
|
267
|
-
async
|
|
267
|
+
async collectDependentsDepthFirst(parentRecord, contextUser, collected, visited) {
|
|
268
268
|
const entityInfo = parentRecord.EntityInfo;
|
|
269
269
|
const recordKey = `${entityInfo.ID}:${parentRecord.PrimaryKey.Values()}`;
|
|
270
270
|
if (visited.has(recordKey))
|
|
@@ -276,10 +276,10 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
276
276
|
const joinField = rel.RelatedEntityJoinField;
|
|
277
277
|
if (!childEntityName || !joinField)
|
|
278
278
|
continue;
|
|
279
|
-
const childRecords = await this.
|
|
279
|
+
const childRecords = await this.loadChildRecords(childEntityName, joinField, parentRecord.PrimaryKey.Values(), contextUser);
|
|
280
280
|
for (const child of childRecords) {
|
|
281
281
|
// Recurse into grandchildren first (depth-first)
|
|
282
|
-
await this.
|
|
282
|
+
await this.collectDependentsDepthFirst(child, contextUser, collected, visited);
|
|
283
283
|
// Then add this child (so it appears after its own dependents)
|
|
284
284
|
const childKey = `${child.EntityInfo.ID}:${child.PrimaryKey.Values()}`;
|
|
285
285
|
if (!visited.has(childKey)) {
|
|
@@ -295,7 +295,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
295
295
|
* of that entity type in a single JSON array, dramatically reducing
|
|
296
296
|
* the number of storage API calls.
|
|
297
297
|
*/
|
|
298
|
-
async
|
|
298
|
+
async batchArchiveDependents(records, context) {
|
|
299
299
|
// Group by entity name
|
|
300
300
|
const byEntity = new Map();
|
|
301
301
|
for (const record of records) {
|
|
@@ -346,7 +346,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
346
346
|
/**
|
|
347
347
|
* Loads all records from a child entity that reference a parent record via a join field.
|
|
348
348
|
*/
|
|
349
|
-
async
|
|
349
|
+
async loadChildRecords(entityName, joinField, parentRecordId, contextUser) {
|
|
350
350
|
const rv = new RunView();
|
|
351
351
|
const escapedId = parentRecordId.replace(/'/g, "''");
|
|
352
352
|
const result = await rv.RunView({
|
|
@@ -366,7 +366,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
366
366
|
/**
|
|
367
367
|
* Reads and parses an archive document from storage.
|
|
368
368
|
*/
|
|
369
|
-
async
|
|
369
|
+
async readDocumentFromStorage(context, storagePath) {
|
|
370
370
|
const buffer = await context.StorageDriver.GetObject({ fullPath: storagePath });
|
|
371
371
|
const jsonContent = buffer.toString('utf8');
|
|
372
372
|
return JSON.parse(jsonContent);
|
|
@@ -374,7 +374,7 @@ let DefaultArchiveDriver = class DefaultArchiveDriver extends BaseArchiveDriver
|
|
|
374
374
|
/**
|
|
375
375
|
* Applies the archived field values from the document back onto the entity record and saves.
|
|
376
376
|
*/
|
|
377
|
-
async
|
|
377
|
+
async applyArchivedFieldsToRecord(context, document) {
|
|
378
378
|
const entityName = document.entityName;
|
|
379
379
|
const md = this.Provider;
|
|
380
380
|
const record = await md.GetEntityObject(entityName, context.ContextUser);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/archiving-engine",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "6.1
|
|
4
|
+
"version": "6.2.0-edge.1",
|
|
5
5
|
"description": "Core archiving engine for MemberJunction - handles field-level archiving, storage, and recovery of entity records.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -15,10 +15,10 @@
|
|
|
15
15
|
"typescript": "^5.9.3"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@memberjunction/core": "6.1
|
|
19
|
-
"@memberjunction/core-entities": "6.1
|
|
20
|
-
"@memberjunction/global": "6.1
|
|
21
|
-
"@memberjunction/storage": "6.1
|
|
18
|
+
"@memberjunction/core": "6.2.0-edge.1",
|
|
19
|
+
"@memberjunction/core-entities": "6.2.0-edge.1",
|
|
20
|
+
"@memberjunction/global": "6.2.0-edge.1",
|
|
21
|
+
"@memberjunction/storage": "6.2.0-edge.1"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|