Nighttime Possum Meandering
    Have ideas to improve npm?Join in the discussion! »

    mongoose-tie

    1.0.0 • Public • Published

    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);
    

    Install

    npm i mongoose-tie

    DownloadsWeekly Downloads

    2

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • avatar