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

WIP: Potential embedded object constructor implementation #6830

Closed
wants to merge 14 commits into from
Closed
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
278 changes: 155 additions & 123 deletions integration-tests/tests/src/tests/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////
import { expect } from "chai";
import Realm, { BSON, UpdateMode } from "realm";
import Realm, { BSON, EmbeddedObject, ObjectSchema, UpdateMode } from "realm";

import { IPerson, Person, PersonSchema } from "../schemas/person-and-dogs";
import {
Expand Down Expand Up @@ -134,33 +134,6 @@ const LinkSchemas = {
},
};

interface IEmbeddedObject {
intValue: number;
}

const EmbeddedObjectSchema: Realm.ObjectSchema = {
name: "EmbeddedObject",
embedded: true,
properties: {
intValue: "int",
},
};

interface IObjectWithEmbeddedObject {
_id: BSON.ObjectId;
// The type needs to allow for the implicit nullability.
embeddedValue: IEmbeddedObject | undefined | null;
}

const ObjectWithEmbeddedObjectSchema: Realm.ObjectSchema = {
name: "ObjectWithEmbeddedObject",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
embeddedValue: EmbeddedObjectSchema.name,
},
};

const DateObjectSchema = {
name: "Date",
properties: {
Expand Down Expand Up @@ -1282,125 +1255,184 @@ describe("Realm.Object", () => {
});

describe("embedded properties", () => {
openRealmBeforeEach({ schema: [EmbeddedObjectSchema, ObjectWithEmbeddedObjectSchema] });

/**
* Creates the Realm object and asserts (using `expect()`) that its embedded value
* is equal to the one provided.
*
* @param realm The realm to use.
* @param objectToCreate The object to create without the primary key (`_id`, will be generated).
* @returns The created Realm object.
*/
function createObjectWithEmbeddedObject(realm: Realm, objectToCreate: Omit<IObjectWithEmbeddedObject, "_id">) {
const objectCreated = realm.write(() => {
return realm.create<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name, objectToCreate);
});
interface IEmbeddedObject {
intValue: number;
}

const objects = realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;
class EmbeddedObject extends Realm.EmbeddedObject {
intValue!: number;

if (objectToCreate.embeddedValue) {
expect(objectCreated.embeddedValue?.intValue).to.equal(objectToCreate.embeddedValue.intValue);
} else {
expect(objectCreated.embeddedValue).to.equal(objectToCreate.embeddedValue);
}
static schema: Realm.ObjectSchema = {
name: "EmbeddedObject",
embedded: true,
properties: {
intValue: "int",
},
};
}

return objectCreated;
interface IObjectWithEmbeddedObject {
_id: BSON.ObjectId;
// The type needs to allow for the implicit nullability.
embeddedValue: IEmbeddedObject | undefined | null;
}

it("sets an embedded property to null on creation", function (this: Mocha.Context & RealmContext) {
this.realm.write(() => {
this.realm.create(ObjectWithEmbeddedObjectSchema.name, { embeddedValue: null });
class ObjectWithEmbeddedObject extends Realm.Object {
static schema: ObjectSchema = {
name: "ObjectWithEmbeddedObject",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
embeddedValue: EmbeddedObject.schema.name,
},
};
}
describe("with object schema", () => {
openRealmBeforeEach({ schema: [EmbeddedObject.schema, ObjectWithEmbeddedObject.schema] });

/**
* Creates the Realm object and asserts (using `expect()`) that its embedded value
* is equal to the one provided.
*
* @param realm The realm to use.
* @param objectToCreate The object to create without the primary key (`_id`, will be generated).
* @returns The created Realm object.
*/
function createObjectWithEmbeddedObject(realm: Realm, objectToCreate: Omit<IObjectWithEmbeddedObject, "_id">) {
const objectCreated = realm.write(() => {
return realm.create<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObject.schema.name, objectToCreate);
});

const objects = realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObject.schema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;

if (objectToCreate.embeddedValue) {
expect(objectCreated.embeddedValue?.intValue).to.equal(objectToCreate.embeddedValue.intValue);
} else {
expect(objectCreated.embeddedValue).to.equal(objectToCreate.embeddedValue);
}

return objectCreated;
}

it("sets an embedded property to null on creation", function (this: Mocha.Context & RealmContext) {
this.realm.write(() => {
this.realm.create(ObjectWithEmbeddedObject.schema.name, { embeddedValue: null });
});

const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObject.schema.name);
expect(objects.length).to.equal(1);
expect(objects[0].embeddedValue).to.be.null;
});

const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0].embeddedValue).to.be.null;
});
it("updates an embedded property to null", function (this: Mocha.Context & RealmContext) {
createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

it("updates an embedded property to null", function (this: Mocha.Context & RealmContext) {
createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObject.schema.name);
const firstObject = objects[0];
this.realm.write(() => {
firstObject.embeddedValue = null;
});
expect(firstObject.embeddedValue).to.be.null;
});

const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
const firstObject = objects[0];
this.realm.write(() => {
firstObject.embeddedValue = null;
it("updates an embedded property to null when set to undefined", function (this: Mocha.Context & RealmContext) {
createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObject.schema.name);
const firstObject = objects[0];
this.realm.write(() => {
firstObject.embeddedValue = undefined;
});
// `undefined` and `null` JS values are always sent as `null` through
// the binding layer. Thus, the value is always retrieved as `null`.
expect(firstObject.embeddedValue).to.be.null;
});
expect(firstObject.embeddedValue).to.be.null;
});

it("updates an embedded property to null when set to undefined", function (this: Mocha.Context & RealmContext) {
createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });
it("updates an embedded property to null with `UpdateMode.Modified`", function (this: Mocha.Context &
RealmContext) {
const objectCreated = createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
const firstObject = objects[0];
this.realm.write(() => {
firstObject.embeddedValue = undefined;
this.realm.write(() => {
this.realm.create(
ObjectWithEmbeddedObject.schema.name,
{
_id: objectCreated._id,
embeddedValue: null,
},
UpdateMode.Modified,
);
});
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObject.schema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;
expect(objects[0].embeddedValue).to.be.null;
});
// `undefined` and `null` JS values are always sent as `null` through
// the binding layer. Thus, the value is always retrieved as `null`.
expect(firstObject.embeddedValue).to.be.null;
});

it("updates an embedded property to null with `UpdateMode.Modified`", function (this: Mocha.Context &
RealmContext) {
const objectCreated = createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });
it("updates an embedded property to null with `UpdateMode.All`", function (this: Mocha.Context & RealmContext) {
const objectCreated = createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

this.realm.write(() => {
this.realm.create(
ObjectWithEmbeddedObjectSchema.name,
{
_id: objectCreated._id,
embeddedValue: null,
},
UpdateMode.Modified,
);
this.realm.write(() => {
this.realm.create(
ObjectWithEmbeddedObject.schema.name,
{
_id: objectCreated._id,
embeddedValue: null,
},
UpdateMode.All,
);
});
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObject.schema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;
expect(objects[0].embeddedValue).to.be.null;
});
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;
expect(objects[0].embeddedValue).to.be.null;
});

it("updates an embedded property to null with `UpdateMode.All`", function (this: Mocha.Context & RealmContext) {
const objectCreated = createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });
it("does not update an embedded property to null when omitted with `UpdateMode.Modified`", function (this: Mocha.Context &
RealmContext) {
const objectCreated = createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });

this.realm.write(() => {
this.realm.create(
ObjectWithEmbeddedObjectSchema.name,
{
_id: objectCreated._id,
embeddedValue: null,
},
UpdateMode.All,
);
// Create an object without the `embeddedValue` field.
this.realm.write(() => {
this.realm.create(
ObjectWithEmbeddedObject.schema.name,
{
_id: objectCreated._id,
},
UpdateMode.Modified,
);
});
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObject.schema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id));
expect(objects[0].embeddedValue?.intValue).to.equal(1);
});
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id)).to.be.true;
expect(objects[0].embeddedValue).to.be.null;
});

it("does not update an embedded property to null when omitted with `UpdateMode.Modified`", function (this: Mocha.Context &
RealmContext) {
const objectCreated = createObjectWithEmbeddedObject(this.realm, { embeddedValue: { intValue: 1 } });
describe("with classes", () => {
it("can open Realm with class extending Realm.EmbeddedObject or Realm.Object", () => {
expect(() => new Realm({ schema: [EmbeddedObject, ObjectWithEmbeddedObject] })).not.to.throw();

// Create an object without the `embeddedValue` field.
this.realm.write(() => {
this.realm.create(
ObjectWithEmbeddedObjectSchema.name,
{
_id: objectCreated._id,
},
UpdateMode.Modified,
class EmbeddedObjectExtendingRealm extends Realm.Object {
intValue!: number;

static schema = EmbeddedObject.schema;
}

expect(() => new Realm({ schema: [EmbeddedObjectExtendingRealm, ObjectWithEmbeddedObject] })).not.to.throw();
});

it("throws if embedded object does not extend Realm.", () => {
class EmbeddedObjectWithoutExtension {
intValue!: number;

static schema = EmbeddedObject.schema;
}
expect(() => new Realm({ schema: [EmbeddedObjectWithoutExtension, ObjectWithEmbeddedObject] })).throws(
"Class 'EmbeddedObjectWithoutExtension' (declaring 'EmbeddedObject' schema) must extend Realm.EmbeddedObject or Realm.Object",
);
});
const objects = this.realm.objects<IObjectWithEmbeddedObject>(ObjectWithEmbeddedObjectSchema.name);
expect(objects.length).to.equal(1);
expect(objects[0]._id.equals(objectCreated._id));
expect(objects[0].embeddedValue?.intValue).to.equal(1);
});
});

Expand Down
3 changes: 1 addition & 2 deletions packages/realm/src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
//
////////////////////////////////////////////////////////////////////////////

import type { AnyRealmObject } from "./Object";
import type { AppConfiguration } from "./app-services/App";
import type { User } from "./app-services/User";
import type { ObjectSchema, RealmObjectConstructor } from "./schema";
Expand Down Expand Up @@ -66,7 +65,7 @@ export type BaseConfiguration = {
* If omitted, the schema will be read from the existing Realm file.
* @since 0.10.0
*/
schema?: (RealmObjectConstructor<AnyRealmObject> | ObjectSchema)[];
schema?: (RealmObjectConstructor | ObjectSchema)[];
/**
* If changing the `schema`, this field is **required** and must be incremented. This only
* applies to local Realms.
Expand Down
26 changes: 26 additions & 0 deletions packages/realm/src/EmbeddedObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2024 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { injectIndirect } from "./indirect";
import type { OmittedRealmTypes } from "./Unmanaged";

export class RealmEmbeddedObject {
constructor() {}
}

injectIndirect("EmbeddedObject", RealmEmbeddedObject);
2 changes: 1 addition & 1 deletion packages/realm/src/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export class RealmObject<T = DefaultObject, RequiredProperties extends keyof Omi
* @param values - The values of the object's properties at creation.
*/
public constructor(realm: Realm, values: Unmanaged<T, RequiredProperties>) {
return realm.create(this.constructor as RealmObjectConstructor, values) as unknown as this;
return realm.create(this.constructor as RealmObjectConstructor<AnyRealmObject>, values) as unknown as this;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/realm/src/Realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ export namespace Realm {
export import flags = ns.flags;

export import Object = ns.RealmObject;
export import EmbeddedObject = ns.RealmEmbeddedObject;
export import App = ns.App;
export import Auth = ns.Auth;
export import BSON = ns.BSON;
Expand Down
2 changes: 2 additions & 0 deletions packages/realm/src/indirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { RealmObject } from "./Object";
import type { Collection } from "./Collection";
import type { App } from "./app-services/App";
import type { User } from "./app-services/User";
import type { EmbeddedObject } from "./EmbeddedObject";

type Indirects = {
Realm: typeof Realm;
Expand All @@ -36,6 +37,7 @@ type Indirects = {
Dictionary: typeof Dictionary;
Set: typeof RealmSet;
Object: typeof RealmObject;
EmbeddedObject: typeof EmbeddedObject;
App: typeof App;
User: typeof User;
};
Expand Down
Loading
Loading