2 / 9
Nov 2023

I know that the Babel Plugin is experimental, but I’m trying to figure out if anyone knows if there is any way to model one-to-one relationships?

The compiler tells me to try

car?: {type: 'object', objectType: 'Car' }

but this doesn’t work.

I’ve also tried the following to no avail:

car?: Realm.Object<Car> car?: Car car?: 'Car'

I’ve been searching through the documentation and forums to see if anyone has done this, but if they have, it doesn’t seem to be shared anywhere. Anyone have any ideas?

10 days later

Thanks Andrew.

I resolved it. I was using quotes on the objectType and it needed to be:

car?: {type: 'object', objectType: Car }

It turns out that, from what I can tell, having Sync in Development Mode doesn’t completely work when using the Babel Plugin.

One-to-one relationships
Setting a variable as Types.Int

car: {type: 'object', objectType: Car} status: Types.Int = 0

Setting these in the Schema on the client does not generate a matching schema on the server, so I have to do that manually in order for the device and server to stay in sync.

Maybe these are an oversight, maybe it just isn’t addressed, but figured it would be valuable to at least document this somewhere.


Furthermore, it seems that writing to the relationship doesn’t work in any of these ways:

// passing in the description and Car object as carToLink in a function and linking after creating task realm.write(() => { task = realm.create(Task, {description}); task.car = carToLink; }); // passing in the description and Car object as carToLink in a function and passing in the car while creating task realm.write(() => { task = realm.create(Task, {description, car: carToLink}); }); // passing in the description and getting the carToLink from the primary key realm.write(() => { let car = realm.objectForPrimaryKey('Car', carToLink._id task = realm.create(Task, {description, car}); }); // passing in the description and getting the carToLink from the primary key and setting it after realm.write(() => { let car = realm.objectForPrimaryKey('Car', carToLink._id task = realm.create(Task, {description); task.car = car; });

TypeScript is complaining about task.car:

Type ‘Car’ is missing the following properties from type ‘{ type: “object”; objectType: Car; }’: type, objectTypets(2739)


One more thing I just realized:

When attempting to access embedded objects through the {type: "object", objectType: EmbeddedCar} when typing in TypeScript, the options are type and objectType. No translation is being done to find the actual object properties. This is probably similar to the above issue with missing properties.

Typing task.embeddedCar. should give some properties of the car like:

task.embeddedCar.manufacturer task.embeddedCar.model task.embeddedCar.year

instead, it’s only giving

task.embeddedCar.type task.embeddedCar.objectType

At this point I’m just going to move back to not using the Babel Plugin.

But if there is anything you want me to test or help work on to get it to production ready, I’m happy to.

1 month later

Any update on this? This is a pretty simple use case that I’m not being able to handle as well

Example:

export class ModelType extends Realm.Object<ModelType, 'id' | 'code' | 'name' | 'description'> { id: Realm.Types.ObjectId = new Realm.BSON.ObjectId(); code: Realm.Types.String; name: Realm.Types.String; description: Realm.Types.String; static primaryKey = 'id'; } export class Model extends Realm.Object< Model, 'fieldStringRequiredWithMinSize' | 'fieldNumber' | 'modelType' > { id: Realm.Types.ObjectId = new Realm.BSON.ObjectId(); fieldString?: Realm.Types.String; fieldStringWithMinSize?: Realm.Types.String; fieldStringRequiredWithMinSize: Realm.Types.String; fieldNumber: Realm.Types.Int; modelType: ModelType; static primaryKey = 'id'; }

Hello @Gabriel_Francischini! Welcome to the forums.

I have not heard back from @Andrew_Meyer, and he is the one who would know. They did post an update to the plugin since my post, but it was very minimal and had nothing to do with realm relationships.

My guess is that it is very low priority for them and its best to not use the babel plugin for anything complex or production.

Hey all, thought I’d post a quick update. @Kurt_Libby1 is correct, the babel-plugin is experimental and we have placed it low on our priorities to fix. The latest updates were actually submitted from the community. If you would like to try and debug this issue yourself, we will review any PRs and try and get them in. At the moment, that is the best I can offer.