Skip to content

Commit

Permalink
model: add script for running all pre-save methods
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed Oct 3, 2015
1 parent 0004d42 commit 3667416
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions update.js
@@ -0,0 +1,34 @@
#!/usr/bin/env node

/**
* Fetches all user instances and calls user.save() on them.
* This is handy for running the pre-save method on all users
* when adding new generated fields to the schema.
*/

var model = require('../model')
var runParallelLimit = require('run-parallel-limit')

model.connect(function (err) {
if (err) throw err
model.User
.find()
.exec(function (err, users) {
if (err) throw err
console.log(users.length)

runParallelLimit(users.map(function (user) {
return function (cb) {
console.log('id', user._id, 'email', user.email, 'name', user.name)
user.save(cb)
}
}), 10, function (err) {
if (err) {
console.log(err)
throw err
}
console.log('\nDONE')
})
})
})

0 comments on commit 3667416

Please sign in to comment.