UNPKG

13.9 kBMarkdownView Raw
1<h1 align="center">
2 <img width="250" src="https://jaredwray.com/images/keyv.svg" alt="keyv">
3 <br>
4 <br>
5</h1>
6
7> Simple key-value storage with support for multiple backends
8
9[![build](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml)
10[![codecov](https://codecov.io/gh/jaredwray/keyv/branch/master/graph/badge.svg?token=bRzR3RyOXZ)](https://codecov.io/gh/jaredwray/keyv)
11[![npm](https://img.shields.io/npm/dm/keyv.svg)](https://www.npmjs.com/package/keyv)
12[![npm](https://img.shields.io/npm/v/keyv.svg)](https://www.npmjs.com/package/keyv)
13
14Keyv provides a consistent interface for key-value storage across multiple backends via storage adapters. It supports TTL based expiry, making it suitable as a cache or a persistent key-value store.
15
16## Features
17
18There are a few existing modules similar to Keyv, however Keyv is different because it:
19
20- Isn't bloated
21- Has a simple Promise based API
22- Suitable as a TTL based cache or persistent key-value store
23- [Easily embeddable](#add-cache-support-to-your-module) inside another module
24- Works with any storage that implements the [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) API
25- Handles all JSON types plus `Buffer`
26- Supports namespaces
27- Wide range of [**efficient, well tested**](#official-storage-adapters) storage adapters
28- Connection errors are passed through (db failures won't kill your app)
29- Supports the current active LTS version of Node.js or higher
30
31## Usage
32
33Install Keyv.
34
35```
36npm install --save keyv
37```
38
39By default everything is stored in memory, you can optionally also install a storage adapter.
40
41```
42npm install --save @keyv/redis
43npm install --save @keyv/mongo
44npm install --save @keyv/sqlite
45npm install --save @keyv/postgres
46npm install --save @keyv/mysql
47npm install --save @keyv/etcd
48```
49
50Create a new Keyv instance, passing your connection string if applicable. Keyv will automatically load the correct storage adapter.
51
52```js
53const Keyv = require('keyv');
54
55// One of the following
56const keyv = new Keyv();
57const keyv = new Keyv('redis://user:pass@localhost:6379');
58const keyv = new Keyv('mongodb://user:pass@localhost:27017/dbname');
59const keyv = new Keyv('sqlite://path/to/database.sqlite');
60const keyv = new Keyv('postgresql://user:pass@localhost:5432/dbname');
61const keyv = new Keyv('mysql://user:pass@localhost:3306/dbname');
62const keyv = new Keyv('etcd://localhost:2379');
63
64// Handle DB connection errors
65keyv.on('error', err => console.log('Connection Error', err));
66
67await keyv.set('foo', 'expires in 1 second', 1000); // true
68await keyv.set('foo', 'never expires'); // true
69await keyv.get('foo'); // 'never expires'
70await keyv.delete('foo'); // true
71await keyv.clear(); // undefined
72```
73
74### Namespaces
75
76You can namespace your Keyv instance to avoid key collisions and allow you to clear only a certain namespace while using the same database.
77
78```js
79const users = new Keyv('redis://user:pass@localhost:6379', { namespace: 'users' });
80const cache = new Keyv('redis://user:pass@localhost:6379', { namespace: 'cache' });
81
82await users.set('foo', 'users'); // true
83await cache.set('foo', 'cache'); // true
84await users.get('foo'); // 'users'
85await cache.get('foo'); // 'cache'
86await users.clear(); // undefined
87await users.get('foo'); // undefined
88await cache.get('foo'); // 'cache'
89```
90
91### Custom Serializers
92
93Keyv uses [`json-buffer`](https://github.com/dominictarr/json-buffer) for data serialization to ensure consistency across different backends.
94
95You can optionally provide your own serialization functions to support extra data types or to serialize to something other than JSON.
96
97```js
98const keyv = new Keyv({ serialize: JSON.stringify, deserialize: JSON.parse });
99```
100
101**Warning:** Using custom serializers means you lose any guarantee of data consistency. You should do extensive testing with your serialisation functions and chosen storage engine.
102
103## Official Storage Adapters
104
105The official storage adapters are covered by [over 150 integration tests](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml) to guarantee consistent behaviour. They are lightweight, efficient wrappers over the DB clients making use of indexes and native TTLs where available.
106
107Database | Adapter | Native TTL
108---|---|---
109Redis | [@keyv/redis](https://github.com/jaredwray/keyv/tree/master/packages/redis) | Yes
110MongoDB | [@keyv/mongo](https://github.com/jaredwray/keyv/tree/master/packages/mongo) | Yes
111SQLite | [@keyv/sqlite](https://github.com/jaredwray/keyv/tree/master/packages/sqlite) | No
112PostgreSQL | [@keyv/postgres](https://github.com/jaredwray/keyv/tree/master/packages/postgres) | No
113MySQL | [@keyv/mysql](https://github.com/jaredwray/keyv/tree/master/packages/mysql) | No
114Etcd | [@keyv/etcd](https://github.com/jaredwray/keyv/tree/master/packages/etcd) | Yes
115
116## Third-party Storage Adapters
117
118You 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.
119
120```js
121const Keyv = require('keyv');
122const myAdapter = require('./my-storage-adapter');
123
124const keyv = new Keyv({ store: myAdapter });
125```
126
127Any store that follows the [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) api will work.
128
129```js
130new Keyv({ store: new Map() });
131```
132
133For example, [`quick-lru`](https://github.com/sindresorhus/quick-lru) is a completely unrelated module that implements the Map API.
134
135```js
136const Keyv = require('keyv');
137const QuickLRU = require('quick-lru');
138
139const lru = new QuickLRU({ maxSize: 1000 });
140const keyv = new Keyv({ store: lru });
141```
142
143The following are third-party storage adapters compatible with Keyv:
144
145- [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple "Least Recently Used" (LRU) cache
146- [keyv-file](https://github.com/zaaack/keyv-file) - File system storage adapter for Keyv
147- [keyv-dynamodb](https://www.npmjs.com/package/keyv-dynamodb) - DynamoDB storage adapter for Keyv
148- [keyv-lru](https://www.npmjs.com/package/keyv-lru) - LRU storage adapter for Keyv
149- [keyv-null](https://www.npmjs.com/package/keyv-null) - Null storage adapter for Keyv
150- [keyv-firestore ](https://github.com/goto-bus-stop/keyv-firestore) – Firebase Cloud Firestore adapter for Keyv
151- [keyv-mssql](https://github.com/pmorgan3/keyv-mssql) - Microsoft Sql Server adapter for Keyv
152- [keyv-memcache](https://github.com/jaredwray/keyv/tree/master/packages/memcache) - Memcache storage adapter for Keyv
153- [keyv-azuretable](https://github.com/howlowck/keyv-azuretable) - Azure Table Storage/API adapter for Keyv
154
155## Add Cache Support to your Module
156
157Keyv is designed to be easily embedded into other modules to add cache support. The recommended pattern is to expose a `cache` option in your modules options which is passed through to Keyv. 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.
158
159You should also set a namespace for your module so you can safely call `.clear()` without clearing unrelated app data.
160
161Inside your module:
162
163```js
164class AwesomeModule {
165 constructor(opts) {
166 this.cache = new Keyv({
167 uri: typeof opts.cache === 'string' && opts.cache,
168 store: typeof opts.cache !== 'string' && opts.cache,
169 namespace: 'awesome-module'
170 });
171 }
172}
173```
174
175Now it can be consumed like this:
176
177```js
178const AwesomeModule = require('awesome-module');
179
180// Caches stuff in memory by default
181const awesomeModule = new AwesomeModule();
182
183// After npm install --save keyv-redis
184const awesomeModule = new AwesomeModule({ cache: 'redis://localhost' });
185
186// Some third-party module that implements the Map API
187const awesomeModule = new AwesomeModule({ cache: some3rdPartyStore });
188```
189
190## API
191
192### new Keyv([uri], [options])
193
194Returns a new Keyv instance.
195
196The Keyv instance is also an `EventEmitter` that will emit an `'error'` event if the storage adapter connection fails.
197
198### uri
199
200Type: `String`<br>
201Default: `undefined`
202
203The connection string URI.
204
205Merged into the options object as options.uri.
206
207### options
208
209Type: `Object`
210
211The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
212
213#### options.namespace
214
215Type: `String`<br>
216Default: `'keyv'`
217
218Namespace for the current instance.
219
220#### options.ttl
221
222Type: `Number`<br>
223Default: `undefined`
224
225Default TTL. Can be overridden by specififying a TTL on `.set()`.
226
227#### options.serialize
228
229Type: `Function`<br>
230Default: `JSONB.stringify`
231
232A custom serialization function.
233
234#### options.deserialize
235
236Type: `Function`<br>
237Default: `JSONB.parse`
238
239A custom deserialization function.
240
241#### options.store
242
243Type: `Storage adapter instance`<br>
244Default: `new Map()`
245
246The storage adapter instance to be used by Keyv.
247
248#### options.adapter
249
250Type: `String`<br>
251Default: `undefined`
252
253Specify an adapter to use. e.g `'redis'` or `'mongodb'`.
254
255### Instance
256
257Keys must always be strings. Values can be of any type.
258
259#### .set(key, value, [ttl])
260
261Set a value.
262
263By default keys are persistent. You can set an expiry TTL in milliseconds.
264
265Returns a promise which resolves to `true`.
266
267#### .get(key, [options])
268
269Returns a promise which resolves to the retrieved value.
270
271##### options.raw
272
273Type: `Boolean`<br>
274Default: `false`
275
276If set to true the raw DB object Keyv stores internally will be returned instead of just the value.
277
278This contains the TTL timestamp.
279
280#### .delete(key)
281
282Deletes an entry.
283
284Returns a promise which resolves to `true` if the key existed, `false` if not.
285
286#### .clear()
287
288Delete all entries in the current namespace.
289
290Returns a promise which is resolved when the entries have been cleared.
291
292# How to Contribute
293
294In this section of the documentation we will cover:
295
2961) How to set up this repository locally
2972) How to get started with running commands
2983) How to contribute changes using Pull Requests
299
300## Dependencies
301
302This package requires the following dependencies to run:
303
3041) [Yarn V1](https://yarnpkg.com/getting-started/install)
3052) [Lerna](https://lerna.js.org/)
3063) [Docker](https://docs.docker.com/get-docker/)
307
308## Setting up your workspace
309
310To contribute to this repository, start by setting up this project locally:
311
3121) Fork this repository into your Git account
3132) Clone the forked repository to your local directory using `git clone`
3143) Install any of the above missing dependencies
315
316## Launching the project
317
318Once the project is installed locally, you are ready to start up its services:
319
3201) Ensure that your Docker service is running.
3212) From the root directory of your project, run the `yarn` command in the command prompt to install yarn.
3223) Run the `yarn bootstrap` command to install any necessary dependencies.
3234) Run `yarn test:services:start` to start up this project's Docker container. The container will launch all services within your workspace.
324
325## Available Commands
326
327Once the project is running, you can execute a variety of commands. The root workspace and each subpackage contain a `package.json` file with a `scripts` field listing all the commands that can be executed from that directory. This project also supports native `yarn`, `lerna`, and `docker` commands.
328
329Here, we'll cover the primary commands that can be executed from the root directory. Unless otherwise noted, these commands can also be executed from a subpackage. If executed from a subpackage, they will only affect that subpackage, rather than the entire workspace.
330
331### `yarn`
332
333The `yarn` command installs yarn in the workspace.
334
335### `yarn bootstrap`
336
337The `yarn bootstrap` command installs all dependencies in the workspace.
338
339### `yarn test:services:start`
340
341The `yarn test:services:start` command starts up the project's Docker container, launching all services in the workspace. This command must be executed from the root directory.
342
343### `yarn test:services:stop`
344
345The `yarn test:services:stop` command brings down the project's Docker container, halting all services. This command must be executed from the root directory.
346
347### `yarn test`
348
349The `yarn test` command runs all tests in the workspace.
350
351### `yarn clean`
352
353The `yarn clean` command removes yarn and all dependencies installed by yarn. After executing this command, you must repeat the steps in *Setting up your workspace* to rebuild your workspace.
354
355## Contributing Changes
356
357Now that you've set up your workspace, you're ready to contribute changes to the `keyv` repository.
358
3591) Make any changes that you would like to contribute in your local workspace.
3602) After making these changes, ensure that the project's tests still pass by executing the `yarn test` command in the root directory.
3613) Commit your changes and push them to your forked repository.
3624) Navigate to the original `keyv` repository and go the *Pull Requests* tab.
3635) Click the *New pull request* button, and open a pull request for the branch in your repository that contains your changes.
3646) Once your pull request is created, ensure that all checks have passed and that your branch has no conflicts with the base branch. If there are any issues, resolve these changes in your local repository, and then commit and push them to git.
3657) Similarly, respond to any reviewer comments or requests for changes by making edits to your local repository and pushing them to Git.
3668) Once the pull request has been reviewed, those with write access to the branch will be able to merge your changes into the `keyv` repository.
367
368If you need more information on the steps to create a pull request, you can find a detailed walkthrough in the [Github documentation](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork)
369
370## License
371
372MIT © Jared Wray & Luke Childs