Skip to content

Commit 2ef7155

Browse files
corydeppennodkz
authored andcommitted
fix: toMongoDottedObject now respects primitives types Date, Decimal128 and other
Close #184
1 parent a59e59e commit 2ef7155

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* @flow */
2+
3+
import { toMongoDottedObject } from '../../utils/toMongoDottedObject';
4+
5+
describe('toMongoDottedObject()', () => {
6+
it('should handle operators using date object values when nested', () => {
7+
expect(toMongoDottedObject({ a: { dateField: { $gte: new Date(100) } } })).toEqual({
8+
'a.dateField': { $gte: new Date(100) },
9+
});
10+
});
11+
});

src/utils/toMongoDottedObject.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
import { Types } from 'mongoose';
44
import { isObject } from 'graphql-compose';
55

6-
const ObjectId = Types.ObjectId;
6+
const primitives = [Types.ObjectId, Date, String, Number, Boolean, Types.Decimal128];
7+
8+
function isPrimitive(value: any) {
9+
return primitives.some(p => value instanceof p);
10+
}
711

812
function _toMongoDottedObject(obj, target = {}, path = [], filter = false) {
913
if (!isObject(obj) && !Array.isArray(obj)) return obj;
14+
if (isPrimitive(obj)) return obj;
1015
const objKeys = Object.keys(obj);
1116

1217
/* eslint-disable */
@@ -23,7 +28,7 @@ function _toMongoDottedObject(obj, target = {}, path = [], filter = false) {
2328
[key]: val,
2429
};
2530
}
26-
} else if (Object(obj[key]) === obj[key] && !(obj[key] instanceof ObjectId)) {
31+
} else if (Object(obj[key]) === obj[key] && !isPrimitive(obj[key])) {
2732
_toMongoDottedObject(
2833
obj[key],
2934
target,

0 commit comments

Comments
 (0)