fix(mongodb): support legacy insert syntax
This commit is contained in:
4 files changed
+67
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
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) {
|
||||
const target = parseCollectionMethodTarget(source, method);
|
||||
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", () => {
|
||||
assert.equal(parseMongoWriteCommand("db.projects.dropIndex()"), null);
|
||||
assert.equal(parseMongoWriteCommand('db.projects.dropIndex("*")'), null);
|
||||
|
||||
Reference in new issue
Block a user