Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace atomic

Index

Functions

createOrUpdate

  • createOrUpdate<T, TCtor>(partition: Partition<T, TCtor>, id: string, updateFn: (previous: InstanceType<TCtor> | undefined) => Thenable<InstanceType<TCtor>>, options?: IAtomicOptions<InstanceType<TCtor>>): Promise<InstanceType<TCtor>>
  • createOrUpdate<T, TCtor>(partition: Partition<T, TCtor>, id: string, updateFn: (previous: InstanceType<TCtor> | undefined) => Thenable<InstanceType<TCtor> | undefined>, options?: IAtomicOptions<InstanceType<TCtor>>): Promise<InstanceType<TCtor> | undefined>
  • Creates or updates a model using the given function. The function will be retried automaticaly in case a conflict happens, so could be called multiple times. This similar to an "upsert" operation.

    For example, this will create a new PageView instance if it doesn't exist, or update the number of views if it does:

    await atomic.createOrUpdate(PageViews.partition(userId), pageId, async existing => {
      if (!existing) {
        existing.views++;
        return existing;
      } else {
        return new PageView({ id: pageId, userId, views: 1 })
      }
    });
    
    throws

    Cosmos.ErrorResponse if the IAtomicOptions.retries are exhausted.

    Type parameters

    • T: { id: string }

    • TCtor: ModelConstructor<T, TCtor>

    Parameters

    • partition: Partition<T, TCtor>
    • id: string

      ID of the model to create or update

    • updateFn: (previous: InstanceType<TCtor> | undefined) => Thenable<InstanceType<TCtor>>

      Function called to update the model. Should return the model after making modifications to it.

        • (previous: InstanceType<TCtor> | undefined): Thenable<InstanceType<TCtor>>
        • Parameters

          • previous: InstanceType<TCtor> | undefined

          Returns Thenable<InstanceType<TCtor>>

    • Optional options: IAtomicOptions<InstanceType<TCtor>>

    Returns Promise<InstanceType<TCtor>>

  • Creates or updates a model using the given function. The function will be retried automaticaly in case a conflict happens, so could be called multiple times.

    You can return the AbortUpdate symbol to cancel the operation and return nothing.

    Type parameters

    • T: { id: string }

    • TCtor: ModelConstructor<T, TCtor>

    Parameters

    • partition: Partition<T, TCtor>
    • id: string

      ID of the model to create or update

    • updateFn: (previous: InstanceType<TCtor> | undefined) => Thenable<InstanceType<TCtor> | undefined>

      Function called to update the model. Should return the model after making modifications to it.

        • (previous: InstanceType<TCtor> | undefined): Thenable<InstanceType<TCtor> | undefined>
        • Parameters

          • previous: InstanceType<TCtor> | undefined

          Returns Thenable<InstanceType<TCtor> | undefined>

    • Optional options: IAtomicOptions<InstanceType<TCtor>>

      Call options

    Returns Promise<InstanceType<TCtor> | undefined>

findOrCreate

  • findOrCreate<T, TCtor>(partition: Partition<T, TCtor>, id: string, createOrModel: InstanceType<TCtor> | (() => Thenable<InstanceType<TCtor>>), options?: IAtomicOptions<InstanceType<TCtor>>): Promise<InstanceType<TCtor>>
  • Finds the model identified by the partition ID, or creates it if it does not exist. You may pass in an instance of a model, or a function that creates an instance of the model.

    throws

    Cosmos.ErrorResponse if the IAtomicOptions.retries are exhausted.

    Type parameters

    • T: { id: string }

    • TCtor: ModelConstructor<T, TCtor>

    Parameters

    • partition: Partition<T, TCtor>
    • id: string
    • createOrModel: InstanceType<TCtor> | (() => Thenable<InstanceType<TCtor>>)
    • Optional options: IAtomicOptions<InstanceType<TCtor>>

    Returns Promise<InstanceType<TCtor>>

update

  • update<M>(model: M, updateFn: (previous: M) => Thenable<M | undefined>, options?: IAtomicOptions<never>, container?: Container): Promise<M>
  • Updates a model using the given function. The function will be retried automaticaly in case a conflict happens, so could be called multiple times.

    Note that when calling this, the properties that were previously on the model may be re-set if the model is reread from the database as a result of a conflict. Therefore, you should make sure to have all mutations happen inside the updateFn method, not before it.

    The update function should return the model when it's ready run the update, or undefined to cancel the operation.

    For example, this will atomically update the number of views on a Page document:

    await atomic.update(Page.partition(pageId), pageId, async existing => {
      page.views++;
      return page;
    });
    
    throws

    Cosmos.ErrorResponse if the document doesn't exist, or the IAtomicOptions.retries are exhausted.

    Type parameters

    Parameters

    • model: M
    • updateFn: (previous: M) => Thenable<M | undefined>

      Function called to update the model. Should return the model after making modifications to it.

        • (previous: M): Thenable<M | undefined>
        • Parameters

          • previous: M

          Returns Thenable<M | undefined>

    • Optional options: IAtomicOptions<never>

      Call options

    • container: Container = ...

    Returns Promise<M>

Generated using TypeDoc