-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportDB.js
45 lines (34 loc) · 1.04 KB
/
importDB.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
const mongoose = require('mongoose');
const fs = require('fs');
const Bob = require('../models/bob');
const Flavor = require('../models/flavor');
const Tag = require('../models/tag');
let db = mongoose.connect('mongodb://localhost/test').connection;
const collectionName = process.argv[2];
db.on('open', function () {
fs.readFile(collectionName + '.json', 'utf8', (err, data) => {
const objects = JSON.parse(data);
console.log('importing', objects.length, collectionName, 'from', collectionName + '.json');
switch (collectionName) {
case 'flavors':
objects.forEach(function (object) {
Flavor.saveFlavor(object);
});
break;
case 'tags':
objects.forEach(function (object) {
Tag.saveTag(object);
});
break;
case 'bobs':
objects.forEach(function (object) {
Bob.saveBob(object);
});
break;
default:
console.log("unknown collection " + collectionName);
}
console.log('done');
process.exit();
});
});