Skip to main content
Version: 2.9

Transaction

Transactions are the unique way to apply a list of operations to the datacloud. A transaction can be merged into another transaction so that both are executed as a single set of operations.

All the operations for a transaction are executed atomically. Either all operations succeed, or none is applied.

There are 5 types of basic operations :

  • Create instance: create a node in the database with specified properties.
  • Update instance property: update an existing node in the database.
  • Delete instance: delete a node from the database. Delete only works if all the relations are removed too. Cascade delete can be activated.
  • Create relation: create a relation of a specific type between 2 existing node.
  • Delete relation: remove the specified relation between 2 specified nodes.

In most cases, we apply the Transactions operations using the “execute()” method. The transaction size is limited but in real-time. It notifies observers of the data in real-time.

Larger transactions with big set of operations (batch updates) must be executed using “executeAsLarge()” method. However, no notification will be generated.

Callbacks can be registered on transaction using “afterExecution()” method. They are executed once the transaction is applied.

Example of transaction:

const tx = new Transaction(true);
const myNewInstanceTag = tx.create(<myModelTag>);
tx.update(myNewInstanceTag, <myPropertyTag>, <myPropertyValue>)
.execute()
.then(() => {
// success tx case
})
.catch((err) => {
// error during tx case
})

Concurrency transactions do not guarantee their execution order.

Index

Constructors

constructor

  • Create a new transaction object.


    Parameters

    • optionalpersist: boolean

      if set to true, will persist to the data source the objects Transaction.create created in that transaction. (Default = true)

    Returns Transaction

Methods

afterExecution

  • afterExecution(callback: (success: boolean, message?: string) => void): Transaction
  • Set a callback to be executed after all the operations in the transaction were executed successfully


    Parameters

    • callback: (success: boolean, message?: string) => void

      the callback to be executed

    Returns Transaction

    this transaction

create

  • Create a new instance.

    A custom map can specify property values for the instance.

    A source can be the orchestrator ({PredefinedDataSource.SERVER}), local ({PredefinedDataSource.SELF}) or an external data source (tag of DBConnector). The source is where the object is persisted and its true value outside local scopes.


    Parameters

    • model: InstanceOrTag

      tag of the model of the instance to be created

    • optionalproperties: Map<InstanceOrTag, any>

      custom map from tag to their value for properties of the instance to be created

    • optionalsource: string

      optional source of the instance

    • optionaltag: string

      optional tag of the instance to be created and must be unique

    Returns string

    the tag of the instance to be created

createRelation

  • Create relation between two instances


    Parameters

    Returns Transaction

    this transaction with the create relation operation registered

delete

deleteAllRelations

  • Delete any number of the relation starting from specified node.

    The specified node might be the origin or (exclusive) the destination of the relation.

    For the relations :

    - a - [rel1] -> b,
    - c - [inverseRel(rel1)] -> a,
    - a - [rel1] -> d,
    - a - [rel2] -> d

    tx.deleteAllRelation(rel1, a) will remove the first and third relations


    Parameters

    • relation: Relation<any, any>

      deleted relation, indicates relation tag and direction

    • origin: InstanceOrTag

      starting node

    Returns Transaction

deleteRelation

  • Delete a relation between two specified instances.

    The relation is only deleted for the relation parameter direction.


    Parameters

    Returns Transaction

    this transaction

execute

  • Execute atomically the transaction at the data sources. Return the transaction result if succeeds.


    Returns Promise<TransactionResult>

    promise resolving with the corresponding TransactionResult if the transaction succeeds, rejecting if the transaction fails

executeAsLarge

  • Execute atomically a transaction at the data source and returns the transaction result if succeeds. The large mode sends the transaction over HTTP, this has the following incidence:

    • Quicker processing of transaction
    • Allows transactions with no limit of size
    • Cannot be used when logged as Guest

    Returns Promise<TransactionResult>

    promise resolving with the corresponding TransactionResult if the transaction succeeds, rejecting if the transaction fails.

getId

  • getId(): string
  • Get the transaction id attached to this transaction


    Returns string

    this transaction id

merge

  • Add all operations of another transaction into this transaction


    Parameters

    • otherTransaction: Transaction

      the other transaction to add operations from

    Returns Transaction

    this transaction

model

  • Retrieve the tag of the model of the instance tag from the created instances in this transaction or from the local database.


    Parameters

    Returns string

    the tag of the model of the given instance

multiUpdate

  • Update multiple properties of a single instance


    Parameters

    Returns Transaction

    this transaction

persist

  • Set the specified instance to be persisted or not in this transaction. Default value of persist is true.


    Parameters

    • instance: InstanceOrTag

      the instance to be persisted

    • optionalpersist: boolean

      determine if the specified instance should be persisted or not (true by default).

    Returns Transaction

    this transaction

persistInstance

  • Change the persistence mode for a single instance in this transaction

    @deprecated

    Parameters

    • instance: InstanceOrTag

      the instance to be persisted

    • optionalpersist: boolean

      the persisting mode

    Returns Transaction

    this transaction

setSource

  • Change the source of all instances created in this transaction

    source can be specified as :

    1. a predefined source type {PredefinedDataSource}
    2. an external data source (Tag of a DBConnector)

    The source of a data object is where the object is persisted.

    By default, the source is the default source configured in the project.


    Parameters

    • source: string

      the new source for instances

    Returns Transaction

    this transaction

update

  • Update the property of an instance


    Type parameters

    • T

    Parameters

    • instance: InstanceOrTag

      tag of the instance to be updated

    • optionalproperty: Property<T>

      the property to be updated

    • optionalvalue: T

      the value of the property to be updated to

    Returns Transaction

    this transaction

staticfrom

  • Start a transaction using an existing transaction in the provided context or a new one if there is none.


    Parameters

    Returns Transaction

    transaction

staticprocess

  • Execute a given transaction if there is no open transaction in the context.


    Parameters

    Returns Promise<boolean>

    boolean indicating if the argument transaction has been processed