Skip to content

lucasvmiguel/kv-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kv-store

Version Build Status Downloads

Description

Key value store library that uses a few database options. It might be the case that you don't want to spend money in a new instance or in another service.

Adapters available:

  • Local
  • MySQL
  • Redis
  • Postgres (roadmap)
  • MongoDB (roadmap)

Installation

npm install --save @lucasvmiguel/kv-store

How to use

MySQL

import * as kvStore from '@lucasvmiguel/kv-store';

const connection = mysql.createConnection({
    host: '...',
    user: '...',
    password: '...',
    port: '...',
    database: '...',
});

await kvStore.init({
  type: 'mysql',
  client: connection,
  tableName: 'kvstore_keyvalues', // OPTIONAL
  debug: false, // OPTIONAL
});

await kvStore.put('USER:123', 'abc');
const abc = await kvStore.get('USER:123');

// Expiration in seconds
await kvStore.putJson('USER:456', {foo: "bar"}, { expiration: 60 });
const fooBar = await kvStore.getJson('USER:456');

Redis

import * as kvStore from '@lucasvmiguel/kv-store';

const connection = redis.createClient(6379, '127.0.0.1')

await kvStore.init({
  type: 'redis',
  client: connection,
  tableName: 'kvstore_keyvalues', // OPTIONAL
  debug: false, // OPTIONAL
});

await kvStore.put('USER:123', 'abc');
const abc = await kvStore.get('USER:123');

// Expiration in seconds
await kvStore.putJson('USER:456', {foo: "bar"}, { expiration: 60 });
const fooBar = await kvStore.getJson('USER:456');

Local

import * as kvStore from '@lucasvmiguel/kv-store';

await kvStore.init({
  type: 'local',
  client: null,
  tableName: 'kvstore_keyvalues', // OPTIONAL
  debug: false, // OPTIONAL
});

await kvStore.put('USER:123', 'abc');
const abc = await kvStore.get('USER:123');

// Expiration in seconds
await kvStore.putJson('USER:456', {foo: "bar"}, { expiration: 60 });
const fooBar = await kvStore.getJson('USER:456');

API Reference

  • expiration is in seconds
  • just pass null to init if is local cache
function init: ({
    type: 'mysql' OR 'redis' OR 'local',
    client: mysql.Connection OR redis.RedisClient OR null,
    tableName?: string;
    debug?: boolean;
}) => Promise<boolean>
function refresh(connection: mysql.Connection OR redis.RedisClient OR null) => Promise<boolean>
function get(key: string) => Promise<string OR null>;
function put(key: string, value: string, options?: {expiration?: number}) => Promise<boolean>
function del(key: string) => Promise<boolean OR null>

License

MIT

About

Key value store library that uses your current database (for you that don't want to spend even more money)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published