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

Commit 1f70a41

Browse files
authored
Merge pull request #7 from mongodb-js/show-query-options-in-viewer
COMPASS-3825: Show skip/limit/project in query view on export
2 parents 94ba7e7 + c9553d5 commit 1f70a41

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

src/components/export-modal/export-modal.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class ExportModal extends PureComponent {
192192
<QueryViewer
193193
query={this.props.query}
194194
disabled={isFullCollection}
195+
ns={this.props.ns}
195196
/>
196197
</div>
197198
<div className={style('toggle-full')}>

src/components/query-viewer/query-viewer.jsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { PureComponent } from 'react';
22
import PropTypes from 'prop-types';
33
import AceEditor from 'react-ace';
44
import classnames from 'classnames';
5-
import toJavascriptString from 'javascript-stringify';
5+
import getShellJS from 'utils/get-shell-js';
66

77
import styles from './query-viewer.less';
88

@@ -32,8 +32,9 @@ class QueryViewer extends PureComponent {
3232

3333
static propTypes = {
3434
query: PropTypes.object.isRequired,
35-
disabled: PropTypes.bool.isRequired
36-
}
35+
disabled: PropTypes.bool.isRequired,
36+
ns: PropTypes.string.isRequired
37+
};
3738

3839
/**
3940
* Render the query viewer component.
@@ -47,9 +48,10 @@ class QueryViewer extends PureComponent {
4748
mode="javascript"
4849
theme="mongodb"
4950
width="100%"
50-
value={toJavascriptString(this.props.query.filter, null, ' ')}
51+
value={getShellJS(this.props.ns, this.props.query)}
5152
editorProps={{ $blockScrolling: Infinity }}
52-
setOptions={OPTIONS} />
53+
setOptions={OPTIONS}
54+
/>
5355
</div>
5456
);
5557
}

src/utils/get-shell-js.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import toJavascriptString from 'javascript-stringify';
2+
import toNS from 'mongodb-ns';
3+
4+
export default function(ns, spec) {
5+
let ret = `db.${toNS(ns).collection}.find(\n`;
6+
ret += ' ' + toJavascriptString(spec.filter, null, '');
7+
if (spec.project) {
8+
ret += ',\n ' + toJavascriptString(spec.project, null, '');
9+
}
10+
ret += '\n)';
11+
if (spec.limit) {
12+
ret += `.limit(${spec.limit})`;
13+
}
14+
if (spec.skip) {
15+
ret += `.skip(${spec.skip})`;
16+
}
17+
return ret;
18+
}

src/utils/get-shell-js.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import getShellJS from './get-shell-js';
2+
3+
describe('get-shell-js', () => {
4+
it('should support simple query', () => {
5+
const ret = getShellJS('lucas.pets', { filter: { name: 'Arlo' } });
6+
const expected = `db.pets.find(
7+
{name:'Arlo'}
8+
)`;
9+
expect(ret).to.equal(expected);
10+
});
11+
it('should support a projection', () => {
12+
const ret = getShellJS('lucas.pets', {
13+
filter: { name: 'Arlo' },
14+
project: { name: 1 }
15+
});
16+
const expected = `db.pets.find(
17+
{name:'Arlo'},
18+
{name:1}
19+
)`;
20+
expect(ret).to.equal(expected);
21+
});
22+
it('should support a skip', () => {
23+
const ret = getShellJS('lucas.pets', {
24+
filter: { name: 'Arlo' },
25+
project: { name: 1 },
26+
limit: 100
27+
});
28+
const expected = `db.pets.find(
29+
{name:'Arlo'},
30+
{name:1}
31+
).limit(100)`;
32+
33+
expect(ret).to.equal(expected);
34+
});
35+
it('should support a limit', () => {
36+
const ret = getShellJS('lucas.pets', {
37+
filter: { name: 'Arlo' },
38+
project: { name: 1 },
39+
limit: 100,
40+
skip: 1
41+
});
42+
const expected = `db.pets.find(
43+
{name:'Arlo'},
44+
{name:1}
45+
).limit(100).skip(1)`;
46+
47+
expect(ret).to.equal(expected);
48+
});
49+
});

0 commit comments

Comments
 (0)