Class WrappedIndexedStorage<T>

An IndexedStorage that makes use of 2 KeyValueStorages to implement the interface. Due to being limited by key/value storages, there are some restrictions on the allowed type definitions:

  • There needs to be exactly 1 type with no references to other types.
  • All other types need to have exactly 1 reference to another type.
  • Types can't reference each other to create a cycle of references.

All of the above to create a tree-like structure of references. Such a tree is then stored in one of the storages. The other storage is used to store all indexes that are used to find the correct tree object when solving queries.

Type Parameters

Hierarchy

  • WrappedIndexedStorage

Implements

Constructors

Properties

indexStorage: KeyValueStorage<string, string[]>
indexes: {
    [K in string]?: Set<StringKey<T[K]>>
}

For every type, the keys on which an index tracks the values and which root object they are contained in. All types for which a defineType call was made will have a key in this object. For all types that are not the root, there will always be an index on their ID value.

logger: Logger = ...
relations: IndexRelation<T>[]

All parent/child relations between all types in the storage, including the keys in both types that are used to reference each other.

rootTypeVar: undefined | StringKey<T>

The variable in which the root type is stored. A separate getter is used to always return the value so the potential undefined does not have to be taken into account.

validDefinition: boolean = false

Keeps track of type validation. If true the defined types create a valid structure that can be used.

valueStorage: KeyValueStorage<string, VirtualObject>

Accessors

  • get rootType(): string
  • The root type for this storage. Use this instead of rootTypeVar to prevent having to check for undefined. This value will always be defined if the type definitions have been validated.

    Returns string

Methods

  • Creates an object of the given type. The storage will generate an identifier for the newly created object.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType

      The type to create.

    • value: CreateTypeObject<T[TType]>

      The value to set for the created object.

    Returns Promise<TypeObject<T[TType]>>

    A representation of the newly created object, including its new identifier.

  • Informs the storage of the definition of a specific type. A definition is a key/value object with the values being a valid ValueTypeDescription. Generally, this call needs to happen for every type of this storage, and before any calls are made to interact with the data.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType

      The type to define.

    • description: T[TType]

      A description of the values stored in objects of that type.

    Returns Promise<void>

  • Deletes the given object. This will also delete all objects that reference that object if the corresponding key is not optional.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType

      The type of the object to delete.

    • id: string

      The identifier of the object.

    Returns Promise<void>

  • Finds all objects matching a specific IndexedQuery.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType

      The type of objects to find.

    • query: {
          [K in string | number | symbol]?: ValueType<T[TType][K]> | (T[TType][K] extends `id:${U}`
              ? ({ [K in "id" | keyof T[U]]?: ValueType<T[U][K]> | (T[U][K] extends `id:${infer U}` ? { [K in "id" | keyof T[U]]?: ValueType<T[U][K]> | (T[U][K] extends `id:${infer U}` ? { [K in "id" | keyof T[U]]?: ValueType<...> | ... 1 more ... | undefined; } : never) | undefined; } : never) | undefined; })
              : never)
      }

      The query to execute.

    Returns Promise<TypeObject<T[TType]>[]>

    A list of objects matching the query.

  • Similar to IndexedStorage.find, but only returns the identifiers of the found objects.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType

      The type of objects to find.

    • query: {
          [K in string | number | symbol]?: ValueType<T[TType][K]> | (T[TType][K] extends `id:${U}`
              ? ({ [K in "id" | keyof T[U]]?: ValueType<T[U][K]> | (T[U][K] extends `id:${infer U}` ? { [K in "id" | keyof T[U]]?: ValueType<T[U][K]> | (T[U][K] extends `id:${infer U}` ? { [K in "id" | keyof T[U]]?: ValueType<...> | ... 1 more ... | undefined; } : never) | undefined; } : never) | undefined; })
              : never)
      }

      The query to execute.

    Returns Promise<string[]>

    A list of identifiers of the matching objects.

  • Finds the IDs of all root objects that contain objects of the given type matching the given query by making use of the indexes applicable to the keys in the query. This function only looks at the keys in the query with primitive values, object values in the query referencing parent objects are not considered. Similarly, only indexes are used, keys without index are also ignored.

    If an array of root IDs is provided as input, the result will be an intersection of this array and the found identifiers.

    If the result is an empty array, it means that there is no valid identifier matching the query, while an undefined result means there is no index matching any of the query keys, so a result can't be determined.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType
    • match: {
          [K in string | number | symbol]?: ValueType<T[TType][K]> | (T[TType][K] extends `id:${U}`
              ? ({ [K in "id" | keyof T[U]]?: ValueType<T[U][K]> | (T[U][K] extends `id:${infer U}` ? { [K in "id" | keyof T[U]]?: ValueType<T[U][K]> | (T[U][K] extends `id:${infer U}` ? { [K in "id" | keyof T[U]]?: ValueType<...> | ... 1 more ... | undefined; } : never) | undefined; } : never) | undefined; })
              : never)
      }
    • Optional rootIds: string[]

    Returns Promise<undefined | string[]>

  • Returns the object of the given type with the given identifier.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType

      The type of object to get.

    • id: string

      The identifier of that object.

    Returns Promise<undefined | TypeObject<T[TType]>>

    A representation of the object, or undefined if there is no object of that type with that identifier.

  • Finds the record in the given object that contains the given type/id combination. This function assumes it was already verified through an index that this object contains the given combination.

    Type Parameters

    • TType extends string

    Parameters

    Returns Record<string, VirtualObject>

  • Returns the sequence of virtual keys that need to be accessed to reach the given type, starting from the root.

    Parameters

    • type: string

    Returns `**${string}**`[]

  • Returns true if the object of the given type with the given identifier exists.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType

      The type of object to get.

    • id: string

      The identifier of that object.

    Returns Promise<boolean>

    Whether this object exists.

  • Sets the value of a specific object. The identifier in the object is used to identify the object.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType

      The type of the object to set.

    • value: TypeObject<T[TType]>

      The new value for the object.

    Returns Promise<void>

  • Sets the value of one specific field in an object.

    Type Parameters

    • TType extends string

    • TKey extends string

    Parameters

    • type: TType

      The type of the object to update.

    • id: string

      The identifier of the object to update.

    • key: TKey

      The key to update.

    • value: ValueType<T[TType][TKey]>

      The new value for the given key.

    Returns Promise<void>

  • Finds all objects of the given type matching the query. The rootIds array can be used to restrict the IDs of root objects to look at, which is relevant for the recursive calls the function does.

    Will throw an error if there is no index that can be used to solve the query.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType
    • query: {
          [K in string | number | symbol]?: ValueType<T[TType][K]> | (T[TType][K] extends `id:${U}`
              ? ({ [K in "id" | keyof T[U]]?: ValueType<T[U][K]> | (T[U][K] extends `id:${infer U}` ? { [K in "id" | keyof T[U]]?: ValueType<T[U][K]> | (T[U][K] extends `id:${infer U}` ? { [K in "id" | keyof T[U]]?: ValueType<...> | ... 1 more ... | undefined; } : never) | undefined; } : never) | undefined; })
              : never)
      }
    • Optional rootIds: string[]

    Returns Promise<VirtualObject[]>

  • Updates the index for a specific key of an object of the given type.

    Parameters

    • type: string
    • key: string
    • value: string
    • rootId: string
    • add: boolean

    Returns Promise<void>

  • Replaces an object of the given type. The identifier in the value is used to determine which object.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType
    • value: TypeObject<T[TType]>
    • replace: true

    Returns Promise<void>

  • Replaces part of an object of the given type with the given partial value. The identifier in the value is used to determine which object.

    Type Parameters

    • TType extends string

    Parameters

    • type: TType
    • partial: Partial<TypeObject<T[TType]>> & {
          id: string;
      }
    • replace: false

    Returns Promise<void>

  • Makes sure the defined types fulfill all the requirements necessary for types on this storage. Will throw an error if this is not the case. This should be called before doing any data interactions. Stores success in a variable so future calls are instantaneous.

    Parameters

    • type: string

    Returns void