Skip to content

Instantly share code, notes, and snippets.

@chebotiuk
Created March 23, 2018 21:56
Show Gist options
  • Save chebotiuk/02cd2f96e61989cb5d379051605070ea to your computer and use it in GitHub Desktop.
Save chebotiuk/02cd2f96e61989cb5d379051605070ea to your computer and use it in GitHub Desktop.
Mongoose
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