1.0.0 • Published 7 years ago
mongoose-tie v1.0.0
mongoose-tie
A simple module to handle arrays of embedded documents that have references to the parent document.
npm install --save mongoose-tie
This library will allow you to simply create a schema for a parent document, which contains an array of embedded documents, and we handle push/pops on the parent document when you POST or DELETE the individual embedded documents. #usage
Embedded Document (/models/persons.js)
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var appRoot = require('app-root-path');
var tie = require('mongoose-tie');
var PersonSchema = new Schema(
{
name: { type: String },
_team : { type: Schema.ObjectId, ref: 'team', required: true }
});
var TeamModel = require(appRoot + '/models/team.js');
tie.auto(PersonSchema,TeamModel,'_team','_people');
module.exports = mongoose.model('persons', PersonSchema);
Parent Document (/models/team.js)
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TeamSchema = new Schema(
{
name: { type: String },
_people : [
{ type: Schema.ObjectId, ref: 'persons', required: true }
]
});
module.exports = mongoose.model('team', TeamSchema);
1.0.0
7 years ago