node-ottoman icon indicating copy to clipboard operation
node-ottoman copied to clipboard

Transaction support proposal

Open gsi-alejandro opened this issue 2 years ago • 4 comments

Ref: CBSE-14946

Proposal

Steps:

  1. Add $transaction method to the ottoman instance.
  2. Update existing methods to support transactions (the proposal will not include any breaking change)
  3. Add a test suit for the transaction feature.
  4. Update the documentation website with the new transaction content.
ottoman.$transaction((ctx) => {

  try {	

    Model.create(doc, { transactionContext: ctx });

    Model.removeById(documentId, { transactionContext: ctx });

    Model.findById(documentId, { transactionContext: ctx });

  } catch (error) {
    if (error instanceof TransactionFailedError) {
      console.error('Transaction did not reach commit point', error)
    }

    if (error instanceof TransactionCommitAmbiguousError) {
      console.error('Transaction possibly committed', error)
    }
  }

});

Note: the way to support transactions will be by passing the ctx via options to the existing method.

Affected methods:

Ottoman:

  • query

Model:

  • count
  • find
  • findById
  • findOne
  • create
  • createMany
  • updateById
  • replaceById
  • updateMany
  • removeById
  • removeMany
  • findOneAndUpdate
  • query

Document:

  • remove
  • save

Notice: Bring support to refdoc indexes will be a little bit more complex, no API change is required it will be done under the hood, but you need to be aware of the work to do in order to achieve transactions if refdoc logic is triggered.

gsi-alejandro avatar Oct 24 '23 17:10 gsi-alejandro

Is there a strong motivation to use .$transaction instead of just .transaction?

ejscribner avatar Oct 24 '23 18:10 ejscribner

No, .$transaction is inspired by Prismajs's API, it's a clear way to highlight the section like an atomic operation (a different kind of section, a special one)

gsi-alejandro avatar Nov 01 '23 13:11 gsi-alejandro

@gsi-alejandro .$transactions will it be made available as an “interactive” transaction?

AV25242 avatar Nov 01 '23 14:11 AV25242

Is there a strong motivation to use .$transaction instead of just .transaction?

Yea I think we should us $.transaction @ejscribner . Also are we planning to make Ottoman available from edge environments ?

AV25242 avatar Nov 01 '23 14:11 AV25242