realm-js
realm-js copied to clipboard
Defining typescript schema with "extends Realm.Object" not support dictionary, embed model and linking objects
Problem
I am using @realm/[email protected], it seems currently only support simple types, but not:
- dictionary with typescript interface
- typescript enum
- typescript union type
- embed model
- linking objects
Is there a plan to support them or I missed something to know?
Currrently I need to use the old way of defining schema property to make my models work.
A little example:
export class NoteModel extends Realm.Object<NoteModel> {
// @ts-ignore
static name = "Note";
static primaryKey = "_id";
_id: Realm.Types.ObjectId = new Realm.BSON.ObjectId();
createdAt: Realm.Types.Date = new Date();
title!: string;
// not work
meta!: Realm.Types.Dictionary< {
type: string;
count: number;
}>
// not work
status!: MyEmbedModel
}
export class MyEmbedModel extends Realm.Object<MyEmbedModel> {
// @ts-ignore
static name = "Status";
static embedded = true;
enabled!: boolean;
}
Solution
No response
How important is this improvement for you?
Would be a major improvement
Yes, this would be very important to me too. What is the recommended way of doing this then? Should forget just defining these using the babel plugin and instead do this using the schema for now?
Thanks so much for reporting this issue. The babel plugin is experimental and we are happy to track any issues our users are having.
I would recommend not using the babel plugin if it is not supporting your use case. At this time it is not our highest priority to support.
We are, however, willing to review any community PRs to fix any bugs or add features to the babel plugin.
What would the recommended practice be if I wanted to use Typescript enums with a realm object? And how would I get Typescript compatibility if I do the schema solution? That's the part I haven't figured out myself.
What would the recommended practice be if I wanted to use Typescript enums with a realm object? And how would I get Typescript compatibility if I do the schema solution? That's the part I haven't figured out myself.
@worldlee78 Here is what I did currently using the schema solution:
export enum OptType {
create = 'reate',
update = 'update'
}
export class OptModel{
static schema: Realm.ObjectSchema = {
name: 'Opt',
primaryKey: '_id',
properties: {
_id: 'objectId',
type: 'string',
},
};
_id!: Realm.BSON.ObjectId;
type!: OptType;
static generate(data: {type: OptType; }) {
return {
_id: new Realm.BSON.ObjectId(),
type: data.type,
};
}
}
Thanks so much for reporting this issue. The babel plugin is experimental and we are happy to track any issues our users are having.
I would recommend not using the babel plugin if it is not supporting your use case. At this time it is not our highest priority to support.
We are, however, willing to review any community PRs to fix any bugs or add features to the babel plugin.
That's so sad! You guys should at least support embedded models since it's so common to use them! You should give babel plugin priority because it makes development in Realm so much easier!
export class NoteModel extends Realm.Object<NoteModel> {
//..
// this should work
status!: MyEmbedModel
}
export class MyEmbedModel extends Realm.Object<MyEmbedModel> {
// ...
static embedded = true;
enabled!: boolean;
}