mongoose-relationship
Note: Maintenence on this module is deprecated. I had personally been using it for a project and over time have realized that bi-directional relationships in mongoose can become extremely complex and hinder performance as a project grows. The more I learn about mongo and designing data for it, the less likely these relationships make sense.
A mongoose plugin that creates and manages relationships between two separate models. These relationships can be One-To-One, One-To-Many, or Many-To-Many. These changes are currently one-direction. If you manipulate a parents "child" property or collection, the child values will not be updated. Only changes made to the child model will update its parent.
Install
Install via NPM
npm install mongoose-relationship
Usage
One-To-Many
var mongoose = Schema = mongooseSchema relationship = ; var ParentSchema = children: type:SchemaObjectId ref:"Child" ;var Parent = mongoose; var ChildSchema = parent: type:SchemaObjectId ref:"Parent" childPath:"children" ;ChildSchema;var Child = mongoose var parent = {};parent;var child = parent:parent_id;child //the parent children property will now contain child's idchild //the parent children property will no longer contain the child's id
Many-To-Many
var mongoose = Schema = mongooseSchema relationship = ; var ParentSchema = children: type:SchemaObjectId ref:"Child" ;var Parent = mongoose; var ChildSchema = parents: type:SchemaObjectId ref:"Parent" childPath:"children" ;ChildSchema;var Child = mongoose var parent = {};parent;var parentTwo = {};parentTwo; var child = {};childparents;childparents;child //both parent and parentTwo children property will now contain the child's idchild //both parent and parentTwo children property will no longer contain the child's id
Many-To-Many with Multiple paths
var mongoose = Schema = mongooseSchema relationship = ; var ParentSchema = children: type:SchemaObjectId ref:"Child" ;var Parent = mongoose; var OtherParentSchema = children: type:SchemaObjectId ref:"Child" ;var OtherParent = mongoose; var ChildSchema = parents: type:SchemaObjectId ref:"Parent" childPath:"children" otherParents: type:SchemaObjectId ref:"OtherParent" childPath:"children" ;ChildSchema;var Child = mongoose var parent = {};parent;var otherParent = {};otherParent; var child = {};childparents;childotherParents;child //both parent and otherParent children property will now contain the child's idchild //both parent and otherParent children property will no longer contain the child's id
One-To-One
This usage scenario will overwrite the parent's field of multiple children are assigned the same parent. The use case for this operation seems to be limited and only included for a sense of completion.
var mongoose = Schema = mongooseSchema relationship = ; var ParentSchema = child: type:SchemaObjectId ref:"Child" ;var Parent = mongoose; var ChildSchema = parent: type:SchemaObjectId ref:"Parent" childPath:"child" ;ChildSchema;var Child = mongoose var parent = {};parent;var child = parent:parent_id;child // The parent's child property will now be set to the child's _id;child // The parent's child property will now be unset
Options
Plugin
The plugin currently has the following options
-
relationshipPathName
A string or array to let the plugin know which path(s) on your schema the relationship will be created. Defaults to relationship
-
triggerMiddleware
Boolean value which, if set to true, will explicitly save any parents entities when a relationship is updated causing save middleware to execute. Defaults to false
Path Value
When creating a path on a schema that will represent the relationship, the childPath option is required
-
childPath
A string which should match an existing path in target ref schema. If this path does not exist the plugin will have no affect on the target ref.
-
validateExistence
Boolean value that tells mongoose-relationship to ensure that the parent exists before setting the relationship for the child. Defaults to false
-
upsert
Boolean value that controls whether a parent should be created if it does not exist upon child save. Defaults to false
Tests
Test can be run simply by installing and running mocha
npm install -g mocha
mocha
Authors
Mike Sabatini @mikesabatini
License
Copyright Mike Sabatini 2014 Licensed under the MIT License. Enjoy