realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Support super classing for Realm.Object model

Open deckyfx opened this issue 3 years ago • 3 comments

Problem

Currently the model class must be direct descendant of Realm.Object class,

But creating many model class with repetitive field or methods could be exhausting (for example when all model should have _id, and createdAt field), so i tried to create a super class BaseModel which extends Realm.Object to handle that common fields and methods, then i would write my actual model extending that BaseModel

In this case i created UserPreference Model extending BaseModel

but then realm throw error like this [Error: Exception in HostFunction: Class 'UserPreference' (declaring 'user_preference' schema) must extend Realm.Object]

Edit, i forgot to put my specs

"@realm/react": "^0.4.1",
"react": "^18.2.0",
"react-native": "0.70.6",
"realm": "^11.3.0" 

Solution

for now i have to rewrite the BaseModel as an Interface, and the model implements it, so i don't miss the basic field that all of my models should have, but at the cost of repetitive code

How important is this improvement for you?

Fairly niche, but nice to have anyway.

deckyfx avatar Dec 09 '22 07:12 deckyfx

@deckyfx Thank you for the suggestion. I agree that superclassing is the next natural step. It does also come up in other Realm SDKs, and it is a feature we would like to implement. I can't give us an estimate for when.

kneth avatar Dec 09 '22 09:12 kneth

Got the same problem here. It would be great if we could superclassing the models.

fkranenburg avatar Jan 12 '23 18:01 fkranenburg

Why this ticket is closed, is this already supported? or auto-closed?

Edit: move to discussion 6615

deckyfx avatar Apr 15 '24 01:04 deckyfx