Plain Old JavaScript Object Store
npm install @codegrill/stojoThis can be used to persist arbitrary JavaScript objects across application restarts or multiple applications. By default it uses sqlite3 to persist a named object to disk.
shell
npm install --save @codegrill/stojo
`Usage
`JavaScript
import { Stojo } from '@codegrill/stojo'const stojo = new Stojo()
stojo.store('my.key', { foo: 'bar' })
.then(() => stojo.fetch('my.key'))
.then(o => console.log(JSON.stringify(o)))
.then(() => stojo.close())
.catch(err => console.error(err))
`
This should output:
`shell
{"foo":"bar"}
`Stojo Class
$3
The constructor takes the following optional parameters:* options
``
* db `` an existing sqlite3 or compatible database connection. If undefined, it will create a sqlite3 instance.
* file `` the database filename to open. Defaults to ./stojo.sqlite3.
* logger `` a logger with the console interface. Defaults to console.
* table `` the create table command. Defaults to a table named stojo.
* insert `` the statement used to insert database rows. Defaults to a sqlite3 insert statement.
* select `` the query used to fetch a row from the database table`JavaScript
import { Stojo } from '@codegrill/stojo'
import sqlite3 from 'sqlite3'
import logger from 'pino'const stojo = new Stojo({
db: new sqlite3.Database('./stojo.sqlite3'),
logger: logger
})
` $3
Added: v0.1.0This will create the database table if it doesn't exist. It will only attempt to create the table once. Subsequent calls will be a no-op. A user is not required to ever call this and will get checked on any store or fetch command.
Returns a Promise to create the database table or no-op
$3
Added: v0.1.0Stores a named object in the store.
* name
`` the unique name of the named Object to store
* object `` the JavaScript Object to storeReturns a Promise to store the object
$3
Added: v0.1.0Fetches a named object from the store.
* name
``` the name of the Object to fetch from the storeReturns the JavaScript Object if it exists or null
Calls close on the db object
Returns a Promise to close the database connection