2.3.0 • Published 2 years ago

kvs v2.3.0

Weekly downloads
3
License
MIT
Repository
-
Last release
2 years ago

kvs

NPM Version Build Status Coverage percentage

Simple key-value store facade for node.

Installation

$ npm install kvs

Methods

set(key, val)
get(key)
del(key)
getset(key, value)
getdel(key)
keys()
clear()

Usage

Classic usage

const {Store} = require('kvs');

const TTL = 100;
const store = Store.create('memory');

(async () => {
  const bucket1 = store.bucket({ttl: TTL /*seconds*/});
  // const memoryBucket = await memoryStore.createBucket({max: 100, ttl: 10/*seconds*/});

  await bucket1.set('foo', 'bar');
  let result = await bucket1.get('foo');
  console.log(result);
  await bucket1.del('foo');

  const userId = 123;
  const key = '#' + userId; // for test if key is not the parameter (user_id) to load.

  // Using namespace "user"
  const bucket2 = store.bucket('user', {
    ttl: TTL /*seconds*/,

    // method to load a thing if it's not in the bucket.
    load: loadUser,
  });

  // `user_id` is the parameter used to load.
  // if no parameter is specified for loading, the `key` will be used.
  let user = await bucket2.get(key, userId);
  console.log(user);

  // Second time fetches user from  bucket2
  user = await bucket2.get(key, userId);
  console.log(user);

  // Outputs:
  // Returning user from slow database.
  // { id: 123, name: 'Bob' }
  // { id: 123, name: 'Bob' }

  await store.close();
})();

async function loadUser(id) {
  await new Promise(resolve => setTimeout(resolve, 100));
  console.log('Returning user from slow database.');
  return {id: id, name: 'Bob'};
}

// Outputs:
// bar
// Returning user from slow database.
// { id: 123, name: 'Bob' }
// { id: 123, name: 'Bob' }

With typings

import {Store} from 'kvs';

interface Settings {
  name: string;
  ttl: number;
}

async () => {
  const store = new Store('memory');
  const bucket = store.bucket<Settings>('settings');
  await bucket.set('name', 'foo');
  await bucket.set('ttl', '3600');

  console.log(await bucket.get('name'));
  // => foo
  console.log(await bucket.get('ttl'));
  // => 3600
};

Tests

$ npm test

License

kvs is licensed under the MIT license.

2.3.0

2 years ago

2.2.2

3 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.3

4 years ago

2.0.4

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.8

8 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.1-4

11 years ago

0.0.1-3

11 years ago

0.0.1-2

11 years ago