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

Adding types for passing objects when constructing objects #6675

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kraenhansen
Copy link
Member

What, How & Why?

This adds types to allow passing object links and embedded objects when creating objects using a class-based model constructor:

import Realm from "realm";

class Contact extends Realm.Object<Contact, "name"> {
    name!: string;
    address!: Address;

    static schema: Realm.ObjectSchema = {
        name: "Contact",
        properties: {
            name: "string",
            address: "Address",
        },
    };
}

class Address extends Realm.Object<Address, "street" | "city"> {
    street!: string;
    city!: string;

    static schema: Realm.ObjectSchema = {
        name: "Address",
        embedded: true,
        properties: {
            street: "string",
            city: "string",
        },
    };
}

Realm.deleteFile({});
let realm = new Realm({ schema: [Contact, Address] });
realm.write(() => {
    const alice = new Contact(realm, {
        name: "Alice",
        address: {
            street: "Grøndals Parkvej 2A",
            // city: "Vanløse"
        },
    });

    const bob = new Contact(realm, {
        name: "Bob",
        address: {
            street: "Griffenfeldsgade 14B",
            city: "København N",
        }
    });
});

realm.objects<Contact>(Contact.schema.name).forEach((c) => {
    console.log(`${c.name} at ${c.address?.street}, ${c.address?.city}`);
});

Unfortunately, the RequiredProperties passed as type parameter ("street" | "city") when declaring Address doesn't cascade as expected, which is why alice can be created without passing city. As an alternative RealmObjectRemappedModelPart type could be extended to make all properties required, but I haven't been able to express the constructor type from the instance type T[K] in a way that I could infer the RequiredProperties correctly.

☑️ ToDos

  • Fix the required properties
  • 📝 Changelog entry
  • 📝 Compatibility label is updated or copied from previous entry
  • 📝 Update COMPATIBILITY.md
  • 🚦 Tests
  • 📦 Updated internal package version in consuming package.jsons (if updating internal packages)
  • 📱 Check the React Native/other sample apps work if necessary
  • 💥 Breaking label has been applied or is not necessary

@kraenhansen kraenhansen self-assigned this May 23, 2024
@cla-bot cla-bot bot added the cla: yes label May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant