Simple key-value storage with support for multiple backends
npx @tessl/cli install tessl/npm-keyv@5.5.0Keyv is a simple key-value storage library that provides a consistent interface for key-value storage across multiple backends via storage adapters. It supports TTL-based expiry, making it suitable as both a cache and persistent key-value store, with built-in event handling, hooks system, and comprehensive type safety.
npm install keyvimport Keyv from "keyv";
import { KeyvHooks, type KeyvOptions, type KeyvStoreAdapter } from "keyv";For CommonJS:
const Keyv = require("keyv");
const { KeyvHooks } = require("keyv");import Keyv from "keyv";
// Create instance with in-memory storage
const keyv = new Keyv();
// Basic operations
await keyv.set('user:1', { name: 'Alice', age: 30 }); // true
const user = await keyv.get('user:1'); // { name: 'Alice', age: 30 }
await keyv.delete('user:1'); // true
// TTL support (expires in 1 minute)
await keyv.set('temp-data', 'expires soon', 60000);
// Type safety
const keyv_typed = new Keyv<string>();
await keyv_typed.set('message', 'hello world');
const message = await keyv_typed.get('message'); // string | undefinedKeyv is built around several key components:
Primary key-value storage functionality including get, set, delete operations with TTL support and batch operations for performance.
class Keyv<GenericValue = any> {
constructor(options?: KeyvOptions);
constructor(store?: KeyvStoreAdapter | KeyvOptions | Map<any, any>, options?: Omit<KeyvOptions, 'store'>);
get<Value = GenericValue>(key: string, options?: {raw?: boolean}): Promise<StoredDataNoRaw<Value> | StoredDataRaw<Value>>;
get<Value = GenericValue>(key: string[], options?: {raw?: boolean}): Promise<Array<StoredDataNoRaw<Value>> | Array<StoredDataRaw<Value>>>;
set<Value = GenericValue>(key: string, value: Value, ttl?: number): Promise<boolean>;
delete(key: string | string[]): Promise<boolean>;
clear(): Promise<void>;
has(key: string | string[]): Promise<boolean | boolean[]>;
// Iterator for async iteration over entries (when supported by storage adapter)
iterator?: IteratorFunction;
}High-performance batch operations for working with multiple keys simultaneously, with optimizations for storage adapters that support native batch operations.
getMany<Value = GenericValue>(keys: string[], options?: {raw?: boolean}): Promise<Array<StoredDataNoRaw<Value>> | Array<StoredDataRaw<Value>>>;
setMany<Value = GenericValue>(entries: KeyvEntry[]): Promise<boolean[]>;
deleteMany(keys: string[]): Promise<boolean>;
hasMany(keys: string[]): Promise<boolean[]>;
interface KeyvEntry {
key: string;
value: any;
ttl?: number;
}Direct access to internal data structures including TTL metadata, useful for debugging and advanced use cases where expiry information is needed.
getRaw<Value = GenericValue>(key: string): Promise<StoredDataRaw<Value> | undefined>;
getManyRaw<Value = GenericValue>(keys: string[]): Promise<Array<StoredDataRaw<Value>>>;
type DeserializedData<Value> = {
value?: Value;
expires?: number | undefined;
};
type StoredDataRaw<Value> = DeserializedData<Value> | undefined;Pluggable storage backend system with support for Redis, MongoDB, SQLite, PostgreSQL, MySQL, and any Map-compatible storage.
interface KeyvStoreAdapter {
opts: any;
namespace?: string;
get<Value>(key: string): Promise<StoredData<Value> | undefined>;
set(key: string, value: any, ttl?: number): any;
delete(key: string): Promise<boolean>;
clear(): Promise<void>;
// Optional methods
setMany?(values: Array<{key: string; value: any; ttl?: number}>): Promise<void>;
getMany?<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>;
has?(key: string): Promise<boolean>;
hasMany?(keys: string[]): Promise<boolean[]>;
deleteMany?(key: string[]): Promise<boolean>;
disconnect?(): Promise<void>;
iterator?<Value>(namespace?: string): AsyncGenerator<Array<string | Awaited<Value> | undefined>, void>;
}Event system for connection monitoring and lifecycle management, plus hooks system for custom functionality on CRUD operations.
enum KeyvHooks {
PRE_SET = 'preSet',
POST_SET = 'postSet',
PRE_GET = 'preGet',
POST_GET = 'postGet',
PRE_GET_MANY = 'preGetMany',
POST_GET_MANY = 'postGetMany',
PRE_GET_RAW = 'preGetRaw',
POST_GET_RAW = 'postGetRaw',
PRE_GET_MANY_RAW = 'preGetManyRaw',
POST_GET_MANY_RAW = 'postGetManyRaw',
PRE_DELETE = 'preDelete',
POST_DELETE = 'postDelete'
}Configuration options and runtime property management for customizing serialization, compression, namespacing, and error handling behavior.
interface KeyvOptions {
emitErrors?: boolean;
namespace?: string;
serialize?: Serialize;
deserialize?: Deserialize;
store?: KeyvStoreAdapter | Map<any, any> | any;
ttl?: number;
compression?: CompressionAdapter | any;
stats?: boolean;
useKeyPrefix?: boolean;
throwOnErrors?: boolean;
}
interface CompressionAdapter {
compress(value: any, options?: any): Promise<any>;
decompress(value: any, options?: any): Promise<any>;
serialize<Value>(data: DeserializedData<Value>): Promise<string> | string;
deserialize<Value>(data: string): Promise<DeserializedData<Value> | undefined> | DeserializedData<Value> | undefined;
}type StoredDataNoRaw<Value> = Value | undefined;
type StoredData<Value> = StoredDataNoRaw<Value> | StoredDataRaw<Value>;
type Serialize = <Value>(data: DeserializedData<Value>) => Promise<string> | string;
type Deserialize = <Value>(data: string) => Promise<DeserializedData<Value> | undefined> | DeserializedData<Value> | undefined;
type IteratorFunction = (argument: any) => AsyncGenerator<any, void>;