-
Type: Improvement
-
Resolution: Fixed
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: Node.js, React Native, Realm Babel
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.
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:
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