Last active
October 8, 2015 05:33
-
-
Save marcbachmann/1419c718cffc75d56861 to your computer and use it in GitHub Desktop.
Small script to populate collections
This file contains hidden or 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
_ = require('lodash') | |
module.exports = | |
populate = (mapName, fromCollection, toCollection, fromId, toId='id') -> | |
indexed = _.indexBy(toCollection, toId) | |
for doc in fromCollection || [] | |
doc[mapName] = indexed[doc[revision_id]] | |
docs = [ | |
{id: 1, title: 'foo', user_id: 2}, | |
{id: 2, title: 'bar', user_id: 10}, | |
{id: 2, title: 'bar'} | |
] | |
users = [ | |
{id: 2, name: 'test'}, | |
{id: 10, name: 'blub'} | |
] | |
// This sets all references | |
populate('users', docs, users, 'user_id') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment