Class ChildModel<T>

Model for some subset of the data

Type Parameters

Hierarchy

  • Model<T>
    • ChildModel

Constructors

Properties

Methods

Constructors

  • Type Parameters

    • T = unknown

    Parameters

    • model: Model<T>

    Returns ChildModel<T>

Properties

ChildModel: typeof ChildModel
bundleTimeout: number
closePromised: Object
connection: any
data: T
fetchOnly: boolean
hasErrored?: boolean
removeAllFilters: Object
root: RootModel
unbundle: Object
unloadDelay: number

Delay in milliseconds before model data actually unloaded after call to Model.unload

Methods

  • Adds a document to the collection, under an id subpath corresponding to either the document's id property if present, or else a new randomly generated id.

    If a callback is provided, it's called when the write is committed or fails.

    Parameters

    • value: any

      Document to add

    • Optional cb: ValueCallback<string>

      Optional callback

    Returns string

  • Adds a document to the collection, under an id subpath corresponding to either the document's id property if present, or else a new randomly generated id.

    If a callback is provided, it's called when the write is committed or fails.

    Parameters

    • subpath: Path

      Optional Collection under which to add the document

    • value: any

      Document to add

    • Optional cb: ValueCallback<string>

      Optional callback

    Returns string

  • Parameters

    • event: string
    • listener: any
    • Optional arg2: any
    • Optional arg3: any

    Returns any

  • Parameters

    • value: any

    Returns Promise<string>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<string>

  • Returns a child model where ShareDB operations are always composed.

    Returns ChildModel<T>

  • Returns a ChildModel scoped to the root path.

    Returns ChildModel<T>

  • Returns a ChildModel scoped to a relative subpath under this model's path.

    Type Parameters

    • S = unknown

      type of data at subpath

    Parameters

    Returns ChildModel<S>

  • Parameters

    • cb: Object

    Returns void

  • Returns Promise<void>

  • Parameters

    • Optional cb: Object

    Returns void

  • Returns void

  • Creates a new child model with a specific named data-loading context. The child model has the same scoped path as this model.

    Contexts are used to track counts of fetches and subscribes, so that all data relating to a context can be unloaded all at once, without having to manually track loaded data.

    Contexts are in a global namespace for each root model, so calling model.context(contextId) from two different places will return child models that both refer to the same context.

    Parameters

    • contextId: string

      context id

    Returns ChildModel<T>

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    • backend: any
    • Optional req: any

    Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    • value: any

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<void>

  • Parameters

    • value: any

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<void>

  • Deletes the value at this relative subpath.

    If a callback is provided, it's called when the write is committed or fails.

    Type Parameters

    • S

      Type of the data returned by delete operation

    Parameters

    Returns S

    the old value at the path

  • Deletes the value at this model's path.

    If a callback is provided, it's called when the write is committed or fails.

    Type Parameters

    • T

      Type of the data returned by delete operation

    Parameters

    Returns T

    the old value at the path

  • Deletes the value at this relative subpath. If not provided deletes at model path

    Promise resolves when commit succeeds, rejected on commit failure.

    Parameters

    • Optional subpath: Path

      Optional subpath to delete

    Returns Promise<void>

    promise

  • Parameters

    Returns Segments

  • Parameters

    • Optional subpath: Path

    Returns void

  • Returns void

  • Call the function with the values at the input paths, returning the value on completion. Unlike start, this only occurs once and does not create a listener for updating based on changes.

    The function should be a pure function - it should always return the same result given the same inputs, and it should be side-effect free.

    Type Parameters

    • Out
    • Ins extends unknown[]

    Parameters

    Returns Out

  • Type Parameters

    • Out
    • Ins extends unknown[]

    Parameters

    Returns Out

  • Parameters

    • id: string

    Returns ChildModel<T>

  • Retrieve data from the server, loading it into the model.

    Parameters

    • collecitonName: string

      Name of colleciton to load item to

    • id: string

      Id of doc to load

    • Optional callback: ErrorCallback

      Callback called when operation completed

    Returns void

  • Promised version of Model.fetchDoc. Instead of a callback, returns a promise that is resolved when operation completed

    Parameters

    • collecitonName: string

      Name of colleciton to load item to

    • id: string

      Id of doc to load

    Returns Promise<void>

  • Promised version of Model.fetch. Instead of a callback, returns a promise that is resolved when operation completed

    Parameters

    Returns Promise<void>

  • Promised version of Model.fetch. Instead of a callback, returns a promise that is resolved when operation completed

    Parameters

    Returns Promise<void>

  • Promised version of Model.fetch. Instead of a callback, returns a promise that is resolved when operation completed

    Returns Promise<void>

  • Creates a live-updating list from items in an object, which results in automatically updating as the input items change.

    Type Parameters

    • S

    Parameters

    • inputPath: PathLike

      Path pointing to an object or array. The path's value is retrieved via model.get(), and each item checked against filter function

    • additionalInputPaths: PathLike[]

      Other parameters can be set in the model, and the filter function will be re-evaluated when these parameters change as well.

    • options: PaginationOptions

      skip - The number of first results to skip limit - The maximum number of results. A limit of zero is equivalent to no limit.

    • fn: FilterFn<S>

      A function or the name of a function defined via model.fn(). The function should have the arguments function(item, key, object, additionalInputs...)

    Returns Filter<S>

  • Type Parameters

    • S

    Parameters

    Returns Filter<S>

  • Type Parameters

    • S

    Parameters

    Returns Filter<S>

  • Type Parameters

    • S

    Parameters

    Returns Filter<S>

  • Defines a named reactive function.

    It's not recommended to use this in most cases. Instead, to share reactive functions, have the components import a shared function to pass to model.start.

    Type Parameters

    • Ins extends unknown[]
    • Out

    Parameters

    • name: string

      name of the function to define

    • fn: ModelFn<Ins, Out>

      either a reactive function that accepts inputs and returns output, or a { get: Function; set: Function } object defining a two-way reactive function

    Returns void

    Deprecated

    The use of named functions is deprecated. Instead, to share a reactive function, you should export it and then require/import it into each file that needs to use it.

  • Gets the value located at this model's path.

    If no value exists at the path, this returns undefined.

    Note: The value is returned by reference, and object values should not be directly modified - use the Model mutator methods instead. The TypeScript compiler will enforce no direct modifications, but there are no runtime guards, which means JavaScript source code could still improperly make direct modifications.

    Returns ReadonlyDeep<T>

  • Gets the value located at a relative subpath.

    If no value exists at the path, this returns undefined.

    Note: The value is returned by reference, and object values should not be directly modified - use the Model mutator methods instead. The TypeScript compiler will enforce no direct modifications, but there are no runtime guards, which means JavaScript source code could still improperly make direct modifications.

    Type Parameters

    • S

    Parameters

    • Optional subpath: Path

    Returns ReadonlyDeep<S>

  • Returns a reference to the ShareDB agent if it is connected directly on the server. Will return null if the ShareDB connection has been disconnected or if we are not in the same process and we do not have a reference to the server-side agent object

    Returns Agent<any>

  • Parameters

    • collectionName: string

    Returns Collection<JSONObject>

  • Gets a shallow copy of the value located at this model's path or a relative subpath.

    If no value exists at the path, this returns undefined.

    Type Parameters

    • S

    Parameters

    Returns ShallowCopiedValue<S>

  • Returns ShallowCopiedValue<T>

  • Gets a deep copy of the value located at this model's path or a relative subpath.

    If no value exists at the path, this returns undefined.

    Type Parameters

    • S

    Parameters

    Returns S

  • Returns T

  • Parameters

    • collecitonName: string
    • id: string

    Returns any

  • Parameters

    • name: string

    Returns Collection<{}>

  • Get the named context or create a new named context if it doesnt exist.

    Parameters

    • contextId: string

      context id

    Returns Context

  • Parameters

    • collectionName: string
    • id: string
    • data: any

    Returns any

  • Gets value at the path if not nullish, otherwise returns provided default value

    Type Parameters

    • S

    Parameters

    • subpath: Path
    • defaultValue: S

      value to return if no value at subpath

    Returns ReadonlyDeep<S>

  • Gets the value located at this model's path or a relative subpath.

    If no value exists at the path, or the value is nullish (null or undefined), this will throw an error.

    Type Parameters

    • S

    Parameters

    Returns ReadonlyDeep<S>

  • Gets array of values of collection at this model's path or relative subpath

    If no values exist at subpath, an empty array is returned

    Type Parameters

    • S

    Parameters

    • Optional subpath: Path

    Returns ReadonlyDeep<S>[]

  • Returns boolean

  • Returns boolean

  • Creates a new Racer UUID

    Returns string

    a new Racer UUID.

  • Parameters

    • Optional value: number

    Returns number

  • Parameters

    Returns number

  • Parameters

    • Optional value: number

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • Optional value: number

    Returns Promise<void>

  • Parameters

    • index: number
    • value: any

    Returns void

  • Parameters

    Returns void

  • Parameters

    • value: any
    • index: number

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • index: number
    • value: any

    Returns Promise<void>

  • Check if subpath is a PathLike

    Parameters

    Returns boolean

    boolean

  • Parameters

    • path: string

    Returns string

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    • from: number
    • to: number

    Returns Promise<void>

  • Parameters

    • from: number
    • to: number
    • howMany: number

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • from: number
    • to: number

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • from: number
    • to: number
    • howmany: number

    Returns Promise<void>

  • Listen to Racer events matching a certain path or path pattern.

    pathPattern is a path pattern that will filter emitted events, calling the handler function only when a mutator matches the pattern.

    Path patterns support a single segment wildcard '*' anywhere in a path, and a multi-segment wildcard '**' at the end of the path. The multi-segment wildcard alone '**' matches all paths.

    Examples of path patterns:

    • 'notes.abc-123.author' - Trigger on a direct modification to a specific note's author. Will not trigger if a sub-property of the author is modified or if the entire note is replaced.
    • notes.*.author - Trigger on a direct modification to any note's author. Will not trigger if a sub-property of the author is modified or if an entire note is replaced.
    • notes.*.author.** - Trigger on a modification to any note's author or any sub-property of author. Will not trigger if an entire note is replaced.

    Type Parameters

    • T extends "remove" | "move" | "change" | "load" | "unload" | "insert"

    Parameters

    • eventType: T
    • pathPattern: PathLike
    • options: Object
    • listener: Object

    Returns Object

  • Parameters

    • eventType: "all"
    • pathPattern: PathLike
    • options: Object
    • listener: Object

    Returns Object

  • Type Parameters

    • T extends "remove" | "move" | "change" | "load" | "unload" | "insert"

    Parameters

    • eventType: T
    • options: Object
    • listener: Object

    Returns Object

  • Parameters

    • eventType: "all"
    • options: Object
    • listener: Object

    Returns Object

  • Listen to Racer events matching a certain path or path pattern, removing the listener after it gets triggered once.

    Type Parameters

    • T extends "remove" | "move" | "change" | "load" | "unload" | "insert"

    Parameters

    • eventType: T
    • pathPattern: string
    • options: Object
    • listener: Object

    Returns Function

  • Type Parameters

    • T extends "remove" | "move" | "change" | "load" | "unload" | "insert"

    Parameters

    • eventType: T
    • options: Object
    • listener: Object

    Returns Function

  • Get the parent {levels} up from current model or root model

    Parameters

    • Optional levels: number

      number of levels to traverse the tree

    Returns Model<unknown>

    parent or root model

  • Passes data to event listeners

    Parameters

    • object: object

      An object whose properties will each be set on the passed argument

    • Optional invert: boolean

    Returns Model<T>

    back a model scoped to the same path

  • Get full path to given subpath

    Parameters

    • Optional subpath: PathLike

      PathLike subpath

    Returns string

  • Removes an item from the end of the array at this model's path or a relative subpath.

    If a callback is provided, it's called when the write is committed or fails.

    Type Parameters

    • V

      type of data at subpath

    Parameters

    Returns V

    the removed item

  • Type Parameters

    • V extends unknown

    Parameters

    Returns V

  • Parameters

    • value: any

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<void>

  • Returns a child model where ShareDB operations are never composed.

    Returns ChildModel<T>

  • Push a value to a model array

    Parameters

    • value: any

    Returns number

    the length of the array

  • Push a value to a model array at subpath

    Parameters

    Returns number

    the length of the array

  • Parameters

    • value: any

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<void>

  • Type Parameters

    • T = {}

    Parameters

    • collectionName: string
    • expression: any
    • Optional options: QueryOptions

    Returns Query<T>

  • Returns void

  • Creates a reference for this model pointing to another path to. Like a symlink, any reads/writes on this to ref will work as if they were done on path directly.

    Type Parameters

    • S

    Parameters

    • to: Refable

      Location that the reference points to

    Returns ChildModel<S>

    a model scoped to path

  • Creates a reference at path pointing to another path to. Like a symlink, any reads/writes on path will work as if they were done on path directly.

    Type Parameters

    • S

    Parameters

    • path: PathLike

      Location at which to create the reference. This must be under a local collection, typically '_page' or a component model.

    • to: Refable

      Location that the reference points to

    • Optional options: RefOptions

    Returns ChildModel<S>

    a model scoped to path

  • Parameters

    Returns RefList

  • Parameters

    Returns RefList

  • Creates an array at outputPath that consists of references to all the objects at collectionPath that have ids matching the ids at idsPath. The array is automatically updated based on changes to the input paths.

    Type Parameters

    • S

    Parameters

    • outputPath: PathLike

      Path at which to create the ref list. This must be under a local collection, typically '_page' or a component model.

    • collectionPath: PathLike

      Path to a Racer collection or a collection-like object, where each id string key maps to an object value with matching id property.

    • idsPath: PathLike

      Path to an array of string ids

    • Optional options: RefListOptions

      Optional

    Returns ChildModel<S>

  • Removes one or more items from the array at this model's path or a relative subpath.

    If a callback is provided, it's called when the write is committed or fails.

    Type Parameters

    • V

    Parameters

    • subpath: Path

      Subpath to remove

    • index: number

      0-based index at which to start removing items

    • Optional howMany: number

      Number of items to remove. Defaults to 1.

    • Optional cb: Callback

    Returns V[]

    array of the removed items

  • Type Parameters

    • V extends unknown

    Parameters

    • index: number
    • Optional howMany: number
    • Optional cb: Callback

    Returns V[]

  • Parameters

    • type: string
    • subpath: Path

    Returns void

  • Parameters

    Returns void

  • Returns void

  • Parameters

    • eventType: "all" | "remove" | "move" | "change" | "load" | "unload" | "insert" | "changeImmediate" | "insertImmediate" | "removeImmediate" | "moveImmediate" | "loadImmediate" | "unloadImmediate"
    • listener: Function

    Returns void

  • Parameters

    • index: number

    Returns Promise<void>

  • Parameters

    Returns Promise<void>

  • Parameters

    • index: number
    • howMany: number

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • index: number

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • index: number
    • howMany: number

    Returns void

  • Parameters

    • expression: any

    Returns any

  • Returns a ChildModel scoped to the root path.

    Returns ChildModel<ModelData>

    ChildModel

  • Returns a ChildModel scoped to an absolute path.

    Type Parameters

    • S = unknown

      Type of data at subpath

    Parameters

    • subpath: Path

      Path of GhildModel to scope to

    Returns ChildModel<S>

    ChildModel

  • Parameters

    Returns T

  • Type Parameters

    • S

    Parameters

    Returns S

  • Sets the array value at this model's path or a relative subpath, based on a strict equality comparison (===) between array items.

    This only issues array insert, remove, and move operations.

    If a callback is provided, it's called when the write is committed or fails.

    Type Parameters

    • S extends any[]

    Parameters

    Returns S

    the value previously at the path

  • Type Parameters

    • S extends any[]

    Parameters

    • value: S

    Returns S

  • Sets the array value at this model's path or a relative subpath, based on a deep equality comparison between array items.

    This only issues array insert, remove, and move operations. Unlike setDiffDeep, this will never issue fine-grained ops inside of array items.

    If a callback is provided, it's called when the write is committed or fails.

    Type Parameters

    • S extends any[]

    Parameters

    Returns S

    the value previously at the path

  • Type Parameters

    • S extends any[]

    Parameters

    • value: S

    Returns S

  • Type Parameters

    • S extends any[]

    Parameters

    Returns Promise<void>

  • Type Parameters

    • S extends any[]

    Parameters

    Returns Promise<void>

  • Set the named context to use for this model.

    Parameters

    • contextId: string

      context id

    Returns void

  • Sets the value at this model's path or a relative subpath, if different from the current value based on a strict equality comparison (===).

    If a callback is provided, it's called when the write is committed or fails.

    Type Parameters

    • S

    Parameters

    Returns ReadonlyDeep<S>

    the value previously at the path

  • Parameters

    • value: T

    Returns ReadonlyDeep<T>

  • Sets the value at this model's path or a relative subpath, if different from the current value based on a recursive deep equal comparison.

    This attempts to issue fine-grained ops on subpaths if possible.

    If a callback is provided, it's called when the write is committed or fails.

    Type Parameters

    • S

    Parameters

    Returns ReadonlyDeep<S>

    the value previously at the path

  • Parameters

    • value: T

    Returns ReadonlyDeep<T>

  • Type Parameters

    • S

    Parameters

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<void>

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    • value: any

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<void>

  • Parameters

    • limit: number

    Returns void

  • Parameters

    Returns T

  • Type Parameters

    • S

    Parameters

    Returns S

  • Parameters

    • value: T

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<void>

  • Parameters

    • value: T

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<void>

  • Type Parameters

    • S

    Parameters

    Returns S

  • Parameters

    • Optional subpath: Path

    Returns Promise<void>

  • Parameters

    • Optional value: boolean

    Returns Model<T>

  • Creates a live-updating list from items in an object, which results in automatically updating as the input items change. The results are sorted by ascending order (default) or by a provided 'fn' parameter.

    Type Parameters

    • S

    Parameters

    • inputPath: PathLike

      Path pointing to an object or array. The path's value is retrieved via model.get(), and each item checked against filter function

    • additionalInputPaths: PathLike[]

      Other parameters can be set in the model, and the filter function will be re-evaluated when these parameters change as well.

    • options: PaginationOptions

      skip - The number of first results to skip limit - The maximum number of results. A limit of zero is equivalent to no limit.

    • fn: SortFn<S>

      A function or the name of a function defined via model.fn().

    Returns Filter<S>

  • Type Parameters

    • S

    Parameters

    Returns Filter<S>

  • Type Parameters

    • S

    Parameters

    Returns Filter<S>

  • Type Parameters

    • S

    Parameters

    Returns Filter<S>

  • Call the function with the values at the input paths, writing the return value to the output path. In addition, whenever any of the input values change, re-invoke the function and set the new return value to the output path.

    The function should be a pure function - it should always return the same result given the same inputs, and it should be side-effect free.

    Type Parameters

    • Out
    • Ins extends unknown[]

    Parameters

    • outputPath: PathLike
    • inputPaths: PathLike[]
    • options: ModelStartOptions
    • fn: string | ModelFn<Ins, Out>

      a reactive function that accepts inputs and returns output; a { get: Function; set: Function } object defining a two-way reactive function; or the name of a function defined via model.fn()

    Returns Out

  • Type Parameters

    • Out
    • Ins extends unknown[]

    Parameters

    Returns Out

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    • index: number
    • text: string

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • index: number
    • text: string

    Returns Promise<void>

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    • index: number
    • howMany: number

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • index: number

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • index: number
    • howMany: number

    Returns Promise<void>

  • Retrieve data from the server, loading it into the model. In addition, subscribe to the items, such that updates from any other client will automatically get reflected in this client's model.

    Any item that's already subscribed will not result in a network call.

    Parameters

    Returns Model<T>

  • Retrieve data from the server, loading it into the model. In addition, subscribe to the items, such that updates from any other client will automatically get reflected in this client's model.

    Any item that's already subscribed will not result in a network call.

    Parameters

    Returns Model<T>

  • Retrieve data from the server, loading it into the model. In addition, subscribe to the items, such that updates from any other client will automatically get reflected in this client's model.

    Any item that's already subscribed will not result in a network call.

    Parameters

    • Optional cb: ErrorCallback

      Callback called when operation completed

    Returns Model<T>

  • Parameters

    Returns void

  • Parameters

    • collecitonName: string
    • id: string

    Returns Promise<void>

  • Promised version of Model.subscribe. Instead of a callback, returns a promise that is resolved when operation completed

    Parameters

    Returns Promise<void>

  • Promised version of Model.subscribe. Instead of a callback, returns a promise that is resolved when operation completed

    Parameters

    Returns Promise<void>

  • Promised version of Model.subscribe. Instead of a callback, returns a promise that is resolved when operation completed

    Returns Promise<void>

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    • subtype: any
    • subtypeOp: any

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • subtype: any
    • subtypeOp: any

    Returns Promise<void>

  • Unfetch a document give collection name and document id

    Parameters

    • collectionName: string

      Collection name

    • id: string

      Document id to be unfeched

    • Optional callback: Object

    Returns void

  • Promised Model.unfetchDoc. Instead of taking a callback, returns a promise that is resolved when operation completed

    Parameters

    • collectionName: string

      Collection name

    • id: string

      Document id to be unfeched

    Returns Promise<void>

    Promise

  • Promised unfetch. See Model.unfetch. Instead of a callback, returns a promise that is resolved when operation completed

    Parameters

    Returns Promise<void>

    Promise

  • Promised unfetch. See Model.unfetch. Instead of a callback, returns a promise that is resolved when operation completed

    Parameters

    Returns Promise<void>

    Promise

  • Promised Model.unfetch. Instead of taking a callback, returns a promise that is resolved when operation completed

    Returns Promise<void>

    Promise

  • Unloads data for this model's context, or for a specific named context.

    Parameters

    • Optional contextId: string

      optional context to unload; defaults to this model's context

    Returns void

  • Parameters

    • value: any

    Returns void

  • Parameters

    Returns void

  • Parameters

    • value: any

    Returns Promise<void>

  • Parameters

    • subpath: Path
    • value: any

    Returns Promise<void>

  • Unsubscribe document by collection name and id

    Parameters

    • collectionName: string

      Name of collection containting document

    • id: string

      Document id to unsubscribe

    • Optional callback: Object

      Optional Called after operation completed

    Returns void

  • Promised version of Model.unsubscribeDoc

    Parameters

    • collectionName: string

      Name of collection containting document

    • id: string

      Document id to unsbscribe

    Returns Promise<void>

  • Promised version of Model.unsubscribe. Instead of taking a callback, returns a promise that is resolved when operation completed

    Returns Promise<void>

  • Calls the callback once all pending operations, fetches, and subscribes have settled.

    Parameters

    • cb: Object

    Returns void

  • Returns Promise<void>