Skip to content
This repository was archived by the owner on Nov 9, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mongoose-fill
# mongoose-fill-promises

mongoose-fill is [mongoose.js](http://mongoosejs.com/) add-on that adds simple api for virtual async fields.
mongoose-fill-promises is [mongoose.js](http://mongoosejs.com/) add-on that adds simple api for virtual async fields.

## why?

Expand All @@ -9,16 +9,24 @@ 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)

## version support - breaking changes

Version 2.0.0 of this library is written as an ESM, and works with mongoose 7.0.0 and above, and requires node 20.0.0 or above.

For compatibility with mongoose < 7 and node < 20 use version 1.x.

This is a fork of `mongoose-fill` which has been archived. Mongoose 7 dropped support for the callback api on queries so this version was modified to use promises.

## api use cases - learn by example

basic use case fills single filed

```javascript
// import of 'mongoose-fill' patches mongoose and returns mongoose object, so you can do:
var mongoose = require('mongoose-fill')
// import of 'mongoose-fill-promises' patches mongoose and returns mongoose object, so you can do:
var mongoose = require('mongoose-fill-promises')
...
// note you should set virtual properties options
// on you schema to get it mongoose-fill work
// on you schema to get it mongoose-fill-promises work
var myParentSchema = new Schema({
....
}, {
Expand Down Expand Up @@ -102,9 +110,9 @@ Also check the code of test for more use cases

### Installation

npm install mongoose-fill
npm install mongoose-fill-promises


### Run tests

npm test
npm test
25 changes: 13 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict'

var mongoose = require('mongoose');
var async = require('async')
var util = require('util')
import mongoose from 'mongoose';
import async from 'async';

var getArgsWithOptions = function(__fill){
var args = [],
Expand Down Expand Up @@ -196,23 +193,24 @@ mongoose.Query.prototype.exec = function (op, cb) {
var p = getPromise(cb)
var promise = p.promise, onResolve = p.onResolve, resolve = p.resolve

_exec.call(this, op, function (err, docs) {
_exec.call(this, op)
.then((docs) => {

if (err || !docs) {
resolve(err, docs)
if (!docs) {
resolve(null, docs)
} else {

async.mapSeries(__fillsSequence, function(__fills, cb){

async.map(__fills, function(__fill, cb){
var useMultiWithSingle = !util.isArray(docs) && !__fill.fill.value && __fill.fill.multi
var useMultiWithSingle = !Array.isArray(docs) && !__fill.fill.value && __fill.fill.multi

if (useMultiWithSingle){
docs = [docs]
}

// TODO: make this also if there is only multi methods when one doc
if (util.isArray(docs)){
if (Array.isArray(docs)){
var args = getArgsWithOptions(__fill)

if (__fill.fill.multi && !__fill.fillEach){
Expand Down Expand Up @@ -255,7 +253,7 @@ mongoose.Query.prototype.exec = function (op, cb) {
if (results && results !== docs){

// convert object map to array in right order
if (!util.isArray(results)){
if (!Array.isArray(results)){
results = ids.map(function(id){
return results[id]
})
Expand Down Expand Up @@ -311,6 +309,9 @@ mongoose.Query.prototype.exec = function (op, cb) {
resolve(err, docs)
})
}
})
.catch((err) => {
resolve(err, null)
});

return promise
Expand Down Expand Up @@ -447,4 +448,4 @@ mongoose.Model.prototype.filled = function(){
this.fill.apply(this, args)
}

module.exports = mongoose
export default mongoose;
Loading