From f4b64324b91fe68aa7791120c3ac89c872b9262f Mon Sep 17 00:00:00 2001 From: Andrew Meyer Date: Wed, 18 May 2022 08:33:14 +0200 Subject: [PATCH 1/3] Type Fix for Returned Schemas * The schema returned from an open realm has a different structure than the input schema for configuration. This structure has now been typed along with updated documentation. * Statically cast the returned schema as it is being used to dynamically update the schema --- CHANGELOG.md | 2 +- docs/realm.js | 29 +++++++++++++++- .../tests/src/tests/dynamic-schema-updates.ts | 2 +- types/index.d.ts | 34 +++++++++++++++++-- 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bb7b2fe05..2eefe4d9bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ x.x.x Release notes (yyyy-MM-dd) ### Fixed * ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?) -* None. +* Add canonical schema type for retruned schemas ([#4580](https://github.com/realm/realm-js/pull/4580)) ### Compatibility * MongoDB Realm Cloud. diff --git a/docs/realm.js b/docs/realm.js index c8c372b292..209ab4b596 100644 --- a/docs/realm.js +++ b/docs/realm.js @@ -54,7 +54,7 @@ class Realm { /** * A normalized representation of the schema provided in the * {@link Realm~Configuration Configuration} when this Realm was constructed. - * @type {Realm~ObjectSchema[]} + * @type {Realm~CanonicalObjectSchema[]} * @readonly * @since 0.12.0 */ @@ -499,6 +499,33 @@ class Realm { * name. Queries can be done using both the public and the underlying property name. */ +/** + * The retreived schema from an open Realm has a different structure as the input schema. + * @typedef Realm~CanonicalObjectSchema + * @type {Object} + * @property {string} name - Represents the object type. + * @property {string} [primaryKey] - The name of a `"string"` or `"int"` property + * that must be unique across all objects of this type within the same Realm. + * @property {boolean} [embedded] - True if the object type is embedded. An embedded object + * can be linked to by at most one parent object. Default value: false. + * @property {Object} properties - + * An object where the keys are property names and the values represent the property type. + */ + +/** + * @typedef Realm~CanonicalObjectSchemaProperty + * @type {Object} + * @property {string} name - The name of this property. + * @property {Realm~PropertyType} type - The type of this property. + * @property {string} [property] - When using `"linkingObjects"` this value will represent the linked property on the linked object + * @property {boolean} optional - Signals that this property may be assigned `null` or `undefined`. + * For `"list"`, `"dictionary"` or `"set"` properties of non-object types, this instead signals whether the values inside the list may be assigned `null` or `undefined`. + * This is not supported for `"list"` or `"set"` properties of object types and `"linkingObjects"` properties. + * @property {boolean} indexed - Signals that this property is indexed. Only supported for + * `"string"`, `"int"`, and `"bool"` properties. + * @property {string} mapTo - The name of the alias this property has been set to. + */ + /** * The type of an object may either be specified as a string equal to the `name` in a * {@link Realm~ObjectSchema ObjectSchema} definition, **or** a constructor that was specified diff --git a/integration-tests/tests/src/tests/dynamic-schema-updates.ts b/integration-tests/tests/src/tests/dynamic-schema-updates.ts index e2bf19c1eb..ea2375d260 100644 --- a/integration-tests/tests/src/tests/dynamic-schema-updates.ts +++ b/integration-tests/tests/src/tests/dynamic-schema-updates.ts @@ -72,7 +72,7 @@ describe("realm._updateSchema", () => { // Copy the schema const updatedSchema = [...realm.schema]; // Locate the Dog schema - const dogSchema = updatedSchema.find((s) => s.name === "Dog"); + const dogSchema = updatedSchema.find((s) => s.name === "Dog") as Realm.ObjectSchema; if (!dogSchema) throw new Error("Schema not found"); // Add a fields property diff --git a/types/index.d.ts b/types/index.d.ts index f0c4ed127f..afec1ed527 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -40,6 +40,7 @@ declare namespace Realm { /** * ObjectSchemaProperty + * This is the structure of a schema as input when configuring a Realm * @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~ObjectSchemaProperty } */ interface ObjectSchemaProperty { @@ -52,9 +53,28 @@ declare namespace Realm { mapTo?: string; } + /** + * CanonicalObjectSchemaProperty + * This depicts the structure of a schema retrieved from a Realm + * @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~CanonicalObjectSchemaProperty } + */ + interface CanonicalObjectSchemaProperty { + name: string; + type: PropertyType; + objectType: string; + property?: string; + optional: boolean; + indexed: boolean; + mapTo: string; + } + // properties types interface PropertiesTypes { - [keys: string]: PropertyType | ObjectSchemaProperty | ObjectSchema; + [keys: string]: ObjectSchemaProperty | ObjectSchema | PropertyType; + } + + interface CanonicalPropertiesTypes { + [keys: string]: CanonicalObjectSchemaProperty; } enum UpdateMode { @@ -67,13 +87,21 @@ declare namespace Realm { * ObjectSchema * @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~ObjectSchema } */ - interface ObjectSchema { + + interface BaseObjectSchema { name: string; primaryKey?: string; embedded?: boolean; + } + + interface ObjectSchema extends BaseObjectSchema { properties: PropertiesTypes; } + interface CanonicalObjectSchema extends BaseObjectSchema { + properties: CanonicalPropertiesTypes; + } + /** * ObjectClass * @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~ObjectClass } @@ -960,7 +988,7 @@ declare class Realm { readonly empty: boolean; readonly path: string; readonly readOnly: boolean; - readonly schema: Realm.ObjectSchema[]; + readonly schema: Realm.CanonicalObjectSchema[]; readonly schemaVersion: number; readonly isInTransaction: boolean; readonly isClosed: boolean; From 3a6a819f7bfc9752db2b4c13916e1abbcd60afb9 Mon Sep 17 00:00:00 2001 From: Andrew Meyer Date: Wed, 18 May 2022 13:58:04 +0200 Subject: [PATCH 2/3] PR suggestions --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eefe4d9bb..e588e28359 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ x.x.x Release notes (yyyy-MM-dd) ### Fixed * ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?) -* Add canonical schema type for retruned schemas ([#4580](https://github.com/realm/realm-js/pull/4580)) +* Add canonical schema type for returned schemas ([#4580](https://github.com/realm/realm-js/pull/4580)) ### Compatibility * MongoDB Realm Cloud. From a2a4c7068d0a7e13bc05fe7681543463ca97eb9d Mon Sep 17 00:00:00 2001 From: Andrew Meyer Date: Tue, 31 May 2022 10:01:31 +0200 Subject: [PATCH 3/3] Update dynaamic schema test --- docs/realm.js | 2 +- integration-tests/tests/src/tests/dynamic-schema-updates.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/realm.js b/docs/realm.js index 3686b7cfec..8e3939b802 100644 --- a/docs/realm.js +++ b/docs/realm.js @@ -500,7 +500,7 @@ class Realm { */ /** - * The retreived schema from an open Realm has a different structure as the input schema. + * The retreived schema from an open Realm might have a different structure as the input schema. * @typedef Realm~CanonicalObjectSchema * @type {Object} * @property {string} name - Represents the object type. diff --git a/integration-tests/tests/src/tests/dynamic-schema-updates.ts b/integration-tests/tests/src/tests/dynamic-schema-updates.ts index ea2375d260..0538a36224 100644 --- a/integration-tests/tests/src/tests/dynamic-schema-updates.ts +++ b/integration-tests/tests/src/tests/dynamic-schema-updates.ts @@ -70,9 +70,9 @@ describe("realm._updateSchema", () => { it("creates a property on an existing class", () => { const realm = new Realm({ schema: [PersonSchema, DogSchema] }); // Copy the schema - const updatedSchema = [...realm.schema]; + const updatedSchema: Realm.ObjectSchema[] = [...realm.schema]; // Locate the Dog schema - const dogSchema = updatedSchema.find((s) => s.name === "Dog") as Realm.ObjectSchema; + const dogSchema = updatedSchema.find((s) => s.name === "Dog"); if (!dogSchema) throw new Error("Schema not found"); // Add a fields property