Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gerold-penz committed Jul 31, 2024
1 parent 68b2fb4 commit cf60901
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 28 deletions.
124 changes: 107 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ store.data.myKey2 = "my-value"
store.data["myKey3"] = "my-value"
store.d.myKey4 = "my-value"
store.data["myKey5"] = "my-value"
store.d["myKey5"] = "my-value"
// Becomes invalid after 30 seconds
store.set("myKey6", "item-with-ttl", 30000)
Expand Down Expand Up @@ -331,14 +331,8 @@ It is no problem at all to use several databases and access them at the same tim
```typescript
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
import { join } from "node:path"
import { exists, mkdir } from "node:fs/promises"
const dbDir = join(__dirname, "databases")
if (!(await exists(dbDir))) {
await mkdir(dbDir)
}
const settingsPath = join(dbDir, "settings.sqlite")
const languagesPath = join(dbDir, "languages.sqlite")
Expand Down Expand Up @@ -372,7 +366,7 @@ const languageKey = settingsStore.get("language")
const currentLanguage = languagesStore.get(languageKey)
console.log(`Current language: "${currentLanguage}"`) // -> Current language: "German"
// Explicitly close DBs
// Close DBs
settingsStore.close()
languagesStore.close()
```
Expand Down Expand Up @@ -585,20 +579,18 @@ console.log(store.getKeys("dynamic:"))
```


## Count Items
## Count All Items

```typescript
getCount(): number
length // --> alias for `getCount()`
(ALPHA) getCountValid(deleteExpired?: boolean): number
```

Returns the number of all items, including those that have already expired.
The fact that possibly expired entries are also counted is for reasons of speed.
First delete the expired items with `deleteExpired()`
if you want to get the number of items that have not yet expired.
Use `getCountValid()` if you want to get the number of items that have not yet expired.
If you do not use `ttlMs` (time to live), `getCount()` is faster than `getCountValid()`.


### Example
Expand All @@ -616,13 +608,23 @@ store.length // --> 2
```


## Count Valid Items (ALPHA !!!)
## Count Valid Items

```typescript
getCountValid(deleteExpired?: boolean): number
```

ToDo: ...
Returns the number of valid (non-expired) items.
Can also delete the expired items.

### deleteExpired

If the parameter is not specified or `false` is passed,
then only the entries that have no expiration date or
whose expiration date is in the future are counted.

If `true` is passed, the expired entries are deleted first
before the entries are counted.


### Example
Expand All @@ -633,14 +635,98 @@ import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
store.set("my-key1", "my-value1")
store.set("my-key2", "my-value2", {ttlMs: 100})
store.set("my-key2", "my-value2", 100)
store.getCountValid() // --> 2
await Bun.sleep(500)
store.getCountValid() // --> 1
```

## Increment

```typescript
incr(key: string, incrBy: number = 1, ttlMs?: number): number
```

Increments the saved number by `incrBy` (default = 1),
saves the new number and returns it.
If the key does not yet exist in the database,
the value is set to 0 before being incremented by `incrBy`.
If a string is stored in the database that can be converted into a number,
this is converted first.
If the stored value cannot be converted into a number, `NaN` is returned.


### key

The key must be a string.

### incrBy

The stored number is increased by this value.

### 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.incr("my-key") // --> 1
store.incr("my-key") // --> 2
```


## Decrement

```typescript
decr(key: string, decrBy: number = 1, ttlMs?: number): number
```

Decrements the saved number by `decrBy` (default = 1),
saves the new number and returns it.
If the key does not yet exist in the database,
the value is set to 0 before being decremented by `decrBy`.
If a string is stored in the database that can be converted into a number,
this is converted first.
If the stored value cannot be converted into a number, `NaN` is returned.


### key

The key must be a string.

### incrBy

The stored number is decreased by this value.

### 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("my-key", 10)
store.decr("my-key") // --> 9
store.decr("my-key") // --> 8
```


## All Functions

Expand Down Expand Up @@ -711,7 +797,11 @@ store.getCountValid() // --> 1
### Count
- `getCount()` --> Number
- `length` --> alias for getCount()
- (ALPHA) `getCountValid(deleteExpired?: boolean)` --> Number
- `getCountValid(deleteExpired?: boolean)` --> Number

### Increment, Decrement
- `incr()` --> Number
- `decr()` --> Number

### Get keys
- `has(key: string)` --> Boolean
Expand Down
8 changes: 1 addition & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class BunSqliteKeyValue {

this.setItemStatement = this.db.query("INSERT OR REPLACE INTO items (key, value, expires) VALUES ($key, $value, $expires)")
this.countStatement = this.db.query("SELECT COUNT(*) AS count FROM items")
this.countValidStatement = this.db.query("SELECT COUNT(*) AS count FROM items WHERE expires IS NULL OR expires < $now")
this.countValidStatement = this.db.query("SELECT COUNT(*) AS count FROM items WHERE expires IS NULL OR expires > $now")

this.getAllItemsStatement = this.db.query("SELECT key, value, expires FROM items")
this.getItemStatement = this.db.query("SELECT value, expires FROM items WHERE key = $key")
Expand Down Expand Up @@ -181,9 +181,6 @@ export class BunSqliteKeyValue {
}


// ALPHA BEGIN ------------------------------------


// Returns the number of all valid (non-expired) items in the database.
// Use `getCount()` if you want the fastet possible method.
getCountValid(deleteExpired?: boolean) {
Expand All @@ -198,9 +195,6 @@ export class BunSqliteKeyValue {
}


// ALPHA END ------------------------------------


// @param ttlMs:
// Time to live in milliseconds.
// Set ttlMs to 0 if you explicitly want to disable expiration.
Expand Down
21 changes: 17 additions & 4 deletions tests/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,24 @@ test("Replace existing item", () => {
})


test("Count/length", () => {
test("Count/length", async () => {
const store: BunSqliteKeyValue = new BunSqliteKeyValue()
store.set<string>(KEY_1, STRING_VALUE_1)

store.set(KEY_1, STRING_VALUE_1)
store.set(KEY_2, STRING_VALUE_2, 30)

expect(store.getCount()).toEqual(2)
expect(store.length).toEqual(2)
expect(store.getCountValid()).toEqual(2)

await Bun.sleep(40)
expect(store.getCountValid()).toEqual(1)
expect(store.getCount()).toEqual(2)

store.set(KEY_2, STRING_VALUE_2, 30)
await Bun.sleep(40)
expect(store.getCountValid(true)).toEqual(1)
expect(store.getCount()).toEqual(1)
expect(store.length).toEqual(1)
})


Expand Down Expand Up @@ -435,7 +448,7 @@ test("Proxy-Object (data, d): set, get and delete values", () => {
})


test("incr/decr", async () => {
test("Increment, Decrement", async () => {
const store = new BunSqliteKeyValue()

// incr/decr
Expand Down

0 comments on commit cf60901

Please sign in to comment.