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 Date and ObjectId arguments in MDB client #6029

Merged
merged 2 commits into from
Aug 3, 2023
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* None

### Fixed
* Fix Jest issues when testing against Realm ([#6003](https://github.com/realm/realm-js/issues/6003))
* 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))
* Rare corruption of files on streaming format (often following compact, convert or copying to a new file). ([#6807](https://github.com/realm/realm-core/issues/6807), since realm-core v12.12.0)
* Trying to search a full-text indexes created as a result of an additive schema change (i.e. applying the differences between the local schema and a synchronized realm's schema) could have resulted in an IllegalOperation error with the error code Column has no fulltext index. (PR [#6823](https://github.com/realm/realm-core/issues/6823), since realm-core v13.2.0).
* Sync progress for DOWNLOAD messages from server state was updated wrongly. This may have resulted in an extra round-trip to the server. ([#6827](https://github.com/realm/realm-core/issues/6827), since realm-core v12.9.0)
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
Loading