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)
npm install --save @lucasvmiguel/kv-store
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');
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');
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');
- 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>