Skip to content

Commit

Permalink
Add Mongoose 8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
T-vK committed Mar 7, 2024
1 parent ef92047 commit 4dd760c
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 1,426 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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-restify-mongoose",
"version": "8.0.1",
"version": "9.0.0",
"description": "Easily create a flexible REST interface for mongoose models",
"keywords": [
"ReST",
Expand Down Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"dot-prop": "^6.0.0",
"lodash.isplainobject": "~4.0.6",
"mongoose": "6.x",
"mongoose": "^8.2.1",
"serialize-error": "^8.1.0",
"zod": "^3.19.1"
},
Expand All @@ -63,12 +63,12 @@
"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
82 changes: 44 additions & 38 deletions test/integration/access.mjs
Original file line number Diff line number Diff line change
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
40 changes: 22 additions & 18 deletions test/integration/contextFilter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ export default function (createFn, setup, dismantle) {
assert.ok(!err);
assert.equal(res.statusCode, 404);

db.models.Customer.findById(customers[0]._id, (err, customer) => {
assert.ok(!err);
assert.notEqual(customer.name, "Bobby");
db.models.Customer.findById(customers[0]._id)
.then((foundCustomer) => {
assert.notEqual(foundCustomer.name, "Bobby");
done();
});
})
.catch(done);
}
);
});
Expand All @@ -184,11 +185,12 @@ export default function (createFn, setup, dismantle) {
assert.ok(!err);
assert.equal(res.statusCode, 204);

db.models.Customer.findById(customers[1]._id, (err, customer) => {
assert.ok(!err);
assert.ok(!customer);
db.models.Customer.findById(customers[1]._id)
.then((foundCustomer) => {
assert.ok(!foundCustomer);
done();
});
})
.catch(done);
}
);
});
Expand All @@ -203,12 +205,13 @@ export default function (createFn, setup, dismantle) {
assert.ok(!err);
assert.equal(res.statusCode, 404);

db.models.Customer.findById(customers[2]._id, (err, customer) => {
assert.ok(!err);
assert.ok(customer);
assert.equal(customer.name, "Mike");
db.models.Customer.findById(customers[2]._id)
.then((foundCustomer) => {
assert.ok(foundCustomer);
assert.equal(foundCustomer.name, "Mike");
done();
});
})
.catch(done);
}
);
});
Expand All @@ -223,11 +226,12 @@ export default function (createFn, setup, dismantle) {
assert.ok(!err);
assert.equal(res.statusCode, 204);

db.models.Customer.countDocuments((err, count) => {
assert.ok(!err);
assert.equal(count, 2);
done();
});
db.models.Customer.countDocuments()
.then((count) => {
assert.equal(count, 2);
done();
})
.catch(done);
}
);
});
Expand Down
3 changes: 1 addition & 2 deletions test/integration/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,13 @@ export default function (createFn, setup, dismantle) {
assert.ok(!err);
assert.equal(res.statusCode, 400);
delete body.message;
delete body.errors.customer.message;
assert.deepEqual(body, {
name: "ValidationError",
_message: "Invoice validation failed",
errors: {
customer: {
kind: "ObjectId",
message:
'Cast to ObjectId failed for value "invalid-id" (type string) at path "customer" because of "BSONTypeError"',
name: "CastError",
path: "customer",
stringValue: '"invalid-id"',
Expand Down
26 changes: 15 additions & 11 deletions test/integration/delete.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ export default function (createFn, setup, dismantle) {
assert.ok(!err);
assert.equal(res.statusCode, 204);

db.models.Customer.find({}, (err, customers) => {
assert.ok(!err);
db.models.Customer.find({})
.then((customers) => {
assert.equal(customers.length, 2);
customers.forEach((customer) => {
assert.ok(customer.name !== "John");
});
done();
});
})
.catch(done);
}
);
});
Expand Down Expand Up @@ -259,14 +260,17 @@ export default function (createFn, setup, dismantle) {
assert.ok(!err);
assert.equal(res.statusCode, 204);

db.models.Customer.find({}, (err, customers) => {
assert.ok(!err);
assert.equal(customers.length, 2);
customers.forEach((customer) => {
assert.ok(customer.name !== "John");
});
done();
});
db.models.Customer.find({})
.then((customers) => {
assert.equal(customers.length, 2);

customers.forEach((customer) => {
assert.ok(customer.name !== "John");
});

done();
})
.catch(done);
}
);
});
Expand Down
Loading

0 comments on commit 4dd760c

Please sign in to comment.