feat: use getColleciton syntax for mongo

This commit is contained in:
Pavel
2025-07-29 16:33:39 +02:00
parent 461f1e39fa
commit 6e8cdc24a3
2 changed files with 13 additions and 10 deletions

View File

@@ -138,15 +138,18 @@ const driver = {
await this.script(dbhan, `db.createCollection('${operation.collection.name}')`); await this.script(dbhan, `db.createCollection('${operation.collection.name}')`);
break; break;
case 'dropCollection': case 'dropCollection':
await this.script(dbhan, `db.dropCollection('${operation.collection}')`); await this.script(dbhan, `db.getCollection('${operation.collection}').drop()`);
break; break;
case 'renameCollection': case 'renameCollection':
await this.script(dbhan, `db.renameCollection('${operation.collection}', '${operation.newName}')`); await this.script(
dbhan,
`db.getCollection('${operation.collection}').renameCollection('${operation.newName}')`
);
break; break;
case 'cloneCollection': case 'cloneCollection':
await this.script( await this.script(
dbhan, dbhan,
`db.collection('${operation.collection}').aggregate([{$out: '${operation.newName}'}]).toArray()` `db.getCollection('${operation.collection}').aggregate([{$out: '${operation.newName}'}]).toArray()`
); );
break; break;
default: default:

View File

@@ -88,14 +88,14 @@ const driver = {
getCollectionUpdateScript(changeSet, collectionInfo) { getCollectionUpdateScript(changeSet, collectionInfo) {
let res = ''; let res = '';
for (const insert of changeSet.inserts) { for (const insert of changeSet.inserts) {
res += `db['${insert.pureName}'].insertOne(${jsonStringifyWithObjectId({ res += `db.getCollection('${insert.pureName}').insertOne(${jsonStringifyWithObjectId({
...insert.document, ...insert.document,
...insert.fields, ...insert.fields,
})});\n`; })});\n`;
} }
for (const update of changeSet.updates) { for (const update of changeSet.updates) {
if (update.document) { if (update.document) {
res += `db['${update.pureName}'].replaceOne(${jsonStringifyWithObjectId( res += `db.getCollection('${update.pureName}').replaceOne(${jsonStringifyWithObjectId(
update.condition update.condition
)}, ${jsonStringifyWithObjectId({ )}, ${jsonStringifyWithObjectId({
...update.document, ...update.document,
@@ -112,13 +112,13 @@ const driver = {
if (!_.isEmpty(set)) updates.$set = set; if (!_.isEmpty(set)) updates.$set = set;
if (!_.isEmpty(unset)) updates.$unset = unset; if (!_.isEmpty(unset)) updates.$unset = unset;
res += `db['${update.pureName}'].updateOne(${jsonStringifyWithObjectId( res += `db.getCollection('${update.pureName}').updateOne(${jsonStringifyWithObjectId(
update.condition update.condition
)}, ${jsonStringifyWithObjectId(updates)});\n`; )}, ${jsonStringifyWithObjectId(updates)});\n`;
} }
} }
for (const del of changeSet.deletes) { for (const del of changeSet.deletes) {
res += `db['${del.pureName}'].deleteOne(${jsonStringifyWithObjectId(del.condition)});\n`; res += `db.getCollection('${del.pureName}').deleteOne(${jsonStringifyWithObjectId(del.condition)});\n`;
} }
return res; return res;
}, },
@@ -128,7 +128,7 @@ const driver = {
}, },
getCollectionExportQueryScript(collection, condition, sort) { getCollectionExportQueryScript(collection, condition, sort) {
return `db['${collection}'] return `db.getCollection('${collection}')
.find(${JSON.stringify(convertToMongoCondition(condition) || {})}) .find(${JSON.stringify(convertToMongoCondition(condition) || {})})
.sort(${JSON.stringify(convertToMongoSort(sort) || {})})`; .sort(${JSON.stringify(convertToMongoSort(sort) || {})})`;
}, },
@@ -182,9 +182,9 @@ const driver = {
async getScriptTemplateContent(scriptTemplate, props) { async getScriptTemplateContent(scriptTemplate, props) {
switch (scriptTemplate) { switch (scriptTemplate) {
case 'dropCollection': case 'dropCollection':
return `db['${props.pureName}'].drop();`; return `db.getCollection('${props.pureName}').drop();`;
case 'findCollection': case 'findCollection':
return `db['${props.pureName}'].find();`; return `db.getCollection('${props.pureName}').find();`;
} }
}, },
}; };