[d565449] | 1 | import {EventEmitter} from 'events';
|
---|
| 2 |
|
---|
| 3 | type WithRequiredProperties<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
---|
| 4 |
|
---|
| 5 | declare class Keyv<Value = any, Options extends Record<string, any> = Record<string, unknown>> extends EventEmitter {
|
---|
| 6 | /**
|
---|
| 7 | * `this.opts` is an object containing at least the properties listed
|
---|
| 8 | * below. However, `Keyv.Options` allows arbitrary properties as well.
|
---|
| 9 | * These properties can be specified as the second type parameter to `Keyv`.
|
---|
| 10 | */
|
---|
| 11 | opts: WithRequiredProperties<
|
---|
| 12 | Keyv.Options<Value>,
|
---|
| 13 | 'deserialize' | 'namespace' | 'serialize' | 'store' | 'uri'
|
---|
| 14 | > &
|
---|
| 15 | Options;
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
|
---|
| 19 | */
|
---|
| 20 | constructor(options?: Keyv.Options<Value> & Options);
|
---|
| 21 | /**
|
---|
| 22 | * @param uri The connection string URI.
|
---|
| 23 | *
|
---|
| 24 | * Merged into the options object as options.uri.
|
---|
| 25 | * @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
|
---|
| 26 | */
|
---|
| 27 | constructor(uri?: string, options?: Keyv.Options<Value> & Options);
|
---|
| 28 |
|
---|
| 29 | /** Returns the value. */
|
---|
| 30 | get(key: string, options?: {raw?: false}): Promise<Value | undefined>;
|
---|
| 31 | /** Returns the raw value. */
|
---|
| 32 | get(key: string, options: {raw: true}): Promise<Keyv.DeserializedData<Value> | undefined>;
|
---|
| 33 |
|
---|
| 34 | /** Returns an array of values. Uses `store.getMany` if it exists, otherwise uses parallel calls to `store.get`. */
|
---|
| 35 | get(key: string[], options?: {raw?: false}): Promise<Array<Value | undefined>>;
|
---|
| 36 | /** Returns an array of raw values. Uses `store.getMany` if it exists, otherwise uses parallel calls to `store.get`. */
|
---|
| 37 | get(key: string[], options: {raw: true}): Promise<Array<Keyv.DeserializedData<Value> | undefined>>;
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Set a value.
|
---|
| 41 | *
|
---|
| 42 | * By default keys are persistent. You can set an expiry TTL in milliseconds.
|
---|
| 43 | */
|
---|
| 44 | set(key: string, value: Value, ttl?: number): Promise<true>;
|
---|
| 45 | /**
|
---|
| 46 | * Deletes an entry.
|
---|
| 47 | *
|
---|
| 48 | * Returns `true` if the key existed, `false` if not.
|
---|
| 49 | */
|
---|
| 50 | delete(key: string | string[]): Promise<boolean>;
|
---|
| 51 | /** Delete all entries in the current namespace. */
|
---|
| 52 | clear(): Promise<void>;
|
---|
| 53 | /** Check if key exists in current namespace. */
|
---|
| 54 | has(key: string): Promise<boolean>;
|
---|
| 55 | /** Iterator */
|
---|
| 56 | iterator(namespace?: string): AsyncGenerator<any, void, any>;
|
---|
| 57 | /**
|
---|
| 58 | * Closes the connection.
|
---|
| 59 | *
|
---|
| 60 | * Returns `undefined` when the connection closes.
|
---|
| 61 | */
|
---|
| 62 | disconnect(): Promise<void>;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | declare namespace Keyv {
|
---|
| 66 | interface Options<Value> {
|
---|
| 67 | [key: string]: any;
|
---|
| 68 |
|
---|
| 69 | /** Namespace for the current instance. */
|
---|
| 70 | namespace?: string | undefined;
|
---|
| 71 | /** A custom serialization function. */
|
---|
| 72 | serialize?: ((data: DeserializedData<Value>) => string) | undefined;
|
---|
| 73 | /** A custom deserialization function. */
|
---|
| 74 | deserialize?: ((data: string) => DeserializedData<Value> | undefined) | undefined;
|
---|
| 75 | /** The connection string URI. */
|
---|
| 76 | uri?: string | undefined;
|
---|
| 77 | /** The storage adapter instance to be used by Keyv. */
|
---|
| 78 | store?: Store<string | undefined> | undefined;
|
---|
| 79 | /** Default TTL. Can be overridden by specififying a TTL on `.set()`. */
|
---|
| 80 | ttl?: number | undefined;
|
---|
| 81 | /** Specify an adapter to use. e.g `'redis'` or `'mongodb'`. */
|
---|
| 82 | adapter?: 'redis' | 'mongodb' | 'mongo' | 'sqlite' | 'postgresql' | 'postgres' | 'mysql' | undefined;
|
---|
| 83 | /** Enable compression option **/
|
---|
| 84 | compression?: CompressionAdapter | undefined;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | interface CompressionAdapter {
|
---|
| 88 | compress(value: any, options?: any): Promise<any>;
|
---|
| 89 | decompress(value: any, options?: any): Promise<any>;
|
---|
| 90 | serialize(value: any): Promise<any>;
|
---|
| 91 | deserialize(value: any): Promise<any>;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | interface DeserializedData<Value> {
|
---|
| 95 | value: Value; expires: number | undefined;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | type StoredData<Value> = DeserializedData<Value> | string | undefined;
|
---|
| 99 |
|
---|
| 100 | interface Store<Value> {
|
---|
| 101 | get(key: string): Value | Promise<Value | undefined> | undefined;
|
---|
| 102 | set(key: string, value: Value, ttl?: number): any;
|
---|
| 103 | delete(key: string): boolean | Promise<boolean>;
|
---|
| 104 | clear(): void | Promise<void>;
|
---|
| 105 | has?(key: string): boolean | Promise<boolean>;
|
---|
| 106 | getMany?(
|
---|
| 107 | keys: string[]
|
---|
| 108 | ): Array<StoredData<Value>> | Promise<Array<StoredData<Value>>> | undefined;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | export = Keyv;
|
---|