Skip to content

Commit

Permalink
Merge pull request #456 from T-vK/main
Browse files Browse the repository at this point in the history
Fix support for CJS and add support for Mongoose 8 (and MongoDB 7)
  • Loading branch information
florianholzapfel authored Mar 8, 2024
2 parents 68ca6e4 + 4dd760c commit 575c4ae
Show file tree
Hide file tree
Showing 35 changed files with 2,229 additions and 2,076 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
mongodb-version: ["4.4", "5.0", "6.0"]
node-version: [14.x, 16.x]
mongodb-version: ["4.4", "5.0", "6.0", "7.0"]
node-version: [16.x, 18.x, 20.x, 21.x]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ npm install express-restify-mongoose --save

## Compatibility

| This library | Mongoose |
| ------------ | -------- |
| >= 7.0.0 | >= 6.x |
| >= 6.0.0 | >= 5.8.0 |
| >= 1.0.0 | 4.x |
| 0.7.5 | 3.x |
| This library | Mongoose | MongoDB | NodeJS
| ------------ | ----------- | ----------- | --------
| >= 9.0.0 | 6.x - 8.x | 3.6.x - 7.x | >=16
| >= 8.0.0 | 6.x | 3.6.x - 6.x | >=18
| >= 7.0.0 | 6.x | 3.6.x - 6.x | >=14
| >= 6.0.0 | 5.8.0 - 6.x | 3.6.x - 6.x | >=6
| >= 1.0.0 | 4.x | 2.4 - 3.4 | >=0.10
| 0.7.5 | 3.x | 2.4 - 3.0 | *

## Contributing

Expand Down
34 changes: 14 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-restify-mongoose",
"version": "8.0.0",
"version": "9.0.0",
"description": "Easily create a flexible REST interface for mongoose models",
"keywords": [
"ReST",
Expand All @@ -23,33 +23,27 @@
"name": "Florian Holzapfel",
"email": "flo.holzapfel@gmail.com"
},
"type": "module",
"exports": {
"import": "./dist/express-restify-mongoose.js",
"require": "./dist/cjs/express-restify-mongoose.js"
},
"main": "./dist/cjs/express-restify-mongoose.js",
"main": "./dist/express-restify-mongoose.js",
"files": [
"dist/"
],
"scripts": {
"build": "run-p build:*",
"build:esm": "swc src --out-dir dist",
"build:cjs": "swc src --config module.type=commonjs --out-dir dist/cjs",
"build:cjs": "swc src --config module.type=commonjs --out-dir dist",
"build:dts": "tsup src/express-restify-mongoose.ts --dts-only",
"lint": "eslint . --ext .js,.ts",
"test": "run-s test:*",
"test:express": "mocha -R spec ./test/express.js --timeout 10s",
"test:filter": "mocha -R spec ./test/integration/resource_filter.js --timeout 10s",
"test:restify": "mocha -R spec ./test/restify.js --timeout 10s",
"test:unit": "mocha -R spec ./test/unit.js",
"test:express": "mocha -R spec ./test/express.mjs --timeout 10s",
"test:filter": "mocha -R spec ./test/integration/resource_filter.mjs --timeout 10s",
"test:restify": "mocha -R spec ./test/restify.mjs --timeout 10s",
"test:unit": "mocha -R spec ./test/unit.mjs",
"tsc": "tsc --noEmit"
},
"dependencies": {
"dot-prop": "^7.2.0",
"dot-prop": "^6.0.0",
"lodash.isplainobject": "~4.0.6",
"mongoose": "6.x",
"serialize-error": "^11.0.0",
"mongoose": "^8.2.1",
"serialize-error": "^8.1.0",
"zod": "^3.19.1"
},
"devDependencies": {
Expand All @@ -62,19 +56,19 @@
"body-parser": "^1.19.0",
"esbuild": "^0.15.12",
"eslint": "^8.25.0",
"eslint-plugin-mocha": "^10.1.0",
"eslint-plugin-mocha": "10.3.0",
"express": "^4.17.1",
"method-override": "^3.0.0",
"mocha": "^10.1.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"request": "^2.88.2",
"restify": "^8.5.1",
"restify": "^4.3.4",
"sinon": "^14.0.1",
"tsup": "^6.3.0",
"typescript": "^4.8.4"
},
"engines": {
"node": ">=14"
"node": ">=16"
}
}
}
7 changes: 7 additions & 0 deletions src/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export function getBuildQuery(
}

if (queryOptions.sort) {
// Necessary to support Mongoose 8
if (typeof queryOptions.sort === 'object') {
Object.keys(queryOptions.sort).forEach(key => {
// @ts-expect-error this is fine 🐶🔥
queryOptions.sort[key] = Number(queryOptions.sort[key]);
});
}
// @ts-expect-error this is fine 🐶🔥
query.sort(queryOptions.sort);
}
Expand Down
10 changes: 6 additions & 4 deletions src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function operations(
options.contextFilter(contextModel, req, (filteredContext) => {
// @ts-expect-error this is fine 🐶🔥
findById(filteredContext, req.params.id)
.findOneAndRemove()
.findOneAndDelete() // switched to findOneAndDelete to add support for Mongoose 7 and 8
.then((item) => {
if (!item) {
return errorHandler(new Error(STATUS_CODES[404]), req, res, next);
Expand All @@ -199,7 +199,7 @@ export function operations(
});
} else {
req.erm.document
?.remove()
?.deleteOne() // switched to deleteOne to add support for Mongoose 7 and 8
.then(() => {
req.erm.statusCode = 204;

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

// @ts-expect-error this is fine 🐶🔥
if (path && path.caster && path.caster.instance === "ObjectID") {
// Add support for Mongoose 7 and 8 while keeping backwards-compatibility to 6 by allowing ObjectID and ObejctId
if (path && path.caster && (path.caster.instance === "ObjectID" || path.caster.instance === "ObjectId")) {
if (Array.isArray(value)) {
for (let j = 0; j < value.length; ++j) {
if (typeof value[j] === "object") {
Expand All @@ -282,7 +283,8 @@ export function operations(
dst[key] = value._id;
}
} else if (isPlainObject(value)) {
if (path && path.instance === "ObjectID") {
// Add support for Mongoose 7 and 8 while keeping backwards-compatibility to 6 by allowing ObjectID and ObejctId
if (path && (path.instance === "ObjectID" || path.instance === "ObjectId")) {
dst[key] = value._id;
} else {
dst[key] = depopulate(value);
Expand Down
3 changes: 2 additions & 1 deletion src/resource_filter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getProperty, hasProperty } from "dot-prop";
import dotProp from "dot-prop";
import mongoose from "mongoose";
import { detective } from "./detective.js";
import { QueryOptions } from "./getQuerySchema.js";
import { Access, ExcludedMap, FilteredKeys } from "./types";
import { weedout } from "./weedout.js";
const { get: getProperty, has: hasProperty } = dotProp; // Because we're using an older version of dotProp that supports CommonJS

export class Filter {
excludedMap: ExcludedMap = new Map();
Expand Down
22 changes: 11 additions & 11 deletions test/express.js → test/express.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import bodyParser from "body-parser";
import express from "express";
import methodOverride from "method-override";

import accessTests from "./integration/access.js";
import contextFilterTests from "./integration/contextFilter.js";
import createTests from "./integration/create.js";
import deleteTests from "./integration/delete.js";
import hookTests from "./integration/hooks.js";
import middlewareTests from "./integration/middleware.js";
import optionsTests from "./integration/options.js";
import readTests from "./integration/read.js";
import updateTests from "./integration/update.js";
import virtualsTests from "./integration/virtuals.js";
import accessTests from "./integration/access.mjs";
import contextFilterTests from "./integration/contextFilter.mjs";
import createTests from "./integration/create.mjs";
import deleteTests from "./integration/delete.mjs";
import hookTests from "./integration/hooks.mjs";
import middlewareTests from "./integration/middleware.mjs";
import optionsTests from "./integration/options.mjs";
import readTests from "./integration/read.mjs";
import updateTests from "./integration/update.mjs";
import virtualsTests from "./integration/virtuals.mjs";

import setupDb from "./integration/setup.js";
import setupDb from "./integration/setup.mjs";

const db = setupDb();

Expand Down
84 changes: 45 additions & 39 deletions test/integration/access.js → test/integration/access.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "assert";
import request from "request";
import { serve } from "../../dist/express-restify-mongoose.js";

import setupDb from "./setup.js";
import setupDb from "./setup.mjs";

export default function (createFn, setup, dismantle) {
const db = setupDb();
Expand Down Expand Up @@ -1075,19 +1075,20 @@ export default function (createFn, setup, dismantle) {
},
});

db.models.Customer.findById(customer._id, (err, customer) => {
assert.ok(!err);
assert.equal(customer.age, 12);
assert.deepEqual(customer.favorites.toObject(), {
animal: "Boar",
color: "Jade",
purchase: {
item: product._id,
number: 1,
},
});
done();
});
db.models.Customer.findById(customer._id)
.then((customer) => {
assert.equal(customer.age, 12);
assert.deepEqual(customer.favorites.toObject(), {
animal: "Boar",
color: "Jade",
purchase: {
item: product._id,
number: 1,
},
});
done();
})
.catch(done);
}
);
});
Expand Down Expand Up @@ -1122,19 +1123,22 @@ export default function (createFn, setup, dismantle) {
},
});

db.models.Customer.findById(customer._id, (err, customer) => {
assert.ok(!err);
assert.equal(customer.age, 12);
assert.deepEqual(customer.favorites.toObject(), {
animal: "Boar",
color: "",
purchase: {
item: product._id,
number: 1,
},
});
done();
});
db.models.Customer.findById(customer._id)
.then((foundCustomer) => {
assert.equal(foundCustomer.age, 12);
assert.deepEqual(foundCustomer.favorites.toObject(), {
animal: "Boar",
color: "",
purchase: {
item: product._id,
number: 1,
},
});
done();
})
.catch(done);


}
);
});
Expand Down Expand Up @@ -1643,11 +1647,11 @@ export default function (createFn, setup, dismantle) {
},
});

db.models.Customer.findById(customer._id, (err, customer) => {
assert.ok(!err);
assert.equal(customer.age, 12);
assert.equal(customer.comment, "Boo");
assert.deepEqual(customer.favorites.toObject(), {
db.models.Customer.findById(customer._id)
.then((foundCustomer) => {
assert.equal(foundCustomer.age, 12);
assert.equal(foundCustomer.comment, "Boo");
assert.deepEqual(foundCustomer.favorites.toObject(), {
animal: "Boar",
color: "Black",
purchase: {
Expand All @@ -1656,7 +1660,8 @@ export default function (createFn, setup, dismantle) {
},
});
done();
});
})
.catch(done);
}
);
});
Expand Down Expand Up @@ -1690,11 +1695,11 @@ export default function (createFn, setup, dismantle) {
},
});

db.models.Customer.findById(customer._id, (err, customer) => {
assert.ok(!err);
assert.equal(customer.age, 12);
assert.equal(customer.comment, "Boo");
assert.deepEqual(customer.favorites.toObject(), {
db.models.Customer.findById(customer._id)
.then((foundCustomer) => {
assert.equal(foundCustomer.age, 12);
assert.equal(foundCustomer.comment, "Boo");
assert.deepEqual(foundCustomer.favorites.toObject(), {
animal: "Boar",
color: "Black",
purchase: {
Expand All @@ -1703,7 +1708,8 @@ export default function (createFn, setup, dismantle) {
},
});
done();
});
})
.catch(done);
}
);
});
Expand Down
Loading

0 comments on commit 575c4ae

Please sign in to comment.