Bun SQLite Key Value
A super fast key-value store with SQLite that uses bun:sqlite
and v8 as a fast JSON replacement.
Bun's lightning-fast
SQLite implementation makes Bun-SQLite-Key-Value
perfect for a fast storage and cache solution with TTL support.
You need Bun to be able to use this package.
The ideas for the implementation come from
bun-sqlite-cache and
bun-kv.
Installation
bun add bun-sqlite-key-value
Usage
Using this key value store is dead simple:
simply create a new BunSqliteKeyValue instance and you're set
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
store.set("my-key", {foo: "bar", baz: [1, 2, 3, 4]})
const value = store.get("my-key")
console.log(value)
Documentation
Open Database
const store = new BunSqliteKeyValue([filename], [options])
-
filename
:
The full path of the SQLite database to open.
Pass an empty string (""
) or ":memory:"
or undefined for an in-memory database.
-
options
:
Defaults to {readwrite: true, create: true}
.
If a number, then it's treated as SQLITE_OPEN_*
constant flags.
Example
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
Write Value
set(key: string, value: any, [ttlMs: number]): void
setValue(key: string, value: any, [ttlMs: number])
-
key
:
The key must be a string.
-
value
:
The value can be any object that can be serialized with
v8.
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 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.
Example
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
store.set("my-key", "my-value")
store.set("my-key-2", "item-with-ttl", 30000)
Read Value
get(key: string): any
getValue(key: string)
key
:
The key must be a string.
Example
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
store.set("my-key", "my-value")
const value = store.get("my-key")
console.log(value)
Read Item
getItem(key: string): {key: string, value: any}
key
:
The key must be a string.
Example
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
store.set("my-key", "my-value")
const item = store.getItem("my-key")
console.log(item)
Read Values
Returns all values in an array whose keys begin with the passed string.
If you plan the names of the keys well, more complex data can be stored.
getValues(startsWithOrKeys: string | string[]): any[]
startsWithOrKeys
:
- string: String with which the keys whose values are to be returned begin.
It is advisable to divide keys into ranges using separators.
For example
"language:de"
, "language:en"
, "language:it"
.
A search for "language:"
would return all languages. - string[]: Array with keys. The returned array is exactly
the same size as the passed array.
Entries that are not found are returned as
undefined
.
Example
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
store.set("language:de", "German")
store.set("language:en", "English")
store.set("language:it", "Italian")
const values = store.getValues("language:")
console.log(values)
Read Items
Returns all items (key, value) in an array whose keys begin with the passed string.
If you plan the names of the keys well, more complex data can be stored.
getItems(startsWithOrKeys: string | string[]): {key: string, value: any}[]
startsWithOrKeys
:
- string: String with which the keys whose items are to be returned begin.
It is advisable to divide keys into ranges using separators.
For example
"language:de"
, "language:en"
, "language:it"
.
A search for "language:"
would return all languages. - string[]: Array with keys. The returned array is exactly
the same size as the passed array.
Entries that are not found are returned as
undefined
.
Example
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
store.set("language:de", "German")
store.set("language:en", "English")
store.set("language:it", "Italian")
const items = store.getItems("language:")
console.log(items)
Multiple Databases
It is no problem at all to use several databases and access them at the same time.
Example
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")
const settingsStore = new BunSqliteKeyValue(settingsPath)
const languagesStore = new BunSqliteKeyValue(languagesPath)
settingsStore.set("language", "de")
settingsStore.set("page-size", "A4")
settingsStore.set("screen-position", {top: 100, left: 100})
settingsStore.set("window-size", {height: 1000, width: 1000})
languagesStore.set("de", "German")
languagesStore.set("en", "English")
languagesStore.set("it", "Italian")
const settingItems = settingsStore.getItems()
console.log(settingItems)
const languageValues = languagesStore.getValues()
console.log(languageValues)
const languageKey = settingsStore.get("language")
const currentLanguage = languagesStore.get(languageKey)
console.log(`Current language: "${currentLanguage}"`)
settingsStore.close()
languagesStore.close()