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

Commit b4aa73c

Browse files
committed
Update to support mongoose > 7 (no more callbacks for mongoose queries), package is type module (ESM)
1 parent b6a4e91 commit b4aa73c

File tree

5 files changed

+843
-1817
lines changed

5 files changed

+843
-1817
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ This just gives you a simple and easy to use API for:
99
- implementing db joins without keeping refs to related objects (with refs you can use `populate`)
1010
- joining mongoose object with any kind of data (with any async service - other db or web service)
1111

12+
## version support - breaking changes
13+
14+
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.
15+
16+
For compatibility with mongoose < 7 and node < 20 use version 1.x.
17+
1218
## api use cases - learn by example
1319

1420
basic use case fills single filed
@@ -107,4 +113,4 @@ npm install mongoose-fill
107113
108114
### Run tests
109115
110-
npm test
116+
npm test

index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
'use strict'
2-
3-
var mongoose = require('mongoose');
4-
var async = require('async')
5-
var util = require('util')
1+
import mongoose from 'mongoose';
2+
import async from 'async';
63

74
var getArgsWithOptions = function(__fill){
85
var args = [],
@@ -196,23 +193,24 @@ mongoose.Query.prototype.exec = function (op, cb) {
196193
var p = getPromise(cb)
197194
var promise = p.promise, onResolve = p.onResolve, resolve = p.resolve
198195

199-
_exec.call(this, op, function (err, docs) {
196+
_exec.call(this, op)
197+
.then((docs) => {
200198

201-
if (err || !docs) {
202-
resolve(err, docs)
199+
if (!docs) {
200+
resolve(null, docs)
203201
} else {
204202

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

207205
async.map(__fills, function(__fill, cb){
208-
var useMultiWithSingle = !util.isArray(docs) && !__fill.fill.value && __fill.fill.multi
206+
var useMultiWithSingle = !Array.isArray(docs) && !__fill.fill.value && __fill.fill.multi
209207

210208
if (useMultiWithSingle){
211209
docs = [docs]
212210
}
213211

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

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

257255
// convert object map to array in right order
258-
if (!util.isArray(results)){
256+
if (!Array.isArray(results)){
259257
results = ids.map(function(id){
260258
return results[id]
261259
})
@@ -311,6 +309,9 @@ mongoose.Query.prototype.exec = function (op, cb) {
311309
resolve(err, docs)
312310
})
313311
}
312+
})
313+
.catch((err) => {
314+
resolve(err, null)
314315
});
315316

316317
return promise
@@ -447,4 +448,4 @@ mongoose.Model.prototype.filled = function(){
447448
this.fill.apply(this, args)
448449
}
449450

450-
module.exports = mongoose
451+
export default mongoose;

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "mongoose-fill",
3-
"version": "1.7.0",
3+
"version": "2.0.0",
44
"description": "Mongoose.js add-on that adds virtual (temporary) async fileds API",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
7-
"test": "node -r babel-register test",
8-
"test-watch": "node-dev --respawn -r babel-register test",
8+
"test": "node test",
9+
"test-watch": "node --watch test",
910
"prepublish": "npm run test"
1011
},
1112
"files": [
@@ -30,14 +31,13 @@
3031
},
3132
"homepage": "https://github.com/whitecolor/mongoose-fill#readme",
3233
"devDependencies": {
33-
"babel-cli": "^6.6.5",
34-
"babel-plugin-transform-runtime": "^6.6.0",
35-
"babel-preset-es2015": "^6.6.0",
36-
"babel-preset-stage-0": "^6.5.0",
37-
"mongoose": "^4.0.5",
38-
"tape": "^4.6.0"
34+
"mongoose": "^8.8.0",
35+
"tape": "^5.9.0"
3936
},
4037
"dependencies": {
41-
"async": "^1.4.2"
38+
"async": "^3.2.6"
39+
},
40+
"engines" : {
41+
"node" : ">=20.0.0"
4242
}
4343
}

test.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import test from 'tape';
2-
import mongoose from './index';
3-
mongoose.connect('mongodb://localhost/mongoose_fill_test');
2+
import mongoose from './index.js';
3+
4+
let opts = process.env.MONGO_NODE_PASSWORD ? { user: 'node', pass: process.env.MONGO_NODE_PASSWORD } : {}
5+
mongoose.connect('mongodb://localhost/mongoose_fill_test', opts);
46

57
const userSchema = new mongoose.Schema({
68
_id: 'number',
@@ -75,10 +77,10 @@ userSchema.fill('friend', function (callback) {
7577
callback(null, val)
7678
})
7779

78-
userSchema.fill('nested.dude', function (callback) {
79-
this.db.model('User')
80+
userSchema.fill('nested.dude', async function (callback) {
81+
const dude = await this.db.model('User')
8082
.findOne({_id: this.dude})
81-
.exec(callback)
83+
callback(null, dude)
8284
})
8385

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

9395
try {
94-
await User.remove({});
96+
await User.deleteMany({});
9597
await User.create(usersData);
9698
t.end();
9799
} catch (err) {
@@ -111,13 +113,12 @@ test('fill one property: purchases', async t => {
111113
}
112114
})
113115

114-
test('fill one property: purchases (exec callback)', async t => {
115-
User.findById(1).fill('purchases').exec((err, user) => {
116-
t.ok(user.name == 'Alex', 'user name is ok');
117-
t.ok(!!user.purchases, 'user purchases present');
118-
t.ok(user.purchases[0].amount == 5, 'first purchase amount is ok');
119-
t.end();
120-
});
116+
test('fill one property: purchases', async t => {
117+
let user = await User.findById(1).fill('purchases');
118+
t.ok(user.name == 'Alex', 'user name is ok');
119+
t.ok(!!user.purchases, 'user purchases present');
120+
t.ok(user.purchases[0].amount == 5, 'first purchase amount is ok');
121+
t.end();
121122
});
122123

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

275276
test.onFinish(() => {
276277
process.exit()
277-
})
278+
})

0 commit comments

Comments
 (0)