I am looking to store an object with unknown keys where the values can either be a boolean or a mongo-like filter object as a property on a Realm.Object
. From what I can tell using realm v12.3.0 and realm/react v 0.6.1, I am unable to do this.
The object I’m looking to store is something like:
// Definition
type ObjectType = Record<string, boolean | Record<string, string>>
export class MainObject extends Realm.Object<MainObject> {
objectProperty!: ObjectType;
static schema: ObjectSchema = {
name: "MainObject",
properties: {
objectProperty: "???"
}
}
}
// Usage
const objectInstance = {
some_arbitrary_key: true,
some_other_arbitrary_key: {
$gte: 4
}
}
realm.write(() => {
realm.create(MainObject, {.objectProperty: objectInstance }, true);
});
Ideally, I would be able to tell Realm to store this as an arbitrary JSON object and be done with it, but I haven’t been able to find a way to do that.
I’ve tried setting the property’s type to mixed{}
, but that fails since the nested objects (some_other_arbitrary_key
in the above example) can’t be stored as a mixed
since it is an Object.
I’m relatively new to Realm, but have a lot of experience with Mongo. If what I’m trying to accomplish above isn’t possible in Realm, I’d be interested to know if there are plans to add arbitrary JSON as a property type at some point in the future.