All Projects → wclr → Mongoose Fill

wclr / Mongoose Fill

Virtual async fileds for mongoose.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Mongoose Fill

Tvrboreact
Dream starter project: React, Redux, React Router, Webpack
Stars: ✭ 13 (-77.19%)
Mutual labels:  mongoose
Friend.ly
A social media platform with a friend recommendation engine based on personality trait extraction
Stars: ✭ 41 (-28.07%)
Mutual labels:  mongoose
Mongomem
In-memory MongoDB Server. Ideal for testing.
Stars: ✭ 51 (-10.53%)
Mutual labels:  mongoose
Essay
A blog system based on Nuxt.js
Stars: ✭ 913 (+1501.75%)
Mutual labels:  mongoose
Littlewin.server
A blog backend server based on koa + mongoose.
Stars: ✭ 32 (-43.86%)
Mutual labels:  mongoose
Mongoose Tsgen
A plug-n-play Typescript interface generator for Mongoose.
Stars: ✭ 43 (-24.56%)
Mutual labels:  mongoose
Mongoose Model Decorators
ES2016 decorator functions for building Mongoose models
Stars: ✭ 11 (-80.7%)
Mutual labels:  mongoose
Node React Ecommerce
Build ECommerce Website Like Amazon By React & Node & MongoDB
Stars: ✭ 1,080 (+1794.74%)
Mutual labels:  mongoose
Es6 Express Mongoose Passport Rest Api
Lightweight boilerplate for Node RESTful API, ES6, Express, Mongoose and Passport 🎁
Stars: ✭ 36 (-36.84%)
Mutual labels:  mongoose
Mongoose Transactions
Atomicity and Transactions for mongoose.
Stars: ✭ 51 (-10.53%)
Mutual labels:  mongoose
Jwt Node Vue
Repositório responsável pelo primeiro projeto da série de vídeos: Coding Stuff.
Stars: ✭ 29 (-49.12%)
Mutual labels:  mongoose
Cmms
Computerized Maintenance Management System
Stars: ✭ 31 (-45.61%)
Mutual labels:  mongoose
Graphql Advanced Projection
Fully customizable Mongoose/MongoDB projection generator.
Stars: ✭ 46 (-19.3%)
Mutual labels:  mongoose
Timeline Vue
💌基于 Vue -> Koa2 -> Mongoose 的留言时间轴,记录美好时光。
Stars: ✭ 14 (-75.44%)
Mutual labels:  mongoose
Cdfang Spider
📊 成都房协网数据分析,喜欢请点 star!
Stars: ✭ 1,063 (+1764.91%)
Mutual labels:  mongoose
Summary
个人总结 持续更新 欢迎提出各种issues
Stars: ✭ 12 (-78.95%)
Mutual labels:  mongoose
Mean Angular4 Chat App
MEAN stack with Angular 4 Chat App
Stars: ✭ 41 (-28.07%)
Mutual labels:  mongoose
Wertik Js
💪 A library that powers your app with GraphQL + Rest API
Stars: ✭ 56 (-1.75%)
Mutual labels:  mongoose
Vue Element Responsive Demo
基于 Vue + Element 的响应式后台模板
Stars: ✭ 54 (-5.26%)
Mutual labels:  mongoose
Fclub
Vue全家桶+Koa+mongoose全栈开发的单页应用 http://wap.fulun.club
Stars: ✭ 49 (-14.04%)
Mutual labels:  mongoose

mongoose-fill

mongoose-fill is mongoose.js add-on that adds simple api for virtual async fields.

why?

This just gives you a simple and easy to use API for:

  • implementing db joins without keeping refs to related objects (with refs you can use populate)
  • joining mongoose object with any kind of data (with any async service - other db or web service)

api use cases - learn by example

basic use case fills single filed

// import of 'mongoose-fill' patches mongoose and returns mongoose object, so you can do:
var mongoose = require('mongoose-fill')
 ...
// note you should set virtual properties options 
// on you schema to get it mongoose-fill work
var myParentSchema = new Schema({
    ....
}, {
  toObject: {
    virtuals: true
  },
  toJSON: {
    virtuals: true 
  }
}

myParentSchema.fill('children', function(callback){
    this.db.model('Child')
        .find({parent: this.id})
        .select('name age')
        .order('-age')
        .exec(callback)
})
...
Parent.findById(1).fill('children').exec().then(function(parent){
    //parent.children <- will be array of children with fields name and age ordered by age
})

filling property using single query for multiple objects

myParentSchema.fill('childrenCount').value(function(callback){
    // `this` is current (found) instance
    this.db.model('Child')
        .count({parent: this.id})
        .exec(callback)
    // multi is used for queries with multiple fields
    }).multi(function(docs, ids, callback){     
    // query childrenCount for all found parents with single db query
    this.db.model('Child')
        .aggregate([{
            $group: {
                _id: '$parent',
                childrenCount: {$sum: 1}
            }},
            {$match: {'_id': {$in: ids}}}
        ], callback)
})
...

// you can place property name that should be filled in `select` option
Parent.find({}).select('name childrenCount').exec().then(function(parents){
    //parent.childrenCount <- will contain count of children
})

using fill options with default values

myParentSchema.fill('children', function(select, order, callback){
    this.db.model('Child')
        .find({parent: this.id})
        .select(select)
        .order(order)
        .exec(callback)
}).options('', '-age')
...

// fill children with only `name age` properties ordered by `age`
Parent.findById(1).fill('children', 'name age', 'age').exec().then(function(parent){
    //parent.children <- will be array of children with fields name and age ordered by age
})

Also check the code of test for more use cases

how does it work

  • adds fill method to mongoose schema object
  • adds fill and filled prototype methods to mongoose model
  • patches mongoose query exec method extending query api with fill method
  • implemented using mongoose virtual setters/getters (actual value is stored in __propName property)

Installation

npm install mongoose-fill

Run tests

npm test

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].