Skip to content

Transactions #1

@aral

Description

@aral

A transaction is a series of updates on a table that are either all persisted together or are not persisted at all. They enable a table to have atomicity.

In JSDB, we write out our tables in JavaScript Data Format (JSDF), which is a transaction log in pure JavaScript. (Transaction log, in this case – confusingly – refers to a single update, not an atomic batch update).

To implement transactions (atomicity for batch changes) in JSDF, we can wrap multiple changes into a single-line immediately-invoked function expression (IIFE) and add that to our append-only log.

e.g.,

// ↑ other statements
(function () { _[0].name = 'Aral'; _[0].age = 43; })()

Without a transaction, the changes would be written out like this instead:

// ↑ other statements
_[0].name = 'Aral';
_[0].age = 43;

In keeping with as transparent an API as possible, here is one way we can implement this functionality:

db.people[0].__transaction-1__.name = 'Aral'
db.people[0].__transaction-1__.age = 43
await db.people.__transaction-1__.persist()

console.log('Transaction persisted.')

At this point we can be reasonably certain that transaction-1 has persisted to disk.

Note 1: transaction-1 is an arbitrary name, you can use any string that’s a valid identifier but it must start and end with two underscores and you must use the same identifier for any change that you want to include in your transaction.

Note 2: atomic transactions will only be implemented at the table-level (not the database-level, which would be a far messier undertaking and can be avoided by designing your tables accordingly).


Mirrors internal issue: #1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions