Dictionary with unknown keys and/or nested objects

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.

I’m also looking for the solution to this. I need an embedded dictionary object of FilterState. Getting the following error.

Error: Schema validation failed due to the following errors:

  • Property ‘Filter.values’ of type ‘dictionary’ has unknown object type ‘FilterState’
type State = { relation: string; value: string; };
type FilterValues = Record<string, State>;

export class Filter extends Object<Filter> {
  _id!: BSON.ObjectId;
  name!: string;
  type!: FilterType;
  values!: FilterValues;

  static schema: ObjectSchema = {
    name: 'Filter',
    properties: {
      _id: { type: 'objectId', default: () => new BSON.ObjectId() },
      name: 'string',
      type: 'string',
      values: { type: 'dictionary', objectType: 'FilterState' },
    },
    primaryKey: '_id',
  };
};

export class FilterState extends Object<FilterState> {
  relation!: string;
  value?: string;

  static schema: ObjectSchema = {
    name: 'FilterState',
    embedded: true,
    properties: {
      relation: 'string',
      value: 'string?',
    },
  };
};

Following the documentation. Using realm 12.5.0 and @react/realm 0.6.2

UPDATE: I found that I had an export problem with FilterState. The above implementation is working as expected. Leaving in case this helps the OP/others.