farmcontrol-ws/scripts/ensure-references.js
Tom Butcher c94dc058ef
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good
Add ensure-references script and update package.json
- Introduced a new script `ensure-references.js` to ensure all documents have a `_reference` field, enhancing data integrity across models.
- Updated `package.json` to include the new script in the npm scripts section for easy execution.
- Refactored database utility functions to exclude additional models from audit logging, improving data privacy.
- Enhanced various schemas with new fields and methods for better tax calculations and inventory management.
2026-09-02 20:55:38 +01:00

105 lines
2.5 KiB
JavaScript

import { editObject } from '../src/database/database.js';
import { generateId } from '../src/database/utils.js';
import { models } from '../src/database/schemas/models.js';
import { mongoServer } from '../src/database/mongo.js';
import { natsServer } from '../src/database/nats.js';
import { redisServer } from '../src/database/redis.js';
const MISSING_REFERENCE_FILTER = {
$or: [
{ _reference: { $exists: false } },
{ _reference: null },
{ _reference: '' },
],
};
const dryRun = process.argv.includes('--dry-run');
async function ensureReferences() {
console.log(
dryRun
? 'Scanning for documents missing _reference (dry run)...'
: 'Ensuring all documents have a _reference...'
);
await mongoServer.connect();
await natsServer.connect();
await redisServer.connect();
let totalMissing = 0;
let totalUpdated = 0;
let totalFailed = 0;
for (const [prefix, entry] of Object.entries(models)) {
const model = entry.model;
if (!model?.schema?.path('_reference')) {
continue;
}
const missing = await model.find(MISSING_REFERENCE_FILTER).select('_id').lean();
if (missing.length === 0) {
continue;
}
console.log(`[${prefix}] ${entry.label}: ${missing.length} missing`);
totalMissing += missing.length;
if (dryRun) {
continue;
}
for (const doc of missing) {
const _reference = generateId()();
const result = await editObject({
model,
id: doc._id,
updateData: { _reference },
auditLog: false,
notify: false,
recalculate: false,
});
if (result?.error) {
totalFailed += 1;
console.error(
` Failed ${doc._id}: ${result.error}${result.code ? ` (${result.code})` : ''}`
);
continue;
}
totalUpdated += 1;
console.log(` ${doc._id} -> ${_reference}`);
}
}
console.log(
dryRun
? `Done. ${totalMissing} document(s) missing _reference.`
: `Done. ${totalUpdated} updated, ${totalFailed} failed, ${totalMissing} found.`
);
}
try {
await ensureReferences();
} catch (error) {
console.error('ensure-references failed:', error);
process.exitCode = 1;
} finally {
try {
await redisServer.disconnect();
} catch {
// ignore disconnect errors on shutdown
}
try {
await mongoServer.disconnect();
} catch {
// ignore disconnect errors on shutdown
}
try {
await natsServer.disconnect();
} catch {
// ignore disconnect errors on shutdown
}
process.exit(process.exitCode ?? 0);
}