Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check update document to only contains atomic operators #267

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/collection/collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bson } from "../../deps.ts";
import { MongoDriverError } from "../error.ts";
import { MongoDriverError, MongoInvalidArgumentError } from "../error.ts";
import { WireProtocol } from "../protocol/mod.ts";
import {
AggregateOptions,
Expand Down Expand Up @@ -220,6 +220,12 @@ export class Collection<T> {
doc: UpdateFilter<T>,
options?: UpdateOptions,
) {
if (!hasAtomicOperators(doc)) {
throw new MongoInvalidArgumentError(
"Update document requires atomic operators",
);
}

return update(this.#protocol, this.#dbName, this.name, filter, doc, {
...options,
multi: options?.multi ?? true,
Expand Down Expand Up @@ -324,3 +330,16 @@ export class Collection<T> {
});
}
}

export function hasAtomicOperators(doc: Bson.Document | Bson.Document[]) {
if (Array.isArray(doc)) {
for (const document of doc) {
if (hasAtomicOperators(document)) {
return true;
}
}
return false;
}
const keys = Object.keys(doc);
return keys.length > 0 && keys[0][0] === "$";
}
13 changes: 13 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ export class MongoServerError extends MongoError implements MongoErrorInfo {
this.codeName = info.codeName;
}
}

/**
* A class representation of a command with invalid arguments
* @public
*/
export class MongoInvalidArgumentError extends MongoError {
/**
* @param info A string containing the error's message.
*/
constructor(info: string) {
super(info);
}
}
24 changes: 19 additions & 5 deletions tests/cases/03_curd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MongoInvalidArgumentError } from "../../src/error.ts";
import { testWithClient } from "../common.ts";
import { assert, assertEquals, assertThrowsAsync } from "../test.deps.ts";

Expand Down Expand Up @@ -47,9 +48,11 @@ export default function curdTests() {
const { upsertedId } = await users.updateOne(
{ _id: "aaaaaaaaaaaaaaaaaaaaaaaa" },
{
username: "user1",
password: "pass1",
date: new Date(dateNow),
$set: {
username: "user1",
password: "pass1",
date: new Date(dateNow),
},
},
{ upsert: true },
);
Expand Down Expand Up @@ -170,7 +173,7 @@ export default function curdTests() {
testWithClient("testUpdateOne", async (client) => {
const db = client.database("test");
const users = db.collection<IUser>("mongo_test_users");
const result = await users.updateOne({}, { username: "USER1" });
const result = await users.updateOne({}, { $set: { username: "USER1" } });
assertEquals(result, {
matchedCount: 1,
modifiedCount: 1,
Expand All @@ -179,12 +182,23 @@ export default function curdTests() {
});
});

testWithClient("testUpdateOne Error", async (client) => { // TODO: move tesr errors to a new file
const db = client.database("test");
const users = db.collection<IUser>("mongo_test_users");
try {
await users.updateOne({}, { username: "USER1" });
assert(false);
} catch (e) {
assert(e instanceof MongoInvalidArgumentError);
}
});

testWithClient("testUpdateOneWithUpsert", async (client) => {
const db = client.database("test");
const users = db.collection<IUser>("mongo_test_users");
const result = await users.updateOne(
{ username: "user2" },
{ username: "USER2" },
{ $set: { username: "USER2" } },
{ upsert: true },
);
assertEquals(result.matchedCount, 1);
Expand Down