-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportDB.js
42 lines (36 loc) · 1.16 KB
/
exportDB.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
const mongoose = require('mongoose');
const fs = require('fs');
const Bob = require('../models/bob');
const Flavor = require('../models/flavor');
const Tag = require('../models/tag');
// Tell mongoose to use native promises (mongoose promises are depricated)
mongoose.Promise = global.Promise;
let db = mongoose.connect('mongodb://localhost/test').connection;
db.on('open', function () {
console.log('db connected');
Flavor.getFlavors().then( function (flavors) {
return saveObjects('flavors.json', flavors);
}).then( function () {
return Tag.getTags().then( function (tags) {
return saveObjects('tags.json', tags);
});
}).then( function () {
return Bob.getBobs().then( function (bobs) {
return saveObjects('bobs.json', bobs);
});
}).then( function () {
console.log("done saving");
process.exit();
}).catch( function (err) {
console.log('caught err' + err);
process.exit();
});
});
function saveObjects(filename, objects) {
return new Promise(function(resolve, reject) {
fs.writeFile(filename, JSON.stringify(objects), function(err) {
if (err) reject(err);
else resolve("objects saved");
});
});
}