I can’t figure out how to do models for realm device sync even if my life depended on it!
I am using
npx create-expo-app MyAwesomeRealmApp --template @realm/expo-template
The docs and the example template give 3 different styles of defining models and none of them work. One of the objectives is to nest a GeoPoint
object.
- The template has defined the models this way:
export class Task extends Realm.Object {
_id: BSON.ObjectId = new BSON.ObjectId();
description!: string;
isComplete: boolean = false;
createdAt: Date = new Date();
userId!: string;
static primaryKey = '_id';
}
the problem with this style is I can’t make it create a collection that’s named tasks
instead the development mode creates a collection named Task
. I am also unsuccessful in nesting a GeoPoint
object. Also don’t see how to define an embedded object this way.
- The modeling suggestions from the app service are like this:
export type Task = {
_id: Realm.BSON.ObjectId;
createdAt: Date;
description: string;
isComplete: boolean;
userId: string;
};
export const TaskSchema = {
name: 'Task',
properties: {
_id: 'objectId',
createdAt: 'date',
description: 'string',
isComplete: 'bool',
userId: 'string',
},
primaryKey: '_id',
};
This fixes the collection name problem, but I still am unsuccessful at nesting a GeoPoint object. The Realm.List seems to be the problem, even tho I can create such an object it fails to insert it. Doesn’t even throw an error ( I have checked all the rules and so on).
- The app service device sync examples also show this way:
export class Dog extends Realm.Object<Dog> {
_id!: BSON.ObjectId;
name!: string;
age!: number;
static schema: ObjectSchema = {
name: "Dog",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
name: "string",
age: "int",
},
};
}
I couldn’t even get the app to run with this style, it throws an error about how it’s not allowed to define a static schema property.
The bottom line is I would like to be able to create something like this:
export class GeoPoint extends Realm.Object {
type: string = "point";
coordinates: Realm.List<number>;
}
export class Task extends Realm.Object {
_id: BSON.ObjectId = new BSON.ObjectId();
description!: string;
isComplete: boolean = false;
createdAt: Date = new Date();
userId!: string;
point: GeoPoint;
static primaryKey = '_id';
}
and be able to create, write, and sync Task objects. Configuring the name of the collection to be ‘tasks’ would also be nice. An example of how to use mixed dictionaries would also be nice.