Skip to content

Commit 575c4ae

Browse files
Merge pull request #456 from T-vK/main
Fix support for CJS and add support for Mongoose 8 (and MongoDB 7)
2 parents 68ca6e4 + 4dd760c commit 575c4ae

35 files changed

+2229
-2076
lines changed

.github/workflows/node.js.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
mongodb-version: ["4.4", "5.0", "6.0"]
15-
node-version: [14.x, 16.x]
14+
mongodb-version: ["4.4", "5.0", "6.0", "7.0"]
15+
node-version: [16.x, 18.x, 20.x, 21.x]
1616
steps:
1717
- uses: actions/checkout@v3
1818
- uses: actions/setup-node@v3

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ npm install express-restify-mongoose --save
1717

1818
## Compatibility
1919

20-
| This library | Mongoose |
21-
| ------------ | -------- |
22-
| >= 7.0.0 | >= 6.x |
23-
| >= 6.0.0 | >= 5.8.0 |
24-
| >= 1.0.0 | 4.x |
25-
| 0.7.5 | 3.x |
20+
| This library | Mongoose | MongoDB | NodeJS
21+
| ------------ | ----------- | ----------- | --------
22+
| >= 9.0.0 | 6.x - 8.x | 3.6.x - 7.x | >=16
23+
| >= 8.0.0 | 6.x | 3.6.x - 6.x | >=18
24+
| >= 7.0.0 | 6.x | 3.6.x - 6.x | >=14
25+
| >= 6.0.0 | 5.8.0 - 6.x | 3.6.x - 6.x | >=6
26+
| >= 1.0.0 | 4.x | 2.4 - 3.4 | >=0.10
27+
| 0.7.5 | 3.x | 2.4 - 3.0 | *
2628

2729
## Contributing
2830

package.json

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-restify-mongoose",
3-
"version": "8.0.0",
3+
"version": "9.0.0",
44
"description": "Easily create a flexible REST interface for mongoose models",
55
"keywords": [
66
"ReST",
@@ -23,33 +23,27 @@
2323
"name": "Florian Holzapfel",
2424
"email": "flo.holzapfel@gmail.com"
2525
},
26-
"type": "module",
27-
"exports": {
28-
"import": "./dist/express-restify-mongoose.js",
29-
"require": "./dist/cjs/express-restify-mongoose.js"
30-
},
31-
"main": "./dist/cjs/express-restify-mongoose.js",
26+
"main": "./dist/express-restify-mongoose.js",
3227
"files": [
3328
"dist/"
3429
],
3530
"scripts": {
3631
"build": "run-p build:*",
37-
"build:esm": "swc src --out-dir dist",
38-
"build:cjs": "swc src --config module.type=commonjs --out-dir dist/cjs",
32+
"build:cjs": "swc src --config module.type=commonjs --out-dir dist",
3933
"build:dts": "tsup src/express-restify-mongoose.ts --dts-only",
4034
"lint": "eslint . --ext .js,.ts",
4135
"test": "run-s test:*",
42-
"test:express": "mocha -R spec ./test/express.js --timeout 10s",
43-
"test:filter": "mocha -R spec ./test/integration/resource_filter.js --timeout 10s",
44-
"test:restify": "mocha -R spec ./test/restify.js --timeout 10s",
45-
"test:unit": "mocha -R spec ./test/unit.js",
36+
"test:express": "mocha -R spec ./test/express.mjs --timeout 10s",
37+
"test:filter": "mocha -R spec ./test/integration/resource_filter.mjs --timeout 10s",
38+
"test:restify": "mocha -R spec ./test/restify.mjs --timeout 10s",
39+
"test:unit": "mocha -R spec ./test/unit.mjs",
4640
"tsc": "tsc --noEmit"
4741
},
4842
"dependencies": {
49-
"dot-prop": "^7.2.0",
43+
"dot-prop": "^6.0.0",
5044
"lodash.isplainobject": "~4.0.6",
51-
"mongoose": "6.x",
52-
"serialize-error": "^11.0.0",
45+
"mongoose": "^8.2.1",
46+
"serialize-error": "^8.1.0",
5347
"zod": "^3.19.1"
5448
},
5549
"devDependencies": {
@@ -62,19 +56,19 @@
6256
"body-parser": "^1.19.0",
6357
"esbuild": "^0.15.12",
6458
"eslint": "^8.25.0",
65-
"eslint-plugin-mocha": "^10.1.0",
59+
"eslint-plugin-mocha": "10.3.0",
6660
"express": "^4.17.1",
6761
"method-override": "^3.0.0",
6862
"mocha": "^10.1.0",
6963
"npm-run-all": "^4.1.5",
7064
"prettier": "^2.7.1",
7165
"request": "^2.88.2",
72-
"restify": "^8.5.1",
66+
"restify": "^4.3.4",
7367
"sinon": "^14.0.1",
7468
"tsup": "^6.3.0",
7569
"typescript": "^4.8.4"
7670
},
7771
"engines": {
78-
"node": ">=14"
72+
"node": ">=16"
7973
}
80-
}
74+
}

src/buildQuery.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export function getBuildQuery(
3939
}
4040

4141
if (queryOptions.sort) {
42+
// Necessary to support Mongoose 8
43+
if (typeof queryOptions.sort === 'object') {
44+
Object.keys(queryOptions.sort).forEach(key => {
45+
// @ts-expect-error this is fine 🐶🔥
46+
queryOptions.sort[key] = Number(queryOptions.sort[key]);
47+
});
48+
}
4249
// @ts-expect-error this is fine 🐶🔥
4350
query.sort(queryOptions.sort);
4451
}

src/operations.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export function operations(
185185
options.contextFilter(contextModel, req, (filteredContext) => {
186186
// @ts-expect-error this is fine 🐶🔥
187187
findById(filteredContext, req.params.id)
188-
.findOneAndRemove()
188+
.findOneAndDelete() // switched to findOneAndDelete to add support for Mongoose 7 and 8
189189
.then((item) => {
190190
if (!item) {
191191
return errorHandler(new Error(STATUS_CODES[404]), req, res, next);
@@ -199,7 +199,7 @@ export function operations(
199199
});
200200
} else {
201201
req.erm.document
202-
?.remove()
202+
?.deleteOne() // switched to deleteOne to add support for Mongoose 7 and 8
203203
.then(() => {
204204
req.erm.statusCode = 204;
205205

@@ -269,7 +269,8 @@ export function operations(
269269
const path = contextModel.schema.path(key);
270270

271271
// @ts-expect-error this is fine 🐶🔥
272-
if (path && path.caster && path.caster.instance === "ObjectID") {
272+
// Add support for Mongoose 7 and 8 while keeping backwards-compatibility to 6 by allowing ObjectID and ObejctId
273+
if (path && path.caster && (path.caster.instance === "ObjectID" || path.caster.instance === "ObjectId")) {
273274
if (Array.isArray(value)) {
274275
for (let j = 0; j < value.length; ++j) {
275276
if (typeof value[j] === "object") {
@@ -282,7 +283,8 @@ export function operations(
282283
dst[key] = value._id;
283284
}
284285
} else if (isPlainObject(value)) {
285-
if (path && path.instance === "ObjectID") {
286+
// Add support for Mongoose 7 and 8 while keeping backwards-compatibility to 6 by allowing ObjectID and ObejctId
287+
if (path && (path.instance === "ObjectID" || path.instance === "ObjectId")) {
286288
dst[key] = value._id;
287289
} else {
288290
dst[key] = depopulate(value);

src/resource_filter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { getProperty, hasProperty } from "dot-prop";
1+
import dotProp from "dot-prop";
22
import mongoose from "mongoose";
33
import { detective } from "./detective.js";
44
import { QueryOptions } from "./getQuerySchema.js";
55
import { Access, ExcludedMap, FilteredKeys } from "./types";
66
import { weedout } from "./weedout.js";
7+
const { get: getProperty, has: hasProperty } = dotProp; // Because we're using an older version of dotProp that supports CommonJS
78

89
export class Filter {
910
excludedMap: ExcludedMap = new Map();

test/express.js renamed to test/express.mjs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import bodyParser from "body-parser";
22
import express from "express";
33
import methodOverride from "method-override";
44

5-
import accessTests from "./integration/access.js";
6-
import contextFilterTests from "./integration/contextFilter.js";
7-
import createTests from "./integration/create.js";
8-
import deleteTests from "./integration/delete.js";
9-
import hookTests from "./integration/hooks.js";
10-
import middlewareTests from "./integration/middleware.js";
11-
import optionsTests from "./integration/options.js";
12-
import readTests from "./integration/read.js";
13-
import updateTests from "./integration/update.js";
14-
import virtualsTests from "./integration/virtuals.js";
5+
import accessTests from "./integration/access.mjs";
6+
import contextFilterTests from "./integration/contextFilter.mjs";
7+
import createTests from "./integration/create.mjs";
8+
import deleteTests from "./integration/delete.mjs";
9+
import hookTests from "./integration/hooks.mjs";
10+
import middlewareTests from "./integration/middleware.mjs";
11+
import optionsTests from "./integration/options.mjs";
12+
import readTests from "./integration/read.mjs";
13+
import updateTests from "./integration/update.mjs";
14+
import virtualsTests from "./integration/virtuals.mjs";
1515

16-
import setupDb from "./integration/setup.js";
16+
import setupDb from "./integration/setup.mjs";
1717

1818
const db = setupDb();
1919

test/integration/access.js renamed to test/integration/access.mjs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "assert";
22
import request from "request";
33
import { serve } from "../../dist/express-restify-mongoose.js";
44

5-
import setupDb from "./setup.js";
5+
import setupDb from "./setup.mjs";
66

77
export default function (createFn, setup, dismantle) {
88
const db = setupDb();
@@ -1075,19 +1075,20 @@ export default function (createFn, setup, dismantle) {
10751075
},
10761076
});
10771077

1078-
db.models.Customer.findById(customer._id, (err, customer) => {
1079-
assert.ok(!err);
1080-
assert.equal(customer.age, 12);
1081-
assert.deepEqual(customer.favorites.toObject(), {
1082-
animal: "Boar",
1083-
color: "Jade",
1084-
purchase: {
1085-
item: product._id,
1086-
number: 1,
1087-
},
1088-
});
1089-
done();
1090-
});
1078+
db.models.Customer.findById(customer._id)
1079+
.then((customer) => {
1080+
assert.equal(customer.age, 12);
1081+
assert.deepEqual(customer.favorites.toObject(), {
1082+
animal: "Boar",
1083+
color: "Jade",
1084+
purchase: {
1085+
item: product._id,
1086+
number: 1,
1087+
},
1088+
});
1089+
done();
1090+
})
1091+
.catch(done);
10911092
}
10921093
);
10931094
});
@@ -1122,19 +1123,22 @@ export default function (createFn, setup, dismantle) {
11221123
},
11231124
});
11241125

1125-
db.models.Customer.findById(customer._id, (err, customer) => {
1126-
assert.ok(!err);
1127-
assert.equal(customer.age, 12);
1128-
assert.deepEqual(customer.favorites.toObject(), {
1129-
animal: "Boar",
1130-
color: "",
1131-
purchase: {
1132-
item: product._id,
1133-
number: 1,
1134-
},
1135-
});
1136-
done();
1137-
});
1126+
db.models.Customer.findById(customer._id)
1127+
.then((foundCustomer) => {
1128+
assert.equal(foundCustomer.age, 12);
1129+
assert.deepEqual(foundCustomer.favorites.toObject(), {
1130+
animal: "Boar",
1131+
color: "",
1132+
purchase: {
1133+
item: product._id,
1134+
number: 1,
1135+
},
1136+
});
1137+
done();
1138+
})
1139+
.catch(done);
1140+
1141+
11381142
}
11391143
);
11401144
});
@@ -1643,11 +1647,11 @@ export default function (createFn, setup, dismantle) {
16431647
},
16441648
});
16451649

1646-
db.models.Customer.findById(customer._id, (err, customer) => {
1647-
assert.ok(!err);
1648-
assert.equal(customer.age, 12);
1649-
assert.equal(customer.comment, "Boo");
1650-
assert.deepEqual(customer.favorites.toObject(), {
1650+
db.models.Customer.findById(customer._id)
1651+
.then((foundCustomer) => {
1652+
assert.equal(foundCustomer.age, 12);
1653+
assert.equal(foundCustomer.comment, "Boo");
1654+
assert.deepEqual(foundCustomer.favorites.toObject(), {
16511655
animal: "Boar",
16521656
color: "Black",
16531657
purchase: {
@@ -1656,7 +1660,8 @@ export default function (createFn, setup, dismantle) {
16561660
},
16571661
});
16581662
done();
1659-
});
1663+
})
1664+
.catch(done);
16601665
}
16611666
);
16621667
});
@@ -1690,11 +1695,11 @@ export default function (createFn, setup, dismantle) {
16901695
},
16911696
});
16921697

1693-
db.models.Customer.findById(customer._id, (err, customer) => {
1694-
assert.ok(!err);
1695-
assert.equal(customer.age, 12);
1696-
assert.equal(customer.comment, "Boo");
1697-
assert.deepEqual(customer.favorites.toObject(), {
1698+
db.models.Customer.findById(customer._id)
1699+
.then((foundCustomer) => {
1700+
assert.equal(foundCustomer.age, 12);
1701+
assert.equal(foundCustomer.comment, "Boo");
1702+
assert.deepEqual(foundCustomer.favorites.toObject(), {
16981703
animal: "Boar",
16991704
color: "Black",
17001705
purchase: {
@@ -1703,7 +1708,8 @@ export default function (createFn, setup, dismantle) {
17031708
},
17041709
});
17051710
done();
1706-
});
1711+
})
1712+
.catch(done);
17071713
}
17081714
);
17091715
});

0 commit comments

Comments
 (0)