UNPKG

14.2 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/main/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
115Memcache | [@keyv/memcache](https://github.com/jaredwray/keyv/tree/master/packages/memcache) | Yes
116
117## Third-party Storage Adapters
118
119You 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.
120
121```js
122const Keyv = require('keyv');
123const myAdapter = require('./my-storage-adapter');
124
125const keyv = new Keyv({ store: myAdapter });
126```
127
128Any store that follows the [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) api will work.
129
130```js
131new Keyv({ store: new Map() });
132```
133
134For example, [`quick-lru`](https://github.com/sindresorhus/quick-lru) is a completely unrelated module that implements the Map API.
135
136```js
137const Keyv = require('keyv');
138const QuickLRU = require('quick-lru');
139
140const lru = new QuickLRU({ maxSize: 1000 });
141const keyv = new Keyv({ store: lru });
142```
143
144The following are third-party storage adapters compatible with Keyv:
145
146- [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple "Least Recently Used" (LRU) cache
147- [keyv-file](https://github.com/zaaack/keyv-file) - File system storage adapter for Keyv
148- [keyv-dynamodb](https://www.npmjs.com/package/keyv-dynamodb) - DynamoDB storage adapter for Keyv
149- [keyv-lru](https://www.npmjs.com/package/keyv-lru) - LRU storage adapter for Keyv
150- [keyv-null](https://www.npmjs.com/package/keyv-null) - Null storage adapter for Keyv
151- [keyv-firestore ](https://github.com/goto-bus-stop/keyv-firestore) – Firebase Cloud Firestore adapter for Keyv
152- [keyv-mssql](https://github.com/pmorgan3/keyv-mssql) - Microsoft Sql Server 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#### .iterator()
293
294Iterate over all entries of the current namespace.
295
296Returns a iterable that can be iterated by for-of loops. For example:
297
298```js
299// please note that the "await" keyword should be used here
300for await (const [key, value] of this.keyv.iterator()) {
301 console.log(key, value);
302};
303```
304
305# How to Contribute
306
307In this section of the documentation we will cover:
308
3091) How to set up this repository locally
3102) How to get started with running commands
3113) How to contribute changes using Pull Requests
312
313## Dependencies
314
315This package requires the following dependencies to run:
316
3171) [Yarn V1](https://yarnpkg.com/getting-started/install)
3182) [Lerna](https://lerna.js.org/)
3193) [Docker](https://docs.docker.com/get-docker/)
320
321## Setting up your workspace
322
323To contribute to this repository, start by setting up this project locally:
324
3251) Fork this repository into your Git account
3262) Clone the forked repository to your local directory using `git clone`
3273) Install any of the above missing dependencies
328
329## Launching the project
330
331Once the project is installed locally, you are ready to start up its services:
332
3331) Ensure that your Docker service is running.
3342) From the root directory of your project, run the `yarn` command in the command prompt to install yarn.
3353) Run the `yarn bootstrap` command to install any necessary dependencies.
3364) Run `yarn test:services:start` to start up this project's Docker container. The container will launch all services within your workspace.
337
338## Available Commands
339
340Once 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.
341
342Here, 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.
343
344### `yarn`
345
346The `yarn` command installs yarn in the workspace.
347
348### `yarn bootstrap`
349
350The `yarn bootstrap` command installs all dependencies in the workspace.
351
352### `yarn test:services:start`
353
354The `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.
355
356### `yarn test:services:stop`
357
358The `yarn test:services:stop` command brings down the project's Docker container, halting all services. This command must be executed from the root directory.
359
360### `yarn test`
361
362The `yarn test` command runs all tests in the workspace.
363
364### `yarn clean`
365
366The `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.
367
368## Contributing Changes
369
370Now that you've set up your workspace, you're ready to contribute changes to the `keyv` repository.
371
3721) Make any changes that you would like to contribute in your local workspace.
3732) After making these changes, ensure that the project's tests still pass by executing the `yarn test` command in the root directory.
3743) Commit your changes and push them to your forked repository.
3754) Navigate to the original `keyv` repository and go the *Pull Requests* tab.
3765) Click the *New pull request* button, and open a pull request for the branch in your repository that contains your changes.
3776) 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.
3787) Similarly, respond to any reviewer comments or requests for changes by making edits to your local repository and pushing them to Git.
3798) 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.
380
381If 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)
382
383## License
384
385MIT © Jared Wray