Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

fix: Add mongodb-query-parser and use its stringify COMPASS-4481 #227

Merged
merged 1 commit into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 62 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"marky": "^1.2.1",
"mime-types": "^2.1.24",
"mongodb-extjson": "^4.0.0-rc1",
"mongodb-query-parser": "^2.1.2",
"mongodb-schema": "^8.2.5",
"object-sizeof": "^1.5.1",
"parse-json": "^5.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/utils/get-shell-js.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { stringify as toJavascriptString } from 'javascript-stringify';
import {stringify} from 'mongodb-query-parser';
import toNS from 'mongodb-ns';

export default function(ns, spec) {
let ret = `db.${toNS(ns).collection}.find(\n`;
ret += ' ' + toJavascriptString(spec.filter, null, '');
ret += ' ' + stringify(spec.filter, '');
if (spec.project) {
ret += ',\n ' + toJavascriptString(spec.project, null, '');
ret += ',\n ' + stringify(spec.project, '');
}
ret += '\n)';
if (spec.limit) {
Expand Down
22 changes: 15 additions & 7 deletions src/utils/get-shell-js.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import {ObjectId} from 'bson';
import getShellJS from './get-shell-js';

describe('get-shell-js', () => {
it('should support simple query', () => {
const ret = getShellJS('lucas.pets', { filter: { name: 'Arlo' } });
const expected = `db.pets.find(
{name:'Arlo'}
{name: 'Arlo'}
)`;
expect(ret).to.equal(expected);
});
it('should support simple ObjectId', () => {
const ret = getShellJS('lucas.pets', { filter: { _id: new ObjectId('deadbeefdeadbeefdeadbeef') } });
const expected = `db.pets.find(
{_id: ObjectId('deadbeefdeadbeefdeadbeef')}
)`;
expect(ret).to.equal(expected);
});
Expand All @@ -14,8 +22,8 @@ describe('get-shell-js', () => {
project: { name: 1 }
});
const expected = `db.pets.find(
{name:'Arlo'},
{name:1}
{name: 'Arlo'},
{name: 1}
)`;
expect(ret).to.equal(expected);
});
Expand All @@ -26,8 +34,8 @@ describe('get-shell-js', () => {
limit: 100
});
const expected = `db.pets.find(
{name:'Arlo'},
{name:1}
{name: 'Arlo'},
{name: 1}
).limit(100)`;

expect(ret).to.equal(expected);
Expand All @@ -40,8 +48,8 @@ describe('get-shell-js', () => {
skip: 1
});
const expected = `db.pets.find(
{name:'Arlo'},
{name:1}
{name: 'Arlo'},
{name: 1}
).limit(100).skip(1)`;

expect(ret).to.equal(expected);
Expand Down