Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-2525

Error when extending a subclass of Realm.Object

      How frequently does the bug occur?

      Always

      Description

      Hi there,

      We want our realm classes to extend a subclass of Realm.Object, so that we can introduce functions that applies to all our realm classes. The code below shows the idea.

      Unable to find source-code formatter for language: ts. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      abstract class MyBaseRealmObject<
        T = unknown,
        RequiredProperties extends keyof OmittedRealmTypes<T> = never,
      > extends Realm.Object<T, RequiredProperties> {
        abstract myOwnFunction(): string; // A function applies to all my realm classes
      }
      
      class TestRealmAddress extends MyBaseRealmObject<TestRealmAddress, 'line1'> {
        static schema: Realm.ObjectSchema = {
          name: 'TestRealmAddress',
          properties: {
            line1: 'string',
            line2: 'string?',
            city: 'string?',
            postcode: 'string?',
          },
        };
        public line1!: Realm.Types.String;
        public line2!: Realm.Types.String | null;
        public city!: Realm.Types.String | null;
        public postcode!: Realm.Types.String | null;
      
        myOwnFunction(): string {
          return '';
        }
      }
      

      But if I use a subclass, I will get an error when I open the realm: Error: Exception in HostFunction: Class 'TestRealmAddress' must extend Realm.Object. TestRealmAddress doesn't extend Realm.Object directly, but it should still be an instance of Realm.Object. To be honest I am not sure if this is a bug or deliberated behaviour.

      The code to open the realm is as below:

      Unable to find source-code formatter for language: ts. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      Realm.open({
          schema: [ TestRealmAddress ],
          schemaVersion: 1,
        });
      

      I attached a mini repo to demo it.

      Many thanks

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      Always

      Reproduction Steps

      I made a mini repo to demo the issue: https://github.com/Yupeng-li/realm-subclass-test

      There are two buttons: one for the schema not using subclass and another one using subclass

      If you tap on the second button, you will see the error.

      Version

      11.10.2, 11.9.0

      What services are you using?

      Local Database only

      Are you using encryption?

      Yes

      Platform OS and version(s)

      All

      Build environment

      RN 71 & RN 72

      Cocoapods version

      1.12

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: