blackcat.js-database 1.0.0-test → 1.0.0

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/dist/index.d.ts CHANGED
@@ -1,751 +1,1304 @@
1
1
  /**
2
- * Represents the base driver interface for database operations.
2
+ * Interface định nghĩa contract cho các storage driver
3
+ * được sử dụng bởi class `Database`.
4
+ * @hidden
5
+ *
6
+ * ⚠️ Đây **không phải database chính**.
7
+ * Interface này chỉ dùng để xây dựng **các driver lưu trữ phụ trợ**
8
+ * như:
9
+ *
10
+ * - `JSONDriver`
11
+ * - `MemoryDriver`
12
+ * - `SQLiteDriver`
13
+ *
14
+ * Các driver này chỉ chịu trách nhiệm:
15
+ *
16
+ * - Lưu trữ toàn bộ dữ liệu database
17
+ * - Trả về dữ liệu database khi được yêu cầu
18
+ *
19
+ * Mọi logic xử lý dữ liệu như:
20
+ *
21
+ * - parse key path (`a.b.c`)
22
+ * - validation
23
+ * - query/filter/map
24
+ * - update dữ liệu
25
+ *
26
+ * đều được thực hiện trong class `Database`.
3
27
  */
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>>;
28
+ interface DatabaseDriver {
29
+ /**
30
+ * Lấy toàn bộ dữ liệu database từ storage.
31
+ *
32
+ * Method này phải trả về **toàn bộ object database**.
33
+ *
34
+ * @template T Kiểu dữ liệu database.
35
+ * @returns Promise chứa toàn bộ dữ liệu database.
36
+ */
37
+ all<T = any>(): Promise<T>;
38
+ /**
39
+ * Ghi toàn bộ dữ liệu database vào storage.
40
+ *
41
+ * ⚠️ Method này **không xử lý key path**.
42
+ * Object database đã được cập nhật trước bởi class `Database`.
43
+ *
44
+ * @param keys Placeholder dùng để tương thích với API của `Database`.
45
+ * Driver có thể bỏ qua tham số này nếu không cần thiết.
46
+ *
47
+ * @param data Toàn bộ object database sau khi đã được cập nhật.
48
+ *
49
+ * @template T Kiểu dữ liệu database.
50
+ * @returns Promise chứa dữ liệu đã được ghi.
51
+ */
52
+ set<T = any>(data: T): Promise<T>;
53
+ /**
54
+ * Xóa toàn bộ dữ liệu database khỏi storage.
55
+ *
56
+ * Thường được sử dụng bởi `Database.deleteAll()`.
57
+ *
58
+ * @returns `true` nếu thao tác thành công.
59
+ */
60
+ delete(): Promise<boolean>;
9
61
  }
10
62
  /**
11
- * Represents the configuration object of the database instance.
63
+ * Cấu hình khởi tạo cho một instance của {@link Database}.
64
+ * @hidden
65
+ *
66
+ * Interface này xác định các tùy chọn cần thiết để thiết lập database,
67
+ * bao gồm driver lưu trữ và các tùy chọn hành vi bổ sung.
68
+ *
69
+ * @example
70
+ * const db = new Database({
71
+ * driver: new MemoryDriver(),
72
+ * debug: true
73
+ * });
12
74
  */
13
75
  interface DatabaseConfiguration {
14
- driver: BaseDriver;
76
+ /**
77
+ * Driver dùng để đọc/ghi dữ liệu của database.
78
+ *
79
+ * Driver chịu trách nhiệm xử lý storage backend
80
+ * (ví dụ: memory, file, json, mongo, redis...).
81
+ */
82
+ driver: DatabaseDriver;
83
+ /**
84
+ * Bật chế độ debug.
85
+ *
86
+ * Khi `true`, database có thể log thêm thông tin về
87
+ * các thao tác đọc/ghi hoặc lỗi để hỗ trợ debug.
88
+ *
89
+ * @defaultValue false
90
+ */
15
91
  debug?: boolean;
16
92
  }
17
93
  /**
18
- * Represents the nullish type (`T` or `null`) and excludes `undefined` from it.
94
+ * Đại diện cho một giá trị thể `T` hoặc `null`.
95
+ * @hidden
19
96
  *
20
- * Type parameters:
97
+ * Utility type này loại bỏ `undefined` khỏi union để đảm bảo
98
+ * giá trị chỉ có thể là kiểu `T` hoặc `null`.
21
99
  *
22
- * - `T` (`any`) - The type to make nullish.
100
+ * Điều này hữu ích khi một API thể không tìm thấy kết quả,
101
+ * nhưng vẫn muốn tránh việc trả về `undefined`.
23
102
  *
24
- * @template T - The type to make nullish.
103
+ * @typeParam T - Kiểu dữ liệu gốc.
104
+ *
105
+ * @example
106
+ * type User = { id: number };
107
+ *
108
+ * const user: Maybe<User> = { id: 1 }; // hợp lệ
109
+ * const noUser: Maybe<User> = null; // hợp lệ
110
+ * const invalid: Maybe<User> = undefined; // lỗi
25
111
  */
26
112
  type Maybe<T> = Exclude<T | null, undefined>;
27
113
  /**
28
- * Conditional type that returns the type based on the condition type result.
114
+ * Utility type điều kiện dùng để trả về một kiểu dữ liệu
115
+ * dựa trên giá trị boolean của điều kiện.
116
+ * @hidden
117
+ *
118
+ * Nếu `T` là `true` thì kiểu trả về sẽ là `IfTrue`.
119
+ * Nếu `T` là `false` thì kiểu trả về sẽ là `IfFalse`.
120
+ *
121
+ * Thường được sử dụng trong các generic type phức tạp
122
+ * để lựa chọn kiểu dữ liệu tại thời điểm compile.
29
123
  *
30
- * Type parameters:
124
+ * @typeParam T - Điều kiện kiểu boolean.
125
+ * @typeParam IfTrue - Kiểu dữ liệu trả về khi `T` là `true`.
126
+ * @typeParam IfFalse - Kiểu dữ liệu trả về khi `T` là `false`. Mặc định là `null`.
31
127
  *
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`.
128
+ * @example
129
+ * type A = If<true, string, number>;
130
+ * // Kết quả: string
131
+ *
132
+ * @example
133
+ * type B = If<false, string, number>;
134
+ * // Kết quả: number
35
135
  *
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`.
136
+ * @example
137
+ * // IfFalse mặc định null
138
+ * type C = If<false, string>;
139
+ * // Kết quả: null
39
140
  */
40
141
  type If<T extends boolean, IfTrue, IfFalse = null> = T extends true ? IfTrue : IfFalse;
41
142
  /**
42
- * Determines if the specified type is object and returns the checking result as boolean.
143
+ * Kiểm tra xem một kiểu dữ liệu phải **object thuần** hay không.
144
+ * @hidden
145
+ *
146
+ * Type này sẽ trả về `true` nếu `T` là object dạng `Record`
147
+ * và **không phải** là:
43
148
  *
44
- * Type parameters:
149
+ * - `null`
150
+ * - `undefined`
151
+ * - `Array`
45
152
  *
46
- * - `T` (`any`) - The type to check.
153
+ * Nếu `T` thuộc các trường hợp trên thì kết quả sẽ là `false`.
47
154
  *
48
- * @template T - The type to check.
155
+ * Utility type này thường được dùng trong các generic phức tạp
156
+ * để xác định liệu một kiểu dữ liệu có thể được xử lý như object hay không.
157
+ *
158
+ * @typeParam T - Kiểu dữ liệu cần kiểm tra.
159
+ *
160
+ * @example
161
+ * type A = IsObject<{ name: string }>;
162
+ * // Kết quả: true
163
+ *
164
+ * @example
165
+ * type B = IsObject<string>;
166
+ * // Kết quả: false
167
+ *
168
+ * @example
169
+ * type C = IsObject<string[]>;
170
+ * // Kết quả: false
171
+ *
172
+ * @example
173
+ * type D = IsObject<null>;
174
+ * // Kết quả: false
49
175
  */
50
176
  type IsObject<T> = T extends null ? false : T extends undefined ? false : T extends any[] ? false : T extends Record<any, any> ? true : false;
51
177
  /**
52
- * Represents a type that works as an array of specified type or `...spread` of specified type.
178
+ * Đại diện cho kiểu dữ liệu thể **mảng của `T`**
179
+ * hoặc **một tuple chứa mảng `T`**.
180
+ * @hidden
53
181
  *
54
- * Type parameters:
182
+ * Utility type này thường được sử dụng để hỗ trợ các API
183
+ * chấp nhận cả hai cách truyền tham số:
55
184
  *
56
- * - `T` (`any`) - The type to convert into rest-or-array type.
185
+ * - truyền nhiều giá trị dạng `...spread`
186
+ * - truyền một mảng các giá trị
57
187
  *
58
- * @template T - The type to convert into rest-or-array type.
188
+ * @typeParam T - Kiểu phần tử của mảng.
189
+ *
190
+ * @example
191
+ * type A = RestOrArray<number>;
192
+ * // number[] | [number[]]
193
+ *
194
+ * @example
195
+ * function example(...values: RestOrArray<number>) {}
196
+ *
197
+ * example(1, 2, 3); // hợp lệ
198
+ * example([1, 2, 3]); // hợp lệ
59
199
  */
60
200
  type RestOrArray<T> = T[] | [T[]];
61
201
  /**
62
- * From the type `A`, extracts the type `T` from the `Array<T>` type, or returns `A` if not array type was specified.
202
+ * Trích xuất kiểu phần tử từ một kiểu mảng.
203
+ * @hidden
204
+ *
205
+ * Nếu `A` là kiểu `Array<T>` thì kết quả sẽ là `T`.
206
+ * Nếu `A` không phải là mảng thì kết quả sẽ giữ nguyên kiểu `A`.
207
+ *
208
+ * Utility type này thường được dùng để lấy kiểu của phần tử
209
+ * bên trong mảng khi làm việc với các generic phức tạp.
63
210
  *
64
- * Type parameters:
211
+ * @typeParam A - Kiểu dữ liệu cần kiểm tra và trích xuất.
65
212
  *
66
- * - `A` (`any`) - The array type to extract the type from.
213
+ * @example
214
+ * type A = ExtractFromArray<number[]>;
215
+ * // Kết quả: number
216
+ *
217
+ * @example
218
+ * type B = ExtractFromArray<string[]>;
219
+ * // Kết quả: string
220
+ *
221
+ * @example
222
+ * type C = ExtractFromArray<boolean>;
223
+ * // Kết quả: boolean
67
224
  *
68
- * @template A - The array type to extract the type from.
225
+ * @example
226
+ * type D = ExtractFromArray<{ id: number }[]>;
227
+ * // Kết quả: { id: number }
69
228
  */
70
229
  type ExtractFromArray<A> = A extends Array<infer T> ? T : A;
71
230
  /**
72
- * Represents a `predicate` callback function from array methods such as `Array.map()`, `Array.find()`, etc.
231
+ * Đại diện cho hàm callback dùng để truy vấn hoặc xử
232
+ * các phần tử trong một mảng.
233
+ * @hidden
234
+ *
235
+ * Type này thường được sử dụng trong các method giống
236
+ * các phương thức của `Array` như:
237
+ *
238
+ * - `map`
239
+ * - `find`
240
+ * - `filter`
241
+ * - `some`
242
+ * - `every`
243
+ * - `findIndex`
244
+ *
245
+ * @typeParam T - Kiểu dữ liệu của từng phần tử trong mảng.
246
+ * @typeParam R - Kiểu dữ liệu trả về của hàm callback. Mặc định là `any`.
247
+ *
248
+ * @param item - Phần tử hiện tại đang được xử lý.
249
+ * @param index - Vị trí của phần tử trong mảng.
250
+ * @param values - Toàn bộ mảng giá trị đang được duyệt.
73
251
  *
74
- * Type parameters:
252
+ * @returns Giá trị trả về của callback.
75
253
  *
76
- * - `T` (`any`) - The type of the item in the array.
77
- * - `R` (`any`, optional) - The return type of the function.
254
+ * @example
255
+ * const result = await db.find((user) => user.id === 1);
256
+ *
257
+ * @example
258
+ * const names = await db.map((user) => user.name);
78
259
  *
79
- * @template T - The type of the item in the array.
80
- * @template R - The return type of the function.
260
+ * @example
261
+ * const admins = await db.filter((user) => user.role === "admin");
81
262
  */
82
263
  type QueryFunction<T, R = any> = (item: T, index: number, values: T[]) => R;
83
264
  /**
84
- * Determines if the specified type is `any` and returns the checking result as boolean.
265
+ * Kiểm tra xem một kiểu dữ liệu phải `any` hay không.
266
+ * @hidden
267
+ *
268
+ * Type này sử dụng kỹ thuật đặc biệt của TypeScript
269
+ * để phát hiện kiểu `any`. Nếu `T` là `any` thì kết quả
270
+ * sẽ là `true`, ngược lại sẽ là `false`.
85
271
  *
86
- * Type parameters:
272
+ * @typeParam T - Kiểu dữ liệu cần kiểm tra.
87
273
  *
88
- * - `T` (`any`) - The type to check.
274
+ * @example
275
+ * type A = IsAny<any>;
276
+ * // Kết quả: true
277
+ *
278
+ * @example
279
+ * type B = IsAny<string>;
280
+ * // Kết quả: false
89
281
  *
90
- * @template T - The type to check.
282
+ * @example
283
+ * type C = IsAny<unknown>;
284
+ * // Kết quả: false
91
285
  */
92
286
  type IsAny<T> = 0 extends (1 & T) ? true : false;
93
287
  /**
94
- * Makes the string union type autocompletable with a `string` type.
288
+ * Tạo kiểu `string` hỗ trợ **autocomplete từ union string**
289
+ * nhưng vẫn cho phép nhập bất kỳ chuỗi nào.
290
+ * @hidden
291
+ *
292
+ * Type này thường dùng khi muốn:
95
293
  *
96
- * Type parameters:
294
+ * - IDE gợi ý các giá trị có sẵn
295
+ * - nhưng vẫn cho phép người dùng nhập string tùy ý
97
296
  *
98
- * - `S` (`string`) - The autocompletable union string type to make compatible with a `string` type.
297
+ * @typeParam S - Union các chuỗi được dùng để autocomplete.
99
298
  *
100
- * @template S - The autocompletable union string type to make compatible with a `string` type.
299
+ * @example
300
+ * type Status = AutocompletableString<"online" | "offline" | "idle">;
301
+ *
302
+ * const a: Status = "online"; // IDE autocomplete
303
+ * const b: Status = "offline"; // IDE autocomplete
304
+ * const c: Status = "custom"; // vẫn hợp lệ
101
305
  */
102
306
  type AutocompletableString<S extends string> = S | (string & {});
103
307
  /**
104
- * Extracts the first key from the specified object path.
105
- * (for example, in key `member.user.id`, the first key will be `member`)
308
+ * Trích xuất **key đầu tiên** từ một object path dạng chuỗi.
309
+ * @hidden
310
+ *
311
+ * Ví dụ với path `"member.user.id"` thì key đầu tiên sẽ là `"member"`.
106
312
  *
107
- * Type parameters:
313
+ * Type này thường được dùng để xác định **object cấp cao nhất**
314
+ * khi làm việc với các path dạng `dot notation`.
108
315
  *
109
- * - `TKey` (`ObjectPath<string, any>`) - The object path to extract the key from.
316
+ * @typeParam TKey - Object path cần trích xuất key đầu tiên.
317
+ *
318
+ * @example
319
+ * type A = FirstObjectKey<"user.profile.name">;
320
+ * // Kết quả: "user"
110
321
  *
111
- * @template TKey - The object path to extract the key from.
322
+ * @example
323
+ * type B = FirstObjectKey<"settings">;
324
+ * // Kết quả: "settings"
112
325
  */
113
326
  type FirstObjectKey<TKey extends ObjectPath<string, any>> = TKey extends `${infer Key}.${infer _Rest}` ? Key : TKey extends string ? TKey : never;
114
327
  /**
115
- * Represents a path to a nested property in an object.
328
+ * Đại diện cho **đường dẫn (path)** tới một thuộc tính trong object,
329
+ * hỗ trợ truy cập các property lồng nhau bằng `dot notation`.
330
+ * @hidden
331
+ *
332
+ * Type này được dùng để tạo **autocomplete path** trong IDE.
333
+ *
334
+ * @typeParam T - Object cần tạo path.
335
+ * @typeParam TKey - Key của object (mặc định là `keyof T`).
116
336
  *
117
- * Type parameters:
337
+ * @example
338
+ * type User = {
339
+ * id: number;
340
+ * profile: {
341
+ * name: string;
342
+ * age: number;
343
+ * };
344
+ * };
345
+ *
346
+ * type Paths = ObjectPath<User>;
118
347
  *
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.
348
+ * // Kết quả:
349
+ * // "id"
350
+ * // "profile"
351
+ * // "profile.name"
352
+ * // "profile.age"
121
353
  *
122
- * @template T - The object to get the path from.
123
- * @template TKey - The key of the object to get the path from.
354
+ * @example
355
+ * type Example = {
356
+ * user: {
357
+ * profile: {
358
+ * name: string;
359
+ * };
360
+ * };
361
+ * };
362
+ *
363
+ * type Paths = ObjectPath<Example>;
364
+ * // "user"
365
+ * // "user.profile"
366
+ * // "user.profile.name"
124
367
  */
125
368
  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
369
  /**
127
- * Extracts the value from the specified object path.
370
+ * Trích xuất kiểu dữ liệu của giá trị tại một object path.
371
+ * @hidden
372
+ *
373
+ * Type này cho phép suy ra kiểu của thuộc tính dựa trên
374
+ * đường dẫn dạng `dot notation`.
128
375
  *
129
- * Type parameters:
376
+ * @typeParam T - Object nguồn.
377
+ * @typeParam P - Object path cần lấy giá trị.
130
378
  *
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.
379
+ * @example
380
+ * type User = {
381
+ * profile: {
382
+ * name: string;
383
+ * age: number;
384
+ * };
385
+ * };
133
386
  *
134
- * @template T - The object to extract the value from.
135
- * @template P - The object path to extract the value from.
387
+ * type A = ObjectValue<User, "profile.name">;
388
+ * // Kết quả: string
389
+ *
390
+ * @example
391
+ * type B = ObjectValue<User, "profile.age">;
392
+ * // Kết quả: number
393
+ *
394
+ * @example
395
+ * type C = ObjectValue<User, "profile">;
396
+ * // Kết quả: { name: string; age: number }
136
397
  */
137
398
  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
399
 
139
400
  /**
140
- * Database class.
141
- *
142
- * Type parameters:
401
+ * Class Database cung cấp API thao tác dữ liệu dạng key-path
402
+ * trên nhiều loại storage khác nhau thông qua `DatabaseDriver`.
143
403
  *
144
- * - `V` (`any`) - The type of the values in the database.
404
+ * Đây **lớp database chính (logic layer)** của hệ thống.
145
405
  *
146
- * @template V (`any`) - The type of the values in the database.
147
- *
148
- * @example
406
+ * Class này chịu trách nhiệm:
407
+ * - parse key path (`user.profile.name`)
408
+ * - validate dữ liệu
409
+ * - thao tác object
410
+ * - query dữ liệu (find, filter, map...)
149
411
  *
150
- * // JSON
151
- * import { Database, JSONDriver } from "blackcat-database";
412
+ * Việc lưu trữ dữ liệu thực tế sẽ được thực hiện bởi các **driver phụ trợ**
413
+ * như:
152
414
  *
153
- * const database = new Database({
154
- * driver: new JSONDriver({ filePath: "./database.json", minifyJSON: false }),
155
- * });
415
+ * - `JSONDriver`
416
+ * - `MemoryDriver`
417
+ * - `SQLiteDriver`
418
+ * - `MongoDriver`
156
419
  *
420
+ * Các driver này chỉ chịu trách nhiệm:
421
+ * - đọc toàn bộ dữ liệu database
422
+ * - ghi toàn bộ dữ liệu database
157
423
  *
424
+ * @template V Kiểu dữ liệu tổng thể của database.
158
425
  */
159
426
  declare class Database<V = any> {
160
427
  /**
161
- * The low-level database driver handling basic operations.
428
+ * Storage driver được sử dụng để đọc và ghi dữ liệu database.
429
+ *
430
+ * Driver phải implement interface `DatabaseDriver`.
431
+ *
432
+ * Ví dụ:
433
+ * ```ts
434
+ * const db = new Database({
435
+ * driver: new JSONDriver({ filePath: "./database.json" }),
436
+ * })
437
+ * ```
162
438
  */
163
- driver: BaseDriver;
164
- constructor(options: DatabaseConfiguration);
439
+ driver: DatabaseDriver;
165
440
  /**
166
- * Gets all the database contents from the cache.
441
+ * Khởi tạo Database instance.
167
442
  *
168
- * Type parameters:
443
+ * @param options Cấu hình database.
444
+ * @param options.driver Driver lưu trữ dữ liệu.
169
445
  *
170
- * - `T` (`object`, defaults to `Record<string, any>`) - The type of object of all the database object to be returned.
446
+ * @example
447
+ * ```ts
448
+ * const db = new Database({
449
+ * driver: new JSONDriver({ filePath: "./database.json" }),
450
+ * })
451
+ * ```
452
+ *
453
+ * Ví dụ với MongoDB:
454
+ *
455
+ * ```ts
456
+ * const db = new Database({
457
+ * driver: new MongoDriver({
458
+ * uri: "mongodb://localhost:27017",
459
+ * databaseName: "mydb"
460
+ * })
461
+ * });
462
+ * ```
463
+ */
464
+ constructor(options: DatabaseConfiguration);
465
+ /**
466
+ * Lấy toàn bộ dữ liệu từ database thông qua driver.
171
467
  *
172
- * @returns {T} Cached database contents.
468
+ * @template T Kiểu dữ liệu object database trả về.
173
469
  *
174
- * @template T (object, defaults to `Record<string, any>`) -
175
- * The type of object of all the database object to be returned.
470
+ * @returns Promise chứa toàn bộ dữ liệu database.
176
471
  *
177
472
  * @example
178
- * const databaseContents = await database.all();
179
- * console.log(databaseContents); // -> { ... (the object of all the data stored in database) }
473
+ * ```ts
474
+ * const data = await db.all();
475
+ * console.log(data.users);
476
+ * ```
180
477
  */
181
478
  all<T extends Record<string, any> = Record<string, any>>(): Promise<T>;
182
479
  /**
183
- * Retrieves a value from database by a key.
480
+ * Lấy giá trị từ database theo key-path.
481
+ *
482
+ * Key có thể là chuỗi dạng path:
483
+ *
484
+ * ```
485
+ * user.profile.name
486
+ * economy.users.123.balance
487
+ * ```
488
+ *
489
+ * Nếu không truyền `key`, method sẽ trả về **toàn bộ database**.
490
+ *
491
+ * @template {AutocompletableString<ObjectPath<V>>} P Key path trong database.
492
+ *
493
+ * @param {AutocompletableString<P>} key Đường dẫn dữ liệu cần lấy.
494
+ *
495
+ * @returns Giá trị tại key hoặc `null` nếu không tồn tại.
184
496
  *
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.
497
+ * @example
498
+ * ```ts
499
+ * const name = await db.get("user.profile.name");
500
+ * ```
187
501
  *
188
502
  * @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
- * // }
503
+ * ```ts
504
+ * const all = await db.get();
505
+ * ```
204
506
  */
205
507
  get<P extends AutocompletableString<ObjectPath<V>>>(key?: AutocompletableString<P>): Promise<Maybe<ObjectValue<V, P>> | V>;
206
508
  /**
207
- * Retrieves a value from database by a key.
208
- *
209
- * - This method is an alias for {@link Database.get()} method.
509
+ * Kiểm tra một key-path có tồn tại trong database hay không.
210
510
  *
211
- * @param {AutocompletableString<P>} key The key to access the target in database by.
212
- * @returns {Maybe<ObjectValue<V, P>>} The value from database.
511
+ * Method này sử dụng `get()` để xác định giá trị tồn tại.
213
512
  *
214
- * @example
215
- * const simpleValue = await database.fetch("simpleValue");
216
- * console.log(simpleValue); // -> 123
513
+ * @template {AutocompletableString<ObjectPath<V>>} P Key path trong database.
217
514
  *
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); // -> []
515
+ * @param {AutocompletableString<P>} key Đường dẫn dữ liệu cần kiểm tra.
221
516
  *
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.
517
+ * @returns `true` nếu key tồn tại, ngược lại `false`.
235
518
  *
236
519
  * @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
- * // }
520
+ * ```ts
521
+ * const exists = await db.has("users.123");
522
+ * ```
259
523
  */
260
524
  has<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<boolean>;
261
525
  /**
262
- * Writes the specified value into database under the specified key.
526
+ * Gán giá trị cho một key-path trong database.
263
527
  *
264
- * @param {AutocompletableString<P>} key The key to write in the target.
265
- * @param {ObjectValue<V, P>} value The value to write.
528
+ * Nếu key-path chưa tồn tại, các object trung gian
529
+ * sẽ được tự động tạo.
266
530
  *
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.
531
+ * @template {AutocompletableString<ObjectPath<V>>} P Key path trong database.
270
532
  *
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`))
533
+ * @param {AutocompletableString<P>} key Đường dẫn dữ liệu cần gán giá trị.
534
+ * @param {ObjectValue<V, P>} value Giá trị mới.
535
+ *
536
+ * @returns {Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>} Giá trị vừa được set hoặc object root nếu value là object.
273
537
  *
274
538
  * @example
275
- * // Assuming that the initial database object for this example is empty.
539
+ * ```ts
540
+ * await db.set("users.123.name", "Alice");
541
+ * ```
276
542
  *
277
- * await database.set("something", "hello");
278
- * const hello = await database.get("something");
543
+ * @example
544
+ * ```ts
545
+ * await db.set("config.prefix", "!");
546
+ * ```
547
+ */
548
+ set<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>;
549
+ /**
550
+ * Xóa một key khỏi database.
279
551
  *
280
- * console.log(hello); // -> "hello"
552
+ * Hỗ trợ nested path bằng dot notation.
281
553
  *
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
554
+ * @typeParam P - Path của object.
285
555
  *
286
- * await database.set("member.inventory", []);
287
- * const inventory = await database.get("member.inventory");
556
+ * @param key - Đường dẫn key cần xóa.
288
557
  *
289
- * console.log(inventory); // -> []
558
+ * @returns
559
+ * - `true` nếu xóa thành công
560
+ * - `false` nếu key không tồn tại
290
561
  *
291
- * // Using objects as value will return the object of key `thats`:
292
- * await database.set("member.user", { name: "Jerry" }); // -> { member: { user: { name: "Jerry" } } }
562
+ * @throws BlackCatError
563
+ * - INVALID_TYPE nếu key không phải string
293
564
  *
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
- * // }
565
+ * @example
566
+ * await db.delete("user.name");
305
567
  */
306
- set<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>;
568
+ delete<P extends AutocompletableString<ObjectPath<V>>>(key?: AutocompletableString<P>): Promise<boolean>;
307
569
  /**
308
- * Update the specified value into database under the specified key.
570
+ * Xóa toàn bộ dữ liệu trong database.
309
571
  *
310
- * @param {AutocompletableString<P>} key The key to update the target.
311
- * @param {ObjectValue<V, P>} value The value to update.
572
+ * Method này gọi trực tiếp `driver.delete()` để reset storage.
312
573
  *
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.
574
+ * @returns `true` sau khi dữ liệu đã được xóa.
316
575
  *
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`))
576
+ * @example
577
+ * await db.deleteAll();
319
578
  */
320
- update<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>;
579
+ deleteAll(): Promise<boolean>;
321
580
  /**
322
- * Performs an arithmetical addition on a target number in database.
581
+ * Cập nhật giá trị của một key-path trong database.
323
582
  *
324
- * [!!!] The type of target value must be a number.
583
+ * Khác với `set()`, method này chỉ ghi đè giá trị của key cuối cùng
584
+ * trong path mà không thay đổi cấu trúc object phía trên.
325
585
  *
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.
586
+ * Nếu các object trung gian trong path chưa tồn tại
587
+ * thì chúng sẽ được tự động tạo.
329
588
  *
330
- * @example
331
- * const additionResult = await database.add("points", 5);
332
- * console.log(additionResult); // -> 10 (5 + 5 = 10)
589
+ * @template P Key path trong database.
333
590
  *
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);
591
+ * @param key Đường dẫn dữ liệu cần cập nhật.
592
+ * @param value Giá trị mới.
338
593
  *
339
- * console.log(unexistentAdditionResult); // -> 3 (0 + 3 = 3)
340
- * // the property didn't exist in database, that's why 0 is added to 3
594
+ * @returns Object cha của key vừa cập nhật.
341
595
  *
342
- * // Assuming that the initial database object for this example is:
343
- * // {
344
- * // points: 5
345
- * // }
596
+ * @example
597
+ * ```ts
598
+ * await db.update("users.123.name", "Alice");
599
+ * ```
346
600
  */
347
- add<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, numberToAdd: number): Promise<number>;
601
+ update<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>;
348
602
  /**
349
- * Performs an arithmetical subtraction on a target number in database.
603
+ * Cộng thêm giá trị vào một key dạng number.
604
+ *
605
+ * Nếu key chưa tồn tại, giá trị mặc định sẽ là `0`.
350
606
  *
351
- * [!!!] The type of target value must be a number.
607
+ * @template P Key path trong database.
352
608
  *
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.
609
+ * @param key Đường dẫn dữ liệu dạng number.
610
+ * @param numberToAdd Số cần cộng thêm.
611
+ *
612
+ * @returns Giá trị mới sau khi cộng.
356
613
  *
357
614
  * @example
358
- * const subtractionResult = await database.subtract("points", 5)
359
- * console.log(subtractionResult) // -> 5 (10 - 5 = 5)
615
+ * ```ts
616
+ * await db.add("economy.users.123.balance", 50);
617
+ * ```
618
+ */
619
+ add<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, numberToAdd: number): Promise<number>;
620
+ /**
621
+ * Trừ giá trị khỏi một key dạng number.
622
+ *
623
+ * Nếu key chưa tồn tại, giá trị mặc định sẽ là `0`.
360
624
  *
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)
625
+ * @template P Key path trong database.
365
626
  *
366
- * console.log(unexistentSubtractionitionResult) // -> 3 (0 - 3 = -3)
367
- * // the property didn't exist in database, so 3 is subtracted from 0
627
+ * @param key Đường dẫn dữ liệu dạng number.
628
+ * @param numberToSubtract Số cần trừ.
368
629
  *
369
- * // Assuming that the initial database object for this example is:
370
- * // {
371
- * // points: 10
372
- * // }
630
+ * @returns Giá trị mới sau khi trừ.
631
+ *
632
+ * @example
633
+ * ```ts
634
+ * await db.subtract("economy.users.123.balance", 20);
635
+ * ```
373
636
  */
374
637
  subtract<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, numberToSubtract: number): Promise<number>;
375
638
  /**
376
- * Pushes the specified value(s) into the target array in database.
639
+ * Thêm một hoặc nhiều phần tử vào cuối array tại key-path.
640
+ *
641
+ * Method này sẽ:
642
+ * - kiểm tra target có phải array không
643
+ * - chỉ thêm các giá trị **chưa tồn tại** trong array
377
644
  *
378
- * [!!!] The type of target value must be an array.
379
- * [!!!] get only values ​​not stored in the database.
645
+ * ⚠️ Nếu key không tồn tại hoặc target không phải array
646
+ * thì sẽ ném lỗi.
380
647
  *
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.
648
+ * @template P Key path trong database.
384
649
  *
385
- * @returns {Promise<Array<ExtractFromArray<ObjectValue<V, P>>>>} Updated target array from database.
650
+ * @param key Đường dẫn tới array cần thêm phần tử.
651
+ * @param values Một hoặc nhiều giá trị cần thêm.
652
+ *
653
+ * @returns Array sau khi thêm phần tử.
386
654
  *
387
655
  * @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"]
656
+ * ```ts
657
+ * await db.push("users.123.roles", "admin");
658
+ * ```
391
659
  *
392
- * // Assuming that the initial database object for this example is:
393
- * // {
394
- * // members: ["Tom"],
395
- * // currencies: ["USD"]
396
- * // }
660
+ * @example
661
+ * ```ts
662
+ * await db.push("queue.songs", song1, song2);
663
+ * ```
397
664
  */
398
665
  push<P extends ObjectPath<V>>(key: AutocompletableString<P>, ...values: RestOrArray<ExtractFromArray<ObjectValue<V, P>>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>;
399
666
  /**
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
- */
667
+ * Thay thế phần tử trong array theo index.
668
+ *
669
+ * @typeParam P - Path trỏ đến array.
670
+ *
671
+ * @param key - Đường dẫn array.
672
+ * @param targetArrayElementIndex - Index cần thay.
673
+ * @param value - Giá trị mới.
674
+ *
675
+ * @returns Array sau khi thay thế.
676
+ *
677
+ * @throws BlackCatError
678
+ * - INVALID_TYPE nếu index không phải number
679
+ * - REQUIRED_PARAMETER_MISSING nếu thiếu value
680
+ * - INVALID_KEY nếu path không tồn tại
681
+ * - INVALID_TARGET nếu target không phải array
682
+ *
683
+ * @example
684
+ * await db.pull("users", 0, { id: 2 });
685
+ */
417
686
  pull<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, targetArrayElementIndex: number, value: ExtractFromArray<ObjectValue<V, P>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>;
418
687
  /**
419
- * Removes the specified element(s) from the target array in database.
688
+ * Xóa phần tử khỏi array theo index.
420
689
  *
421
- * [!!!] The type of target value must be an array.
690
+ * thể truyền nhiều index hoặc một array index.
422
691
  *
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.
692
+ * @typeParam P - Path trỏ đến array.
426
693
  *
427
- * @returns {Promise<Array<ExtractFromArray<ObjectValue<V, P>>>>} Updated target array from database.
694
+ * @param key - Đường dẫn array.
695
+ * @param targetArrayElementIndexes - Các index cần xóa.
428
696
  *
429
- * @example
430
- * console.log(await database.pop("members", 1)); // -> ["Jerry", "Tom"]
697
+ * @returns Danh sách phần tử đã bị xóa.
431
698
  *
432
- * console.log(await database.pop("currencies", 1)); // -> ["VND", "Euro"]
699
+ * @throws BlackCatError
700
+ * - INVALID_TARGET nếu target không phải array
701
+ * - REQUIRED_PARAMETER_MISSING nếu thiếu index
702
+ * - ONE_OR_MORE_ARRAY_TYPES_INVALID nếu index không phải number
433
703
  *
434
- * // Assuming that the initial database object for this example is:
435
- * // {
436
- * // members: ["Jerry", "William", "Tom"],
437
- * // currencies: ["VND", "USD", "Euro"]
438
- * // }
704
+ * @example
705
+ * await db.pop("users", 0);
706
+ * await db.pop("numbers", [1, 2, 3]);
439
707
  */
440
708
  pop<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, ...targetArrayElementIndexes: RestOrArray<ExtractFromArray<number>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>;
441
709
  /**
442
- * Determines whether the specified target is an array.
710
+ * Kiểm tra xem giá trị tại key có phải là Array hay không.
443
711
  *
444
- * @param {AutocompletableString<P>} key The key to access the target in database by.
445
- * @returns {Promise<boolean>} Whether the target is an array.
712
+ * @typeParam P - Path của object trong database.
713
+ * @param key - Đường dẫn key cần kiểm tra.
446
714
  *
447
- * @example
448
- * console.log(await databse.isTargetArray("array")); // => true
449
- * console.log(await databse.isTargetArray("notArray")); // => false
715
+ * @returns `true` nếu giá trị là Array, ngược lại `false`.
450
716
  *
451
- * // Assuming that the initial database object for this example is:
452
- * // {
453
- * // array: [],
454
- * // notArray: 123
455
- * // }
717
+ * @example
718
+ * const result = await db.isTargetArray("users");
719
+ * console.log(result);
456
720
  */
457
721
  isTargetArray<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<boolean>;
458
722
  /**
459
- * Determines whether the specified target is a number.
723
+ * Kiểm tra xem giá trị tại key có phải là Number hay không.
460
724
  *
461
- * @param {AutocompletableString<P>} key The key to access the target in database by.
462
- * @returns {Promise<boolean>} Whether the target is a number.
725
+ * @typeParam P - Path của object trong database.
463
726
  *
464
- * @example
465
- * console.log(await database.isTargetNumber("number")); // -> true
466
- * console.log(await database.isTargetNumber("notNumber")); // -> false
727
+ * @param key - Đường dẫn key cần kiểm tra.
728
+ *
729
+ * @returns `true` nếu giá trị là Number, ngược lại `false`.
467
730
  *
468
- * // Assuming that the initial database object for this example is:
469
- * // {
470
- * // number: 123,
471
- * // notNumber: []
472
- * // }
731
+ * @example
732
+ * const result = await db.isTargetNumber("stats.score");
733
+ * console.log(result);
473
734
  */
474
735
  isTargetNumber<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<boolean>;
475
736
  /**
476
- * Returns an array of object keys by specified database key.
737
+ * Lấy danh sách các key của object trong database.
477
738
  *
478
- * If `key` parameter is omitted, then an array of object keys of database root object will be returned.
739
+ * Nếu không truyền `key` trả về các key cấp cao nhất của database.
479
740
  *
480
- * Type parameters:
741
+ * @typeParam P - Path của object.
481
742
  *
482
- * - `TKeys` (`TupleOrArray<string>`, defaults to `K[]`) - The tuple or array of a type of keys to be returned.
743
+ * @param key - Path của object cần lấy danh sách key.
483
744
  *
484
- * @param {P} [key] The key to access the target in database by.
485
- * @returns {Array<ObjectPath<P>>} Database object keys array.
745
+ * @returns Mảng các key tồn tại (không bao gồm `null` hoặc `undefined`).
486
746
  *
487
747
  * @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
- * // }
748
+ * const rootKeys = await db.keys();
749
+ * const userKeys = await db.keys("user");
517
750
  */
518
751
  keys<P extends AutocompletableString<ObjectPath<V>>>(key?: P): Promise<ObjectPath<P>[]>;
519
752
  /**
520
- * Determines the number of keys in the root of the database.
521
- * @type {Promise<number>} amount of data.
753
+ * Lấy số lượng key cấp cao nhất trong database.
754
+ *
755
+ * @returns Tổng số key ở root level.
756
+ *
757
+ * @example
758
+ * const count = await db.size();
522
759
  */
523
760
  size(): Promise<number>;
524
761
  /**
525
- * Returns an array of object values by specified database key.
762
+ * Lấy danh sách các giá trị từ database.
526
763
  *
527
- * If `key` parameter is omitted, then an array of object values of database root object will be returned.
764
+ * Nếu không truyền `key` trả về toàn bộ values root level.
765
+ * Nếu truyền `key` → trả về values của object tại path đó.
528
766
  *
529
- * @param {P} [key] The key to access the target in database by.
530
- * @returns {Array<ObjectValue<V, P>>} Database object values array.
767
+ * @typeParam P - Path của object trong database.
768
+ *
769
+ * @param key - Đường dẫn object cần lấy values.
770
+ *
771
+ * @returns Mảng các giá trị.
531
772
  *
532
773
  * @example
533
- * const prop3Values = await database.values("prop3");
534
- * console.log(prop3Values); // -> [789, { prop6: 111 }]
774
+ * const allValues = await db.values();
535
775
  *
536
- * const prop5Values = await database.values("prop3.prop5");
537
- * console.log(prop5Values); // -> []
776
+ * const userValues = await db.values("users");
777
+ */
778
+ values<P extends AutocompletableString<ObjectPath<V>>>(key?: P): Promise<ObjectValue<V, P>[]>;
779
+ /**
780
+ * Lấy ngẫu nhiên một phần tử từ array tại path được chỉ định.
538
781
  *
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)
782
+ * @typeParam P - Path trỏ đến array trong database.
542
783
  *
543
- * const databaseValues = await database.values();
544
- * // in this example, `key` parameter is omitted - object values of database object are being returned
784
+ * @param key - Đường dẫn đến array.
545
785
  *
546
- * console.log(databaseValues); // -> [123, 456, { prop4: 789, prop5: { prop6: 111 } }]
786
+ * @returns Một phần tử ngẫu nhiên trong array hoặc `null` nếu array rỗng.
547
787
  *
548
- * const unexistentValues = await database.values("somethingElse");
549
- * console.log(unexistentValues); // -> [] (empty since the key `somethingElse` does not exist in database)
788
+ * @throws BlackCatError
789
+ * - INVALID_TARGET nếu giá trị tại key không phải array
550
790
  *
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
- * // }
791
+ * @example
792
+ * const randomUser = await db.random("users");
562
793
  */
563
- values<P extends AutocompletableString<ObjectPath<V>>>(key?: P): Promise<ObjectValue<V, P>[]>;
794
+ random<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<Maybe<ObjectValue<V, P>>>;
564
795
  /**
565
- * This method works the same way as `Array.find()`.
796
+ * Tìm phần tử đầu tiên thỏa điều kiện.
566
797
  *
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.
798
+ * Hoạt động tương tự `Array.prototype.find`.
569
799
  *
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.
800
+ * @param queryFunction - Hàm kiểm tra điều kiện.
573
801
  *
574
- * @returns {Promise<Maybe<V>>} The search
802
+ * @returns Phần tử đầu tiên thỏa điều kiện hoặc `null` nếu không tìm thấy.
803
+ *
804
+ * @example
805
+ * const user = await db.find(u => u.id === 1);
575
806
  */
576
807
  find(queryFunction: QueryFunction<V>): Promise<Maybe<V>>;
577
808
  /**
578
- * This method works the same way as `Array.map()`.
809
+ * Biến đổi tất cả giá trị trong database thành một mảng mới.
810
+ *
811
+ * Hoạt động giống `Array.prototype.map`.
579
812
  *
580
- * Calls a defined callback function on each element of an array,
581
- * and returns an array that contains the results.
813
+ * @typeParam TReturnType - Kiểu dữ liệu của phần tử trả về.
582
814
  *
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.
815
+ * @param queryFunction - Hàm transform từng phần tử.
586
816
  *
587
- * @returns {Promise<TReturnType[]>}
817
+ * @returns Mảng kết quả sau khi transform.
818
+ *
819
+ * @example
820
+ * const names = await db.map(user => user.name);
588
821
  */
589
822
  map<TReturnType>(queryFunction: QueryFunction<V, TReturnType>): Promise<TReturnType[]>;
590
823
  /**
591
- * This method works the same way as `Array.findIndex()`.
824
+ * Tìm index của phần tử đầu tiên thỏa điều kiện.
592
825
  *
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.
826
+ * Hoạt động giống `Array.prototype.findIndex`.
595
827
  *
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.
828
+ * @param queryFunction - Hàm kiểm tra điều kiện.
599
829
  *
600
- * @returns {Promise<number>}
830
+ * @returns Index của phần tử hoặc `-1` nếu không tìm thấy.
831
+ *
832
+ * @example
833
+ * const index = await db.findIndex(user => user.id === 1);
601
834
  */
602
835
  findIndex(queryFunction: QueryFunction<V>): Promise<number>;
603
836
  /**
604
- * This method works the same way as `Array.filter()`.
837
+ * Lọc các phần tử thỏa điều kiện.
838
+ *
839
+ * Hoạt động giống `Array.prototype.filter`.
605
840
  *
606
- * Iterates over root database values, finds all the element that match the
607
- * specified condition in the callback function and returns the result.
841
+ * @param queryFunction - Hàm kiểm tra điều kiện.
608
842
  *
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.
843
+ * @returns Mảng các phần tử thỏa điều kiện.
612
844
  *
613
- * @returns {Promise<V[]>}
845
+ * @example
846
+ * const admins = await db.filter(user => user.role === "admin");
614
847
  */
615
848
  filter(queryFunction: QueryFunction<V>): Promise<V[]>;
616
849
  /**
617
- * This method works the same way as `Array.some()`.
850
+ * Kiểm tra ít nhất một phần tử thỏa điều kiện hay không.
851
+ *
852
+ * Hoạt động giống `Array.prototype.some`.
618
853
  *
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.
854
+ * @param queryFunction - Hàm kiểm tra điều kiện.
622
855
  *
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.
856
+ * @returns `true` nếu tồn tại phần tử thỏa điều kiện.
626
857
  *
627
- * @returns {Promise<boolean>}
858
+ * @example
859
+ * const hasAdmin = await db.some(user => user.role === "admin");
628
860
  */
629
861
  some(queryFunction: QueryFunction<V>): Promise<boolean>;
630
862
  /**
631
- * This method works the same way as `Array.every()`.
863
+ * Kiểm tra tất cả phần tử thỏa điều kiện hay không.
864
+ *
865
+ * Hoạt động giống `Array.prototype.every`.
632
866
  *
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.
867
+ * @param queryFunction - Hàm kiểm tra điều kiện.
636
868
  *
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.
869
+ * @returns `true` nếu tất cả phần tử đều thỏa điều kiện.
640
870
  *
641
- * @returns {Promise<boolean>}
871
+ * @example
872
+ * const allActive = await db.every(user => user.active === true);
642
873
  */
643
874
  every(queryFunction: QueryFunction<V>): Promise<boolean>;
875
+ }
876
+
877
+ /**
878
+ * Cấu hình khởi tạo cho {@link JSONDriver}.
879
+ */
880
+ interface JSONDriverOptions {
881
+ /**
882
+ * Đường dẫn tới file JSON dùng làm database.
883
+ *
884
+ * Ví dụ:
885
+ * ```ts
886
+ * "./database.json"
887
+ * "./data/users.json"
888
+ * ```
889
+ */
890
+ filePath: string;
891
+ /**
892
+ * Nếu bật, nội dung JSON sẽ được **minify**
893
+ * (không format khoảng trắng) để giảm dung lượng file.
894
+ *
895
+ * Nếu tắt, JSON sẽ được format với indentation để dễ đọc.
896
+ *
897
+ * @default false
898
+ */
899
+ minifyJSON?: boolean;
900
+ }
901
+ /**
902
+ * Driver lưu trữ dữ liệu dạng JSON.
903
+ *
904
+ * ⚠️ Đây **không phải database hoàn chỉnh**.
905
+ * Class này chỉ đóng vai trò **driver lưu trữ (storage layer)**
906
+ * cho class chính `Database`.
907
+ *
908
+ * Toàn bộ logic xử lý key path (`a.b.c`), validate dữ liệu,
909
+ * và các thao tác database sẽ được thực hiện trong class `Database`.
910
+ *
911
+ * `JSONDriver` chỉ chịu trách nhiệm:
912
+ *
913
+ * - Đọc dữ liệu từ file JSON
914
+ * - Ghi dữ liệu xuống file JSON
915
+ *
916
+ * @example
917
+ * ```ts
918
+ * const database = new Database({
919
+ * driber: new JSONDriver({\
920
+ * filePath: "./database.json",
921
+ * minifyJSON: false
922
+ * })
923
+ * });
924
+ * ```
925
+ */
926
+ declare class JSONDriver implements DatabaseDriver {
927
+ /**
928
+ * Đường dẫn tới file database JSON.
929
+ */
930
+ private filePath;
644
931
  /**
645
- * Picks a random element of array in database and returns the picked array element.
932
+ * Cho biết minify JSON khi ghi file hay không.
933
+ */
934
+ minifyJSON: boolean;
935
+ /**
936
+ * Khởi tạo một instance mới của {@link JSONDriver}.
646
937
  *
647
- * [!!!] The type of target value must be an array.
938
+ * @param options - Cấu hình driver
648
939
  *
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.
940
+ * @param options.filePath
941
+ * Đường dẫn tới file JSON dùng làm database.
942
+ *
943
+ * @param options.minifyJSON
944
+ * Nếu `true`, nội dung JSON sẽ được minify khi ghi ra file để giảm dung lượng.
651
945
  *
652
946
  * @example
653
- * const array = await database.get("array"); // assuming that the array is ['example1', 'example2', 'example3']
654
- * console.log(array); // -> ['example1', 'example2', 'example3']
947
+ * ```ts
948
+ * const database = new Database({
949
+ * driver: db = new JSONDriver({
950
+ * filePath: "./database.json"
951
+ * })
952
+ * });
953
+ * ```
954
+ */
955
+ constructor(options: JSONDriverOptions);
956
+ /**
957
+ * Đọc toàn bộ dữ liệu từ file database JSON.
958
+ *
959
+ * Nếu file không tồn tại hoặc lỗi parse,
960
+ * method sẽ trả về object rỗng.
655
961
  *
656
- * const randomArrayElement = await database.random("exampleArray");
657
- * console.log(randomArrayElement); // -> randomly picked array element: either 'example1', 'example2', or 'example3'
962
+ * @template V Kiểu dữ liệu database.
963
+ * @returns Promise chứa toàn bộ dữ liệu database.
658
964
  */
659
- random<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<Maybe<ObjectValue<V, P>>>;
965
+ all<V>(): Promise<V>;
660
966
  /**
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.
967
+ * Ghi toàn bộ dữ liệu database xuống file JSON.
968
+ *
969
+ * Method này **không xử key path**.
970
+ * Logic cập nhật dữ liệu sẽ được xử lý ở class `Database`
971
+ * trước khi truyền object hoàn chỉnh vào đây.
972
+ *
973
+ * @param data Toàn bộ dữ liệu database sau khi đã được cập nhật.
974
+ *
975
+ * @template R Kiểu dữ liệu database.
976
+ * @returns Promise chứa dữ liệu đã ghi.
664
977
  */
665
- delete<P extends AutocompletableString<ObjectPath<V>>>(key?: AutocompletableString<P>): Promise<boolean>;
978
+ set<R = any>(data: R): Promise<R>;
666
979
  /**
667
- * Deletes everything from the database.
980
+ * Xóa toàn bộ database.
981
+ *
982
+ * Method này chỉ reset file JSON về `{}`.
668
983
  *
669
- * - This method is an alias for {@link QuickMongo.clear()} method.
670
- * @returns {Promise<boolean>} `true` if cleared successfully, `false` otherwise.
984
+ * @returns `true` nếu reset thành công.
671
985
  */
672
- deleteAll(): Promise<boolean>;
986
+ delete(): Promise<boolean>;
673
987
  }
674
988
 
675
- interface JSONDriverOptions {
989
+ /**
990
+ * Driver lưu trữ dữ liệu trong RAM.
991
+ *
992
+ * ⚠️ Đây **không phải database chính**.
993
+ * Class này chỉ là **storage driver phụ trợ** cho class `Database`.
994
+ *
995
+ * Toàn bộ logic xử lý:
996
+ * - key path (`a.b.c`)
997
+ * - validation
998
+ * - update dữ liệu
999
+ *
1000
+ * đều được thực hiện trong class `Database`.
1001
+ *
1002
+ * `MemoryDriver` chỉ chịu trách nhiệm:
1003
+ * - lưu trữ object database trong RAM
1004
+ * - trả về toàn bộ dữ liệu
1005
+ *
1006
+ * Dữ liệu sẽ **mất khi ứng dụng restart**.
1007
+ *
1008
+ * Phù hợp cho:
1009
+ * - testing
1010
+ * - cache runtime
1011
+ * - development
1012
+ *
1013
+ * @example
1014
+ * ```ts
1015
+ * const database = new Database({
1016
+ * driver: new MemoryDriver({
1017
+ * users: {},
1018
+ * guilds: {},
1019
+ * settings: {
1020
+ * prefix: "!"
1021
+ * }
1022
+ * })
1023
+ * });
1024
+ * ```
1025
+ *
1026
+ * hoặc
1027
+ *
1028
+ * ```ts
1029
+ * const database = new Database({
1030
+ * driver: new MemoryDriver()
1031
+ * });
1032
+ * ```
1033
+ */
1034
+ declare class MemoryDriver implements DatabaseDriver {
676
1035
  /**
677
- * JSON database file path.
678
- * @type {string}
1036
+ * Object chứa toàn bộ dữ liệu database trong RAM.
679
1037
  */
680
- filePath: string;
1038
+ private store;
681
1039
  /**
682
- * Minifies the JSON content in database file to save some space.
683
- * @type {boolean}
1040
+ * Khởi tạo MemoryDriver.
1041
+ *
1042
+ * @param initialData Dữ liệu khởi tạo ban đầu.
1043
+ * * @example
1044
+ * ```ts
1045
+ * const database = new Database({
1046
+ * driver: new MemoryDriver({
1047
+ * users: {},
1048
+ * guilds: {},
1049
+ * settings: {
1050
+ * prefix: "!"
1051
+ * }
1052
+ * })
1053
+ * });
1054
+ * ```
1055
+ *
1056
+ * hoặc
1057
+ *
1058
+ * ```ts
1059
+ * const database = new Database({
1060
+ * driver: new MemoryDriver()
1061
+ * });
1062
+ * ```
684
1063
  */
685
- minifyJSON?: boolean;
1064
+ constructor(initialData?: Record<string, any>);
1065
+ /**
1066
+ * Trả về toàn bộ dữ liệu database.
1067
+ *
1068
+ * @template T Kiểu dữ liệu database.
1069
+ */
1070
+ all<T = any>(): Promise<T>;
1071
+ /**
1072
+ * Ghi toàn bộ database vào memory.
1073
+ *
1074
+ * ⚠️ Method này **không xử lý key-path**.
1075
+ * Dữ liệu đã được xử lý trước bởi `Database`.
1076
+ *
1077
+ * @param data Toàn bộ database object
1078
+ */
1079
+ set<T = any>(data: T): Promise<T>;
1080
+ /**
1081
+ * Xóa toàn bộ dữ liệu database trong memory.
1082
+ */
1083
+ delete(): Promise<boolean>;
686
1084
  }
1085
+
687
1086
  /**
688
- * JSONDriver class.
1087
+ * Các tùy chọn cấu hình cho SQLiteDriver.
1088
+ * @hidden
1089
+ *
1090
+ * Interface này định nghĩa các thiết lập được sử dụng khi
1091
+ * khởi tạo driver SQLite cho hệ thống database.
689
1092
  */
690
- declare class JSONDriver implements BaseDriver {
1093
+ interface SQLiteDriverOptions {
691
1094
  /**
692
- * JSON database file path.
693
- * @type {string}
1095
+ * Đường dẫn đến file cơ sở dữ liệu SQLite.
1096
+ *
1097
+ * Nếu không được cung cấp, driver có thể tạo
1098
+ * một file database mặc định tùy theo cách triển khai.
1099
+ *
1100
+ * Ví dụ:
1101
+ * `"./database.sqlite"`
694
1102
  */
695
- private filePath;
1103
+ filePath?: string;
696
1104
  /**
697
- * Minifies the JSON content in database file to save some space.
698
- * @type {boolean}
1105
+ * Tên bảng được sử dụng để lưu trữ dữ liệu key-value.
1106
+ *
1107
+ * Nếu không được chỉ định, driver có thể sử dụng
1108
+ * một tên bảng mặc định (ví dụ: `"data"`).
1109
+ *
1110
+ * Ví dụ:
1111
+ * `"storage"`
699
1112
  */
700
- minifyJSON: boolean;
1113
+ table?: string;
1114
+ }
1115
+ /**
1116
+ * Driver lưu trữ dữ liệu bằng SQLite.
1117
+ *
1118
+ * ⚠️ Đây **không phải database chính**.
1119
+ * Class này chỉ là **driver lưu trữ phụ trợ** cho `Database`.
1120
+ *
1121
+ * Logic xử lý dữ liệu như:
1122
+ * - parse key path
1123
+ * - update object
1124
+ * - validation
1125
+ *
1126
+ * sẽ được thực hiện bởi class `Database`.
1127
+ *
1128
+ * Driver này chỉ:
1129
+ * - serialize database thành JSON
1130
+ * - lưu JSON vào SQLite
1131
+ * - trả về JSON khi đọc
1132
+ *
1133
+ * Toàn bộ database được lưu trong **một row duy nhất**.
1134
+ *
1135
+ * @example
1136
+ * ```ts
1137
+ * const database = new Database({
1138
+ * driver: new SQLiteDriver({
1139
+ * filePath: "database.sqlite",
1140
+ * table: "json_store"
1141
+ * })
1142
+ * })
1143
+ * ```
1144
+ */
1145
+ declare class SQLiteDriver implements DatabaseDriver {
701
1146
  /**
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.
1147
+ * SQLite database instance.
705
1148
  */
706
- constructor(options: JSONDriverOptions);
1149
+ private db;
1150
+ /**
1151
+ * Tên table lưu trữ dữ liệu JSON.
1152
+ */
1153
+ private table;
707
1154
  /**
708
- * Returns all data from JSON database.
1155
+ * Khởi tạo SQLiteDriver.
709
1156
  *
710
- * @returns {Promise<any>} All data from JSON database.
1157
+ * @param filePath Đường dẫn file SQLite
1158
+ * @param table Tên table lưu JSON database
1159
+ * @example
1160
+ * ```ts
1161
+ * const database = new Database({
1162
+ * driver: new SQLiteDriver({
1163
+ * filePath: "database.sqlite",
1164
+ * table: "json_store"
1165
+ * })
1166
+ * })
1167
+ * ```
1168
+ */
1169
+ constructor(options?: SQLiteDriverOptions);
1170
+ /**
1171
+ * Lấy toàn bộ dữ liệu database từ SQLite.
711
1172
  *
712
- * @template V The type of data being returned.
1173
+ * @template T Kiểu dữ liệu database.
713
1174
  */
714
- all<V>(): Promise<V>;
1175
+ all<T = any>(): Promise<T>;
715
1176
  /**
716
- * Parses the key and fetches the value from JSON database.
1177
+ * Ghi toàn bộ database vào SQLite.
1178
+ *
1179
+ * ⚠️ Method này **không xử lý key-path**.
1180
+ * `Database` đã cập nhật object trước khi truyền vào đây.
717
1181
  *
718
- * Type parameters:
1182
+ * @param data Toàn bộ database object
1183
+ */
1184
+ set<T = any>(data: T): Promise<T>;
1185
+ /**
1186
+ * Reset toàn bộ database.
1187
+ */
1188
+ delete(): Promise<boolean>;
1189
+ }
1190
+
1191
+ /**
1192
+ * Cấu hình cho MongoDriver.
1193
+ */
1194
+ interface MongoDriverOptions {
1195
+ /**
1196
+ * Chuỗi kết nối MongoDB.
719
1197
  *
720
- * - `V` - The type of data being returned.
1198
+ * @default "mongodb://localhost:27017"
1199
+ */
1200
+ mongourl?: string;
1201
+ /**
1202
+ * Tên database MongoDB.
721
1203
  *
722
- * @param {string} key The key in JSON database.
723
- * @returns {Promise<V>} The data from JSON database.
1204
+ * @default "database"
1205
+ */
1206
+ databaseName?: string;
1207
+ /**
1208
+ * Tên collection lưu dữ liệu JSON.
724
1209
  *
725
- * @template V The type of data being returned.
1210
+ * @default "json_store"
726
1211
  */
727
- get<V = any>(key: string): Promise<V | null>;
1212
+ collectionName?: string;
1213
+ }
1214
+ /**
1215
+ * Driver lưu trữ dữ liệu bằng MongoDB.
1216
+ *
1217
+ * ⚠️ Đây **không phải database chính**.
1218
+ * Class này chỉ là **driver lưu trữ phụ trợ** cho class `Database`.
1219
+ *
1220
+ * Logic xử lý:
1221
+ * - parse key path
1222
+ * - validate dữ liệu
1223
+ * - cập nhật object
1224
+ *
1225
+ * sẽ được xử lý bởi class `Database`.
1226
+ *
1227
+ * `MongoDriver` chỉ chịu trách nhiệm:
1228
+ * - lưu toàn bộ database object
1229
+ * - trả về database object
1230
+ *
1231
+ * Toàn bộ database được lưu trong **một document duy nhất**.
1232
+ *
1233
+ * @example
1234
+ * ```ts
1235
+ * const database = new Database({
1236
+ * driver: new MongoDriver({
1237
+ * mongourl: "mongodb://localhost:27017",
1238
+ * databaseName: "mydb",
1239
+ * collectionName: "store"
1240
+ * })
1241
+ * });
1242
+ * ```
1243
+ */
1244
+ declare class MongoDriver implements DatabaseDriver {
1245
+ /**
1246
+ * MongoDB client instance.
1247
+ */
1248
+ private client;
1249
+ /**
1250
+ * MongoDB database instance.
1251
+ */
1252
+ private db;
1253
+ /**
1254
+ * Collection lưu dữ liệu JSON.
1255
+ */
1256
+ private collection;
1257
+ /**
1258
+ * Tên database MongoDB.
1259
+ */
1260
+ private databaseName;
1261
+ /**
1262
+ * Tên collection MongoDB.
1263
+ */
1264
+ private collectionName;
728
1265
  /**
729
- * Parses the key and sets the value in JSON database.
1266
+ * Khởi tạo MongoDriver.
730
1267
  *
731
- * Type parameters:
1268
+ * @param options Cấu hình MongoDB driver.
732
1269
  *
733
- * - `V` - The type of data being set.
734
- * - `R` - The type of data being returned.
1270
+ * @example
1271
+ * ```ts
1272
+ * const database = new Database({
1273
+ * driver: new MongoDriver({
1274
+ * mongourl: "mongodb://localhost:27017",
1275
+ * databaseName: "mydb",
1276
+ * collectionName: "store"
1277
+ * })
1278
+ * });
1279
+ */
1280
+ constructor(options?: MongoDriverOptions);
1281
+ /**
1282
+ * Thiết lập kết nối MongoDB nếu chưa kết nối.
1283
+ */
1284
+ private connect;
1285
+ /**
1286
+ * Lấy toàn bộ dữ liệu database từ MongoDB.
1287
+ */
1288
+ all<T = any>(): Promise<T>;
1289
+ /**
1290
+ * Ghi toàn bộ database vào MongoDB.
735
1291
  *
736
- * @param {string} key The key in JSON database.
737
- * @returns {Promise<R>} The data from JSON database.
1292
+ * ⚠️ Method này **không xử key-path**.
1293
+ * Object database đã được xử bởi class `Database`.
738
1294
  *
739
- * @template V The type of data being set.
740
- * @template R The type of data being returned.
1295
+ * @param data Toàn bộ object database
741
1296
  */
742
- set<V = any, R = any>(key: string, value: V): Promise<R>;
1297
+ set<T = any>(data: T): Promise<T>;
743
1298
  /**
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.
1299
+ * Reset toàn bộ database về object rỗng.
747
1300
  */
748
- delete<V = any>(key?: V): Promise<boolean>;
1301
+ delete(): Promise<boolean>;
749
1302
  }
750
1303
 
751
- export { Database, JSONDriver };
1304
+ export { Database, JSONDriver, MemoryDriver, MongoDriver, SQLiteDriver };