Skip to content

Commit

Permalink
Fix Date and ObjectId arguments in MDB client
Browse files Browse the repository at this point in the history
* Date and ObjectId were being transformed into an empty object on the way to the MDB client
* Modified the cleanArguments utility to not overwite these objects with an empty one
  • Loading branch information
takameyer committed Aug 2, 2023
1 parent e387985 commit bbccd66
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* None

### Fixed
* Fix Jest issues when testing against Realm ([#6003](https://github.com/realm/realm-js/issues/6003))
* None
* Fix Jest issues when testing against Realm. ([#6003](https://github.com/realm/realm-js/issues/6003))
* Fix Date and ObjectId arguments being empty objects in MongoDB client. ([#6030](https://github.com/realm/realm-js/issues/6030))

### Compatibility
* React Native >= v0.71.4
Expand Down
55 changes: 35 additions & 20 deletions integration-tests/tests/src/tests/sync/mongo-db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ type ChangeEvent<T extends Document> = Realm.Services.MongoDB.ChangeEvent<T>;
type InsertEvent<T extends Document> = Realm.Services.MongoDB.InsertEvent<T>;

type TestDocument = {
_id: number;
_id: BSON.ObjectId;
text?: string;
isLast?: boolean;
date?: Date;
};

type CollectionContext = { collection: MongoDBCollection<TestDocument> };
Expand Down Expand Up @@ -85,18 +86,20 @@ describe.skipIf(environment.missingServer, "MongoDB Client", function () {
});

describe("MongoDBCollection", function () {
const insertedId1 = 1;
const insertedId2 = 2;
const insertedId3 = 3;
const insertedId1 = new BSON.ObjectId();
const insertedId2 = new BSON.ObjectId();
const insertedId3 = new BSON.ObjectId();
const insertedText = "Test document";
const nonExistentId = 100;
const insertedDate = new Date("2020-01-01");
const nonExistentId = new BSON.ObjectId();

async function insertThreeDocuments(collection: MongoDBCollection<TestDocument>): Promise<void> {
const { insertedIds } = await collection.insertMany([
{ _id: insertedId1, text: insertedText },
{ _id: insertedId2, text: insertedText },
{ _id: insertedId3, text: insertedText },
]);
const insertionDocuments = [
{ _id: insertedId1, text: insertedText, date: insertedDate },
{ _id: insertedId2, text: insertedText, date: insertedDate },
{ _id: insertedId3, text: insertedText, date: insertedDate },
];
const { insertedIds } = await collection.insertMany(insertionDocuments);
expect(insertedIds).to.have.length(3);
}

Expand Down Expand Up @@ -148,7 +151,7 @@ describe.skipIf(environment.missingServer, "MongoDB Client", function () {
const docs = await this.collection.find();
expect(docs).to.have.length(3);
for (const doc of docs) {
expect(doc).to.have.all.keys("_id", "text");
expect(doc).to.have.all.keys("_id", "text", "date");
}
});

Expand Down Expand Up @@ -184,14 +187,14 @@ describe.skipIf(environment.missingServer, "MongoDB Client", function () {
await insertThreeDocuments(this.collection);

const doc = await this.collection.findOne({ _id: insertedId3 });
expect(doc).to.deep.equal({ _id: insertedId3, text: insertedText });
expect(doc).to.deep.equal({ _id: insertedId3, text: insertedText, date: insertedDate });
});

it("returns first document using empty filter", async function (this: TestContext) {
await insertThreeDocuments(this.collection);

const doc = await this.collection.findOne();
expect(doc).to.deep.equal({ _id: insertedId1, text: insertedText });
expect(doc).to.deep.equal({ _id: insertedId1, text: insertedText, date: insertedDate });
});

it("returns null when there are no matches", async function (this: TestContext) {
Expand All @@ -213,7 +216,7 @@ describe.skipIf(environment.missingServer, "MongoDB Client", function () {
{ $set: { text: updatedText } },
{ returnNewDocument: true },
);
expect(newDoc).to.deep.equal({ _id: insertedId3, text: updatedText });
expect(newDoc).to.deep.equal({ _id: insertedId3, text: updatedText, date: insertedDate });
});

it("returns null when there are no matches", async function (this: TestContext) {
Expand Down Expand Up @@ -290,7 +293,7 @@ describe.skipIf(environment.missingServer, "MongoDB Client", function () {
await insertThreeDocuments(this.collection);

const oldDoc = await this.collection.findOneAndDelete({ _id: insertedId3 });
expect(oldDoc).to.deep.equal({ _id: insertedId3, text: insertedText });
expect(oldDoc).to.deep.equal({ _id: insertedId3, text: insertedText, date: insertedDate });

await expectToNotFindDoc(this.collection, { _id: insertedId3 }, { expectedCount: 2 });
});
Expand All @@ -299,7 +302,7 @@ describe.skipIf(environment.missingServer, "MongoDB Client", function () {
await insertThreeDocuments(this.collection);

const oldDoc = await this.collection.findOneAndDelete();
expect(oldDoc).to.deep.equal({ _id: insertedId1, text: insertedText });
expect(oldDoc).to.deep.equal({ _id: insertedId3, text: insertedText, date: insertedDate });

await expectToNotFindDoc(this.collection, { _id: insertedId1 }, { expectedCount: 2 });
});
Expand All @@ -325,7 +328,7 @@ describe.skipIf(environment.missingServer, "MongoDB Client", function () {
describe("#insertOne", function () {
it("inserts document with id", async function (this: TestContext) {
const result = await this.collection.insertOne({ _id: insertedId1 });
expect(result.insertedId).to.equal(insertedId1);
expect(result.insertedId).to.deep.equal(insertedId1);

await expectToFindDoc(this.collection, { _id: insertedId1 }, { expectedCount: 1 });
});
Expand Down Expand Up @@ -389,7 +392,11 @@ describe.skipIf(environment.missingServer, "MongoDB Client", function () {
const result = await this.collection.updateOne({ _id: insertedId3 }, { $set: { text: updatedText } });
expect(result).to.deep.equal({ matchedCount: 1, modifiedCount: 1 });

await expectToFindDoc(this.collection, { _id: insertedId3, text: updatedText }, { expectedCount: 3 });
await expectToFindDoc(
this.collection,
{ _id: insertedId3, text: updatedText, date: insertedDate },
{ expectedCount: 3 },
);
});

it("does not update any document when there are no matches", async function (this: TestContext) {
Expand Down Expand Up @@ -428,8 +435,16 @@ describe.skipIf(environment.missingServer, "MongoDB Client", function () {
const result = await this.collection.updateMany({ _id: { $gt: insertedId1 } }, { $set: { text: updatedText } });
expect(result).to.deep.equal({ matchedCount: 2, modifiedCount: 2 });

await expectToFindDoc(this.collection, { _id: insertedId2, text: updatedText }, { expectedCount: 3 });
await expectToFindDoc(this.collection, { _id: insertedId3, text: updatedText }, { expectedCount: 3 });
await expectToFindDoc(
this.collection,
{ _id: insertedId2, text: updatedText, date: insertedDate },
{ expectedCount: 3 },
);
await expectToFindDoc(
this.collection,
{ _id: insertedId3, text: updatedText, date: insertedDate },
{ expectedCount: 3 },
);
});

it("does not update any document when there are no matches", async function (this: TestContext) {
Expand Down
3 changes: 2 additions & 1 deletion packages/realm/src/app-services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export function cleanArguments(args: unknown[] | unknown): unknown[] | unknown {
// Note: `undefined` elements in the array is not removed.
return args.map(cleanArguments);
}
if (args === null || typeof args !== "object") {
// Checking for constructor to allow for `new Date()` and `new ObjectId()` and similar.
if (args === null || typeof args !== "object" || args?.constructor !== Object) {
return args;
}
const result: { [key: string]: unknown } = {};
Expand Down

0 comments on commit bbccd66

Please sign in to comment.