blackcat.js-database 1.0.0-test → 1.0.1

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