fix(mongodb): support legacy insert syntax
This commit is contained in:
4 files changed
+67
No files matched your search
@@ -421,6 +421,16 @@ export function parseMongoWriteCommand(input: string): MongoWriteCommand | null
|
|||||||
return Array.isArray(JSON.parse(docs)) ? { kind: "insert", collection: insertMany.collection, docsJson: docs } : null;
|
return Array.isArray(JSON.parse(docs)) ? { kind: "insert", collection: insertMany.collection, docsJson: docs } : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const insert = parseCollectionMethodTarget(source, "insert");
|
||||||
|
if (insert) {
|
||||||
|
const args = parseMethodArgs(source, insert.methodCallIndex);
|
||||||
|
if (!args || args.length !== 1 || !args[0]?.trim()) return null;
|
||||||
|
const docs = normalizeJsonArgument(args[0]);
|
||||||
|
if (!docs) return null;
|
||||||
|
const value = JSON.parse(docs);
|
||||||
|
return value !== null && typeof value === "object" ? { kind: "insert", collection: insert.collection, docsJson: docs } : null;
|
||||||
|
}
|
||||||
|
|
||||||
for (const method of ["updateOne", "updateMany"] as const) {
|
for (const method of ["updateOne", "updateMany"] as const) {
|
||||||
const target = parseCollectionMethodTarget(source, method);
|
const target = parseCollectionMethodTarget(source, method);
|
||||||
if (!target) continue;
|
if (!target) continue;
|
||||||
|
|||||||
+27
@@ -288,6 +288,33 @@ test("parseMongoWriteCommand accepts unquoted insert and update commands", () =>
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("parseMongoWriteCommand accepts legacy insert commands", () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
parseMongoWriteCommand(`db.getCollection("accounting_reconciliations").insert({
|
||||||
|
"accountId": 999,
|
||||||
|
"status": "done"
|
||||||
|
});`),
|
||||||
|
{
|
||||||
|
kind: "insert",
|
||||||
|
collection: "accounting_reconciliations",
|
||||||
|
docsJson: '{\n "accountId": 999,\n "status": "done"\n }',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
assert.deepEqual(parseMongoWriteCommand("db.products.insert([{name: 'first'}, {name: 'second'}])"), {
|
||||||
|
kind: "insert",
|
||||||
|
collection: "products",
|
||||||
|
docsJson: '[{"name": "first"}, {"name": "second"}]',
|
||||||
|
});
|
||||||
|
assert.deepEqual(parseMongoWriteCommand("db.products.insertMany([{name: 'first'}, {name: 'second'}])"), {
|
||||||
|
kind: "insert",
|
||||||
|
collection: "products",
|
||||||
|
docsJson: '[{"name": "first"}, {"name": "second"}]',
|
||||||
|
});
|
||||||
|
assert.equal(parseMongoWriteCommand("db.products.insert({name: 'demo'}, {writeConcern: {w: 1}})"), null);
|
||||||
|
assert.equal(parseMongoWriteCommand("db.products.insert()"), null);
|
||||||
|
assert.equal(parseMongoWriteCommand("db.products.insert('demo')"), null);
|
||||||
|
});
|
||||||
|
|
||||||
test("parseMongoWriteCommand accepts updateMany arrayFilters options", () => {
|
test("parseMongoWriteCommand accepts updateMany arrayFilters options", () => {
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
parseMongoWriteCommand(`db.issue_3231.updateMany(
|
parseMongoWriteCommand(`db.issue_3231.updateMany(
|
||||||
|
|||||||
@@ -1613,6 +1613,16 @@ export function parseMongoWriteCommand(input: string): MongoWriteCommand | null
|
|||||||
return Array.isArray(JSON.parse(docs)) ? { kind: "insert", collection: insertMany.collection, docsJson: docs } : null;
|
return Array.isArray(JSON.parse(docs)) ? { kind: "insert", collection: insertMany.collection, docsJson: docs } : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const insert = parseCollectionMethodTarget(source, "insert");
|
||||||
|
if (insert) {
|
||||||
|
const args = parseMethodArgs(source, insert.methodCallIndex);
|
||||||
|
if (!args || args.length !== 1 || !args[0]?.trim()) return null;
|
||||||
|
const docs = normalizeJsonArgument(args[0]);
|
||||||
|
if (!docs) return null;
|
||||||
|
const value = JSON.parse(docs);
|
||||||
|
return value !== null && typeof value === "object" ? { kind: "insert", collection: insert.collection, docsJson: docs } : null;
|
||||||
|
}
|
||||||
|
|
||||||
for (const method of ["updateOne", "updateMany"] as const) {
|
for (const method of ["updateOne", "updateMany"] as const) {
|
||||||
const target = parseCollectionMethodTarget(source, method);
|
const target = parseCollectionMethodTarget(source, method);
|
||||||
if (!target) continue;
|
if (!target) continue;
|
||||||
|
|||||||
+20
@@ -295,6 +295,26 @@ test("parseMongoWriteCommand accepts supported write commands", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("parseMongoWriteCommand accepts legacy insert commands", () => {
|
||||||
|
assert.deepEqual(parseMongoWriteCommand('db.getCollection("accounting_reconciliations").insert({"accountId":999,"status":"done"});'), {
|
||||||
|
kind: "insert",
|
||||||
|
collection: "accounting_reconciliations",
|
||||||
|
docsJson: '{"accountId":999,"status":"done"}',
|
||||||
|
});
|
||||||
|
assert.deepEqual(parseMongoWriteCommand('db.projects.insert([{"name":"first"},{"name":"second"}])'), {
|
||||||
|
kind: "insert",
|
||||||
|
collection: "projects",
|
||||||
|
docsJson: '[{"name":"first"},{"name":"second"}]',
|
||||||
|
});
|
||||||
|
assert.deepEqual(parseMongoWriteCommand('db.projects.insertMany([{"name":"first"},{"name":"second"}])'), {
|
||||||
|
kind: "insert",
|
||||||
|
collection: "projects",
|
||||||
|
docsJson: '[{"name":"first"},{"name":"second"}]',
|
||||||
|
});
|
||||||
|
assert.equal(parseMongoWriteCommand('db.projects.insert({"name":"demo"},{"writeConcern":{"w":1}})'), null);
|
||||||
|
assert.equal(parseMongoWriteCommand("db.projects.insert()"), null);
|
||||||
|
});
|
||||||
|
|
||||||
test("parseMongoWriteCommand rejects invalid dropIndex and dropIndexes commands", () => {
|
test("parseMongoWriteCommand rejects invalid dropIndex and dropIndexes commands", () => {
|
||||||
assert.equal(parseMongoWriteCommand("db.projects.dropIndex()"), null);
|
assert.equal(parseMongoWriteCommand("db.projects.dropIndex()"), null);
|
||||||
assert.equal(parseMongoWriteCommand('db.projects.dropIndex("*")'), null);
|
assert.equal(parseMongoWriteCommand('db.projects.dropIndex("*")'), null);
|
||||||
|
|||||||
Reference in new issue
Block a user