Mongoose Relationships
... because sometimes embedded documents aren't enough.
A plugin for Mongoose adding a simple syntax for model relationships and providing useful helpers to empower them.
This is an early release with limited functionalities. I'm looking for feedback on the API and features (been exploring a few different solutions, nothing's impossible!).
I'm inspiring from various libraries in Ruby I've used throughout the years. Might not be your cup of tea.
Goals
- Be unobtrusive and compliant with the ways of Mongoose (coding style, testing, API).
Usage
First, npm install mongo-relation
.
Add relationships to your schema through either hasMany
, belongsTo
or habtm
(has and belongs to many).
- {String}
ModelName
is the name of the Model. - {Object}
options
- {String}
through
if you want to specify what path to use for the relationship. Else the path will be created for you by pluralizing theModelName
. - {String}
dependent
takes either "delete" or "nullify" and indicated what to do when the element is removed from the parent'sthrough
array.
- {String}
var mongoose = ;; YourSchema;
It's good to take note that for "has and belongs to many" type relationships, the dependent option only deletes the reference, not the actual referenced document.
Examples
One to Many
UserSchema; // uses the 'author' path for the relationPostSchema;
Has and Belongs to Many
PostSchema;CategorySchema;
Methods
Every Document
that has their Schema
plugged with mongo-relation
has access to the following methods.
Let's use this starting point:
var mongoose = ;; // UserSchema stores an Array of ObjectIds for postsvar UserSchema = posts: mongooseSchemaObjectId; // PostSchema stores an ObjectId for the authorvar PostSchema = title : String author : mongooseSchemaObjectId; // Attach the pluginUserSchema;PostSchema; var User = mongoose Post = mongoose;
create
Takes care of creating the child document and the links between it and the parent document.
- {Object|Array}
objs
representation of the child document(s) to create - {Function}
callback
(optional) function returning an error if any, the new parent document and the created post(s)
Example:
var user = ; userposts; // Using an `Array`userposts;
build
Instantiates a child document, appends its reference to the parent document and returns the child document. Does not save anything.
- {Object}
obj
representation of the child document(s) to create
Example:
var post = userposts;// post.author === user._id
append
Allows pushing of an already existing document into the parent document. Creates all the right references.
Works with either a saved or unsaved document.
The parent document is not saved, you'll have to do that yourself.
- {Document}
child
document to push. - {Function}
callback
called with an error if any and the child document w/ references.
Example:
var post = ; userposts;
concat
Just like Array.prototype.concat
, it appends an Array
to another Array
- {Document}
child
document to push. - {Function}
callback
called with an error if any and the child document w/ references.
Example:
var posts = ; userposts;
find
It's the same as a Mongoose.Query
. Only looks through the children documents.
See Mongoose.Query for the params
Example:
userposts;
populate
Some sugary syntax to populate the parent document's child documents.
- {Array}
fields
(optional) you want to get back with each child document - {Function}
callback
called with an error and the populate document
Example:
userposts;
remove
Depending on the dependent
option, it'll either delete or nullify the
- {ObjectId}
id
of the document to remove - {Function}
callback
(optional) called after the deed is done with an error if any and the new parent document.
Example:
userposts;
Testing
Mongo-Relation uses Mocha with Should. Tests are located in ./test
and should be ran with the make test
command.