mongoose-pusher
Mongoose plugin to allow "pusher" and "puller" functions which acts like setters for arrays.
Install
npm install mongoose-pusher --save
Usage
var mongoose = Schema = mongooseSchema pusherPlugin = ; var SomeSchema = someArrayField: type: String // can be any type { return ; } { this; } ; SomeSchema; SomeSchmeamethods { /*...*/}; var SomeModel = mongoose; // These functions call the setter, which is custom from the pluginvar someObject = someArrayField: 'a' 'b' 'c' // called through modified setter;someObject; // These functions are modified to call the pushersomeObjectsomeArrayField;someObjectsomeArrayField;someObjectsomeArrayField; // These call pusher and puller (if necessary)someObjectsomeArrayFieldsomeObjectsomeArrayField; // These call the pullersomeObjectsomeArrayField;someObjectsomeArrayField;someObjectsomeArrayField;someObjectsomeArrayField;someObjectsomeArrayField;
Notes
push
andpull
are called for each element of the array and are never called with more than one element (no need to slice arguments).push
is called for each element to be added to the array whenever the array is constructed with elements or one ofpush
,nonAtomicPush
,set
,addToSet
,splice
, andunshift
functions are called.addToSet
will callpush
for all elements regardless of whether its already in the set.
pull
is called for each element removed from the array whenever one ofpull
,pop
,$pop
,shift
,$shift
,set
, orsplice
are called.pull
, likeaddToSet
, might call the puller for elements that aren't in the set.
push
andpull
, likeset
, are called with the context of the parent document.push
orpull
are not mutually required or exclusive, that is they do not have to both be specified, but they can be.- Use of
push
will override the setter on the field, so if you put a setter in,push
will not be called upon construction.
Tests
npm install -g nodeunit
nodeunit tests
License
MIT found in LICENSE file.