Skip to content
This repository has been archived by the owner on Nov 9, 2024. It is now read-only.

Commit

Permalink
Update to support mongoose > 7 (no more callbacks for mongoose querie…
Browse files Browse the repository at this point in the history
…s), package is type module (ESM)
  • Loading branch information
gavinaiken committed Nov 6, 2024
1 parent b6a4e91 commit b4aa73c
Show file tree
Hide file tree
Showing 5 changed files with 843 additions and 1,817 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ 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.

## api use cases - learn by example

basic use case fills single filed
Expand Down Expand Up @@ -107,4 +113,4 @@ npm install mongoose-fill
### 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;
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "mongoose-fill",
"version": "1.7.0",
"version": "2.0.0",
"description": "Mongoose.js add-on that adds virtual (temporary) async fileds API",
"main": "index.js",
"type": "module",
"scripts": {
"test": "node -r babel-register test",
"test-watch": "node-dev --respawn -r babel-register test",
"test": "node test",
"test-watch": "node --watch test",
"prepublish": "npm run test"
},
"files": [
Expand All @@ -30,14 +31,13 @@
},
"homepage": "https://github.com/whitecolor/mongoose-fill#readme",
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-plugin-transform-runtime": "^6.6.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-0": "^6.5.0",
"mongoose": "^4.0.5",
"tape": "^4.6.0"
"mongoose": "^8.8.0",
"tape": "^5.9.0"
},
"dependencies": {
"async": "^1.4.2"
"async": "^3.2.6"
},
"engines" : {
"node" : ">=20.0.0"
}
}
29 changes: 15 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import test from 'tape';
import mongoose from './index';
mongoose.connect('mongodb://localhost/mongoose_fill_test');
import mongoose from './index.js';

let opts = process.env.MONGO_NODE_PASSWORD ? { user: 'node', pass: process.env.MONGO_NODE_PASSWORD } : {}
mongoose.connect('mongodb://localhost/mongoose_fill_test', opts);

const userSchema = new mongoose.Schema({
_id: 'number',
Expand Down Expand Up @@ -75,10 +77,10 @@ userSchema.fill('friend', function (callback) {
callback(null, val)
})

userSchema.fill('nested.dude', function (callback) {
this.db.model('User')
userSchema.fill('nested.dude', async function (callback) {
const dude = await this.db.model('User')
.findOne({_id: this.dude})
.exec(callback)
callback(null, dude)
})

const User = mongoose.model('User', userSchema)
Expand All @@ -91,7 +93,7 @@ test('setup', async t => {
];

try {
await User.remove({});
await User.deleteMany({});
await User.create(usersData);
t.end();
} catch (err) {
Expand All @@ -111,13 +113,12 @@ test('fill one property: purchases', async t => {
}
})

test('fill one property: purchases (exec callback)', async t => {
User.findById(1).fill('purchases').exec((err, user) => {
t.ok(user.name == 'Alex', 'user name is ok');
t.ok(!!user.purchases, 'user purchases present');
t.ok(user.purchases[0].amount == 5, 'first purchase amount is ok');
t.end();
});
test('fill one property: purchases', async t => {
let user = await User.findById(1).fill('purchases');
t.ok(user.name == 'Alex', 'user name is ok');
t.ok(!!user.purchases, 'user purchases present');
t.ok(user.purchases[0].amount == 5, 'first purchase amount is ok');
t.end();
});

test('fill multiple properties with select: purchases, actions', async t => {
Expand Down Expand Up @@ -274,4 +275,4 @@ test('should fill nested on model that has no fill property (promise)', t => {

test.onFinish(() => {
process.exit()
})
})
Loading

0 comments on commit b4aa73c

Please sign in to comment.