UNPKG

9.81 kBTypeScriptView Raw
1import { LoDashStatic, ObjectChain } from "lodash";
2declare module "lodash" {
3 interface ObjectChain<T> {
4 /**
5 * @description Be careful: This function overwrites the whole database.
6 */
7 write(): T & Promise<T>;
8 }
9 interface PrimitiveChain<T> {
10 write(): T & Promise<T>;
11 }
12 interface CollectionChain<T> {
13 write(): ArrayLike<T> & Promise<ArrayLike<T>>;
14 }
15 interface FunctionChain<T> {
16 write(): T & Promise<T>;
17 }
18 interface StringChain {
19 write(): string & Promise<string>;
20 }
21}
22
23declare let Lowdb: Lowdb.lowdb;
24export = Lowdb;
25
26declare namespace Lowdb {
27 interface AdapterOptions<SchemaT = any> {
28 defaultValue?: SchemaT | undefined;
29 serialize?: ((data: SchemaT) => string) | undefined;
30 deserialize?: ((serializedData: string) => SchemaT) | undefined;
31 }
32
33 interface BaseAdapter<SchemaT = any> extends AdapterOptions<SchemaT> {
34 readonly "@@reference": SchemaT;
35 new<SchemaT = any>(
36 source: string,
37 options?: AdapterOptions<SchemaT>,
38 ): BaseAdapter<SchemaT>;
39 source: string;
40 }
41
42 interface AdapterSync<SchemaT = any> extends BaseAdapter<SchemaT> {
43 new<SchemaT = any>(
44 source: string,
45 options?: AdapterOptions<SchemaT>,
46 ): AdapterSync<SchemaT>;
47 write(state: object): void;
48 }
49
50 interface AdapterAsync<SchemaT = any> extends BaseAdapter<SchemaT> {
51 new<SchemaT = any>(
52 source: string,
53 options?: AdapterOptions<SchemaT>,
54 ): AdapterAsync<SchemaT>;
55 write(state: object): Promise<void>;
56 }
57
58 interface LowdbBase<SchemaT> {
59 getState: () => SchemaT;
60 setState: (state: SchemaT) => this;
61 }
62
63 interface LowdbSync<SchemaT> extends LowdbBase<SchemaT>, ObjectChain<SchemaT> {
64 _: LoDashStatic;
65 read: () => this;
66 /**
67 * @description Be careful: This function overwrites the whole database.
68 */
69 write<T = void>(returnValue?: T): T & Promise<T>;
70 }
71
72 interface LowdbAsync<SchemaT> extends LowdbBase<SchemaT>, ObjectChain<SchemaT> {
73 _: LoDashStatic;
74 read: () => Promise<this>;
75 /**
76 * @description Be careful: This function overwrites the whole database.
77 */
78 write<T = void>(returnValue?: T): T & Promise<T>;
79 }
80
81 interface LowdbFpSync<SchemaT> extends LowdbBase<SchemaT> {
82 /**
83 * @description Be careful: This function overwrites the whole database.
84 */
85 write<T = void>(returnValue?: T): T;
86 /**
87 * @description Returns a function that allows you to access/modify the database at a given path.
88 * @example
89 * ```js
90 * const posts = db('posts')
91 * const firstPost = posts(all => all[0])
92 * posts.write((allPosts) => [...allPosts, {title: 'Yup!'}])
93 * ```
94 */
95 <TKey extends keyof SchemaT>(
96 path: TKey | [TKey],
97 defaultValue?: SchemaT[TKey],
98 ): FpReturnSync<SchemaT[TKey]>;
99 <TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey]>(
100 path: [TKey, TSubKey],
101 defaultValue?: SchemaT[TKey][TSubKey],
102 ): FpReturnSync<SchemaT[TKey][TSubKey]>;
103 <
104 TKey extends keyof SchemaT,
105 TSubKey extends keyof SchemaT[TKey],
106 TSubKey2 extends keyof SchemaT[TKey][TSubKey],
107 >(
108 path: [TKey, TSubKey, TSubKey2],
109 defaultValue?: SchemaT[TKey][TSubKey][TSubKey2],
110 ): FpReturnSync<SchemaT[TKey][TSubKey][TSubKey2]>;
111 <
112 TKey extends keyof SchemaT,
113 TSubKey extends keyof SchemaT[TKey],
114 TSubKey2 extends keyof SchemaT[TKey][TSubKey],
115 TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2],
116 >(
117 path: [TKey, TSubKey, TSubKey2, TSubKey3],
118 defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3],
119 ): FpReturnSync<SchemaT[TKey][TSubKey][TSubKey2][TSubKey3]>;
120 <
121 TKey extends keyof SchemaT,
122 TSubKey extends keyof SchemaT[TKey],
123 TSubKey2 extends keyof SchemaT[TKey][TSubKey],
124 TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2],
125 TSubKey4 extends keyof SchemaT[TKey][TSubKey][TSubKey2][TSubKey3],
126 >(
127 path: [TKey, TSubKey, TSubKey2, TSubKey3, TSubKey4],
128 defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4],
129 ): FpReturnSync<SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4]>;
130 <T = any>(path: string | string[], defaultValue?: T): FpReturnSync<T>;
131 }
132
133 interface LowdbFpAsync<SchemaT> extends LowdbBase<SchemaT> {
134 /**
135 * @description Be careful: This function overwrites the whole database.
136 */
137 write<T = void>(returnValue?: T): Promise<T>;
138 /**
139 * @description Returns a function that allows you to access/modify the database at a given path.
140 * @example
141 * ```js
142 * const posts = db('posts')
143 * const firstPost = posts(all => all[0])
144 * posts.write((allPosts) => [...allPosts, {title: 'Yup!'}])
145 * ```
146 */
147 <TKey extends keyof SchemaT>(
148 path: TKey | [TKey],
149 defaultValue?: SchemaT[TKey],
150 ): FpReturnAsync<SchemaT[TKey]>;
151 <TKey extends keyof SchemaT, TSubKey extends keyof SchemaT[TKey]>(
152 path: [TKey, TSubKey],
153 defaultValue?: SchemaT[TKey][TSubKey],
154 ): FpReturnAsync<SchemaT[TKey][TSubKey]>;
155 <
156 TKey extends keyof SchemaT,
157 TSubKey extends keyof SchemaT[TKey],
158 TSubKey2 extends keyof SchemaT[TKey][TSubKey],
159 >(
160 path: [TKey, TSubKey, TSubKey2],
161 defaultValue?: SchemaT[TKey][TSubKey][TSubKey2],
162 ): FpReturnAsync<SchemaT[TKey][TSubKey][TSubKey2]>;
163 <
164 TKey extends keyof SchemaT,
165 TSubKey extends keyof SchemaT[TKey],
166 TSubKey2 extends keyof SchemaT[TKey][TSubKey],
167 TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2],
168 >(
169 path: [TKey, TSubKey, TSubKey2, TSubKey3],
170 defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3],
171 ): FpReturnAsync<SchemaT[TKey][TSubKey][TSubKey2][TSubKey3]>;
172 <
173 TKey extends keyof SchemaT,
174 TSubKey extends keyof SchemaT[TKey],
175 TSubKey2 extends keyof SchemaT[TKey][TSubKey],
176 TSubKey3 extends keyof SchemaT[TKey][TSubKey][TSubKey2],
177 TSubKey4 extends keyof SchemaT[TKey][TSubKey][TSubKey2][TSubKey3],
178 >(
179 path: [TKey, TSubKey, TSubKey2, TSubKey3, TSubKey4],
180 defaultValue?: SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4],
181 ): FpReturnAsync<SchemaT[TKey][TSubKey][TSubKey2][TSubKey3][TSubKey4]>;
182 <T = any>(path: string | string[], defaultValue?: T): FpReturnAsync<T>;
183 }
184
185 interface FpReturnBase<PathT> {
186 /**
187 * Execute a series of functions on the data at a given path.
188 * Result of previous function is the input of the next one.
189 * Returns the result of the last function.
190 */
191 <R1>(f1: (a1: PathT) => R1): R1;
192 // <R1>(f1: [(a1: PathT) => R1]): R1;
193 <R1, R2>(f1: [(a1: PathT) => R1, (a: R1) => R2]): R2;
194 <R1, R2, R3>(f1: [(a1: PathT) => R1, (a: R1) => R2, (a: R2) => R3]): R3;
195 <R1, R2, R3, R4>(
196 f1: [(a1: PathT) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4],
197 ): R4;
198 <R1, R2, R3, R4, R5>(
199 f1: [
200 (a1: PathT) => R1,
201 (a: R1) => R2,
202 (a: R2) => R3,
203 (a: R3) => R4,
204 (a: R4) => R5,
205 ],
206 ): R5;
207 <R1, R2, R3, R4, R5, R6>(
208 f1: [
209 (a1: PathT) => R1,
210 (a: R1) => R2,
211 (a: R2) => R3,
212 (a: R3) => R4,
213 (a: R4) => R5,
214 (a: R5) => R6,
215 ],
216 ): R6;
217 <R1, R2, R3, R4, R5, R6, R7>(
218 f1: [
219 (a1: PathT) => R1,
220 (a: R1) => R2,
221 (a: R2) => R3,
222 (a: R3) => R4,
223 (a: R4) => R5,
224 (a: R5) => R6,
225 (a: R6) => R7,
226 ],
227 ): R7;
228 (funcs: Array<(a: any) => any>): any;
229 }
230 interface FpReturnSync<PathT> extends FpReturnBase<PathT> {
231 /**
232 * @description Writes the change to the database, based on the callback's return value.
233 * @example
234 * ```js
235 * posts.write((allPosts) => [...allPosts, {title: 'Yup!'}])
236 * ```
237 */
238 write<R1 extends PathT>(f1: (a1: PathT) => R1): R1;
239 }
240 interface FpReturnAsync<PathT> extends FpReturnBase<PathT> {
241 /**
242 * @description Writes the change to the database, based on the callback's return value.
243 * @example
244 * ```js
245 * posts.write((allPosts) => [...allPosts, {title: 'Yup!'}])
246 * ```
247 */
248 write<R1 extends PathT>(f1: (a1: PathT) => R1): Promise<R1>;
249 }
250
251 interface lowdb {
252 <AdapterT extends AdapterAsync>(adapter: AdapterT): Promise<LowdbAsync<AdapterT[ReferenceProperty]>>;
253 <AdapterT extends AdapterSync>(adapter: AdapterT): LowdbSync<AdapterT[ReferenceProperty]>;
254 }
255
256 interface lowdbFp {
257 <AdapterT extends AdapterAsync>(adapter: AdapterT): Promise<LowdbFpAsync<AdapterT[ReferenceProperty]>>;
258 <AdapterT extends AdapterSync>(adapter: AdapterT): LowdbFpSync<AdapterT[ReferenceProperty]>;
259 }
260}
261
262type ReferenceProperty = "@@reference";
263
\No newline at end of file