Skip to content

Commit

Permalink
feat: add export to language button in delete modal COMPASS-7335 (#5032)
Browse files Browse the repository at this point in the history
* chore: basic integration with export to language modal

* chore: fix issues with the transpiler integration

* chore: add support for delete many

* chore: simplify integration, so later the update_many is easier

* chore: add test to code export

* chore: refactor, get the query from the state directly

* chore: linter happy

* chore: remove deprecated telemetry

* chore: add test to export modal

* chore: add test to export modal

* chore: design fixes

* chore: use vertical scrolling for the delete preview

* chore: test exportMode exclusion in compilation

* chore: refactor export-to-language so all queries use same event

* chore: fix linter

* chore: change button to Cancel

* chore: fix missing test

* chore: fix missing test

* chore: forgot to remove .only

* chore: fix linting complains

* chore: fix tests when added exportMode

* chore: fix e2e test

* chore: adapt so it compiles for the update dialog

* chore: revert unwanted changes

* chore: fix references to results in the bson transpiler

* chore: add todo ticket to update bulk export

* chore: refactor, extract export button out of ReadonlyFilter

* chore: after the refactor, this is not necessary

* chore: remove dead code
  • Loading branch information
kmruiz authored Nov 7, 2023
1 parent 41b2b79 commit dfcc0fa
Show file tree
Hide file tree
Showing 50 changed files with 585 additions and 209 deletions.
3 changes: 3 additions & 0 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/bson-transpilers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const getTranspiler = (loadTree, visitor, generator, symbols) => {

const result = {};
Object.keys(input).map((k) => {
result[k] = k === 'options' ? input[k] : compile(input[k], idiomatic, true);
result[k] = (k === 'options' || k === 'exportMode') ? input[k] : compile(input[k], idiomatic, true);
});
if (!('options' in result) ||
!('uri' in result.options) ||
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/pythontogo.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/pythontojava.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/pythontophp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/pythontoruby.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/pythontorust.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/shelltogo.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/shelltojava.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/shelltophp.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/shelltoruby.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/shelltorust.js

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions packages/bson-transpilers/symbols/go/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,25 @@ Templates:
const options = spec.options;
const uri = spec.options.uri
const filter = spec.filter || {};
const exportMode = spec.exportMode;
delete spec.options;
delete spec.filter;
delete spec.exportMode;

const indent = (depth) => ' '.repeat(depth)

let driverMethod;
switch (exportMode) {
case 'Delete Query':
driverMethod = 'DeleteMany';
break;
case 'Update Query':
driverMethod = 'UpdateMany';
break;
default:
driverMethod = 'Find';
}

const comment = []
.concat('// Requires the MongoDB Go Driver')
.concat('// https://go.mongodb.org/mongo-driver')
Expand Down Expand Up @@ -107,9 +121,8 @@ Templates:
.concat(comment)
.concat(connect)
.concat('')
.concat('// Find data')
.concat(`${coll}`)
.concat(`_, err = coll.Find(ctx, ${filter}${optsStr})`)
.concat(`_, err = coll.${driverMethod}(ctx, ${filter}${optsStr})`)
.concat('if err != nil {')
.concat(' log.Fatal(err)')
.concat('}')
Expand Down
30 changes: 25 additions & 5 deletions packages/bson-transpilers/symbols/java/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Templates:
newStr = str.substr(1, str.length - 2);
}
const uri = `"${newStr.replace(/\\([\s\S])|(")/g, '\\$1$2')}"`;


const exportMode = spec.exportMode;
delete spec.exportMode;

const connection = `MongoClient mongoClient = new MongoClient(
new MongoClientURI(
Expand All @@ -42,7 +42,21 @@ Templates:

MongoCollection<Document> collection = database.getCollection("${spec.options.collection}");`;


let driverMethod;
let driverResult;
switch (exportMode) {
case 'Delete Query':
driverMethod = 'deleteMany';
driverResult = 'DeleteResult';
break;
case 'Update Query':
driverMethod = 'updateMany';
driverResult = 'UpdateResult';
break;
default:
driverMethod = 'find';
driverResult = 'FindIterable<Document>';
}
if ('aggregation' in spec) {
return `${comment}\n */\n\n${connection}

Expand All @@ -51,6 +65,7 @@ Templates:

let warning = '';
const defs = Object.keys(spec).reduce((s, k) => {
if (!spec[k]) return s;
if (k === 'options' || k === 'maxTimeMS' || k === 'skip' || k === 'limit' || k === 'collation') return s;
if (s === '') return `Bson ${k} = ${spec[k]};`;
return `${s}
Expand All @@ -75,11 +90,14 @@ Templates:
case 'collation':
warning = '\n *\n * Warning: translating collation to Java not yet supported, so will be ignored';
return s;
case 'exportMode':
return s;
default:
if (!spec[k]) return s;
return `${s}
.${k}(${k})`;
}
}, 'FindIterable<Document> result = collection.find(filter)');
}, `${driverResult} result = collection.${driverMethod}(filter)`);

return `${comment}${warning}\n */\n\n${defs}

Expand Down Expand Up @@ -974,8 +992,10 @@ Templates:
'import org.bson.Document;\n';
if (mode === 'Query') {
imports += 'import com.mongodb.client.FindIterable;';
} else {
} else if (mode === 'Pipeline') {
imports += 'import com.mongodb.client.AggregateIterable;';
} else if (mode === 'Delete Query') {
imports += 'import com.mongodb.client.result.DeleteResult;';
}
return imports;
}
Expand Down
29 changes: 18 additions & 11 deletions packages/bson-transpilers/symbols/javascript/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,29 @@ Templates:
project: 'projection'
};

const exportMode = spec.exportMode;
delete spec.exportMode;

const args = {};
Object.keys(spec).forEach((k) => {
if (k !== 'options') {
args[k in translateKey ? translateKey[k] : k] = spec[k];
}
});

let cmd;
let defs;
if (exportMode == 'Delete Query') {
defs = `const filter = ${args.filter};\n`;
cmd = `const result = coll.deleteMany(filter);`;
}
if ('aggregation' in spec) {
const agg = spec.aggregation;
cmd = `const cursor = coll.aggregate(agg);`;
cmd = `const cursor = coll.aggregate(agg);\nconst result = await cursor.toArray();`;
defs = `const agg = ${agg};\n`;
} else {
} else if (!cmd) {
let opts = '';
const args = {};
Object.keys(spec).forEach((k) => {
if (k !== 'options') {
args[k in translateKey ? translateKey[k] : k] = spec[k];
}
});


if (Object.keys(args).length > 0) {
defs = Object.keys(args).reduce((s, k) => {
if (k !== 'filter') {
Expand All @@ -53,15 +61,14 @@ Templates:
}, '');
opts = opts === '' ? '' : `, { ${opts} }`;
}
cmd = `const cursor = coll.find(filter${opts});`;
cmd = `const cursor = coll.find(filter${opts});\nconst result = await cursor.toArray();`;
}
return `${comment}\n\n${defs}
const client = await MongoClient.connect(
'${spec.options.uri}'
);
const coll = client.db('${spec.options.database}').collection('${spec.options.collection}');
${cmd}
const result = await cursor.toArray();
await client.close();`;
}
EqualitySyntaxTemplate: &EqualitySyntaxTemplate !!js/function >
Expand Down
16 changes: 15 additions & 1 deletion packages/bson-transpilers/symbols/php/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ Templates:
};
const options = spec.options;
const filter = spec.filter || {};
const exportMode = spec.exportMode;
delete spec.options;
delete spec.filter;
delete spec.exportMode;

comment = []
.concat('// Requires the MongoDB PHP Driver')
Expand All @@ -77,6 +79,18 @@ Templates:
;
}

let driverMethod;
switch (exportMode) {
case 'Delete Query':
driverMethod = 'delete_many';
break;
case 'Update Query':
driverMethod = 'update_many';
break;
default:
driverMethod = 'find';
}

let args = Object.keys(spec).reduce(
(result, k) => {
let val = this.utils.removePHPObject(spec[k]);
Expand All @@ -92,7 +106,7 @@ Templates:
.concat('')
.concat(client)
.concat(collection)
.concat(`$cursor = $collection->find(${this.utils.removePHPObject(filter)}${args});`)
.concat(`$cursor = $collection->${driverMethod}(${this.utils.removePHPObject(filter)}${args});`)
.join('\n')
;
}
Expand Down
16 changes: 15 additions & 1 deletion packages/bson-transpilers/symbols/python/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,25 @@ Templates:
maxTimeMS: 'max_time_ms'
};
const options = spec.options;
const exportMode = spec.exportMode;
delete spec.options;
delete spec.exportMode;

const connect = `client = MongoClient('${options.uri}')`;
const coll = `client['${options.database}']['${options.collection}']`;

let driverMethod;
switch (exportMode) {
case 'Delete Query':
driverMethod = 'delete_many';
break;
case 'Update Query':
driverMethod = 'update_many';
break;
default:
driverMethod = 'find';
}

if ('aggregation' in spec) {
return `${comment}\n\n${connect}\nresult = ${coll}.aggregate(${spec.aggregation})`;
}
Expand All @@ -59,7 +73,7 @@ Templates:
},
''
);
const cmd = `result = ${coll}.find(\n${args}\n)`;
const cmd = `result = ${coll}.${driverMethod}(\n${args}\n)`;

return `${comment}\n\n${vars}\n\n${cmd}`;
}
Expand Down
17 changes: 16 additions & 1 deletion packages/bson-transpilers/symbols/ruby/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,27 @@ Templates:
};
const options = spec.options;
const filter = spec.filter || {}
const exportMode = spec.exportMode;

delete spec.options;
delete spec.filter
delete spec.exportMode;

const connect = `client = Mongo::Client.new('${options.uri}', :database => '${options.database}')`;
const coll = `client.database['${options.collection}']`;

let driverMethod;
switch (exportMode) {
case 'Delete Query':
driverMethod = 'delete_many';
break;
case 'Update Query':
driverMethod = 'update_many';
break;
default:
driverMethod = 'find';
}

if ('aggregation' in spec) {
return `${comment}\n\n${connect}\nresult = ${coll}.aggregate(${spec.aggregation})`;
}
Expand All @@ -82,7 +97,7 @@ Templates:
''
);

const cmd = `result = ${coll}.find(${filter}${args ? `, {\n${args}\n}` : ''})`;
const cmd = `result = ${coll}.${driverMethod}(${filter}${args ? `, {\n${args}\n}` : ''})`;

return `${comment}\n\n${vars}\n\n${cmd}`;
}
Expand Down
23 changes: 21 additions & 2 deletions packages/bson-transpilers/symbols/rust/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ Templates:

const options = spec.options;
const filter = spec.filter || 'None';
const exportMode = spec.exportMode;
delete spec.options;
delete spec.filter;
delete spec.exportMode;

const connect = `let client = Client::with_uri_str("${options.uri}").await?;`
const coll = `client.database("${options.database}").collection::<Document>("${options.collection}")`;
Expand All @@ -64,6 +66,23 @@ Templates:
return `${comment}\n\n${connect}\nlet result = ${coll}.aggregate(${agg}, None).await?;`;
}

let driverMethod;
let optionsName;
switch (exportMode) {
case 'Delete Query':
driverMethod = 'delete_many';
optionsName = 'DeleteOptions';
break;
case 'Update Query':
driverMethod = 'update_many';
optionsName = 'UpdateOptions';
break;
default:
driverMethod = 'find';
optionsName = 'FindOptions';
break;
}

const findOpts = [];
for (const k in spec) {
let optName = k;
Expand All @@ -81,10 +100,10 @@ Templates:
}
let optStr = '';
if (findOpts.length > 0) {
optStr = `let options = mongodb::options::FindOptions::builder()\n${findOpts.join('\n')}\n .build();\n`;
optStr = `let options = mongodb::options::${optionsName}::builder()\n${findOpts.join('\n')}\n .build();\n`;
}
let optRef = optStr ? 'options' : 'None';
const cmd = `let result = ${coll}.find(${filter}, ${optRef}).await?;`;
const cmd = `let result = ${coll}.${driverMethod}(${filter}, ${optRef}).await?;`;

return `${comment}\n\n${connect}\n${optStr}${cmd}`;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/bson-transpilers/symbols/shell/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ Templates:
DriverTemplate: &DriverTemplate !!js/function >
(spec) => {
let cmd;
if (spec.exportMode === 'Delete Query') {
cmd = `deleteMany(${spec.filter})`
}
if ('aggregation' in spec) {
cmd = `aggregate(${spec.aggregation})`;
} else {
} else if (!cmd) {
const project = 'project' in spec ? `, ${spec.project}` : '';
cmd = Object.keys(spec).reduce((s, k) => {
if (k !== 'options' && k !== 'project' && k !== 'filter') {
if (k !== 'options' && k !== 'project' && k !== 'filter' && k != 'exportMode') {
s = `${s}.${k}(${spec[k]})`;
}
return s;
Expand Down
34 changes: 34 additions & 0 deletions packages/bson-transpilers/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const assert = require('assert');
const transpilers = require('../index');

const SAMPLE = transpilers.javascript.java;

const VALID_OPTIONS = {
uri: 'mongodb://localhost',
database: 'test',
collection: 'webscale'
};

const INVALID_JS = '{ ... }';
const VALID_JS = '({ a : 1 })';

describe('bson transpiler', function() {
describe('#compileWithDriver', function() {
it('does not compile internal options like "options"', function() {
const result = SAMPLE.compileWithDriver({
options: VALID_OPTIONS,
filter: VALID_JS
});
assert.ok(result.includes('webscale'));
});

it('does not compile internal options like "exportMode"', function() {
const result = SAMPLE.compileWithDriver({
options: VALID_OPTIONS,
exportMode: INVALID_JS,
filter: VALID_JS
});
assert.ok(result.includes('webscale'));
});
});
});
Loading

0 comments on commit dfcc0fa

Please sign in to comment.