Skip to content

Commit 857cf1e

Browse files
fix(compatibility): fix mongoose 7 compatibility (#1040)
1 parent 3e6e1ac commit 857cf1e

File tree

13 files changed

+168
-192
lines changed

13 files changed

+168
-192
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
fetch-depth: 0
2424
- uses: actions/setup-node@v2
2525
with:
26-
node-version: 14.17.6
26+
node-version: 14.20.1
2727
- uses: actions/cache@v2
2828
with:
2929
path: '**/node_modules'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"jest": "26.6.3",
6060
"jest-extended": "0.11.5",
6161
"lint-staged": "12.3.7",
62-
"mongoose": "5.13.15",
62+
"mongoose": "7.2.2",
6363
"mongoose-fixture-loader": "1.0.2",
6464
"semantic-release": "19.0.3",
6565
"semantic-release-npm-deprecate-old-versions": "1.3.2",

src/services/filters-parser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ function FiltersParser(model, timezone, options) {
2222
// automatically cast 'looking like' string value to ObjectId
2323
// CF Github Issue https://github.com/Automattic/mongoose/issues/1399
2424
const { ObjectId } = options.Mongoose.Types;
25-
if (ObjectId.isValid(value) && ObjectId(value).toString() === value) {
26-
return ObjectId(value);
25+
if (ObjectId.isValid(value) && new ObjectId(value).toString() === value) {
26+
return new ObjectId(value);
2727
}
2828

2929
return value;
@@ -172,8 +172,8 @@ function FiltersParser(model, timezone, options) {
172172
throw new InvalidFiltersFormatError('Filters cannot be a raw array');
173173
}
174174
if (!_.isString(condition.field)
175-
|| !_.isString(condition.operator)
176-
|| _.isUndefined(condition.value)) {
175+
|| !_.isString(condition.operator)
176+
|| _.isUndefined(condition.value)) {
177177
throw new InvalidFiltersFormatError('Invalid condition format');
178178
}
179179

src/services/has-many-getter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class HasManyGetter {
7878
async _getRecordsAndRecordIds() {
7979
let id = this._params.recordId;
8080
if (OBJECTID_REGEXP.test(this._params.recordId)) {
81-
id = this._opts.Mongoose.Types.ObjectId(id);
81+
id = new this._opts.Mongoose.Types.ObjectId(id);
8282
}
8383

8484
const parentRecords = await this._parentModel
@@ -117,7 +117,7 @@ class HasManyGetter {
117117
// NOTICE: Convert values to strings, so ObjectIds could be easily searched and compared.
118118
const recordIdStrings = childRecordIds.map((recordId) => String(recordId));
119119
// NOTICE: indexOf could be improved by making a Map from record-ids to their index.
120-
recordsSorted = _.sortBy(childRecords, record => recordIdStrings.indexOf(String(record._id))); // eslint-disable-line
120+
recordsSorted = _.sortBy(childRecords, record => recordIdStrings.indexOf(String(record._id))); // eslint-disable-line
121121
}
122122

123123
let sortedChildRecords = descending ? recordsSorted.reverse() : recordsSorted;

src/services/search-builder.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ function SearchBuilder(model, opts, params, searchFields) {
2121

2222
_.each(model.schema.paths, (value, key) => {
2323
if ((searchFields && !searchFields.includes(value.path))
24-
|| value.path === model.schema.options.versionKey) {
24+
|| value.path === model.schema.options.versionKey) {
2525
return;
2626
}
2727

2828
const condition = {};
2929
const searchValue = params.search.replace(/[-[\]{}()*+!<=:?./\\^$|#\s,]/g, '\\$&');
3030
const searchRegexp = new RegExp(`.*${searchValue}.*`, 'i');
3131

32-
if (value.instance === 'ObjectID') {
32+
if (value.instance === 'ObjectID' || value.instance === 'ObjectId') {
3333
if (new RegExp('^[0-9a-fA-F]{24}$').test(params.search)) {
34-
condition[key] = opts.Mongoose.Types.ObjectId(params.search);
34+
condition[key] = new opts.Mongoose.Types.ObjectId(params.search);
3535
pushCondition(condition, key);
3636
}
3737
} else if (value.instance === 'Number') {

src/utils/field-analyser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class FieldAnalyser {
148148
if (fieldInfo.enumValues && fieldInfo.enumValues.length) {
149149
return 'Enum';
150150
}
151-
if (fieldInfo.instance === 'ObjectID') {
151+
if (fieldInfo.instance === 'ObjectID' || fieldInfo.instance === 'ObjectId') {
152152
// Deal with ObjectID
153153
return 'String';
154154
}

src/utils/schema.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ const getNestedFieldType = (mongooseSchema, nestedFieldPath) => {
2121

2222
let nestedFieldDeclaration;
2323

24-
if (mongooseSchema.tree) {
24+
if (mongooseSchema.tree?.[currentFieldName]) {
2525
nestedFieldDeclaration = mongooseSchema.tree[currentFieldName];
26-
} else if (mongooseSchema.type) {
26+
} else if (mongooseSchema.type?.[currentFieldName]) {
2727
nestedFieldDeclaration = mongooseSchema.type[currentFieldName];
28-
} else {
28+
} else if (mongooseSchema[currentFieldName]) {
2929
nestedFieldDeclaration = mongooseSchema[currentFieldName];
30+
} else if (mongooseSchema.type?.tree?.[currentFieldName]) {
31+
nestedFieldDeclaration = mongooseSchema.type?.tree?.[currentFieldName];
3032
}
3133

3234
if (!nestedFieldDeclaration) return undefined;

test/tests/adapters/mongoose.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ const { Schema } = mongoose;
77
describe('adapters > schema-adapter', () => {
88
afterEach((done) => {
99
delete mongoose.models.Foo;
10-
delete mongoose.modelSchemas.Foo;
1110
delete mongoose.models.User;
12-
delete mongoose.modelSchemas.User;
1311
delete mongoose.models.Bar;
14-
delete mongoose.modelSchemas.Bar;
12+
13+
// For older mongoose versions
14+
if (mongoose.modelSchemas) {
15+
delete mongoose.modelSchemas.Foo;
16+
delete mongoose.modelSchemas.User;
17+
delete mongoose.modelSchemas.Bar;
18+
}
1519
done();
1620
});
1721

test/tests/services/filters-parser.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('service > filters-parser', () => {
155155
const parser = defaultParser.getParserForType('ObjectId');
156156

157157
expect(parser('53c2ae8528d75d572c06adbc')).toBeInstanceOf(mongoose.Types.ObjectId);
158-
expect(parser(ObjectId('53c2ae8528d75d572c06adbc'))).toBeInstanceOf(mongoose.Types.ObjectId);
158+
expect(parser(new ObjectId('53c2ae8528d75d572c06adbc'))).toBeInstanceOf(mongoose.Types.ObjectId);
159159
expect(parser('53c2ae8528d75d572c06adbcc')).not.toBeInstanceOf(mongoose.Types.ObjectId);
160160
expect(parser('star wars with the same ')).not.toBeInstanceOf(mongoose.Types.ObjectId);
161161
});

test/tests/services/resource-getter.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('service > resource-getter', () => {
6464
const getter = new ResourceGetter(ReviewModel, params, options, user);
6565
expect(await getter.perform()).toStrictEqual({
6666
__v: 0, // mongoose version
67-
_id: mongoose.Types.ObjectId('507f1f77bcf86cd799439011'),
67+
_id: new mongoose.Types.ObjectId('507f1f77bcf86cd799439011'),
6868
rating: 0,
6969
});
7070
});

0 commit comments

Comments
 (0)