@haxtra/kvstore-sqlite

0.11.0 • Public • Published

kvstore-sqlite

Basic key-value store for SQLite databases.

Install

npm install @haxtra/kvstore-sqlite

Fineprint

  • requires super-sqlite3 or better-sqlite3 instance as the database driver
  • everything is passed through JSON on the way in and out, so:
    • NaN, Infinity become nulls
    • no circular references
    • if data is not JSONable, it will throw

Usage

Create instance

const KVStore = require('@haxtra/kvstore-sqlite')

const kv = new KVStore(superSqlite3Instance, {
    table: 'kvstore'  // table to use for the store, default "kvstore"
})

// when running for the first time, create the table
kv.createTable()

API

Note: All methods are synchronous.

Set/update value

kv.set(keyName, jsonableData)

Retrieve value from the store, falling back to optional default

kv.get(keyName, defaultValue?)

Remove key from the store

kv.delete(keyName)

Check if key exists, returns timestamp of the last update, or false if key is not set

kv.has(keyName)

Get count of stored keys

kv.count()

Get array of stored keys

kv.keys(page?, perPage?)

Get full store data, as an array of objects

kv.data(page?, perPage?)

Remove all data from the store

kv.purge()

Schema

Create table

Create store table with preconfigured name, defaults to kvstore. Returns true on success, false if table exists, throws on invalid table name.

kv.createTable()

Get schema

Get schema definition as a SQL string, with respect to current config.

kv.getSchema()

Schema file

Get absolute path to schema.sql file:

require.resolve('@haxtra/kvstore-sqlite/schema.sql')

License

MIT

Package Sidebar

Install

npm i @haxtra/kvstore-sqlite

Weekly Downloads

0

Version

0.11.0

License

MIT

Unpacked Size

6.23 kB

Total Files

5

Last publish

Collaborators

  • haxtra