Skip to content

Commit

Permalink
getSet() function documented
Browse files Browse the repository at this point in the history
  • Loading branch information
gerold-penz committed Aug 15, 2024
1 parent 8774c4d commit 405652d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The ideas for the implementation come from
- Write and Read Values
- [`set()`](#write-value)
- [`get()`](#read-value)
- [`getSet()`](#read-and-write-value-in-one-step)
- [`getValues()`](#read-multiple-values)
- [`getRandomValue()`](#read-random-value)
- `getValuesSet()` --> Set with values
Expand Down Expand Up @@ -222,6 +223,48 @@ store.data["myKey"] // --> "my-value"
```


## Read and Write Value (in one step)

```typescript
getSet(key: string, value: any, ttlMs?: number)
```

Atomically sets key to value and returns the old value stored at key.
Inspired by: https://docs.keydb.dev/docs/commands/#getset

### key

The key must be a string.

### value

The value can be any object that can be serialized with
[v8](https://github.com/nodejs/node/blob/main/doc/api/v8.md#serialization-api).
This means that not only simple data types (string, number) are possible,
but also more complex types such as sets or maps.
You can find a list of the
[supported data types](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types) here.

### ttlMs (optional)

"Time to live" in milliseconds. After this time,
the item becomes invalid and is deleted from the database
the next time it is accessed or when the application is started.
Set the value to 0 if you want to explicitly deactivate the process.

### Example

```typescript
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
store.set("key-1", "string-value-1")
store.getSet("key-1", "string-value-2")) // --> "string-value-1"
store.get("key-1") // --> "string-value-2"
```


## Read Multiple Values

```typescript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bun-sqlite-key-value",
"version": "1.10.5",
"version": "1.10.6",
"author": {
"name": "Gerold Penz",
"email": "[email protected]",
Expand Down
3 changes: 1 addition & 2 deletions tests/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ test("Append", async () => {
test("getSet()", async () => {
const store = new BunSqliteKeyValue()

store.append(KEY_1, STRING_VALUE_1)
store.set(KEY_1, STRING_VALUE_1)
expect(store.getSet(KEY_1, STRING_VALUE_2)).toEqual(STRING_VALUE_1)
expect(store.get<string>(KEY_1)).toEqual(STRING_VALUE_2)
})
Expand Down Expand Up @@ -652,5 +652,4 @@ test("hmSet(), hmGet()", async () => {
const result = store.hmGet(KEY_1, ["field-1", "field-100"])
expect(result?.["field-1"]).toEqual("value-1")
expect(result?.["field-100"]).toBeUndefined()

})

0 comments on commit 405652d

Please sign in to comment.