blackcat.js-database 1.0.0-test
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/index.d.mts +751 -0
- package/dist/index.d.ts +751 -0
- package/dist/index.js +995 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +969 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,751 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the base driver interface for database operations.
|
|
3
|
+
*/
|
|
4
|
+
interface BaseDriver {
|
|
5
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
6
|
+
set<T = any>(key: string, value: T): Promise<T>;
|
|
7
|
+
delete(key?: any): Promise<boolean>;
|
|
8
|
+
all(): Promise<Record<string, any>>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Represents the configuration object of the database instance.
|
|
12
|
+
*/
|
|
13
|
+
interface DatabaseConfiguration {
|
|
14
|
+
driver: BaseDriver;
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Represents the nullish type (`T` or `null`) and excludes `undefined` from it.
|
|
19
|
+
*
|
|
20
|
+
* Type parameters:
|
|
21
|
+
*
|
|
22
|
+
* - `T` (`any`) - The type to make nullish.
|
|
23
|
+
*
|
|
24
|
+
* @template T - The type to make nullish.
|
|
25
|
+
*/
|
|
26
|
+
type Maybe<T> = Exclude<T | null, undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* Conditional type that returns the type based on the condition type result.
|
|
29
|
+
*
|
|
30
|
+
* Type parameters:
|
|
31
|
+
*
|
|
32
|
+
* - `T` (`boolean`) - The condition to return a boolean value.
|
|
33
|
+
* - `IfTrue` (`any`) - The type to be returned if the condition type `T` is `true`.
|
|
34
|
+
* - `IfFalse` (`null`, optional) - The type to be returned if the condition type `T` is `false`.
|
|
35
|
+
*
|
|
36
|
+
* @template T - The condition to return a boolean value.
|
|
37
|
+
* @template IfTrue - The type to be returned if the condition type `T` is `true`.
|
|
38
|
+
* @template IfFalse - The type to be returned if the condition type `T` is `false`.
|
|
39
|
+
*/
|
|
40
|
+
type If<T extends boolean, IfTrue, IfFalse = null> = T extends true ? IfTrue : IfFalse;
|
|
41
|
+
/**
|
|
42
|
+
* Determines if the specified type is object and returns the checking result as boolean.
|
|
43
|
+
*
|
|
44
|
+
* Type parameters:
|
|
45
|
+
*
|
|
46
|
+
* - `T` (`any`) - The type to check.
|
|
47
|
+
*
|
|
48
|
+
* @template T - The type to check.
|
|
49
|
+
*/
|
|
50
|
+
type IsObject<T> = T extends null ? false : T extends undefined ? false : T extends any[] ? false : T extends Record<any, any> ? true : false;
|
|
51
|
+
/**
|
|
52
|
+
* Represents a type that works as an array of specified type or `...spread` of specified type.
|
|
53
|
+
*
|
|
54
|
+
* Type parameters:
|
|
55
|
+
*
|
|
56
|
+
* - `T` (`any`) - The type to convert into rest-or-array type.
|
|
57
|
+
*
|
|
58
|
+
* @template T - The type to convert into rest-or-array type.
|
|
59
|
+
*/
|
|
60
|
+
type RestOrArray<T> = T[] | [T[]];
|
|
61
|
+
/**
|
|
62
|
+
* From the type `A`, extracts the type `T` from the `Array<T>` type, or returns `A` if not array type was specified.
|
|
63
|
+
*
|
|
64
|
+
* Type parameters:
|
|
65
|
+
*
|
|
66
|
+
* - `A` (`any`) - The array type to extract the type from.
|
|
67
|
+
*
|
|
68
|
+
* @template A - The array type to extract the type from.
|
|
69
|
+
*/
|
|
70
|
+
type ExtractFromArray<A> = A extends Array<infer T> ? T : A;
|
|
71
|
+
/**
|
|
72
|
+
* Represents a `predicate` callback function from array methods such as `Array.map()`, `Array.find()`, etc.
|
|
73
|
+
*
|
|
74
|
+
* Type parameters:
|
|
75
|
+
*
|
|
76
|
+
* - `T` (`any`) - The type of the item in the array.
|
|
77
|
+
* - `R` (`any`, optional) - The return type of the function.
|
|
78
|
+
*
|
|
79
|
+
* @template T - The type of the item in the array.
|
|
80
|
+
* @template R - The return type of the function.
|
|
81
|
+
*/
|
|
82
|
+
type QueryFunction<T, R = any> = (item: T, index: number, values: T[]) => R;
|
|
83
|
+
/**
|
|
84
|
+
* Determines if the specified type is `any` and returns the checking result as boolean.
|
|
85
|
+
*
|
|
86
|
+
* Type parameters:
|
|
87
|
+
*
|
|
88
|
+
* - `T` (`any`) - The type to check.
|
|
89
|
+
*
|
|
90
|
+
* @template T - The type to check.
|
|
91
|
+
*/
|
|
92
|
+
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
93
|
+
/**
|
|
94
|
+
* Makes the string union type autocompletable with a `string` type.
|
|
95
|
+
*
|
|
96
|
+
* Type parameters:
|
|
97
|
+
*
|
|
98
|
+
* - `S` (`string`) - The autocompletable union string type to make compatible with a `string` type.
|
|
99
|
+
*
|
|
100
|
+
* @template S - The autocompletable union string type to make compatible with a `string` type.
|
|
101
|
+
*/
|
|
102
|
+
type AutocompletableString<S extends string> = S | (string & {});
|
|
103
|
+
/**
|
|
104
|
+
* Extracts the first key from the specified object path.
|
|
105
|
+
* (for example, in key `member.user.id`, the first key will be `member`)
|
|
106
|
+
*
|
|
107
|
+
* Type parameters:
|
|
108
|
+
*
|
|
109
|
+
* - `TKey` (`ObjectPath<string, any>`) - The object path to extract the key from.
|
|
110
|
+
*
|
|
111
|
+
* @template TKey - The object path to extract the key from.
|
|
112
|
+
*/
|
|
113
|
+
type FirstObjectKey<TKey extends ObjectPath<string, any>> = TKey extends `${infer Key}.${infer _Rest}` ? Key : TKey extends string ? TKey : never;
|
|
114
|
+
/**
|
|
115
|
+
* Represents a path to a nested property in an object.
|
|
116
|
+
*
|
|
117
|
+
* Type parameters:
|
|
118
|
+
*
|
|
119
|
+
* - `T` (`any`) - The object to get the path from.
|
|
120
|
+
* - `TKey` (`keyof T`, defaults to `keyof T`) - The key of the object to get the path from.
|
|
121
|
+
*
|
|
122
|
+
* @template T - The object to get the path from.
|
|
123
|
+
* @template TKey - The key of the object to get the path from.
|
|
124
|
+
*/
|
|
125
|
+
type ObjectPath<T, TKey extends keyof T = keyof T> = IsAny<T> extends true ? string : T extends string | number | boolean | symbol ? string : T extends Array<any> ? TKey & string : TKey extends string ? T[TKey] extends Array<any> ? TKey : T[TKey] extends Record<string, any> ? `${TKey}` | `${TKey}.${ObjectPath<T[TKey]>}` : TKey : never;
|
|
126
|
+
/**
|
|
127
|
+
* Extracts the value from the specified object path.
|
|
128
|
+
*
|
|
129
|
+
* Type parameters:
|
|
130
|
+
*
|
|
131
|
+
* - `T` (`any`) - The object to extract the value from.
|
|
132
|
+
* - `P` (`ObjectPath<T>` or `AutocompletableString<ObjectPath<T>>`) - The object path to extract the value from.
|
|
133
|
+
*
|
|
134
|
+
* @template T - The object to extract the value from.
|
|
135
|
+
* @template P - The object path to extract the value from.
|
|
136
|
+
*/
|
|
137
|
+
type ObjectValue<T, P extends ObjectPath<T> | AutocompletableString<ObjectPath<T>>> = T extends AutocompletableString<P> | string | number | boolean | symbol ? T : P extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? Rest extends ObjectPath<T[Key]> ? ObjectValue<T[Key], Rest> : null : never : P extends keyof T ? T[P] : T;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Database class.
|
|
141
|
+
*
|
|
142
|
+
* Type parameters:
|
|
143
|
+
*
|
|
144
|
+
* - `V` (`any`) - The type of the values in the database.
|
|
145
|
+
*
|
|
146
|
+
* @template V (`any`) - The type of the values in the database.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
*
|
|
150
|
+
* // JSON
|
|
151
|
+
* import { Database, JSONDriver } from "blackcat-database";
|
|
152
|
+
*
|
|
153
|
+
* const database = new Database({
|
|
154
|
+
* driver: new JSONDriver({ filePath: "./database.json", minifyJSON: false }),
|
|
155
|
+
* });
|
|
156
|
+
*
|
|
157
|
+
*
|
|
158
|
+
*/
|
|
159
|
+
declare class Database<V = any> {
|
|
160
|
+
/**
|
|
161
|
+
* The low-level database driver handling basic operations.
|
|
162
|
+
*/
|
|
163
|
+
driver: BaseDriver;
|
|
164
|
+
constructor(options: DatabaseConfiguration);
|
|
165
|
+
/**
|
|
166
|
+
* Gets all the database contents from the cache.
|
|
167
|
+
*
|
|
168
|
+
* Type parameters:
|
|
169
|
+
*
|
|
170
|
+
* - `T` (`object`, defaults to `Record<string, any>`) - The type of object of all the database object to be returned.
|
|
171
|
+
*
|
|
172
|
+
* @returns {T} Cached database contents.
|
|
173
|
+
*
|
|
174
|
+
* @template T (object, defaults to `Record<string, any>`) -
|
|
175
|
+
* The type of object of all the database object to be returned.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* const databaseContents = await database.all();
|
|
179
|
+
* console.log(databaseContents); // -> { ... (the object of all the data stored in database) }
|
|
180
|
+
*/
|
|
181
|
+
all<T extends Record<string, any> = Record<string, any>>(): Promise<T>;
|
|
182
|
+
/**
|
|
183
|
+
* Retrieves a value from database by a key.
|
|
184
|
+
*
|
|
185
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
186
|
+
* @returns {Maybe<ObjectValue<V, P>>} The value of the target in database.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* const simpleValue = await database.get("simpleValue");
|
|
190
|
+
* console.log(simpleValue); // -> 123
|
|
191
|
+
*
|
|
192
|
+
* const databaseObjectPropertyAccessed = await database.get("youCanAlso.accessDatabaseObjectProperties.likeThat");
|
|
193
|
+
* console.log(databaseObjectPropertyAccessed); // -> "hello world!"
|
|
194
|
+
*
|
|
195
|
+
* // Assuming that the initial database object for this example is:
|
|
196
|
+
* // {
|
|
197
|
+
* // simpleValue: 123,
|
|
198
|
+
* // youCanAlso: {
|
|
199
|
+
* // accessDatabaseObjectProperties: {
|
|
200
|
+
* // likeThat: "hello world!"
|
|
201
|
+
* // }
|
|
202
|
+
* // }
|
|
203
|
+
* // }
|
|
204
|
+
*/
|
|
205
|
+
get<P extends AutocompletableString<ObjectPath<V>>>(key?: AutocompletableString<P>): Promise<Maybe<ObjectValue<V, P>> | V>;
|
|
206
|
+
/**
|
|
207
|
+
* Retrieves a value from database by a key.
|
|
208
|
+
*
|
|
209
|
+
* - This method is an alias for {@link Database.get()} method.
|
|
210
|
+
*
|
|
211
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
212
|
+
* @returns {Maybe<ObjectValue<V, P>>} The value from database.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* const simpleValue = await database.fetch("simpleValue");
|
|
216
|
+
* console.log(simpleValue); // -> 123
|
|
217
|
+
*
|
|
218
|
+
* // You can use the dot notation to access the database object properties:
|
|
219
|
+
* const playerInventory = await database.fetch("player.inventory");
|
|
220
|
+
* console.log(playerInventory); // -> []
|
|
221
|
+
*
|
|
222
|
+
* // Assuming that the initial database object for this example is:
|
|
223
|
+
* // {
|
|
224
|
+
* // simpleValue: 123,
|
|
225
|
+
* // player: {
|
|
226
|
+
* // inventory: []
|
|
227
|
+
* // }
|
|
228
|
+
* // }
|
|
229
|
+
*/
|
|
230
|
+
fetch<P extends AutocompletableString<ObjectPath<V>>>(key?: AutocompletableString<P>): Promise<Maybe<ObjectValue<V, P>> | V>;
|
|
231
|
+
/**
|
|
232
|
+
* Determines if the data is stored in database.
|
|
233
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
234
|
+
* @returns {boolean} Whether the data is stored in database.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* const isSimpleValueInDatabase = await database.has("simpleValue");
|
|
238
|
+
* console.log(isSimpleValueInDatabase); // -> true
|
|
239
|
+
*
|
|
240
|
+
* const somethingElse = await database.has("somethingElse");
|
|
241
|
+
* console.log(somethingElse); // -> false
|
|
242
|
+
*
|
|
243
|
+
* // You can use the dot notation to check the database object properties:
|
|
244
|
+
* const isObjectInDatabase = await database.has("youCanAlso.accessObjectProperties.likeThat");
|
|
245
|
+
* console.log(isObjectInDatabase); // -> true
|
|
246
|
+
*
|
|
247
|
+
* // Assuming that the initial database object for this example is:
|
|
248
|
+
* // {
|
|
249
|
+
* // simpleValue: 123,
|
|
250
|
+
* // player: {
|
|
251
|
+
* // inventory: []
|
|
252
|
+
* // },
|
|
253
|
+
* // youCanAlso: {
|
|
254
|
+
* // accessObjectProperties: {
|
|
255
|
+
* // likeThat: "hello world!"
|
|
256
|
+
* // }
|
|
257
|
+
* // }
|
|
258
|
+
* // }
|
|
259
|
+
*/
|
|
260
|
+
has<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<boolean>;
|
|
261
|
+
/**
|
|
262
|
+
* Writes the specified value into database under the specified key.
|
|
263
|
+
*
|
|
264
|
+
* @param {AutocompletableString<P>} key The key to write in the target.
|
|
265
|
+
* @param {ObjectValue<V, P>} value The value to write.
|
|
266
|
+
*
|
|
267
|
+
* @returns {Promise<If<IsObject<V>, FirstObjectKey<P>, V>>}
|
|
268
|
+
* - If the `value` parameter's type is not an object (string, number, boolean, etc), then the specified
|
|
269
|
+
* `value` parameter (type of `ObjectValue<V, P>`) will be returned.
|
|
270
|
+
*
|
|
271
|
+
* - If an object is specified in the `value` parameter, then the object of the first key will be returned.
|
|
272
|
+
* (type of `FirstObjectKey<P>` - first object key (e.g. in key `member.user.id`, the first key will be `member`))
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* // Assuming that the initial database object for this example is empty.
|
|
276
|
+
*
|
|
277
|
+
* await database.set("something", "hello");
|
|
278
|
+
* const hello = await database.get("something");
|
|
279
|
+
*
|
|
280
|
+
* console.log(hello); // -> "hello"
|
|
281
|
+
*
|
|
282
|
+
* // You can use the dot notation to write data in objects:
|
|
283
|
+
* const dotNotationSetResult = await database.set("member.user.id", 2000);
|
|
284
|
+
* console.log(dotNotationSetResult); // -> 2000
|
|
285
|
+
*
|
|
286
|
+
* await database.set("member.inventory", []);
|
|
287
|
+
* const inventory = await database.get("member.inventory");
|
|
288
|
+
*
|
|
289
|
+
* console.log(inventory); // -> []
|
|
290
|
+
*
|
|
291
|
+
* // Using objects as value will return the object of key `thats`:
|
|
292
|
+
* await database.set("member.user", { name: "Jerry" }); // -> { member: { user: { name: "Jerry" } } }
|
|
293
|
+
*
|
|
294
|
+
* // After these manipulations, the database object will look like this:
|
|
295
|
+
* // {
|
|
296
|
+
* // "something": "hello",
|
|
297
|
+
* // "member": {
|
|
298
|
+
* // "inventory": [],
|
|
299
|
+
* // "user": {
|
|
300
|
+
* // "name": "Jerry",
|
|
301
|
+
* // "id": 2000
|
|
302
|
+
* // }
|
|
303
|
+
* // }
|
|
304
|
+
* // }
|
|
305
|
+
*/
|
|
306
|
+
set<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>;
|
|
307
|
+
/**
|
|
308
|
+
* Update the specified value into database under the specified key.
|
|
309
|
+
*
|
|
310
|
+
* @param {AutocompletableString<P>} key The key to update the target.
|
|
311
|
+
* @param {ObjectValue<V, P>} value The value to update.
|
|
312
|
+
*
|
|
313
|
+
* @returns {Promise<If<IsObject<V>, FirstObjectKey<P>, V>>}
|
|
314
|
+
* - If the `value` parameter's type is not an object (string, number, boolean, etc), then the specified
|
|
315
|
+
* `value` parameter (type of `ObjectValue<V, P>`) will be returned.
|
|
316
|
+
*
|
|
317
|
+
* - If an object is specified in the `value` parameter, then the object of the first key will be returned.
|
|
318
|
+
* (type of `FirstObjectKey<P>` - first object key (e.g. in key `member.user.id`, the first key will be `member`))
|
|
319
|
+
*/
|
|
320
|
+
update<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>;
|
|
321
|
+
/**
|
|
322
|
+
* Performs an arithmetical addition on a target number in database.
|
|
323
|
+
*
|
|
324
|
+
* [!!!] The type of target value must be a number.
|
|
325
|
+
*
|
|
326
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
327
|
+
* @param {number} numberToAdd The number to add to the target number in database.
|
|
328
|
+
* @returns {Promise<number>} Addition operation result.
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* const additionResult = await database.add("points", 5);
|
|
332
|
+
* console.log(additionResult); // -> 10 (5 + 5 = 10)
|
|
333
|
+
*
|
|
334
|
+
* // Notice that we don't need to assign a value to unexistent properties in database
|
|
335
|
+
* // before performing an addition since the initial target value is 0 and will be used
|
|
336
|
+
* // as the value of the unexistent property:
|
|
337
|
+
* const unexistentAdditionResult = await database.add("somethingElse", 3);
|
|
338
|
+
*
|
|
339
|
+
* console.log(unexistentAdditionResult); // -> 3 (0 + 3 = 3)
|
|
340
|
+
* // the property didn't exist in database, that's why 0 is added to 3
|
|
341
|
+
*
|
|
342
|
+
* // Assuming that the initial database object for this example is:
|
|
343
|
+
* // {
|
|
344
|
+
* // points: 5
|
|
345
|
+
* // }
|
|
346
|
+
*/
|
|
347
|
+
add<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, numberToAdd: number): Promise<number>;
|
|
348
|
+
/**
|
|
349
|
+
* Performs an arithmetical subtraction on a target number in database.
|
|
350
|
+
*
|
|
351
|
+
* [!!!] The type of target value must be a number.
|
|
352
|
+
*
|
|
353
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
354
|
+
* @param {number} numberToSubtract The number to subtract from the target number in database.
|
|
355
|
+
* @returns {Promise<number>} Subtraction operation result.
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* const subtractionResult = await database.subtract("points", 5)
|
|
359
|
+
* console.log(subtractionResult) // -> 5 (10 - 5 = 5)
|
|
360
|
+
*
|
|
361
|
+
* // Notice that we don't need to assign a value to unexistent properties in database
|
|
362
|
+
* // before performing a subtraction since the initial target value is 0 and will be used
|
|
363
|
+
* // as the value of the unexistent property:
|
|
364
|
+
* const unexistentSubtractionitionResult = await database.subtract("somethingElse", 3)
|
|
365
|
+
*
|
|
366
|
+
* console.log(unexistentSubtractionitionResult) // -> 3 (0 - 3 = -3)
|
|
367
|
+
* // the property didn't exist in database, so 3 is subtracted from 0
|
|
368
|
+
*
|
|
369
|
+
* // Assuming that the initial database object for this example is:
|
|
370
|
+
* // {
|
|
371
|
+
* // points: 10
|
|
372
|
+
* // }
|
|
373
|
+
*/
|
|
374
|
+
subtract<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, numberToSubtract: number): Promise<number>;
|
|
375
|
+
/**
|
|
376
|
+
* Pushes the specified value(s) into the target array in database.
|
|
377
|
+
*
|
|
378
|
+
* [!!!] The type of target value must be an array.
|
|
379
|
+
* [!!!] get only values not stored in the database.
|
|
380
|
+
*
|
|
381
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
382
|
+
* @param {RestOrArray<ExtractFromArray<ObjectValue<V, P>>>} values
|
|
383
|
+
* The value(s) to be pushed into the target array in database.
|
|
384
|
+
*
|
|
385
|
+
* @returns {Promise<Array<ExtractFromArray<ObjectValue<V, P>>>>} Updated target array from database.
|
|
386
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
* console.log(await database.push("members", "Jerry")); // -> ["Tom", "Jerry"]
|
|
389
|
+
* // You can also pass in multiple values to push into the target array:
|
|
390
|
+
* console.log(await database.push("currencies", "VND")); // -> ["USD", "VND"]
|
|
391
|
+
*
|
|
392
|
+
* // Assuming that the initial database object for this example is:
|
|
393
|
+
* // {
|
|
394
|
+
* // members: ["Tom"],
|
|
395
|
+
* // currencies: ["USD"]
|
|
396
|
+
* // }
|
|
397
|
+
*/
|
|
398
|
+
push<P extends ObjectPath<V>>(key: AutocompletableString<P>, ...values: RestOrArray<ExtractFromArray<ObjectValue<V, P>>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>;
|
|
399
|
+
/**
|
|
400
|
+
* Replaces the specified element in target array with the specified value in the target array in database.
|
|
401
|
+
*
|
|
402
|
+
* [!!!] The type of target value must be an array.
|
|
403
|
+
*
|
|
404
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
405
|
+
* @param {number} targetArrayElementIndex The index to find the element in target array by.
|
|
406
|
+
* @param {V} value The value to be pushed into the target array in database.
|
|
407
|
+
* @returns {Promise<Array<ExtractFromArray<ObjectValue<V, P>>>>} Updated target array from database.
|
|
408
|
+
*
|
|
409
|
+
* @example
|
|
410
|
+
* console.log(await database.pull("members", 1, "James")); // -> ["Jerry", "James", "Tom"]
|
|
411
|
+
*
|
|
412
|
+
* // Assuming that the initial database object for this example is:
|
|
413
|
+
* // {
|
|
414
|
+
* // members: ["Jerry", "William", "Tom"]
|
|
415
|
+
* // }
|
|
416
|
+
*/
|
|
417
|
+
pull<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, targetArrayElementIndex: number, value: ExtractFromArray<ObjectValue<V, P>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>;
|
|
418
|
+
/**
|
|
419
|
+
* Removes the specified element(s) from the target array in database.
|
|
420
|
+
*
|
|
421
|
+
* [!!!] The type of target value must be an array.
|
|
422
|
+
*
|
|
423
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
424
|
+
* @param {RestOrArray<ExtractFromArray<number>>} targetArrayElementIndexes
|
|
425
|
+
* The index(es) to find the element(s) in target array by.
|
|
426
|
+
*
|
|
427
|
+
* @returns {Promise<Array<ExtractFromArray<ObjectValue<V, P>>>>} Updated target array from database.
|
|
428
|
+
*
|
|
429
|
+
* @example
|
|
430
|
+
* console.log(await database.pop("members", 1)); // -> ["Jerry", "Tom"]
|
|
431
|
+
*
|
|
432
|
+
* console.log(await database.pop("currencies", 1)); // -> ["VND", "Euro"]
|
|
433
|
+
*
|
|
434
|
+
* // Assuming that the initial database object for this example is:
|
|
435
|
+
* // {
|
|
436
|
+
* // members: ["Jerry", "William", "Tom"],
|
|
437
|
+
* // currencies: ["VND", "USD", "Euro"]
|
|
438
|
+
* // }
|
|
439
|
+
*/
|
|
440
|
+
pop<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, ...targetArrayElementIndexes: RestOrArray<ExtractFromArray<number>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>;
|
|
441
|
+
/**
|
|
442
|
+
* Determines whether the specified target is an array.
|
|
443
|
+
*
|
|
444
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
445
|
+
* @returns {Promise<boolean>} Whether the target is an array.
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* console.log(await databse.isTargetArray("array")); // => true
|
|
449
|
+
* console.log(await databse.isTargetArray("notArray")); // => false
|
|
450
|
+
*
|
|
451
|
+
* // Assuming that the initial database object for this example is:
|
|
452
|
+
* // {
|
|
453
|
+
* // array: [],
|
|
454
|
+
* // notArray: 123
|
|
455
|
+
* // }
|
|
456
|
+
*/
|
|
457
|
+
isTargetArray<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<boolean>;
|
|
458
|
+
/**
|
|
459
|
+
* Determines whether the specified target is a number.
|
|
460
|
+
*
|
|
461
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
462
|
+
* @returns {Promise<boolean>} Whether the target is a number.
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* console.log(await database.isTargetNumber("number")); // -> true
|
|
466
|
+
* console.log(await database.isTargetNumber("notNumber")); // -> false
|
|
467
|
+
*
|
|
468
|
+
* // Assuming that the initial database object for this example is:
|
|
469
|
+
* // {
|
|
470
|
+
* // number: 123,
|
|
471
|
+
* // notNumber: []
|
|
472
|
+
* // }
|
|
473
|
+
*/
|
|
474
|
+
isTargetNumber<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<boolean>;
|
|
475
|
+
/**
|
|
476
|
+
* Returns an array of object keys by specified database key.
|
|
477
|
+
*
|
|
478
|
+
* If `key` parameter is omitted, then an array of object keys of database root object will be returned.
|
|
479
|
+
*
|
|
480
|
+
* Type parameters:
|
|
481
|
+
*
|
|
482
|
+
* - `TKeys` (`TupleOrArray<string>`, defaults to `K[]`) - The tuple or array of a type of keys to be returned.
|
|
483
|
+
*
|
|
484
|
+
* @param {P} [key] The key to access the target in database by.
|
|
485
|
+
* @returns {Array<ObjectPath<P>>} Database object keys array.
|
|
486
|
+
*
|
|
487
|
+
* @example
|
|
488
|
+
* const prop3Keys = await database.keys("prop3");
|
|
489
|
+
* console.log(prop3Keys) // -> ["prop4", "prop5"]
|
|
490
|
+
*
|
|
491
|
+
* const prop5Keys = await database.keys("prop3.prop5");
|
|
492
|
+
* console.log(prop5Keys) // -> ["prop6"]
|
|
493
|
+
*
|
|
494
|
+
* const prop6Keys = await database.keys("prop3.prop5.prop6");
|
|
495
|
+
* console.log(prop6Keys)
|
|
496
|
+
* // -> [] (empty since the value in `prop6`, 111, is a primitive value and not an actual object)
|
|
497
|
+
*
|
|
498
|
+
* const databaseKeys = await database.keys();
|
|
499
|
+
* // in this example, `key` parameter is omitted - object keys of database object are being returned
|
|
500
|
+
*
|
|
501
|
+
* console.log(databaseKeys) // -> ["prop1", "prop2", "prop3"]
|
|
502
|
+
*
|
|
503
|
+
* const unexistentKeys = await database.keys("somethingElse");
|
|
504
|
+
* console.log(unexistentKeys) // -> [] (empty since the key `somethingElse` does not exist in database)
|
|
505
|
+
*
|
|
506
|
+
* // Assuming that the initial database object for this example is:
|
|
507
|
+
* // {
|
|
508
|
+
* // prop1: 123,
|
|
509
|
+
* // prop2: 456,
|
|
510
|
+
* // prop3: {
|
|
511
|
+
* // prop4: 789,
|
|
512
|
+
* // prop5: {
|
|
513
|
+
* // prop6: 111
|
|
514
|
+
* // }
|
|
515
|
+
* // }
|
|
516
|
+
* // }
|
|
517
|
+
*/
|
|
518
|
+
keys<P extends AutocompletableString<ObjectPath<V>>>(key?: P): Promise<ObjectPath<P>[]>;
|
|
519
|
+
/**
|
|
520
|
+
* Determines the number of keys in the root of the database.
|
|
521
|
+
* @type {Promise<number>} amount of data.
|
|
522
|
+
*/
|
|
523
|
+
size(): Promise<number>;
|
|
524
|
+
/**
|
|
525
|
+
* Returns an array of object values by specified database key.
|
|
526
|
+
*
|
|
527
|
+
* If `key` parameter is omitted, then an array of object values of database root object will be returned.
|
|
528
|
+
*
|
|
529
|
+
* @param {P} [key] The key to access the target in database by.
|
|
530
|
+
* @returns {Array<ObjectValue<V, P>>} Database object values array.
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* const prop3Values = await database.values("prop3");
|
|
534
|
+
* console.log(prop3Values); // -> [789, { prop6: 111 }]
|
|
535
|
+
*
|
|
536
|
+
* const prop5Values = await database.values("prop3.prop5");
|
|
537
|
+
* console.log(prop5Values); // -> []
|
|
538
|
+
*
|
|
539
|
+
* const prop6Values = await database.values("prop3.prop5.prop6");
|
|
540
|
+
* console.log(prop6Values);
|
|
541
|
+
* // -> [] (empty since the value in `prop6`, 111, is a primitive value and not an actual object)
|
|
542
|
+
*
|
|
543
|
+
* const databaseValues = await database.values();
|
|
544
|
+
* // in this example, `key` parameter is omitted - object values of database object are being returned
|
|
545
|
+
*
|
|
546
|
+
* console.log(databaseValues); // -> [123, 456, { prop4: 789, prop5: { prop6: 111 } }]
|
|
547
|
+
*
|
|
548
|
+
* const unexistentValues = await database.values("somethingElse");
|
|
549
|
+
* console.log(unexistentValues); // -> [] (empty since the key `somethingElse` does not exist in database)
|
|
550
|
+
*
|
|
551
|
+
* // Assuming that the initial database object for this example is:
|
|
552
|
+
* // {
|
|
553
|
+
* // prop1: 123,
|
|
554
|
+
* // prop2: 456,
|
|
555
|
+
* // prop3: {
|
|
556
|
+
* // prop4: 789,
|
|
557
|
+
* // prop5: {
|
|
558
|
+
* // prop6: 111
|
|
559
|
+
* // }
|
|
560
|
+
* // }
|
|
561
|
+
* // }
|
|
562
|
+
*/
|
|
563
|
+
values<P extends AutocompletableString<ObjectPath<V>>>(key?: P): Promise<ObjectValue<V, P>[]>;
|
|
564
|
+
/**
|
|
565
|
+
* This method works the same way as `Array.find()`.
|
|
566
|
+
*
|
|
567
|
+
* Iterates over root database values, finds the element in database values array
|
|
568
|
+
* by specified condition in the callback function and returns the result.
|
|
569
|
+
*
|
|
570
|
+
* @param {QueryFunction<V>} queryFunction
|
|
571
|
+
* A function that accepts up to three arguments.
|
|
572
|
+
* The `find` method calls the `queryFunction` once for each element in database object values array.
|
|
573
|
+
*
|
|
574
|
+
* @returns {Promise<Maybe<V>>} The search
|
|
575
|
+
*/
|
|
576
|
+
find(queryFunction: QueryFunction<V>): Promise<Maybe<V>>;
|
|
577
|
+
/**
|
|
578
|
+
* This method works the same way as `Array.map()`.
|
|
579
|
+
*
|
|
580
|
+
* Calls a defined callback function on each element of an array,
|
|
581
|
+
* and returns an array that contains the results.
|
|
582
|
+
*
|
|
583
|
+
* @param {QueryFunction<V, TReturnType>} queryFunction
|
|
584
|
+
* A function that accepts up to three arguments.
|
|
585
|
+
* The `map` method calls the `queryFunction` once for each element in database object values array.
|
|
586
|
+
*
|
|
587
|
+
* @returns {Promise<TReturnType[]>}
|
|
588
|
+
*/
|
|
589
|
+
map<TReturnType>(queryFunction: QueryFunction<V, TReturnType>): Promise<TReturnType[]>;
|
|
590
|
+
/**
|
|
591
|
+
* This method works the same way as `Array.findIndex()`.
|
|
592
|
+
*
|
|
593
|
+
* Iterates over root database values, finds the index of the element in database values array
|
|
594
|
+
* by specified condition in the callback function and returns the result.
|
|
595
|
+
*
|
|
596
|
+
* @param {QueryFunction<V>} queryFunction
|
|
597
|
+
* A function that accepts up to three arguments.
|
|
598
|
+
* The `findIndex` method calls the `queryFunction` once for each element in database object values array.
|
|
599
|
+
*
|
|
600
|
+
* @returns {Promise<number>}
|
|
601
|
+
*/
|
|
602
|
+
findIndex(queryFunction: QueryFunction<V>): Promise<number>;
|
|
603
|
+
/**
|
|
604
|
+
* This method works the same way as `Array.filter()`.
|
|
605
|
+
*
|
|
606
|
+
* Iterates over root database values, finds all the element that match the
|
|
607
|
+
* specified condition in the callback function and returns the result.
|
|
608
|
+
*
|
|
609
|
+
* @param {QueryFunction<V>} queryFunction
|
|
610
|
+
* A function that accepts up to three arguments.
|
|
611
|
+
* The `filter` method calls the `queryFunction` once for each element in database object values array.
|
|
612
|
+
*
|
|
613
|
+
* @returns {Promise<V[]>}
|
|
614
|
+
*/
|
|
615
|
+
filter(queryFunction: QueryFunction<V>): Promise<V[]>;
|
|
616
|
+
/**
|
|
617
|
+
* This method works the same way as `Array.some()`.
|
|
618
|
+
*
|
|
619
|
+
* Iterates over root database values and checks if the
|
|
620
|
+
* specified condition in the callback function returns `true`
|
|
621
|
+
* for **any** of the elements of the database object values array.
|
|
622
|
+
*
|
|
623
|
+
* @param {QueryFunction<V>} queryFunction
|
|
624
|
+
* A function that accepts up to three arguments.
|
|
625
|
+
* The `some` method calls the `queryFunction` once for each element in database object values array.
|
|
626
|
+
*
|
|
627
|
+
* @returns {Promise<boolean>}
|
|
628
|
+
*/
|
|
629
|
+
some(queryFunction: QueryFunction<V>): Promise<boolean>;
|
|
630
|
+
/**
|
|
631
|
+
* This method works the same way as `Array.every()`.
|
|
632
|
+
*
|
|
633
|
+
* Iterates over root database values and checks if the
|
|
634
|
+
* specified condition in the callback function returns `true`
|
|
635
|
+
* for **all** of the elements of the database object values array.
|
|
636
|
+
*
|
|
637
|
+
* @param {QueryFunction<V>} queryFunction
|
|
638
|
+
* A function that accepts up to three arguments.
|
|
639
|
+
* The `every` method calls the `queryFunction` once for each element in database object values array.
|
|
640
|
+
*
|
|
641
|
+
* @returns {Promise<boolean>}
|
|
642
|
+
*/
|
|
643
|
+
every(queryFunction: QueryFunction<V>): Promise<boolean>;
|
|
644
|
+
/**
|
|
645
|
+
* Picks a random element of array in database and returns the picked array element.
|
|
646
|
+
*
|
|
647
|
+
* [!!!] The type of target value must be an array.
|
|
648
|
+
*
|
|
649
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
650
|
+
* @returns {Promise<Maybe<ObjectValue<V, P>>>} The randomly picked element in the database array.
|
|
651
|
+
*
|
|
652
|
+
* @example
|
|
653
|
+
* const array = await database.get("array"); // assuming that the array is ['example1', 'example2', 'example3']
|
|
654
|
+
* console.log(array); // -> ['example1', 'example2', 'example3']
|
|
655
|
+
*
|
|
656
|
+
* const randomArrayElement = await database.random("exampleArray");
|
|
657
|
+
* console.log(randomArrayElement); // -> randomly picked array element: either 'example1', 'example2', or 'example3'
|
|
658
|
+
*/
|
|
659
|
+
random<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<Maybe<ObjectValue<V, P>>>;
|
|
660
|
+
/**
|
|
661
|
+
* Deletes the data from database by key.
|
|
662
|
+
* @param {AutocompletableString<P>} key The key to access the target in database by.
|
|
663
|
+
* @returns {Promise<boolean>} Whether the deletion was successful.
|
|
664
|
+
*/
|
|
665
|
+
delete<P extends AutocompletableString<ObjectPath<V>>>(key?: AutocompletableString<P>): Promise<boolean>;
|
|
666
|
+
/**
|
|
667
|
+
* Deletes everything from the database.
|
|
668
|
+
*
|
|
669
|
+
* - This method is an alias for {@link QuickMongo.clear()} method.
|
|
670
|
+
* @returns {Promise<boolean>} `true` if cleared successfully, `false` otherwise.
|
|
671
|
+
*/
|
|
672
|
+
deleteAll(): Promise<boolean>;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
interface JSONDriverOptions {
|
|
676
|
+
/**
|
|
677
|
+
* JSON database file path.
|
|
678
|
+
* @type {string}
|
|
679
|
+
*/
|
|
680
|
+
filePath: string;
|
|
681
|
+
/**
|
|
682
|
+
* Minifies the JSON content in database file to save some space.
|
|
683
|
+
* @type {boolean}
|
|
684
|
+
*/
|
|
685
|
+
minifyJSON?: boolean;
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* JSONDriver class.
|
|
689
|
+
*/
|
|
690
|
+
declare class JSONDriver implements BaseDriver {
|
|
691
|
+
/**
|
|
692
|
+
* JSON database file path.
|
|
693
|
+
* @type {string}
|
|
694
|
+
*/
|
|
695
|
+
private filePath;
|
|
696
|
+
/**
|
|
697
|
+
* Minifies the JSON content in database file to save some space.
|
|
698
|
+
* @type {boolean}
|
|
699
|
+
*/
|
|
700
|
+
minifyJSON: boolean;
|
|
701
|
+
/**
|
|
702
|
+
* JSONDriver class.
|
|
703
|
+
* @param options.filePath Json database file path.
|
|
704
|
+
* @param options.minifyJSON Minifies the JSON content in database file to save some space.
|
|
705
|
+
*/
|
|
706
|
+
constructor(options: JSONDriverOptions);
|
|
707
|
+
/**
|
|
708
|
+
* Returns all data from JSON database.
|
|
709
|
+
*
|
|
710
|
+
* @returns {Promise<any>} All data from JSON database.
|
|
711
|
+
*
|
|
712
|
+
* @template V The type of data being returned.
|
|
713
|
+
*/
|
|
714
|
+
all<V>(): Promise<V>;
|
|
715
|
+
/**
|
|
716
|
+
* Parses the key and fetches the value from JSON database.
|
|
717
|
+
*
|
|
718
|
+
* Type parameters:
|
|
719
|
+
*
|
|
720
|
+
* - `V` - The type of data being returned.
|
|
721
|
+
*
|
|
722
|
+
* @param {string} key The key in JSON database.
|
|
723
|
+
* @returns {Promise<V>} The data from JSON database.
|
|
724
|
+
*
|
|
725
|
+
* @template V The type of data being returned.
|
|
726
|
+
*/
|
|
727
|
+
get<V = any>(key: string): Promise<V | null>;
|
|
728
|
+
/**
|
|
729
|
+
* Parses the key and sets the value in JSON database.
|
|
730
|
+
*
|
|
731
|
+
* Type parameters:
|
|
732
|
+
*
|
|
733
|
+
* - `V` - The type of data being set.
|
|
734
|
+
* - `R` - The type of data being returned.
|
|
735
|
+
*
|
|
736
|
+
* @param {string} key The key in JSON database.
|
|
737
|
+
* @returns {Promise<R>} The data from JSON database.
|
|
738
|
+
*
|
|
739
|
+
* @template V The type of data being set.
|
|
740
|
+
* @template R The type of data being returned.
|
|
741
|
+
*/
|
|
742
|
+
set<V = any, R = any>(key: string, value: V): Promise<R>;
|
|
743
|
+
/**
|
|
744
|
+
* Parses the key and deletes it from JSON database. If no key is provided, clears the database.
|
|
745
|
+
* @param {V} key The key in JSON database.
|
|
746
|
+
* @returns {Promise<boolean>} `true` if deleted successfully.
|
|
747
|
+
*/
|
|
748
|
+
delete<V = any>(key?: V): Promise<boolean>;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
export { Database, JSONDriver };
|