UNPKG

@keyv/mysql

Version:

MySQL/MariaDB storage adapter for Keyv

100 lines (96 loc) 3.64 kB
import EventEmitter from 'node:events'; import { KeyvStoreAdapter, StoredData } from 'keyv'; import mysql, { ConnectionOptions } from 'mysql2'; type KeyvMysqlOptions = { dialect?: "mysql"; uri?: string; table?: string; keySize?: number; intervalExpiration?: number; iterationLimit?: string | number; } & ConnectionOptions; type QueryType<T> = Promise<T extends mysql.RowDataPacket[][] | mysql.RowDataPacket[] | mysql.ResultSetHeader | mysql.ResultSetHeader[] ? T : never>; /** * MySQL storage adapter for Keyv. * Provides a persistent key-value store using MySQL as the backend. */ declare class KeyvMysql extends EventEmitter implements KeyvStoreAdapter { /** * Indicates whether TTL (Time To Live) support is enabled. * Set to true when intervalExpiration is configured. */ ttlSupport: boolean; /** * Configuration options for the MySQL adapter. */ opts: KeyvMysqlOptions; /** * Optional namespace for key prefixing. */ namespace?: string; /** * Query function for executing SQL statements against the MySQL database. */ query: <T>(sqlString: string) => QueryType<T>; /** * Creates a new KeyvMysql instance. * @param keyvOptions - Configuration options or connection URI string */ constructor(keyvOptions?: KeyvMysqlOptions | string); /** * Retrieves a value from the store by key. * @param key - The key to retrieve * @returns The stored value or undefined if not found */ get<Value>(key: string): Promise<StoredData<Value>>; /** * Retrieves multiple values from the store by their keys. * @param keys - Array of keys to retrieve * @returns Array of stored values in the same order as the input keys, with undefined for missing keys */ getMany<Value>(keys: string[]): Promise<StoredData<Value>[]>; /** * Sets a value in the store for the given key. * If the key already exists, it will be updated. * @param key - The key to set * @param value - The value to store * @returns Promise that resolves when the operation completes */ set(key: string, value: any): Promise<never>; /** * Deletes a key-value pair from the store. * @param key - The key to delete * @returns True if the key existed and was deleted, false if the key did not exist */ delete(key: string): Promise<boolean>; /** * Deletes multiple key-value pairs from the store. * @param key - Array of keys to delete * @returns True if at least one key was deleted, false if no keys were found */ deleteMany(key: string[]): Promise<boolean>; /** * Clears all entries from the store. * If a namespace is set, only entries within that namespace are cleared. * @returns Promise that resolves when the operation completes */ clear(): Promise<void>; /** * Returns an async iterator for iterating over all key-value pairs in the store. * @param namespace - Optional namespace to filter results * @yields Arrays containing [key, value] pairs */ iterator(namespace?: string): AsyncGenerator<any, void, any>; /** * Checks if a key exists in the store. * @param key - The key to check * @returns True if the key exists, false otherwise */ has(key: string): Promise<boolean>; /** * Disconnects from the MySQL database and closes the connection pool. * @returns Promise that resolves when disconnected */ disconnect(): Promise<void>; } export { KeyvMysql, type KeyvMysqlOptions, KeyvMysql as default };