Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
model: add script for running all pre-save methods
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}) | ||
}) | ||
}) | ||
|