feat: mongosh for script and stream methods

This commit is contained in:
Pavel
2025-07-24 15:52:59 +02:00
parent b8e50737d2
commit 06a845697a

View File

@@ -1,9 +1,8 @@
const _ = require('lodash'); const _ = require('lodash');
const stream = require('stream'); const stream = require('stream');
const isPromise = require('is-promise');
const driverBase = require('../frontend/driver'); const driverBase = require('../frontend/driver');
const Analyser = require('./Analyser'); const Analyser = require('./Analyser');
const { MongoClient, ObjectId, AbstractCursor, Long } = require('mongodb'); const { MongoClient, ObjectId, Long } = require('mongodb');
const { EJSON } = require('bson'); const { EJSON } = require('bson');
const { NodeDriverServiceProvider } = require('@mongosh/service-provider-node-driver'); const { NodeDriverServiceProvider } = require('@mongosh/service-provider-node-driver');
const { ElectronRuntime } = require('@mongosh/browser-runtime-electron'); const { ElectronRuntime } = require('@mongosh/browser-runtime-electron');
@@ -30,24 +29,10 @@ function serializeMongoData(row) {
); );
} }
async function readCursor(cursor, options) {
options.recordset({ __isDynamicStructure: true });
await cursor.forEach((row) => {
options.row(serializeMongoData(row));
});
}
function deserializeMongoData(value) { function deserializeMongoData(value) {
return deserializeJsTypesFromJsonParse(EJSON.deserialize(value)); return deserializeJsTypesFromJsonParse(EJSON.deserialize(value));
} }
function findArrayResult(resValue) {
if (!_.isPlainObject(resValue)) return null;
const arrays = _.values(resValue).filter((x) => _.isArray(x));
if (arrays.length == 1) return arrays[0];
return null;
}
async function getScriptableDb(dbhan) { async function getScriptableDb(dbhan) {
const db = dbhan.getDatabase(); const db = dbhan.getDatabase();
const collections = await db.listCollections().toArray(); const collections = await db.listCollections().toArray();
@@ -107,11 +92,20 @@ const driver = {
}; };
}, },
async script(dbhan, sql) { async script(dbhan, sql) {
let func; const connectionString = dbhan.client.s.url;
func = eval(`(db,ObjectId) => ${sql}`); const serviceProvider = await NodeDriverServiceProvider.connect(connectionString);
const db = await getScriptableDb(dbhan); const runtime = new ElectronRuntime(serviceProvider);
const res = func(db, ObjectId.createFromHexString); const exprValue = await runtime.evaluate(sql);
if (isPromise(res)) await res;
const { printable } = exprValue;
if (Array.isArray(printable)) {
return printable;
} else if ('documents' in printable) {
return printable.documents;
}
return printable;
}, },
async operation(dbhan, operation, options) { async operation(dbhan, operation, options) {
const { type } = operation; const { type } = operation;
@@ -154,19 +148,35 @@ const driver = {
return; return;
} }
console.log(exprValue); const { printable, type } = exprValue;
if (exprValue.type === 'Document') { if (type === 'Document') {
options.recordset({ __isDynamicStructure: true }); options.recordset({ __isDynamicStructure: true });
options.row(exprValue.printable); options.row(printable);
} else if (exprValue.type === 'Cursor' || exprValue.type === 'AggregationCursor') { } else if (type === 'Cursor' || exprValue.type === 'AggregationCursor') {
options.recordset({ __isDynamicStructure: true }); options.recordset({ __isDynamicStructure: true });
for (const doc of exprValue.printable.documents) { for (const doc of printable.documents) {
options.row(doc); options.row(doc);
} }
} else { } else {
options.recordset({ __isDynamicStructure: true }); if (Array.isArray(printable)) {
options.row(exprValue.printable); options.recordset({ __isDynamicStructure: true });
for (const row of printable) {
options.row(row);
}
} else if ('documents' in printable) {
options.recordset({ __isDynamicStructure: true });
for (const row of printable.documents) {
options.row(row);
}
} else {
options.info({
printable: printable,
time: new Date(),
severity: 'info',
message: 'Query returned not supported value.',
});
}
} }
options.done(); options.done();