Loopback-next: Docs: How to create a transaction and pass it around through various repo CRUD operations

Created on 19 Mar 2019  路  1Comment  路  Source: strongloop/loopback-next

Description / Steps to reproduce / Feature proposal

Cross posting my comments:

Talked to @bajtos @raymondfeng, what we should do regarding transaction support:

  • shorter term: explain to users how to open a transaction and pass the transaction token/handles around in options.
  • longer term: have a spike on a more elegant solution.

There are different workarounds that our users have posted:

Loopback 3 provides transaction support at model level.

In LoopBack 4 this should be at the Repository level.

There will be sugar API in a repository to delegate the work to the datasource level.

//obtain a transaction object with  repo.beginTransaction()
let transaction: Transaction =
await customerRepository.beginTransaction({isolationLevel: Post.Transaction.READ_COMMITTED});

// isolationLevel values:
// Transaction.READ_UNCOMMITTED
// Transaction.READ_COMMITTED (default)
// Transaction.REPEATABLE_READ
// Transaction.SERIALIZABLE

//pass the transaction inside the existing options object
await customerRepository.create( customer, { transaction} ) ;

//pass the transaction inside the existing options object
await orderRepository.create( order, { transaction} ); 

// the two repositories above (customer and order) have the same data source, 
// so they can share the transaction.

// finish working with the transaction, either with commit or rollback
await transaction.commit();//  ( OR await transaction.rollback() )

The Transaction type mentioned above should be a wrapper for the typescript type of the transaction object in Juggler.

When using the juggler bridge, we need to make sure that the options we set in the wrapper type are consistent with what juggler is expecting.

Since LoopBack 4 doesn't support a distributed transaction, all repos using the transaction must be from the same data source. It is the job of wrapper Transaction to throw an exception if user attempts to use a transaction against disparate data sources.

DefaultCRUDRepository will implement an additional interface to handle transactions.

Acceptance criteria

  • [x] Only covers relational databases (NoSQL databases are out of scope)
  • [x] Document relational database transaction support in LoopBack 4

    • [x] Transaction wrapper type

    • [x] How to create a transaction using repo.beginTransaction() and isolation level argument

    • [x] How to pass transaction object into the options object of various repository CRUD methods

    • [x] How to terminate a transaction via commit() or rollback()

  • [x] Create mocha test case for 1 SQL relational database (juggler tests have exhaustive transaction tests already)
2019Q3 Docs Slipped p1

Most helpful comment

loopback4-spring arising from Issue #1599 is a good example of using decorator style to perform transaction.

>All comments

loopback4-spring arising from Issue #1599 is a good example of using decorator style to perform transaction.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

acrodrig picture acrodrig  路  3Comments

mhdawson picture mhdawson  路  3Comments

frodoe7 picture frodoe7  路  3Comments

teambitcodeGIT picture teambitcodeGIT  路  3Comments

half-blood-programmer picture half-blood-programmer  路  3Comments