mongoose plugin, populate and keep updated references to documents in other collection.
denormalization plugin for mongoose
This plugins helps you create denormalized schemas by copying references from other collection and keeping them up to date.
Just like model.populate except that data is stored in the collection instead of being populated for each query
$ npm install mongoosefiller
// plugin optionsoptions =id: String // (optional default to path._id) path of the id of our reference modelpath: String // (optional default to '') path to property to keep in sync with ref modelpos: String // (optional) pos operator prefix used to update model in collection arrayref : String // reference Model name (collection we are copying data from)dest: String // destination Model name (collection we are copying data to)select: String // (optional default to all) list of space separated field to include or excludesync: String // (optional default to select) list of space separated field to keep in sync after first save.
// examplesvar UserSchema =firstname: type: Stringlastname : type: Stringemail : type: String;var PostSchema =message: type: String;// add user.firstname, user.lastname, user.email path to schema// fill user.* with data from User// update documents every time a change occur in UserPostSchemapluginfillerpath: 'user'ref : 'User'dest: 'Post';friendSchema =date: type: Date;ListSchema =name: type: Stringfriends: friendSchema;// add firstname, lastname path to schema// fill friends with data from User// update friends in List (using positional operator List.friends.$._id)// every time a change occur in UserfriendSchemapluginfillerref : 'User'dest : 'List'pos : 'friends.$.'select: 'firstname lastname';
- Embedded array check examples/friends.js
- Embedded doc check examples/post.js
fill event is triggered on the denormalized schema when the the ref doc change and collection is updated
userset'name' 'new-name'save;PostSchemaon'fill'// all post docs have have been saved;
if you define a path attribute, like in the following example, the plugin will create a virtual id accessor
PostSchemapluginfillerpath: 'user'ref : 'User'dest: 'Post';// ...Postcreatedocconsole.logpostuserid == postuser_idtoString;;
I have done a basic test on one doc
- read perfs with 10 embedded friends: 5ms
- read perfs with 10 populated friends: 16ms
- read perfs with 100 embedded friends: 36ms
- read perfs with 100 populated friends: 67ms
- read perfs with 1000 embedded friends: 94ms
- read perfs with 1000 populated friends: 254m