Skip to content

Instantly share code, notes, and snippets.

@marcbachmann
Last active October 8, 2015 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcbachmann/1419c718cffc75d56861 to your computer and use it in GitHub Desktop.
Save marcbachmann/1419c718cffc75d56861 to your computer and use it in GitHub Desktop.
Small script to populate collections
_ = 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