UNPKG

realm

Version:

Realm by MongoDB is an offline-first mobile database: an alternative to SQLite and key-value stores

41 lines (40 loc) 2.27 kB
import { Collection, Dictionary, List } from "./internal"; import { AnyRealmObject } from "./Object"; export type AnyCollection = Collection<any, any, any, any, any>; export type AnyDictionary = Dictionary<any>; export type AnyList = List<any>; type ExtractPropertyNamesOfType<T, PropType> = { [K in keyof T]: T[K] extends PropType ? K : never; }[keyof T]; /** * Exchanges properties defined as {@link List} with an optional {@link Array}. */ type RealmListsRemappedModelPart<T> = { [K in ExtractPropertyNamesOfType<T, AnyList>]?: T[K] extends List<infer GT> ? Array<GT | Unmanaged<GT>> : never; }; /** * Exchanges properties defined as {@link Dictionary} with an optional key to mixed value object. */ type RealmDictionaryRemappedModelPart<T> = { [K in ExtractPropertyNamesOfType<T, AnyDictionary>]?: T[K] extends Dictionary<infer ValueType> ? { [key: string]: ValueType; } : never; }; /** Omits all properties of a model which are not defined by the schema */ export type OmittedRealmTypes<T> = Omit<T, keyof AnyRealmObject | ExtractPropertyNamesOfType<T, Function> | ExtractPropertyNamesOfType<T, AnyCollection> | ExtractPropertyNamesOfType<T, AnyDictionary>>; /** Make all fields optional except those specified in K */ type OptionalExcept<T, K extends keyof T> = Partial<T> & Pick<T, K>; /** * Omits all properties of a model which are not defined by the schema, * making all properties optional except those specified in RequiredProperties. */ type OmittedRealmTypesWithRequired<T, RequiredProperties extends keyof OmittedRealmTypes<T>> = OptionalExcept<OmittedRealmTypes<T>, RequiredProperties>; /** Remaps realm types to "simpler" types (arrays and objects) */ type RemappedRealmTypes<T> = RealmListsRemappedModelPart<T> & RealmDictionaryRemappedModelPart<T>; /** * 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<T, RequiredProperties extends keyof OmittedRealmTypes<T> = never> = OmittedRealmTypesWithRequired<T, RequiredProperties> & RemappedRealmTypes<T>; export {};