Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add non-convertable query also to field index
fix invalid tslint.json and removed debugging info
  • Loading branch information
marcj committed Feb 8, 2019
1 parent ddf1e6f commit 7eb5f21
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/mongo/src/mapping.ts
Expand Up @@ -479,7 +479,6 @@ export function convertClassQueryToMongo<T, K extends keyof T>(
fieldNamesMap: {[name: string]: boolean} = {},
): { [path: string]: any } {
return convertQueryToMongo(classType, target, (convertClassType: ClassType<any>, path: string, value: any) => {
console.log('convert classtomongo', path, value, propertyClassToMongo(convertClassType, path, value));
return propertyClassToMongo(convertClassType, path, value);
}, fieldNamesMap);
}
Expand Down Expand Up @@ -533,6 +532,7 @@ export function convertQueryToMongo<T, K extends keyof T>(
(fieldValue as any)[j] = (queryValue as any[]).map(v => converter(classType, i, v));
} else if (j === '$text' || j === '$exists' || j === '$mod' || j === '$size' || j === '$type' || j === '$regex' || j === '$where') {
//don't transform
fieldNamesMap[i] = true;
} else {
fieldNamesMap[i] = true;
(fieldValue as any)[j] = converter(classType, i, queryValue);
Expand Down
19 changes: 16 additions & 3 deletions packages/mongo/tests/mongo-query.spec.ts
Expand Up @@ -29,37 +29,50 @@ class Simple {
}

test('simple', () => {
const fieldNames = {};
const m = convertPlainQueryToMongo(Simple, {
id: {$qt: '1'}
});
}, fieldNames);

expect(m['id']['$qt']).toBe(1);
expect(Object.keys(fieldNames)).toEqual(['id']);
});

test('simple class', () => {
const partial = propertyClassToMongo(Simple, 'config', new SimpleConfig(['a', 'b']));
expect(partial).toEqual(['a', 'b']);
const fieldNames = {};

const m = convertClassQueryToMongo(Simple, {
id: {$qt: '1'},
config: new SimpleConfig(['a', 'b'])
});
}, fieldNames);

expect(m['id']['$qt']).toBe(1);
expect(m['config']).toEqual(['a', 'b']);
expect(Object.keys(fieldNames)).toEqual(['id', 'config']);
});

test('simple 2', () => {
const m = convertPlainQueryToMongo(Simple, {id: {dif: 1}});
expect(m).toEqual({id: {dif: 1}});
});

test('regex', () => {
const fieldNames = {};
const m = convertPlainQueryToMongo(Simple, {id: {$regex: /0-9+/}}, fieldNames);
expect(m).toEqual({id: {$regex: /0-9+/}});
expect(Object.keys(fieldNames)).toEqual(['id']);
});

test('and', () => {
const m = convertPlainQueryToMongo(Simple, {$and: [{id: '1'}, {id: '2'}]});
const fieldNames = {};
const m = convertPlainQueryToMongo(Simple, {$and: [{id: '1'}, {id: '2'}]}, fieldNames);
expect(m).toEqual({$and: [{id: 1}, {id: 2}]});

expect(m['$and'][0]['id']).toBe(1);
expect(m['$and'][1]['id']).toBe(2);
expect(Object.keys(fieldNames)).toEqual(['id']);
});

test('in', () => {
Expand Down
3 changes: 0 additions & 3 deletions tslint.json
@@ -1,7 +1,4 @@
{
// "rulesDirectory": [
// "node_modules/codelyzer"
// ],
"rules": {
"arrow-return-shorthand": true,
"callable-types": true,
Expand Down

0 comments on commit 7eb5f21

Please sign in to comment.