You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I seem to found an idea to do this, but extensive testing needs to be done.
In short, seems Realm only checks the naming of the model class constructor, no deep checking there, so the idea is we trick the compiler into thinking your own superclass is Realm.Object<T>
Realmjs version 12.6.2
File 1, BaseModel.tsx
import ORealm, {BSON} from 'realm'; // import Realm as ORealm, so we can export our own Realm namespace
import {DefaultObject} from 'realm/dist/internal'; // import some types
class BaseModel<T = DefaultObject> extends ORealm.Object<T> { // extends from Realm.Object
_id!: BSON.ObjectId; // declare base properties that will owned by all models extended from this class
// declare methods that will inherited by the child class
sayHello() {
return 'hi';
}
}
// Export out own realm
export namespace Realm {
export const Object = BaseModel;
}
Now File 2, Experimental.tsx
import {ObjectSchema} from 'realm';
import {Realm} from './BaseModel'; // import our exported Realm namesmpace
class Experimental extends Realm.Object<Experimental> { // Extends from out own BaseModel
name!: string; // properties that this class has
// declare schema
static schema: ObjectSchema = {
name: 'Experimental',
properties: {
_id: 'objectId',
name: 'string',
},
primaryKey: '_id',
};
}
export default Experimental;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The original topic is here #5170
I seem to found an idea to do this, but extensive testing needs to be done.
In short, seems Realm only checks the naming of the model class constructor, no deep checking there, so the idea is we trick the compiler into thinking your own superclass is
Realm.Object<T>
Realmjs version 12.6.2
File 1, BaseModel.tsx
Now File 2, Experimental.tsx
Now to test it
The result will be
To Do
Experimental instanceof BaseModel
, should be yes tho)Any other ideas?
Beta Was this translation helpful? Give feedback.
All reactions