0.0.3 • Published 9 years ago

mongoose-atree v0.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

Mongoose ancestors tree plugin

Mongoose tree plugin using ancestor array method.

Fields added (hardcoded) :

  • parents : array of parents ObjectIds
  • parent : direct parent
  • depth : item depth
  • order : local order

See atree.coffee for methods and details.

Usage

Declaration

atree = require "mongoose-atree"
birdSchema = mongoose.Schema
    specname: { type: String }

birdSchema.plugins atree

bird1 = new Bird()
bird2 = new Bird(parent=bird1)
bird3 = new Bird(parent=bird2)

bird1.getTree({depth: 3})

Modify tree

  • change order of same parent's node : change "order" field
  • move an item and its children : change item's "parent" field
  • copy an item and its children : TODO
  • remove an item and its children : remove item

Query tree

  • get immediate children of an item : item.getChildren(args, cb) with args
  • get immediate children of an item (without retrieving parent) : Model.GetChildren(item, args, cb) with item, args
  • get tree roots : Model.GetRoots(args, cb) with args
  • get item subtree : item.getTree(args, cb) with args
  • get full tree : Model.GetTree(item, args, cb) with args

args is an object containing :

  • query : returned objects have to satisfy query
  • order : sort results with custom order
  • fields : just return these fields
  • limit : limit number of results
  • skip : skip n first results
  • depth : max tree depth
  • indexed: boolean, if true returns an indexed (by id) tree
  • flat : boolean, if true returns a flat (but ordered) tree

Warning

To get returned results tree data (children, parents, ..), getters should be outputted in toJSON or toObject :

birdSchema.set("toObject", {getters: true})
birdSchema.set("toJSON", {getters: true})

this is not set by default by plugin