Options
All
  • Public
  • Public/Protected
  • All
Menu

The transaction write API object. It unions a set of functions (set, update and remove) that are similar to regular set, update and remove with the only difference that the transaction counterparts will retry writes if the state of data received with get would change.

Type parameters

  • ReadResult

Hierarchy

  • TransactionWrite

Index

Properties

Methods

Properties

data

data: ReadResult

The result of the read function.

Methods

remove

  • remove<Model>(collection: Collection<Model>, id: string): Promise<void>
  • remove<Model>(ref: Ref<Model>): Promise<void>
  • Removes a document.

    import { transaction, field, collection } from 'typesaurus'
    
    type Counter = { count: number }
    const counters = collection<Counter>('counters')
    
    transaction(async ({ get, remove }) => {
      const counter = await get('420')
      if (counter === 420) await remove(counter.ref)
    })
    transaction(
      ({ get }) => get('420'),
      ({ data: counter, remove }) => {
        console.log(counter.data.count)
        return remove(counter.ref)
      }
    )

    Type parameters

    • Model

    Parameters

    • collection: Collection<Model>

      The collection to remove document in

    • id: string

      The id of the documented to remove

    Returns Promise<void>

    Promise that resolves when the operation is complete.

  • Type parameters

    • Model

    Parameters

    • ref: Ref<Model>

      The reference to the document to remove

    Returns Promise<void>

set

  • set<Model>(ref: Ref<Model>, data: SetModel<Model>): Promise<void>
  • set<Model>(collection: Collection<Model>, id: string, data: SetModel<Model>): Promise<void>
  • Sets a document to the given data.

    import { transaction, collection } from 'typesaurus'
    
    type Counter = { count: number }
    const counters = collection<Counter>('counters')
    
    transaction(
      ({ get }) => get('420'),
      ({ data: counter, set }) =>
        set(counter.ref, { count: counter.data.count + 1 })
    )

    Type parameters

    • Model

    Parameters

    • ref: Ref<Model>

      the reference to the document to set

    • data: SetModel<Model>

      the document data

    Returns Promise<void>

  • Type parameters

    • Model

    Parameters

    • collection: Collection<Model>

      the collection to set document in

    • id: string

      the id of the document to set

    • data: SetModel<Model>

      the document data

    Returns Promise<void>

update

  • update<Model>(collection: Collection<Model>, id: string, data: Field<Model>[]): Promise<void>
  • update<Model>(ref: Ref<Model>, data: Field<Model>[]): Promise<void>
  • update<Model>(collection: Collection<Model>, id: string, data: UpdateModel<Model>): Promise<void>
  • update<Model>(ref: Ref<Model>, data: UpdateModel<Model>): Promise<void>
  • Updates a document.

    import { transaction, field, collection } from 'typesaurus'
    
    type Counter = { count: number }
    const counters = collection<Counter>('counters')
    
    transaction(
      ({ get }) => get('420'),
      ({ data: counter, update }) =>
        update(counter.ref, { count: counter.data.count + 1 })
      //=> { __type__: 'doc', data: { count: 43 }, ... }
    )
    
    // ...or using field paths:
    transaction(
      ({ get }) => get('420'),
      ({ data: counter, update }) =>
        update(counter.ref, [field('count', counter.data.count + 1)])
    )

    Type parameters

    • Model

    Parameters

    • collection: Collection<Model>

      the collection to update document in

    • id: string

      the id of the document to update

    • data: Field<Model>[]

      the document data to update

    Returns Promise<void>

    A promise that resolves when operation is finished

  • Type parameters

    • Model

    Parameters

    • ref: Ref<Model>

      the reference to the document to set

    • data: Field<Model>[]

      the document data to update

    Returns Promise<void>

  • Type parameters

    • Model

    Parameters

    • collection: Collection<Model>

      the collection to update document in

    • id: string

      the id of the document to update

    • data: UpdateModel<Model>

      the document data to update

    Returns Promise<void>

  • Type parameters

    • Model

    Parameters

    • ref: Ref<Model>

      the reference to the document to set

    • data: UpdateModel<Model>

      the document data to update

    Returns Promise<void>

upset

  • upset<Model>(ref: Ref<Model>, data: UpsetModel<Model>): Promise<void>
  • upset<Model>(collection: Collection<Model>, id: string, data: UpsetModel<Model>): Promise<void>
  • Sets or updates a document with the given data.

    import { transaction, collection } from 'typesaurus'
    
    type Counter = { count: number }
    const counters = collection<Counter>('counters')
    
    transaction(
      ({ get }) => get('420'),
      ({ data: counter, upset }) =>
        upset(counter.ref, { count: counter.data.count + 1 })
    )

    Type parameters

    • Model

    Parameters

    • ref: Ref<Model>

      the reference to the document to set or update

    • data: UpsetModel<Model>

      the document data

    Returns Promise<void>

  • Type parameters

    • Model

    Parameters

    • collection: Collection<Model>

      the collection to set document in

    • id: string

      the id of the document to set

    • data: UpsetModel<Model>

      the document data

    Returns Promise<void>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc