Mongoose Relationships
A plugin for Mongoose for model relationships and helpers.
Goals
- Be unobtrusive and compliant with the ways of Mongoose (coding style, testing, API).
Usage
var mongoose = ;;
One to Many (belongsTo/hasMany)
var postSchema = {};postSchema;var Post = mongoose; var commentSchema = {};commentSchema;var Comment = mongoose;
create
Creates a child document associated to the parent.
- {Object|Array} objs child document(s) // simple objects
- {Function} callback callback(err, docs)
- {Error} err error from mongoose
- {Document|Array} docs Array or single persisted model
var postSchema = content: String ;postSchema; var commentSchema = content: String ;commentSchema; var post = content: "Relations are helpful." ; postcomments; postcomments;
build
Instantiates children/a child document(s) with the appropriate associations.
- {Object|Array} objs child document(s) // simple objects
var comment = postcomments;console;console; var post = content: "Relations are easy." ;var comments = postcomments;console;comments;
concat
Associates & saves initialized document(s) with the appropriate associations.
- {Document|Array} docs documents to associate. // mongoose documents
- {Function} callback callback(err, docs)
- {Error} err error from mongoose
- {Document|Array} docs Array or single persisted document(s)
var post = content: "Concating is great." ;var comment = ;postcomments; var comments = ;postcomments; // existing doc var comment = ;comment;
append
Sugar for #concat
find
Inject params for the association, then fallback on Model.find
- {Object} docs (optional) query conditions
- {String} fields (optional) fields to populate
- {Object} options (optional) query options
- {String} callback (optional) callback(err, docs)
- {Error} err error from mongoose
- {Document} doc Found document
var postOne = content: "Find all the things." postTwo = content: "Found all the things." ; postOnecomments;
Todo
- Document belongsTo
- add touch to hasMany
- add touch to belongsTo
- Refactor hasAndBelongsToMany
- Document hasAndBelongsToMany
- Add embedsMany
- Add emdedsOne