diff --git a/package.json b/package.json index b825167b..9911e8fd 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "eslint": "8.47.0", "jsdoc-babel": "^0.5.0", "jsdoc-to-markdown": "^7.1.1", - "mongoose": "^8.0.0", + "mongoose": "7.6.x", "nyc": "^15.1.0", "sinon": "15.2.0", "ts-mocha": "^10.0.0", @@ -94,7 +94,7 @@ "winston": "^3.7.2" }, "peerDependencies": { - "mongoose": "^8.0.0" + "mongoose": "^7.5.0 || ^8.0.0" }, "engines": { "node": ">=14.0.0" diff --git a/src/driver/collection.ts b/src/driver/collection.ts index 03716f2a..90b91ef7 100644 --- a/src/driver/collection.ts +++ b/src/driver/collection.ts @@ -27,6 +27,10 @@ import { } from '@/src/collections/options'; import { JSONAPIDeleteResult } from '../collections/collection'; +import { version } from 'mongoose'; + +const IS_MONGOOSE_7 = version.startsWith('7.'); + type NodeCallback = (err: Error | null, res: ResultType | null) => unknown; /** @@ -125,7 +129,9 @@ export class Collection extends MongooseCollection { processSortOption(options); } const res = await this.collection.findOneAndUpdate(filter, update, options); - if (options?.includeResultMetadata !== false) { + if (IS_MONGOOSE_7) { + return options?.includeResultMetadata === false ? res.value : res; + } else if (options?.includeResultMetadata !== false) { return res.value; } return res; @@ -141,7 +147,9 @@ export class Collection extends MongooseCollection { processSortOption(options); } const res = await this.collection.findOneAndDelete(filter, options); - if (options?.includeResultMetadata !== false) { + if (IS_MONGOOSE_7) { + return options?.includeResultMetadata === false ? res.value : res; + } else if (options?.includeResultMetadata !== false) { return res.value; } return res; @@ -158,7 +166,9 @@ export class Collection extends MongooseCollection { processSortOption(options); } const res = await this.collection.findOneAndReplace(filter, newDoc, options); - if (options?.includeResultMetadata !== false) { + if (IS_MONGOOSE_7) { + return options?.includeResultMetadata === false ? res.value : res; + } else if (options?.includeResultMetadata !== false) { return res.value; } return res;