Skip to content

Commit

Permalink
feat(transactions)!: transactions with automated rollback (#6)
Browse files Browse the repository at this point in the history
* test: add not to calledWith assertion

* fix: upgrade monocart-coverage-reports

In reference to issue raised where coverage of static async
class methods incorrectly reported branch coverage.

cenfun/monocart-coverage-reports#67

* fix: export HTTP engine from defaults

* refactor!: throw errors on failed writes to engines

* test: add engine api test

* test: add test for exported HTTP engine

* feat(transactions): add transactional engine wrapper with commit capability

* test(transactions): commit throws the underlying exception on failure

* feat(transactions): add automatic rollback when a transaction fails

* feat(transactions): allow Persist.addEngine to create transactional engines

* docs(transactions): add documentation on using transactional engines
  • Loading branch information
acodeninja authored Sep 9, 2024
1 parent 34f61aa commit b33bcd5
Show file tree
Hide file tree
Showing 22 changed files with 2,572 additions and 579 deletions.
51 changes: 47 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ export class Tag extends Persist.Type.Model {

const tag = new Tag({tag: 'documentation', description: 'How to use the persist library'});

FileEngine.find(Tag, {tag: 'documentation'});
await FileEngine.find(Tag, {tag: 'documentation'});
// [Tag {tag: 'documentation', description: 'How to use the persist library'}]

FileEngine.search(Tag, 'how to');
await FileEngine.search(Tag, 'how to');
// [Tag {tag: 'documentation', description: 'How to use the persist library'}]
```

Expand All @@ -169,7 +169,26 @@ export class Tag extends Persist.Type.Model {
static tag = Persist.Type.String.required;
}

Persist.getEngine('local', FileEngine).put(new Tag({tag: 'documentation'}));
await Persist.getEngine('local', FileEngine).put(new Tag({tag: 'documentation'}));
```

### HTTP Storage Engine

To store models using an S3 Bucket, use the `S3` storage engine.

```javascript
import Persist from "@acodeninja/persist";
import HTTPEngine from "@acodeninja/persist/engine/http";

Persist.addEngine('remote', HTTPEngine, {
host: 'https://api.example.com',
});

export class Tag extends Persist.Type.Model {
static tag = Persist.Type.String.required;
}

await Persist.getEngine('remote', HTTPEngine).put(new Tag({tag: 'documentation'}));
```

### S3 Storage Engine
Expand All @@ -189,5 +208,29 @@ export class Tag extends Persist.Type.Model {
static tag = Persist.Type.String.required;
}

Persist.getEngine('remote', S3Engine).put(new Tag({tag: 'documentation'}));
await Persist.getEngine('remote', S3Engine).put(new Tag({tag: 'documentation'}));
```

## Transactions

Create transactions to automatically roll back on failure to update.

```javascript
import Persist from "@acodeninja/persist";
import S3Engine from "@acodeninja/persist/engine/s3";

Persist.addEngine('remote', S3Engine, {
bucket: 'test-bucket',
client: new S3Client(),
transactions: true,
});

export class Tag extends Persist.Type.Model {
static tag = Persist.Type.String.required;
}

const transaction = Persist.getEngine('remote', S3Engine).start();

await transaction.put(new Tag({tag: 'documentation'}));
await transaction.commit();
```
3 changes: 3 additions & 0 deletions exports/engine/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import HTTPEngine from '../../src/engine/HTTPEngine.js';

export default HTTPEngine;
Loading

0 comments on commit b33bcd5

Please sign in to comment.