This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
/
seed.js
61 lines (51 loc) 路 1.68 KB
/
seed.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
54
55
56
57
58
59
60
61
/*
This seed file is only a placeholder. It should be expanded and altered
to fit the development of your application.
It uses the same file the server uses to establish
the database connection:
--- server/db/index.js
The name of the database used is set in your environment files:
--- server/env/*
This seed file has a safety check to see if you already have users
in the database. If you are developing multiple applications with the
fsg scaffolding, keep in mind that fsg always uses the same database
name in the environment files.
*/
var Promise = require("bluebird");
var mongoose = Promise.promisifyAll(require("mongoose"));
var chalk = require("chalk");
var connectToDb = require("./server/db");
var User = Promise.promisifyAll(mongoose.model("User"));
var Product = Promise.promisifyAll(mongoose.model("Product"));
var http = require("http");
var seedUsers = function() {
var users = require("./seeds/user");
return User.createAsync(users);
};
var seedProducts = function() {
var products = require("./seeds/product");
return Product.createAsync(products);
};
var dropDatabase = function() {
return new Promise(function(resolve, reject) {
mongoose.connection.db.dropDatabase(function(err, result) {
if (err) reject(err);
else {
resolve(result);
}
});
});
};
connectToDb.then(function() {
dropDatabase().then(function() {
return seedUsers();
}).then(function() {
return seedProducts();
}).then(function() {
console.log(chalk.green("Seed successful!"));
process.kill(0);
}).catch(function(err) {
console.error(err);
process.kill(1);
});
});