-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-db.ts
More file actions
44 lines (36 loc) · 1.21 KB
/
Copy pathsetup-db.ts
File metadata and controls
44 lines (36 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import * as mongoose from 'mongoose';
import { Record, RecordSchema } from './src/api/schemas/record.schema';
import * as fs from 'fs';
import { AppConfig } from './src/app.config';
import * as readline from 'readline';
async function setupDatabase() {
try {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
'Do you want to clean up the existing records collection? (Y/N): ',
async (answer) => {
rl.close();
const data = JSON.parse(fs.readFileSync('data.json', 'utf-8'));
const recordModel: mongoose.Model<Record> = mongoose.model<Record>(
'Record',
RecordSchema,
);
await mongoose.connect(AppConfig.mongoUrl);
if (answer.toLowerCase() === 'y') {
await recordModel.deleteMany({});
console.log('Existing collection cleaned up.');
}
const records = await recordModel.insertMany(data);
console.log(`Inserted ${records.length} records successfully!`);
mongoose.disconnect();
},
);
} catch (error) {
console.error('Error setting up the database:', error);
mongoose.disconnect();
}
}
setupDatabase();