// Type definitions for Lowdb 1.0 // Project: https://github.com/typicode/lowdb // Definitions by: typicode // Bazyli Brzóska // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.1 import { LoDashStatic, ObjectChain } from "lodash"; declare module "lodash" { interface ObjectChain { /** * @description Be careful: This function overwrites the whole database. */ write(): T & Promise; } interface PrimitiveChain { write(): T & Promise; } interface CollectionChain { write(): ArrayLike & Promise>; } interface FunctionChain { write(): T & Promise; } interface StringChain { write(): string & Promise; } } declare let Lowdb: Lowdb.lowdb; export = Lowdb; declare namespace Lowdb { interface AdapterOptions { defaultValue?: SchemaT; serialize?: (data: SchemaT) => string; deserialize?: (serializedData: string) => SchemaT; } interface BaseAdapter extends AdapterOptions { readonly "@@reference": SchemaT; new ( source: string, options?: AdapterOptions ): BaseAdapter; source: string; } interface AdapterSync extends BaseAdapter { new ( source: string, options?: AdapterOptions ): AdapterSync; write(state: object): void; } interface AdapterAsync extends BaseAdapter { new ( source: string, options?: AdapterOptions ): AdapterAsync; write(state: object): Promise; } interface LowdbBase { getState: () => SchemaT; setState: (state: SchemaT) => this; } interface LowdbSync extends LowdbBase, ObjectChain { _: LoDashStatic; read: () => this; /** * @description Be careful: This function overwrites the whole database. */ write(returnValue?: T): T & Promise; } interface LowdbAsync extends LowdbBase, ObjectChain { _: LoDashStatic; read: () => Promise; /** * @description Be careful: This function overwrites the whole database. */ write(returnValue?: T): T & Promise; } interface LowdbFpSync extends LowdbBase { /** * @description Be careful: This function overwrites the whole database. */ write(returnValue?: T): T; /** * @description Returns a function that allows you to access/modify the database at a given path. * @example * ```js * const posts = db('posts') * const firstPost = posts(all => all[0]) * posts.write((allPosts) => [...allPosts, {title: 'Yup!'}]) * ``` */ ( path: TKey | [TKey], defaultValue?: SchemaT[TKey] ): FpReturnSync; ( path: [TKey, TSubKey], defaultValue?: SchemaT[TKey][TSubKey] ): FpReturnSync; < TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey], TSubKey2 extends keyof SchemaT[TKey][TSubKey] >( path: [TKey, TSubKey, TSubKey2], defaultValue?: SchemaT[TKey][TSubKey][TSubKey2] ): FpReturnSync; < TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey], TSubKey2 extends keyof SchemaT[TKey][TSubKey], TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2] >( path: [TKey, TSubKey, TSubKey2, TSubKey3], defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3] ): FpReturnSync; < TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey], TSubKey2 extends keyof SchemaT[TKey][TSubKey], TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2], TSubKey4 extends keyof SchemaT[TKey][TSubKey][TSubKey2][TSubKey3] >( path: [TKey, TSubKey, TSubKey2, TSubKey3, TSubKey4], defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4] ): FpReturnSync; (path: string | string[], defaultValue?: T): FpReturnSync; } interface LowdbFpAsync extends LowdbBase { /** * @description Be careful: This function overwrites the whole database. */ write(returnValue?: T): Promise; /** * @description Returns a function that allows you to access/modify the database at a given path. * @example * ```js * const posts = db('posts') * const firstPost = posts(all => all[0]) * posts.write((allPosts) => [...allPosts, {title: 'Yup!'}]) * ``` */ ( path: TKey | [TKey], defaultValue?: SchemaT[TKey] ): FpReturnAsync; ( path: [TKey, TSubKey], defaultValue?: SchemaT[TKey][TSubKey] ): FpReturnAsync; < TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey], TSubKey2 extends keyof SchemaT[TKey][TSubKey] >( path: [TKey, TSubKey, TSubKey2], defaultValue?: SchemaT[TKey][TSubKey][TSubKey2] ): FpReturnAsync; < TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey], TSubKey2 extends keyof SchemaT[TKey][TSubKey], TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2] >( path: [TKey, TSubKey, TSubKey2, TSubKey3], defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3] ): FpReturnAsync; < TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey], TSubKey2 extends keyof SchemaT[TKey][TSubKey], TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2], TSubKey4 extends keyof SchemaT[TKey][TSubKey][TSubKey2][TSubKey3] >( path: [TKey, TSubKey, TSubKey2, TSubKey3, TSubKey4], defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4] ): FpReturnAsync; (path: string | string[], defaultValue?: T): FpReturnAsync; } interface FpReturnBase { /** * Execute a series of functions on the data at a given path. * Result of previous function is the input of the next one. * Returns the result of the last function. */ (f1: (a1: PathT) => R1): R1; // (f1: [(a1: PathT) => R1]): R1; (f1: [(a1: PathT) => R1, (a: R1) => R2]): R2; (f1: [(a1: PathT) => R1, (a: R1) => R2, (a: R2) => R3]): R3; ( f1: [(a1: PathT) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4] ): R4; ( f1: [ (a1: PathT) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4, (a: R4) => R5 ] ): R5; ( f1: [ (a1: PathT) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4, (a: R4) => R5, (a: R5) => R6 ] ): R6; ( f1: [ (a1: PathT) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4, (a: R4) => R5, (a: R5) => R6, (a: R6) => R7 ] ): R7; (funcs: Array<(a: any) => any>): any; } interface FpReturnSync extends FpReturnBase { /** * @description Writes the change to the database, based on the callback's return value. * @example * ```js * posts.write((allPosts) => [...allPosts, {title: 'Yup!'}]) * ``` */ write(f1: (a1: PathT) => R1): R1; } interface FpReturnAsync extends FpReturnBase { /** * @description Writes the change to the database, based on the callback's return value. * @example * ```js * posts.write((allPosts) => [...allPosts, {title: 'Yup!'}]) * ``` */ write(f1: (a1: PathT) => R1): Promise; } interface lowdb { (adapter: AdapterT): Promise>; (adapter: AdapterT): LowdbSync; } interface lowdbFp { (adapter: AdapterT): Promise>; (adapter: AdapterT): LowdbFpSync; } } type ReferenceProperty = "@@reference";