filekeystore
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

FileKeyStore GitHub actions build NPM version Coverage Status

Introduction

File-based key-value data store that supports the basic CRD (create, read, and delete) operations. This data store is meant to be used as a local storage for one single process on one machine.

Installation

npm i filekeystore

Usage

// JavaScript
const { DataStore } = require('filekeystore')
// TypeScript
import { DataStore } from 'filekeystore'

dataStore = new DataStore(file="data", path=".") 
//path is optional and defaults to current working directory

Create Operation

const response = dataStore.create({
    key: 'user', // string (max 32 characters)
    value: { // object (cannot be empty and less than 16KB in size)
        name: 'Nikhil Taneja',
        username: 'itsNikhil',
        isAwesome: true
    },
    expiry: 3 // number (TTL in sec, optional)
});
response // Promise<string>
    .then(result => console.log(result))
    .catch(error => console.log(error))

// Output
$ Write complete! Saved to "/path/file.json"

Read Operation

const response = dataStore.read('user');
response // Promise<string>
    .then(result => console.log(JSON.parse(result)))
    .catch(error => console.log(error))

// Output
$ {name: "Nikhil Taneja", username: "itsNikhil", isAwesome: true}

Delete Operation

const response = dataStore.delete('user');
response // Promise<string>
    .then(result => console.log(result))
    .catch(error => console.log(error))

// Output
$ Deleted!

Tests

Unit tests

npm run test

> mocha -r ts-node/register tests/**/*.test.ts

  DataStore Create Tests:
    √ should initialise with correct filepathshould write data to fileshould write data to file with TTLshould reject create operation if key already existsshould reject create operation if key length greater than 32

  DataStore Delete Tests:
    √ should reject delete operation if key not foundshould delete data if key exists

  DataStore Read Tests:
    √ should return data if key existsshould return data before TTL expireshould reject read operation if key not foundshould reject read operation if TTL expires (1502ms)

  11 passing (2s)

Test lint

npm run lint

Test coverage

npm run coverage


Package Sidebar

Install

npm i filekeystore

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

31.4 kB

Total Files

14

Last publish

Collaborators

  • itsnikhil