From a33613e04acbf0ca8691e60560d3b9a479105051 Mon Sep 17 00:00:00 2001 From: Gerold Penz Date: Thu, 8 Aug 2024 18:31:03 +0200 Subject: [PATCH] Transactions documentation --- README.md | 33 +++++++++++++++++++++++++++++++++ examples/transactionExample.ts | 15 ++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2b2a74b..60b5e56 100644 --- a/README.md +++ b/README.md @@ -787,6 +787,38 @@ store.get("my-key") // --> "Hello!World!" ``` +## Database Transactions + +Transactions can be used to combine several database statements. +These combined database statements are processed much faster than +if they were executed individually. +The more database statements are combined, the greater the speed advantage. +You can find more infos in the +[Bun documentation](https://bun.sh/docs/api/sqlite#transactions). + +### Example + +```typescript +import { BunSqliteKeyValue } from "bun-sqlite-key-value" + +const store = new BunSqliteKeyValue() + +store.db.transaction(() => { + store.set("key1", "100") + store.set("key2", "200") + store.set("key3", "300") +})() + +store.db.transaction(() => { + const value1 = store.get("key1") + const value2 = store.get("key2") + const value3 = store.get("key3") + const total = value1 + value2 + value3 + store.set("total1", total) +})() +``` + + ## All Functions ### Database @@ -857,6 +889,7 @@ store.get("my-key") // --> "Hello!World!" ### Count - `getCount()` --> Number +- `count()` - `length` --> alias for getCount() - `getCountValid(deleteExpired?: boolean)` --> Number diff --git a/examples/transactionExample.ts b/examples/transactionExample.ts index 2b6225c..e56340e 100644 --- a/examples/transactionExample.ts +++ b/examples/transactionExample.ts @@ -2,10 +2,19 @@ import { BunSqliteKeyValue } from "../src" const store = new BunSqliteKeyValue() + +store.db.transaction(() => { + store.set("key1", "100") + store.set("key2", "200") + store.set("key3", "300") +})() + store.db.transaction(() => { - const value = (store.get("key") ?? 1000) + 200 - store.set("key", value) - console.log(value) + const value1 = store.get("key1") + const value2 = store.get("key2") + const value3 = store.get("key3") + const total = value1 + value2 + value3 + store.set("total1", total) })()