From 62bcbff03a373db092b17b3f3bd55c325bb5fb3a Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Mon, 17 Jun 2024 14:17:31 +0200 Subject: [PATCH 1/3] Make counter types requirable --- packages/realm/src/Object.ts | 3 ++- packages/realm/src/Unmanaged.ts | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/realm/src/Object.ts b/packages/realm/src/Object.ts index 6851b1b01a..1d7c479f62 100644 --- a/packages/realm/src/Object.ts +++ b/packages/realm/src/Object.ts @@ -31,6 +31,7 @@ import { OrderedCollection, Realm, RealmObjectConstructor, + RequirableProperties, Results, TypeAssertionError, TypeHelpers, @@ -152,7 +153,7 @@ const PROXY_HANDLER: ProxyHandler> = { * properties not specified will be optional, and will default to a sensible * null value if no default is specified elsewhere. */ -export class RealmObject = never> { +export class RealmObject = never> { /** * This property is stored on the per class prototype when transforming the schema. * @internal diff --git a/packages/realm/src/Unmanaged.ts b/packages/realm/src/Unmanaged.ts index 3d01ce22e1..6cf8d1f8ee 100644 --- a/packages/realm/src/Unmanaged.ts +++ b/packages/realm/src/Unmanaged.ts @@ -61,9 +61,9 @@ type RealmSetRemappedModelPart = { /** * Exchanges properties defined as a {@link Counter} with a `number`. */ -type RealmCounterRemappedModelPart = { - [K in ExtractPropertyNamesOfTypeExcludingNullability]?: Counter | number | Exclude; -}; +type RealmCounterRemappedModelPart> = OptionalExcept<{ + [K in ExtractPropertyNamesOfTypeExcludingNullability]: Counter | number | Exclude; +}, RequiredProperties & ExtractPropertyNamesOfTypeExcludingNullability>; /** Omits all properties of a model which are not defined by the schema */ export type OmittedRealmTypes = Omit< @@ -76,6 +76,8 @@ export type OmittedRealmTypes = Omit< | ExtractPropertyNamesOfTypeExcludingNullability >; +export type RequirableProperties = Omit; + /** Make all fields optional except those specified in K */ type OptionalExcept = Partial & Pick; @@ -83,24 +85,25 @@ type OptionalExcept = Partial & Pick; * Omits all properties of a model which are not defined by the schema, * making all properties optional except those specified in RequiredProperties. */ -type OmittedRealmTypesWithRequired> = OptionalExcept< +type OmittedRealmTypesWithRequired> = OptionalExcept< OmittedRealmTypes, - RequiredProperties + RequiredProperties & keyof OmittedRealmTypes >; /** Remaps realm types to "simpler" types (arrays and objects) */ -type RemappedRealmTypes = RealmListRemappedModelPart & +type RemappedRealmTypes> = + RealmListRemappedModelPart & RealmDictionaryRemappedModelPart & RealmSetRemappedModelPart & - RealmCounterRemappedModelPart; + RealmCounterRemappedModelPart; /** * Joins `T` stripped of all keys which value extends {@link Collection} and all inherited from {@link Realm.Object}, * with only the keys which value extends {@link List}, remapped as {@link Array}. All properties are optional * except those specified in `RequiredProperties`. */ -export type Unmanaged = never> = OmittedRealmTypesWithRequired< +export type Unmanaged = never> = OmittedRealmTypesWithRequired< T, RequiredProperties > & - RemappedRealmTypes; + RemappedRealmTypes; \ No newline at end of file From 658ef90f6eb0d572c7e460d711dc625dd82d4fdc Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Mon, 17 Jun 2024 14:25:21 +0200 Subject: [PATCH 2/3] Minor tweaks --- packages/realm/src/Unmanaged.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/realm/src/Unmanaged.ts b/packages/realm/src/Unmanaged.ts index 6cf8d1f8ee..42d59f9937 100644 --- a/packages/realm/src/Unmanaged.ts +++ b/packages/realm/src/Unmanaged.ts @@ -61,9 +61,9 @@ type RealmSetRemappedModelPart = { /** * Exchanges properties defined as a {@link Counter} with a `number`. */ -type RealmCounterRemappedModelPart> = OptionalExcept<{ +type RealmCounterRemappedModelPart> = OptionalExceptRequired]: Counter | number | Exclude; -}, RequiredProperties & ExtractPropertyNamesOfTypeExcludingNullability>; +}, RequiredProperties>; /** Omits all properties of a model which are not defined by the schema */ export type OmittedRealmTypes = Omit< @@ -78,16 +78,17 @@ export type OmittedRealmTypes = Omit< export type RequirableProperties = Omit; -/** Make all fields optional except those specified in K */ -type OptionalExcept = Partial & Pick; +/** Make all fields optional except those specified in RequiredProperties */ +type OptionalExceptRequired> = Partial & Pick; /** * Omits all properties of a model which are not defined by the schema, * making all properties optional except those specified in RequiredProperties. */ -type OmittedRealmTypesWithRequired> = OptionalExcept< +type OmittedRealmTypesWithRequired> = OptionalExceptRequired< + T, OmittedRealmTypes, - RequiredProperties & keyof OmittedRealmTypes + RequiredProperties >; /** Remaps realm types to "simpler" types (arrays and objects) */ From 1b1522141e75daa413c624b52218934d3f114f7b Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Mon, 17 Jun 2024 15:11:52 +0200 Subject: [PATCH 3/3] Remap realm objects --- packages/realm/src/Unmanaged.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/realm/src/Unmanaged.ts b/packages/realm/src/Unmanaged.ts index 42d59f9937..081df238ba 100644 --- a/packages/realm/src/Unmanaged.ts +++ b/packages/realm/src/Unmanaged.ts @@ -65,6 +65,10 @@ type RealmCounterRemappedModelPart]: Counter | number | Exclude; }, RequiredProperties>; +type RealmObjectRemappedModelPart> = OptionalExceptRequired>]: Unmanaged | undefined | null; +}, RequiredProperties>; + /** Omits all properties of a model which are not defined by the schema */ export type OmittedRealmTypes = Omit< T, @@ -74,6 +78,7 @@ export type OmittedRealmTypes = Omit< | ExtractPropertyNamesOfType | ExtractPropertyNamesOfType | ExtractPropertyNamesOfTypeExcludingNullability + | ExtractPropertyNamesOfTypeExcludingNullability> >; export type RequirableProperties = Omit; @@ -93,10 +98,11 @@ type OmittedRealmTypesWithRequired> = - RealmListRemappedModelPart & - RealmDictionaryRemappedModelPart & - RealmSetRemappedModelPart & - RealmCounterRemappedModelPart; + & RealmListRemappedModelPart + & RealmDictionaryRemappedModelPart + & RealmSetRemappedModelPart + & RealmCounterRemappedModelPart + & RealmObjectRemappedModelPart; /** * Joins `T` stripped of all keys which value extends {@link Collection} and all inherited from {@link Realm.Object},