Created
March 23, 2018 21:56
-
-
Save chebotiuk/02cd2f96e61989cb5d379051605070ea to your computer and use it in GitHub Desktop.
Mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('libs/mongoose') | |
mongoose.set('debug', true) | |
var users = [ | |
{ | |
username: 'Admin', | |
password: 'admin_password' | |
}, | |
{ | |
username: 'Manager', | |
password: 'managers_password' | |
}, | |
{ | |
username: 'Client', | |
password: 'clients_password' | |
} | |
] | |
function requireModels () { | |
require('models/user') | |
return Promise.all( | |
Object.keys(mongoose.models) | |
.map(model => mongoose.models[model].ensureIndexes()) | |
) | |
} | |
function saveUser(userOptions) { | |
var user = new mongoose.models.User(userOptions) // new User(userOptions) | |
return user.save() | |
} | |
function dropDatabase() { | |
var db = mongoose.connection.db | |
return db.dropDatabase() | |
} | |
function createUsers() { | |
return Promise.all( | |
users.map(saveUser) | |
) | |
} | |
function close() { | |
return mongoose.disconnect() | |
} | |
mongoose.connection.on('open', dropDatabase) | |
.then(requireModels) | |
.then(createUsers) | |
.then(close) | |
.then(() => { | |
console.log('Seed has been successful!') | |
}) | |
.catch(err => { | |
close() | |
throw err | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment