CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes45downloads
README.md430 linesDownload Raw Back to keyv
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 backends8 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## Features17 18There are a few existing modules similar to Keyv, however Keyv is different because it:19 20- Isn't bloated21- Has a simple Promise based API22- Suitable as a TTL based cache or persistent key-value store23- [Easily embeddable](#add-cache-support-to-your-module) inside another module24- Works with any storage that implements the [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) API25- Handles all JSON types plus `Buffer`26- Supports namespaces27- Wide range of [**efficient, well tested**](#official-storage-adapters) storage adapters28- Connection errors are passed through (db failures won't kill your app)29- Supports the current active LTS version of Node.js or higher30 31## Usage32 33Install Keyv.34 35```36npm install --save keyv37```38 39By default everything is stored in memory, you can optionally also install a storage adapter.40 41```42npm install --save @keyv/redis43npm install --save @keyv/mongo44npm install --save @keyv/sqlite45npm install --save @keyv/postgres46npm install --save @keyv/mysql47npm install --save @keyv/etcd48```49 50Create a new Keyv instance, passing your connection string if applicable. Keyv will automatically load the correct storage adapter.51 52```js53const Keyv = require('keyv');54 55// One of the following56const 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 errors65keyv.on('error', err => console.log('Connection Error', err));66 67await keyv.set('foo', 'expires in 1 second', 1000); // true68await keyv.set('foo', 'never expires'); // true69await keyv.get('foo'); // 'never expires'70await keyv.delete('foo'); // true71await keyv.clear(); // undefined72```73 74### Namespaces75 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```js79const 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'); // true83await cache.set('foo', 'cache'); // true84await users.get('foo'); // 'users'85await cache.get('foo'); // 'cache'86await users.clear(); // undefined87await users.get('foo'); // undefined88await cache.get('foo'); // 'cache'89```90 91### Custom Serializers92 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```js98const 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 Adapters104 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 TTL108---|---|---109Redis | [@keyv/redis](https://github.com/jaredwray/keyv/tree/master/packages/redis) | Yes110MongoDB | [@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) | Yes115Memcache | [@keyv/memcache](https://github.com/jaredwray/keyv/tree/master/packages/memcache) | Yes116 117## Third-party Storage Adapters118 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```js122const 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```js131new 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```js137const 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) cache147- [keyv-file](https://github.com/zaaack/keyv-file) - File system storage adapter for Keyv148- [keyv-dynamodb](https://www.npmjs.com/package/keyv-dynamodb) - DynamoDB storage adapter for Keyv149- [keyv-lru](https://www.npmjs.com/package/keyv-lru) - LRU storage adapter for Keyv150- [keyv-null](https://www.npmjs.com/package/keyv-null) - Null storage adapter for Keyv151- [keyv-firestore ](https://github.com/goto-bus-stop/keyv-firestore) – Firebase Cloud Firestore adapter for Keyv152- [keyv-mssql](https://github.com/pmorgan3/keyv-mssql) - Microsoft Sql Server adapter for Keyv153- [keyv-azuretable](https://github.com/howlowck/keyv-azuretable) - Azure Table Storage/API adapter for Keyv154- [keyv-arango](https://github.com/TimMikeladze/keyv-arango) - ArangoDB storage adapter for Keyv155- [keyv-momento](https://github.com/momentohq/node-keyv-adaptor/) - Momento storage adapter for Keyv156 157## Add Cache Support to your Module158 159Keyv 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.160 161You should also set a namespace for your module so you can safely call `.clear()` without clearing unrelated app data.162 163Inside your module:164 165```js166class AwesomeModule {167	constructor(opts) {168		this.cache = new Keyv({169			uri: typeof opts.cache === 'string' && opts.cache,170			store: typeof opts.cache !== 'string' && opts.cache,171			namespace: 'awesome-module'172		});173	}174}175```176 177Now it can be consumed like this:178 179```js180const AwesomeModule = require('awesome-module');181 182// Caches stuff in memory by default183const awesomeModule = new AwesomeModule();184 185// After npm install --save keyv-redis186const awesomeModule = new AwesomeModule({ cache: 'redis://localhost' });187 188// Some third-party module that implements the Map API189const awesomeModule = new AwesomeModule({ cache: some3rdPartyStore });190```191 192## Compression193 194Keyv supports `gzip` and `brotli` compression. To enable compression, pass the `compress` option to the constructor.195 196```js197const KeyvGzip = require('@keyv/compress-gzip');198const Keyv = require('keyv');199 200const keyvGzip = new KeyvGzip();201const keyv = new Keyv({ compression: KeyvGzip });202```203 204You can also pass a custom compression function to the `compression` option. Following the pattern of the official compression adapters.205 206### Want to build your own? 207 208Great! Keyv is designed to be easily extended. You can build your own compression adapter by following the pattern of the official compression adapters based on this interface:209 210```typescript211interface CompressionAdapter {212	async compress(value: any, options?: any);213	async decompress(value: any, options?: any);214	async serialize(value: any);215	async deserialize(value: any);216}217```218 219In addition to the interface, you can test it with our compression test suite using @keyv/test-suite:220 221```js222const {keyvCompresstionTests} = require('@keyv/test-suite');223const KeyvGzip = require('@keyv/compress-gzip');224 225keyvCompresstionTests(test, new KeyvGzip());226```227 228## API229 230### new Keyv([uri], [options])231 232Returns a new Keyv instance.233 234The Keyv instance is also an `EventEmitter` that will emit an `'error'` event if the storage adapter connection fails.235 236### uri237 238Type: `String`<br>239Default: `undefined`240 241The connection string URI.242 243Merged into the options object as options.uri.244 245### options246 247Type: `Object`248 249The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.250 251#### options.namespace252 253Type: `String`<br>254Default: `'keyv'`255 256Namespace for the current instance.257 258#### options.ttl259 260Type: `Number`<br>261Default: `undefined`262 263Default TTL. Can be overridden by specififying a TTL on `.set()`.264 265#### options.compression266 267Type: `@keyv/compress-<compression_package_name>`<br>268Default: `undefined`269 270Compression package to use. See [Compression](#compression) for more details.271 272#### options.serialize273 274Type: `Function`<br>275Default: `JSONB.stringify`276 277A custom serialization function.278 279#### options.deserialize280 281Type: `Function`<br>282Default: `JSONB.parse`283 284A custom deserialization function.285 286#### options.store287 288Type: `Storage adapter instance`<br>289Default: `new Map()`290 291The storage adapter instance to be used by Keyv.292 293#### options.adapter294 295Type: `String`<br>296Default: `undefined`297 298Specify an adapter to use. e.g `'redis'` or `'mongodb'`.299 300### Instance301 302Keys must always be strings. Values can be of any type.303 304#### .set(key, value, [ttl])305 306Set a value.307 308By default keys are persistent. You can set an expiry TTL in milliseconds.309 310Returns a promise which resolves to `true`.311 312#### .get(key, [options])313 314Returns a promise which resolves to the retrieved value.315 316##### options.raw317 318Type: `Boolean`<br>319Default: `false`320 321If set to true the raw DB object Keyv stores internally will be returned instead of just the value.322 323This contains the TTL timestamp.324 325#### .delete(key)326 327Deletes an entry.328 329Returns a promise which resolves to `true` if the key existed, `false` if not.330 331#### .clear()332 333Delete all entries in the current namespace.334 335Returns a promise which is resolved when the entries have been cleared.336 337#### .iterator()338 339Iterate over all entries of the current namespace.340 341Returns a iterable that can be iterated by for-of loops. For example:342 343```js344// please note that the "await" keyword should be used here345for await (const [key, value] of this.keyv.iterator()) {346  console.log(key, value);347};348```349 350# How to Contribute351 352In this section of the documentation we will cover:353 3541) How to set up this repository locally3552) How to get started with running commands3563) How to contribute changes using Pull Requests357 358## Dependencies359 360This package requires the following dependencies to run:361 3621) [Yarn V1](https://yarnpkg.com/getting-started/install)3633) [Docker](https://docs.docker.com/get-docker/)364 365## Setting up your workspace366 367To contribute to this repository, start by setting up this project locally:368 3691) Fork this repository into your Git account3702) Clone the forked repository to your local directory using `git clone`3713) Install any of the above missing dependencies372 373## Launching the project374 375Once the project is installed locally, you are ready to start up its services:376 3771) Ensure that your Docker service is running.3782) From the root directory of your project, run the `yarn` command in the command prompt to install yarn.3793) Run the `yarn bootstrap` command to  install any necessary dependencies.3804) Run `yarn test:services:start` to start up this project's Docker container. The container will launch all services within your workspace.381 382## Available Commands383 384Once 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`, and `docker` commands.385 386Here, 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.387 388### `yarn`389 390The `yarn` command installs yarn in the workspace.391 392### `yarn bootstrap`393 394The `yarn bootstrap` command installs all dependencies in the workspace.395 396### `yarn test:services:start`397 398The `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.399 400### `yarn test:services:stop`401 402The `yarn test:services:stop` command brings down the project's Docker container, halting all services. This command must be executed from the root directory.403 404### `yarn test`405 406The `yarn test` command runs all tests in the workspace.407 408### `yarn clean`409 410The `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.411 412## Contributing Changes413 414Now that you've set up your workspace, you're ready to contribute changes to the `keyv` repository.415 4161) Make any changes that you would like to contribute in your local workspace.4172) After making these changes, ensure that the project's tests still pass by executing the `yarn test` command in the root directory.4183) Commit your changes and push them to your forked repository.4194) Navigate to the original `keyv` repository and go the *Pull Requests* tab.4205) Click the *New pull request* button, and open a pull request for the branch in your repository that contains your changes.4216) 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.4227) Similarly, respond to any reviewer comments or requests for changes by making edits to your local repository and pushing them to Git.4238) 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.424 425If 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)426 427## License428 429MIT © Jared Wray430 
basant307/AI_Governance_Project · CoolFace