Package detail

@keyvhq/core

microlinkhq166kMIT2.1.7

Simple key-value storage with support for multiple backends

cache, key, keyv, store

readme

keyv logo

Last version Coverage Status NPM Status

Keyv is a simple key-value storage with support for multiple backend adapters (MySQL, PostgreSQL, SQLite, Redis, Mongo, DynamoDB, Firestore, Memcached, and more).

Features

Forked from keyv, plus:

  • It isn't bloated.
  • It supports namespaces.
  • It supports TTL based expiry.
  • It has a simple Promise based API.
  • It handles all JSON types plus Buffer.
  • It's support a vary of storages adapters.
  • It can be easily embed inside another module.
  • It works with any storage that implements the Map API.
  • it handles database errors (db failures won't kill your app).
  • It supports the current active LTS version of Node.js or higher.
  • It's suitable as a TTL based cache or persistent key-value store.

Installation

npm install @keyvhq/core --save

You can optionally install the storage adapter you want to use:

npm install @keyvhq/redis --save
npm install @keyvhq/mongo --save
npm install @keyvhq/sqlite --save
npm install @keyvhq/postgres --save
npm install @keyvhq/mysql --save

If you don't provide a specific storage adapter, a in-memory storage adapter is used by default.

Usage

Just create a new Keyv instance, using an specific storage adapter:

const keyv = new Keyv() // in-memory, by default
const keyvRedis = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379')})
const keyvMongo = new Keyv({ store: new KeyvMongo('mongodb://user:pass@localhost:27017/dbname')})
const keyvSQLite = new Keyv({ store: new KeyvSQLite('sqlite://path/to/database.sqlite')})
const keyvPostgreSQL = new Keyv({ store: new KeyvPostgreSQL('postgresql://user:pass@localhost:5432/dbname')})
const keyvMySQL = new Keyv({ store: new KeyvMySQL('mysql://user:pass@localhost:3306/dbname')})

await keyv.set('foo', 'expires in 1 second', 1000) // true
await keyv.set('foo', 'never expires') // true
await keyv.get('foo') // 'never expires'
await keyv.has('foo') // true
await keyv.delete('foo') // true
await keyv.has('foo') // false
await keyv.clear() // undefined

Namespaces

You can namespace your Keyv instance to avoid key collisions and allow you to clear only a certain namespace while using the same database.

const users = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379'), namespace: 'users' })
const cache = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379'), namespace: 'cache' })

await users.set('foo', 'users') // true
await cache.set('foo', 'cache') // true
await users.get('foo') // 'users'
await cache.get('foo') // 'cache'
await users.clear() // undefined
await users.get('foo') // undefined
await cache.get('foo') // 'cache'

Serialization

Keyv uses json-buffer for data serialization to ensure consistency across different backends.

You can optionally provide your own serialization functions to support extra data types or to serialize to something other than JSON.

The following example is using @keyvhq/compress as serializer:

const KeyvCompress = require('@keyvhq/compress')
const Keyv = require('@keyvhq/core')

const keyv = KeyvCompress(
  new Keyv({
    serialize: v8.serialize,
    deserialize: v8.deserialize
  })
)

Storage adapters

Keyv is designed to be easily embedded into other modules to add cache support.

Caching will work in memory by default and users have the option to also install a Keyv storage adapter and pass in a connection string, or any other storage that implements the Map API.

const KeyvRedis = require('@keyvhq/redis')
const Keyv = require('@keyvhq/core')
const got = require('got')

const cache = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379') })

await got('https://keyv.js.org', { cache })

The recommended pattern is to expose a cache option in your modules options which is passed through to Keyv.

For example, quick-lru is a completely unrelated module that implements the Map API.

const Keyv = require('@keyvhq/core')
const QuickLRU = require('quick-lru')

const lru = new QuickLRU({ maxSize: 1000 })
const keyv = new Keyv({ store: lru })

You should also set a namespace for your module so you can safely call .clear() without clearing unrelated app data.

All the adapters

The official storage adapters are covered by over 150 integration tests to guarantee consistent behaviour. They are lightweight, efficient wrappers over the DB clients making use of indexes and native TTLs where available.

Decorators

Community

You can also use third-party storage adapters or build your own. Keyv will wrap these storage adapters in TTL functionality and handle complex types internally.

  • keyv-anyredis - Support for Redis clusters and alternative Redis clients.
  • keyv-dynamodb - DynamoDB storage adapter for Keyv.
  • keyv-file - File system storage adapter for Keyv.
  • keyv-firestore – Firebase Cloud Firestore adapter for Keyv.
  • keyv-lru – An in-memory LRU back-end for Keyv.
  • keyv-memcache - Memcache storage adapter for Keyv.
  • keyv-mssql - Microsoft SQL Server adapter for Keyv.
  • keyv-s3 - Amazon S3 storage adapter for Keyv.
  • quick-lru - Simple "Least Recently Used" (LRU) cache.

API

constructor([options])

Returns a new Keyv instance.

options

Type: Object

The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.

namespace

Type: String
Default: undefined

Namespace for the current instance.

ttl

Type: Number
Default: undefined

Default TTL in milliseconds. Can be overridden by specifying a TTL on .set().

serialize

Type: Function
Default: JSONB.stringify

A custom serialization function.

deserialize

Type: Function
Default: JSONB.parse

A custom deserialization function.

store

Type: Storage adapter instance
Default: new Map()

The storage adapter instance to be used by Keyv.

raw

Type: Boolean
Default: false

If set to true the raw DB object Keyv stores internally will be returned instead of just the value.

This contains the TTL timestamp.

.set(key, value, [ttl])

Set a value.

By default keys are persistent. You can set an expiry TTL in milliseconds.

Returns a promise which resolves to true.

.get(key, [options])

Returns a promise which resolves to the retrieved value.

.has(key)

Returns a promise which resolves to a boolean, indicating existence of a key.

.delete(key)

Deletes an entry.

Returns a promise which resolves to true if the key existed, false if not.

.clear()

Delete all entries in the current namespace.

Returns a promise which is resolved when the entries have been cleared.

When calling clear(), on a keyv instance with no namespace, all keys are cleared.

.iterator()

Returns an async iterator, which iterates over all the key-value pairs in the namespace. When called without a namespace, it iterates over all entries in the database.

The iterator shouldn't be used in environments where performance is key, or there are more than 1000 entries in the database, use an ORM or a native driver if you need to iterate over all entries.

License

keyv © Luke Childs, released under the MIT License.
Maintained by Microlink with help from contributors.

microlink.io · GitHub microlinkhq · X @microlinkhq

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

2.1.7 (2025-02-20)

Note: Version bump only for package @keyvhq/monorepo

2.1.6 (2024-09-24)

Note: Version bump only for package @keyvhq/monorepo

2.1.5 (2024-07-30)

Note: Version bump only for package @keyvhq/monorepo

2.1.4 (2024-04-28)

Performance Improvements

2.1.3 (2024-04-28)

Bug Fixes

2.1.2 (2024-04-25)

Bug Fixes

  • redis: expose Redis constructor (b8ead88), closes #205

2.1.1 (2024-04-23)

Note: Version bump only for package @keyvhq/monorepo

2.1.0 (2023-09-29)

Features

2.0.3 (2023-06-20)

Note: Version bump only for package @keyvhq/monorepo

2.0.2 (2023-05-16)

Note: Version bump only for package @keyvhq/monorepo

2.0.1 (2023-05-08)

Note: Version bump only for package @keyvhq/monorepo

2.0.0 (2023-04-22)

Features

BREAKING CHANGES

  • The EventEmitter behavior has been completely removed

1.6.28 (2023-02-27)

Note: Version bump only for package @keyvhq/monorepo

1.6.27 (2023-01-30)

Note: Version bump only for package @keyvhq/monorepo

1.6.26 (2023-01-29)

Note: Version bump only for package @keyvhq/monorepo

1.6.25 (2022-10-20)

Note: Version bump only for package @keyvhq/monorepo

1.6.24 (2022-09-24)

Note: Version bump only for package @keyvhq/monorepo

1.6.23 (2022-08-24)

Note: Version bump only for package @keyvhq/monorepo

1.6.22 (2022-08-19)

Note: Version bump only for package @keyvhq/monorepo

1.6.21 (2022-07-22)

Note: Version bump only for package @keyvhq/monorepo

1.6.20 (2022-07-12)

Note: Version bump only for package @keyvhq/monorepo

1.6.19 (2022-07-12)

Note: Version bump only for package @keyvhq/monorepo

1.6.18 (2022-07-12)

Note: Version bump only for package @keyvhq/monorepo

1.6.17 (2022-06-27)

Note: Version bump only for package @keyvhq/monorepo

1.6.16 (2022-06-22)

Bug Fixes

1.6.15 (2022-06-07)

Note: Version bump only for package @keyvhq/monorepo

1.6.14 (2022-05-29)

Bug Fixes

  • added async deserialize/serialize functions (703b98b)

1.6.13 (2022-05-22)

Bug Fixes

  • associate emitErros with keyv instance (3a88e0a)

1.6.12 (2022-05-12)

Note: Version bump only for package @keyvhq/monorepo

1.6.11 (2022-04-05)

Note: Version bump only for package @keyvhq/monorepo

1.6.10 (2022-04-01)

Bug Fixes

  • types: fixed typings in redis (9fc462f)

1.6.9 (2022-03-30)

Bug Fixes

  • core: wait deserialize promise (b5f3f1c)

1.6.8 (2022-03-28)

Bug Fixes

  • multi: wait deserialize promise (898fcbe)

1.6.7 (2022-03-13)

Note: Version bump only for package @keyvhq/monorepo

1.6.6 (2022-03-09)

Bug Fixes

  • changed all adapter apis to support (uri, option) (f78a5dd)
  • correct glob pattern for nano-staged (85008fd)
  • linter (328d51c)
  • linter (c36c964)

1.6.5 (2022-03-09)

Bug Fixes

  • changed all adapter apis to support (uri, option) (f78a5dd)
  • correct glob pattern for nano-staged (85008fd)
  • linter (328d51c)
  • linter (c36c964)

1.6.4 (2022-01-24)

Note: Version bump only for package @keyvhq/monorepo

1.6.3 (2021-12-01)

Bug Fixes

1.6.2 (2021-11-08)

Note: Version bump only for package @keyvhq/monorepo

1.6.1 (2021-10-14)

Note: Version bump only for package @keyvhq/monorepo

1.6.0 (2021-10-05)

Bug Fixes

  • test: increased timeout for iterator resolving (1a51f3e)

Features

1.5.2 (2021-09-22)

Note: Version bump only for package @keyvhq/monorepo

1.5.1 (2021-09-16)

Note: Version bump only for package @keyvhq/monorepo

1.5.0 (2021-09-15)

Bug Fixes

  • test: t.fail instead of t.throw (98b9551)

Features

Performance Improvements

  • do parallel when is possible (fed8ded)

1.4.0 (2021-09-12)

Bug Fixes

  • convert key to string if possible (4a232b9)
  • fixed namespacing bugs (f14cd4f)
  • namespace api (fd07736)
  • no namespaced iteration of maps (77b9724)
  • test: t.fail instead of t.throw (5dfd9bb)

Features

  • use empty namespace by default (a2872d3)

1.3.0 (2021-09-01)

Bug Fixes

  • mongo: updated check for nonexistent key (2f9a153)
  • redis iterator not working when keys are present (ba7d28c)

Features

1.2.7 (2021-08-24)

Note: Version bump only for package @keyvhq/monorepo

1.2.6 (2021-08-24)

Note: Version bump only for package @keyvhq/monorepo

1.2.5 (2021-08-19)

Features

  • memoize: add a way to invalidate on demand (82e713b)

1.2.4 (2021-08-19)

Bug Fixes

  • stale ttl value could be 0 (694ea16)

1.2.3 (2021-08-17)

Bug Fixes

1.2.2 (2021-08-16)

Note: Version bump only for package @keyvhq/monorepo

1.2.1 (2021-08-12)

Note: Version bump only for package @keyvhq/monorepo

1.2.0 (2021-08-11)

Bug Fixes

  • clear pending after stale is refreshed (ee49c03)

Features

  • expose more memoize methods (c032e93)

1.1.1 (2021-07-30)

Note: Version bump only for package @keyvhq/monorepo

1.1.0 (2021-07-30)

Bug Fixes

Features

1.0.2 (2021-07-16)

Note: Version bump only for package @keyvhq/monorepo

1.0.1 (2021-07-16)

Note: Version bump only for package @keyvhq/monorepo

(2021-07-16)

1.0.0 (2021-07-16)

Bug Fixes

  • add missing dev dependency (a7a6d76)

Features

0.2.4 (2021-07-01)

Bug Fixes

  • typo keyvNamepsaceTests -> keyvNamepsaceTests (d5b5577)
  • typo opts -> options (ce95753)

0.2.0 (2021-05-24)

0.1.1-alpha.0 (2021-04-18)

0.0.6-alpha.0 (2021-04-17)

0.0.5-alpha.0 (2021-04-17)

0.0.4-alpha.0 (2021-04-17)

0.0.3-alpha.0 (2021-04-17)

0.0.2-alpha.0 (2021-04-17)

0.0.1-alpha.0 (2021-04-17)

Bug Fixes

  • package: update @keyv/sql to version 1.0.6 (#14) (5bc9bbf)
  • package: update @keyv/sql to version 1.0.6 (#18) (31cde3e)
  • package: update @keyv/sql to version 1.1.0 (#19) (bd65ae2)
  • package: update @keyv/sql to version 1.1.1 (#22) (f59d50d), closes #20
  • package: update @keyv/sql to version 1.1.2 (#20) (8545d89)
  • package: update json-buffer to version 3.0.1 (b3f8119)
  • package: update keyv-sequelize to version 0.1.0 (#3) (fddf4de)
  • package: update sql to version 0.2.0 (#6) (e74c6a9)
  • package: update sql to version 0.2.3 (#8) (c270bb8), closes #7
  • package: update sql to version 0.2.4 (#4) (1f6e98f)
  • package: update sql to version 0.2.4 (#4) (dee04f0)
  • package: update sql to version 0.2.4 (#9) (c30c07e)
  • package: update sql to version 1.0.2 (#11) (b191741), closes #10
  • package: update sql to version 1.0.2 (#6) (7e7e235), closes #5
  • package: update sql to version 1.0.2 (#6) (26e3e96), closes #5
  • package: update sql to version 1.0.3 (#9) (c72121d)
  • package: update mysql2 to version 1.4.2 (#17) (fd78034)
  • package: update pg to version 7.2.0 (#15) (261f1b6)
  • package: update sqlite3 to version 3.1.9 (#13) (0255ff3)
  • package: update sqlite3 to version 4.0.2 (#40) (8e5324e), closes #34

Deps

Features

  • async serialize/deserialize support (#95) (e9eeecc)

Reverts

  • Revert "Add everything to .npmignore apart from dist" (e3c3771)
  • Revert "pkg.files over .npmignore" (e99b529)
  • Revert "Revert "Support passing both URL and collection name to constructor (#25)"" (b863ca3), closes #25
  • Revert "Remove dynamic require (#119)" (ec9253e), closes #119

BREAKING CHANGES

  • since exports are now different

Signed-off-by: Jytesh 44925963+Jytesh@users.noreply.github.com