UNPKG

enmap

Version:

A simple database wrapper to make sqlite database interactions much easier for beginners, with additional array helper methods.

61 lines 3.33 kB
import Database from 'better-sqlite3'; export interface EnmapOptions<V = unknown, SV = unknown> { name?: string; dataDir?: string; ensureProps?: boolean; autoEnsure?: V; serializer?: (value: V, key: string) => SV; deserializer?: (value: SV, key: string) => V; inMemory?: boolean; sqliteOptions?: Database.Options; } type MathOps = 'add' | 'addition' | '+' | 'sub' | 'subtract' | '-' | 'mult' | 'multiply' | '*' | 'div' | 'divide' | '/' | 'exp' | 'exponent' | '^' | 'mod' | 'modulo' | '%' | 'rand' | 'random'; type Path<T, Key extends keyof T = keyof T> = Key extends string ? T[Key] extends Record<string, any> ? `${Key}.${Path<T[Key], Exclude<keyof T[Key], keyof any[]>> & string}` | `${Key}.${Exclude<keyof T[Key], keyof any[]> & string}` | Key : Key : never; /** * A simple, synchronous, fast key/value storage build around better-sqlite3. * Contains extra utility methods for managing arrays and objects. */ export default class Enmap<V = any, SV = unknown> { #private; constructor(options: EnmapOptions<V, SV>); set(key: string, value: any, path?: Path<V>): this; get(key: string, path?: Path<V>): any; has(key: string): boolean; delete(key: string, path?: Path<V>): this; clear(): void; get size(): number; get count(): number; get length(): number; get db(): Database.Database; get autonum(): string; keys(): string[]; indexes(): string[]; values(): V[]; entries(): [string, V][]; update(key: string, valueOrFunction: Partial<V> | ((data: V) => V)): V; observe(key: string, path?: Path<V>): any; push(key: string, value: V, path?: Path<V>, allowDupes?: boolean): this; math(key: string, operation: MathOps, operand: number, path?: Path<V>): number | null; inc(key: string, path?: Path<V>): this; dec(key: string, path?: Path<V>): this; ensure(key: string, defaultValue: any, path?: Path<V>): any; includes(key: string, value: V, path?: Path<V>): boolean; remove(key: string, val: V | ((value: V) => boolean), path?: Path<V>): this; export(): string; import(data: string, overwrite?: boolean, clear?: boolean): this; static multi<V = unknown, SV = unknown>(names: string[], options?: Omit<EnmapOptions<V, SV>, 'name'>): Record<string, Enmap<V, SV>>; random(count?: number): [string, V][]; randomKey(count?: number): string[]; every(valueOrFunction: ((val: V, key: string) => boolean) | any, path?: Path<V>): boolean; some(valueOrFunction: ((val: V, key: string) => boolean) | any, path?: Path<V>): boolean; map<R>(pathOrFn: ((val: V, key: string) => R) | string): R[]; find(pathOrFn: ((val: V, key: string) => boolean) | string, value?: any): V | null; findIndex(pathOrFn: ((val: V, key: string) => boolean) | string, value?: any): string | null; reduce<R>(predicate: (accumulator: R, val: V, key: string) => R, initialValue: R): R; filter(pathOrFn: ((val: V, key: string) => boolean) | string, value?: any): V[]; sweep(pathOrFn: ((val: V, key: string) => boolean) | string, value?: any): number; changed(cb: (key: string, oldValue: V | undefined, newValue: V | undefined) => void): void; partition(pathOrFn: ((val: V, key: string) => boolean) | string, value?: any): [V[], V[]]; } export {}; //# sourceMappingURL=index.d.ts.map