@varasto/sqlite-storage
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

@varasto/sqlite-storage

npm

Implementation of storage which stores data into SQLite database.

Each namespace in the storage is an table in the database, with following kind of schema:

CREATE TABLE IF NOT EXISTS "namespace" (
  key TEXT NOT NULL PRIMARY KEY,
  value TEXT NOT NULL,
  UNIQUE (key) ON CONFLICT REPLACE
)

Installation

$ npm install --save @varasto/sqlite-storage

Usage

The package provides an function called createSqliteStorage, which takes an SQLite database instance provided by SQLite library as argument. The function then returns an storage implementation that is capable of storing JSON objects into the database, where each value is identified by namespace and key, that must be valid URL slugs.

Basic usage of SQLite storage looks like this:

import { createSqliteStorage } from '@varasto/sqlite-storage';
import { open } from 'sqlite';
import sqlite3 from 'sqlite3';
import { JsonObject } from 'type-fest';

const database = await open({
  filename: './data.db',
  driver: sqlite3.Database,
});
const storage = createSqliteStorage(database);

The function also takes an optional configuration object, which supports these settings:

Property Default value Description
dropEmptyTables false If true, once an namespace is detected to be empty, it's associated table is automatically dropped.

Custom serializers

By default, JSON.stringify is used for serializing data written to file system and JSON.parse is used for deserializing data retrieved from file system. However, you can also use your own custom serialization functions by passing them as options to the createSqliteStorage function.

const storage = createSqliteStorage(
  database,
  {
    serialize: (data: string): JsonObject => ({}),
    deserialize: (data: JsonObject): string => "",
  }
);

Readme

Keywords

Package Sidebar

Install

npm i @varasto/sqlite-storage

Weekly Downloads

0

Version

4.0.0

License

MIT

Unpacked Size

16.7 kB

Total Files

12

Last publish

Collaborators

  • rauli