@harlem/extension-transaction

The official transaction extension for Harlem

Usage no npm install needed!

<script type="module">
  import harlemExtensionTransaction from 'https://cdn.skypack.dev/@harlem/extension-transaction';
</script>

README

Harlem Transaction Extension

npm

This is the official transaction extension for Harlem. The transaction extension adds the ability to rollback a set of mutations in the event of an error.

Getting Started

Follow the steps below to get started using the transaction extension.

Installation

Before installing this extension make sure you have installed @harlem/core.

yarn add @harlem/extension-transaction
# or
npm install @harlem/extension-transaction

Registration

To get started simply register this extension with the store you wish to extend.

import transactionExtension from '@harlem/extension-transaction';

import {
    createStore
} from '@harlem/core';

const STATE = {
    firstName: 'Jane',
    lastName: 'Smith'
};

const {
    state,
    getter,
    mutation,
    transaction,
    onBeforeTransaction,
    onAfterTransaction,
    onTransactionSuccess,
    onTransactionError,
} = createStore('example', STATE, {
    extensions: [
        transactionExtension()
    ]
});

The transaction extension adds several new methods to the store instance (highlighted above).

Usage

Defining a transaction

A transaction can be defined the same way you define any other core functionality (eg. getter, mutation etc.).

export default transaction('my-transaction', (payload, mutate) => {
    /*
    Any mutations run during this method
    will be rolled back if an error occurs
    */
});

Considerations

  • This extension comes with a performance cost. The entire state tree is cloned and stored before a transaction is run in order to rollback in the event of an error.
  • This extension does not protect against issues attempting to roll back state in async scenarios