Skip to content

Commit

Permalink
Transactions documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gerold-penz committed Aug 8, 2024
1 parent 7c93f7d commit a33613e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -857,6 +889,7 @@ store.get("my-key") // --> "Hello!World!"

### Count
- `getCount()` --> Number
- `count()`
- `length` --> alias for getCount()
- `getCountValid(deleteExpired?: boolean)` --> Number

Expand Down
15 changes: 12 additions & 3 deletions examples/transactionExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})()


Expand Down

0 comments on commit a33613e

Please sign in to comment.