couchbaselabs / node-ottoman

Node.js ODM for Couchbase
https://ottomanjs.com/
Apache License 2.0
287 stars 98 forks source link

Transaction support proposal #741

Closed gsi-alejandro closed 2 months ago

gsi-alejandro commented 10 months ago

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:

Model:

Document:

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.

ejscribner commented 10 months ago

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

gsi-alejandro commented 10 months ago

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)

AV25242 commented 10 months ago

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

AV25242 commented 10 months ago

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 ?