Skip to content

Commit

Permalink
add compatibility for Mongoose 7
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 23, 2023
1 parent b95175a commit 1430a8d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
16 changes: 13 additions & 3 deletions src/driver/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResultType = any> = (err: Error | null, res: ResultType | null) => unknown;

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 1430a8d

Please sign in to comment.