-
Notifications
You must be signed in to change notification settings - Fork 0
/
populatedb.js
53 lines (45 loc) · 1.21 KB
/
populatedb.js
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
45
46
47
48
49
50
51
52
53
'use strict'
const mongoose = require('mongoose');
const ContentModel = require('./content/model');
const contentNames = [
'Angels with Filthy Souls',
'Good Will Hunting 2: Hunting Season, Bluntman and Chronic',
'Angels with Even Filthier Souls'
];
const createRecords = async (names) => {
const records = [];
for await (const name of names) {
const content = new ContentModel({ name });
try{
const record = await content.save();
records.push(record);
} catch (err) {
throw err;
}
}
return records;
}
const connectToDB = async () => {
try {
const args = process.argv.slice(2);
const URI = args[0];
await mongoose.connect(URI);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
return db;
} catch (err) {
console.error(err);
}
}
const init = async () => {
try {
const db = await connectToDB();
const records = await createRecords(contentNames);
console.log(records);
} catch (err) {
console.error(err);
} finally {
mongoose.connection.close();
}
}
init();