From e99ed09d4c8eb4c85af0f18aa380f7d9fa04501c Mon Sep 17 00:00:00 2001 From: Chris Ward Date: Tue, 14 Jan 2025 19:46:18 -0800 Subject: [PATCH] feat(all): Add `updated_at` to all entities (#763) --- scripts/dbRefresh.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/scripts/dbRefresh.ts b/scripts/dbRefresh.ts index 7e997a8a1..e59f06871 100644 --- a/scripts/dbRefresh.ts +++ b/scripts/dbRefresh.ts @@ -1,4 +1,4 @@ -import { readdirSync, unlinkSync, writeFileSync } from 'fs'; +import { readdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs'; import { execSync } from 'child_process'; @@ -63,13 +63,30 @@ const uploadTablesFromFolder = (jsonDbDir: string, collectionPrefix = '') => { // --jsonArray // --drop console.log(`importing ${dataName}...`); + + // Read the JSON file + const data = JSON.parse(readFileSync(filepath, 'utf8')); + + // Add updated_at field to each record + const updatedData = data.map((record: any) => ({ + ...record, + updated_at: new Date().toISOString(), + })); + + // Write the modified data to a temporary file + const tempFilepath = `${jsonDbDir}/temp-${filename}`; + writeFileSync(tempFilepath, JSON.stringify(updatedData, null, 2), 'utf8'); + const exec_string = `mongoimport --uri ${mongodbUri}` + ` --collection ${collectionName}` + - ` --file ${filepath}` + + ` --file ${tempFilepath}` + ' --jsonArray' + ' --drop'; execSync(exec_string); + + // Remove the temporary file + unlinkSync(tempFilepath); }); // Make collections table