Gathering detailed insights and metrics for @keyvhq/sql
Gathering detailed insights and metrics for @keyvhq/sql
Gathering detailed insights and metrics for @keyvhq/sql
Gathering detailed insights and metrics for @keyvhq/sql
npm install @keyvhq/sql
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
429 Stars
1,300 Commits
19 Forks
5 Watching
1 Branches
42 Contributors
Updated on 24 Nov 2024
JavaScript (88.64%)
TypeScript (6.18%)
HTML (5.09%)
Dockerfile (0.09%)
Cumulative downloads
Total Downloads
Last day
-36.7%
95
Compared to previous day
Last week
-24.8%
597
Compared to previous week
Last month
-6.4%
3,207
Compared to previous month
Last year
257.7%
31,287
Compared to previous year
1
5
Keyv is a simple key-value storage with support for multiple backend adapters (MySQL, PostgreSQL, SQLite, Redis, Mongo, DynamoDB, Firestore, Memcached, and more).
Forked from keyv, plus:
Buffer
.1npm install @keyvhq/core --save
You can optionally install the storage adapter you want to use:
1npm install @keyvhq/redis --save 2npm install @keyvhq/mongo --save 3npm install @keyvhq/sqlite --save 4npm install @keyvhq/postgres --save 5npm install @keyvhq/mysql --save
If you don't provide a specific storage adapter, a in-memory storage adapter is used by default.
Just create a new Keyv instance, using an specific storage adapter:
1const keyv = new Keyv() // in-memory, by default
2const keyvRedis = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379')})
3const keyvMongo = new Keyv({ store: new KeyvMongo('mongodb://user:pass@localhost:27017/dbname')})
4const keyvSQLite = new Keyv({ store: new KeyvSQLite('sqlite://path/to/database.sqlite')})
5const keyvPostgreSQL = new Keyv({ store: new KeyvPostgreSQL('postgresql://user:pass@localhost:5432/dbname')})
6const keyvMySQL = new Keyv({ store: new KeyvMySQL('mysql://user:pass@localhost:3306/dbname')})
7
8await keyv.set('foo', 'expires in 1 second', 1000) // true
9await keyv.set('foo', 'never expires') // true
10await keyv.get('foo') // 'never expires'
11await keyv.has('foo') // true
12await keyv.delete('foo') // true
13await keyv.has('foo') // false
14await keyv.clear() // undefined
You can namespace your Keyv instance to avoid key collisions and allow you to clear only a certain namespace while using the same database.
1const users = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379'), namespace: 'users' }) 2const cache = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379'), namespace: 'cache' }) 3 4await users.set('foo', 'users') // true 5await cache.set('foo', 'cache') // true 6await users.get('foo') // 'users' 7await cache.get('foo') // 'cache' 8await users.clear() // undefined 9await users.get('foo') // undefined 10await cache.get('foo') // 'cache'
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:
1const KeyvCompress = require('@keyvhq/compress') 2const Keyv = require('@keyvhq/core') 3 4const keyv = KeyvCompress( 5 new Keyv({ 6 serialize: v8.serialize, 7 deserialize: v8.deserialize 8 }) 9)
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.
1const KeyvRedis = require('@keyvhq/redis') 2const Keyv = require('@keyvhq/core') 3const got = require('got') 4 5const cache = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379') }) 6 7await 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.
1const Keyv = require('@keyvhq/core') 2const QuickLRU = require('quick-lru') 3 4const lru = new QuickLRU({ maxSize: 1000 }) 5const 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.
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.
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.
Returns a new Keyv instance.
Type: Object
The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
Type: String
Default: undefined
Namespace for the current instance.
Type: Number
Default: undefined
Default TTL in milliseconds. Can be overridden by specifying a TTL on .set()
.
Type: Function
Default: JSONB.stringify
A custom serialization function.
Type: Function
Default: JSONB.parse
A custom deserialization function.
Type: Storage adapter instance
Default: new Map()
The storage adapter instance to be used by Keyv.
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 a value.
By default keys are persistent. You can set an expiry TTL in milliseconds.
Returns a promise which resolves to true
.
Returns a promise which resolves to the retrieved value.
Returns a promise which resolves to a boolean, indicating existence of a key.
Deletes an entry.
Returns a promise which resolves to true
if the key existed, false
if not.
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.
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.
keyv © Luke Childs, released under the MIT License.
Maintained by Microlink with help from contributors.
microlink.io · GitHub microlinkhq · X @microlinkhq
No vulnerabilities found.
No security vulnerabilities found.