https://github.com/lucasvmiguel/kv-store
Key value store library that uses your current database (for you that don't want to spend even more money)
https://github.com/lucasvmiguel/kv-store
keyvalue mysql nodejs redis typescript
Last synced: about 1 year ago
JSON representation
Key value store library that uses your current database (for you that don't want to spend even more money)
- Host: GitHub
- URL: https://github.com/lucasvmiguel/kv-store
- Owner: lucasvmiguel
- License: mit
- Created: 2019-04-18T09:44:43.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T18:23:09.000Z (over 3 years ago)
- Last Synced: 2025-03-18T04:42:52.020Z (about 1 year ago)
- Topics: keyvalue, mysql, nodejs, redis, typescript
- Language: JavaScript
- Homepage:
- Size: 172 KB
- Stars: 4
- Watchers: 0
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://www.npmjs.org/package/@lucasvmiguel/kv-store)
[](https://travis-ci.org/lucasvmiguel/kv-store)
[](https://www.npmjs.org/package/@lucasvmiguel/kv-store)
## 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
```bash
npm install --save @lucasvmiguel/kv-store
```
## How to use
#### MySQL
```js
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
```js
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
```js
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
```typescript
function init: ({
type: 'mysql' OR 'redis' OR 'local',
client: mysql.Connection OR redis.RedisClient OR null,
tableName?: string;
debug?: boolean;
}) => Promise
```
```js
function refresh(connection: mysql.Connection OR redis.RedisClient OR null) => Promise
```
```js
function get(key: string) => Promise;
```
```js
function put(key: string, value: string, options?: {expiration?: number}) => Promise
```
```js
function del(key: string) => Promise
```
## License
[MIT](LICENSE)