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/README.md +414 -2
- package/dist/index.d.mts +1101 -458
- package/dist/index.d.ts +1101 -458
- package/dist/index.js +742 -456
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +728 -455
- package/dist/index.mjs.map +1 -1
- package/package.json +20 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,751 +1,1394 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
94
|
+
* Đại diện cho một giá trị có thể là `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
|
-
*
|
|
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
|
-
*
|
|
103
|
+
* @typeParam T - Kiểu dữ liệu gốc.
|
|
23
104
|
*
|
|
24
|
-
* @
|
|
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
|
-
|
|
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
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
128
|
+
* @typeParam T - Điều kiện kiểu boolean.
|
|
129
|
+
* @typeParam IfTrue - Kiểu dữ liệu trả về khi `T` là `true`.
|
|
130
|
+
* @typeParam IfFalse - Kiểu dữ liệu trả về khi `T` là `false`. Mặc định là `null`.
|
|
35
131
|
*
|
|
36
|
-
* @
|
|
37
|
-
*
|
|
38
|
-
*
|
|
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
|
-
*
|
|
147
|
+
* Kiểm tra xem một kiểu dữ liệu có phải là **object thuần** hay không.
|
|
148
|
+
* @hidden
|
|
43
149
|
*
|
|
44
|
-
* Type
|
|
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
|
-
* - `
|
|
153
|
+
* - `null`
|
|
154
|
+
* - `undefined`
|
|
155
|
+
* - `Array`
|
|
47
156
|
*
|
|
48
|
-
*
|
|
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
|
-
*
|
|
182
|
+
* Đại diện cho kiểu dữ liệu có thể là **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
|
-
*
|
|
189
|
+
* - truyền nhiều giá trị dạng `...spread`
|
|
190
|
+
* - truyền một mảng các giá trị
|
|
55
191
|
*
|
|
56
|
-
*
|
|
192
|
+
* @typeParam T - Kiểu phần tử của mảng.
|
|
57
193
|
*
|
|
58
|
-
* @
|
|
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
|
-
*
|
|
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
|
-
*
|
|
215
|
+
* @typeParam A - Kiểu dữ liệu cần kiểm tra và trích xuất.
|
|
65
216
|
*
|
|
66
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
235
|
+
* Đại diện cho hàm callback dùng để truy vấn hoặc xử lý
|
|
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
|
-
*
|
|
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
|
-
*
|
|
77
|
-
*
|
|
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
|
-
* @
|
|
80
|
-
*
|
|
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
|
-
*
|
|
269
|
+
* Kiểm tra xem một kiểu dữ liệu có phải là `any` hay không.
|
|
270
|
+
* @hidden
|
|
85
271
|
*
|
|
86
|
-
* Type
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
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
|
-
*
|
|
301
|
+
* @typeParam S - Union các chuỗi được dùng để autocomplete.
|
|
97
302
|
*
|
|
98
|
-
*
|
|
303
|
+
* @example
|
|
304
|
+
* type Status = AutocompletableString<"online" | "offline" | "idle">;
|
|
99
305
|
*
|
|
100
|
-
*
|
|
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
|
-
*
|
|
105
|
-
*
|
|
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
|
|
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
|
-
*
|
|
320
|
+
* @typeParam TKey - Object path cần trích xuất key đầu tiên.
|
|
110
321
|
*
|
|
111
|
-
* @
|
|
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
|
-
*
|
|
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
|
|
336
|
+
* Type này được dùng để tạo **autocomplete path** trong IDE.
|
|
118
337
|
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
338
|
+
* @typeParam T - Object cần tạo path.
|
|
339
|
+
* @typeParam TKey - Key của object (mặc định là `keyof T`).
|
|
121
340
|
*
|
|
122
|
-
* @
|
|
123
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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
|
-
*
|
|
132
|
-
*
|
|
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
|
-
* @
|
|
135
|
-
*
|
|
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
|
|
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
|
-
*
|
|
408
|
+
* Đây là **lớp database chính (logic layer)** của hệ thống.
|
|
147
409
|
*
|
|
148
|
-
*
|
|
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
|
-
*
|
|
151
|
-
*
|
|
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
|
-
*
|
|
154
|
-
*
|
|
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
|
-
*
|
|
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:
|
|
164
|
-
constructor(options: DatabaseConfiguration);
|
|
443
|
+
driver: DatabaseDriver;
|
|
165
444
|
/**
|
|
166
|
-
*
|
|
445
|
+
* Khởi tạo Database instance.
|
|
167
446
|
*
|
|
168
|
-
*
|
|
447
|
+
* @param options Cấu hình database.
|
|
448
|
+
* @param options.driver Driver lưu trữ dữ liệu.
|
|
169
449
|
*
|
|
170
|
-
*
|
|
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
|
-
* @
|
|
472
|
+
* @template T Kiểu dữ liệu object database trả về.
|
|
173
473
|
*
|
|
174
|
-
* @
|
|
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
|
-
*
|
|
179
|
-
*
|
|
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
|
-
*
|
|
484
|
+
* Lấy giá trị từ database theo key-path.
|
|
184
485
|
*
|
|
185
|
-
*
|
|
186
|
-
* @returns {Maybe<ObjectValue<V, P>>} The value of the target in database.
|
|
486
|
+
* Key có thể là chuỗi dạng path:
|
|
187
487
|
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
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
|
-
*
|
|
513
|
+
* Tìm và trả về một bản ghi đầu tiên khớp với điều kiện trong database.
|
|
208
514
|
*
|
|
209
|
-
*
|
|
515
|
+
* Hàm này có hai cách sử dụng:
|
|
210
516
|
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
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
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
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
|
-
*
|
|
219
|
-
*
|
|
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
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
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 có tồn tại trong database hay không.
|
|
235
551
|
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
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
|
-
*
|
|
241
|
-
* console.log(somethingElse); // -> false
|
|
556
|
+
* @param {AutocompletableString<P>} key Đường dẫn dữ liệu cần kiểm tra.
|
|
242
557
|
*
|
|
243
|
-
*
|
|
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
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
265
|
-
* @param {ObjectValue<V, P>} value The value to write.
|
|
572
|
+
* @template {AutocompletableString<ObjectPath<V>>} P Key path trong database.
|
|
266
573
|
*
|
|
267
|
-
* @
|
|
268
|
-
*
|
|
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
|
-
*
|
|
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 là object.
|
|
273
578
|
*
|
|
274
579
|
* @example
|
|
275
|
-
*
|
|
580
|
+
* ```ts
|
|
581
|
+
* await db.set("users.123.name", "Alice");
|
|
582
|
+
* ```
|
|
276
583
|
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
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
|
-
*
|
|
593
|
+
* Hỗ trợ nested path bằng dot notation.
|
|
281
594
|
*
|
|
282
|
-
*
|
|
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
|
-
*
|
|
287
|
-
* const inventory = await database.get("member.inventory");
|
|
597
|
+
* @param key - Đường dẫn key cần xóa.
|
|
288
598
|
*
|
|
289
|
-
*
|
|
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
|
-
*
|
|
292
|
-
*
|
|
603
|
+
* @throws BlackCatError
|
|
604
|
+
* - INVALID_TYPE nếu key không phải string
|
|
293
605
|
*
|
|
294
|
-
*
|
|
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
|
-
|
|
609
|
+
delete<P extends AutocompletableString<ObjectPath<V>>>(key?: AutocompletableString<P>): Promise<boolean>;
|
|
307
610
|
/**
|
|
308
|
-
*
|
|
611
|
+
* Xóa toàn bộ dữ liệu trong database.
|
|
309
612
|
*
|
|
310
|
-
*
|
|
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
|
|
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
|
-
*
|
|
318
|
-
*
|
|
617
|
+
* @example
|
|
618
|
+
* await db.deleteAll();
|
|
319
619
|
*/
|
|
320
|
-
|
|
620
|
+
deleteAll(): Promise<boolean>;
|
|
321
621
|
/**
|
|
322
|
-
*
|
|
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
|
-
*
|
|
639
|
+
* @returns Số lượng phần tử đã bị xóa khỏi mảng.
|
|
325
640
|
*
|
|
326
|
-
* @
|
|
327
|
-
*
|
|
328
|
-
*
|
|
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
|
-
*
|
|
332
|
-
*
|
|
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
|
-
*
|
|
335
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
681
|
+
* @example
|
|
682
|
+
* ```ts
|
|
683
|
+
* await db.update("users.123.name", "Alice");
|
|
684
|
+
* ```
|
|
346
685
|
*/
|
|
347
|
-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
359
|
-
*
|
|
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
|
-
*
|
|
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ẽ là `0`.
|
|
365
709
|
*
|
|
366
|
-
*
|
|
367
|
-
* // the property didn't exist in database, so 3 is subtracted from 0
|
|
710
|
+
* @template P Key path trong database.
|
|
368
711
|
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
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
|
-
*
|
|
724
|
+
* Thêm một hoặc nhiều phần tử vào cuối array tại key-path.
|
|
377
725
|
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
726
|
+
* Method này sẽ:
|
|
727
|
+
* - kiểm tra target có phải array không
|
|
728
|
+
* - chỉ thêm các giá trị **chưa tồn tại** trong array
|
|
380
729
|
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
741
|
+
* ```ts
|
|
742
|
+
* await db.push("users.123.roles", "admin");
|
|
743
|
+
* ```
|
|
391
744
|
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
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
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
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
|
-
*
|
|
773
|
+
* Xóa phần tử khỏi array theo index.
|
|
420
774
|
*
|
|
421
|
-
*
|
|
775
|
+
* Có thể truyền nhiều index hoặc một array index.
|
|
422
776
|
*
|
|
423
|
-
* @
|
|
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
|
-
* @
|
|
779
|
+
* @param key - Đường dẫn array.
|
|
780
|
+
* @param targetArrayElementIndexes - Các index cần xóa.
|
|
428
781
|
*
|
|
429
|
-
* @
|
|
430
|
-
* console.log(await database.pop("members", 1)); // -> ["Jerry", "Tom"]
|
|
782
|
+
* @returns Danh sách phần tử đã bị xóa.
|
|
431
783
|
*
|
|
432
|
-
*
|
|
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
|
-
*
|
|
435
|
-
*
|
|
436
|
-
*
|
|
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
|
-
*
|
|
795
|
+
* Kiểm tra xem giá trị tại key có phải là Array hay không.
|
|
443
796
|
*
|
|
444
|
-
* @
|
|
445
|
-
* @
|
|
797
|
+
* @typeParam P - Path của object trong database.
|
|
798
|
+
* @param key - Đường dẫn key cần kiểm tra.
|
|
446
799
|
*
|
|
447
|
-
* @
|
|
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
|
-
*
|
|
452
|
-
*
|
|
453
|
-
*
|
|
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
|
-
*
|
|
808
|
+
* Kiểm tra xem giá trị tại key có phải là Number hay không.
|
|
460
809
|
*
|
|
461
|
-
* @
|
|
462
|
-
* @returns {Promise<boolean>} Whether the target is a number.
|
|
810
|
+
* @typeParam P - Path của object trong database.
|
|
463
811
|
*
|
|
464
|
-
* @
|
|
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
|
-
*
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
814
|
+
* @returns `true` nếu giá trị là 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
|
-
*
|
|
822
|
+
* Lấy danh sách các key của object trong database.
|
|
477
823
|
*
|
|
478
|
-
*
|
|
824
|
+
* Nếu không truyền `key` → trả về các key cấp cao nhất của database.
|
|
479
825
|
*
|
|
480
|
-
*
|
|
826
|
+
* @typeParam P - Path của object.
|
|
481
827
|
*
|
|
482
|
-
*
|
|
828
|
+
* @param key - Path của object cần lấy danh sách key.
|
|
483
829
|
*
|
|
484
|
-
* @
|
|
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
|
|
489
|
-
*
|
|
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
|
-
*
|
|
521
|
-
*
|
|
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
|
-
*
|
|
847
|
+
* Lấy danh sách các giá trị từ database.
|
|
526
848
|
*
|
|
527
|
-
*
|
|
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
|
-
* @
|
|
530
|
-
*
|
|
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
|
|
534
|
-
* console.log(prop3Values); // -> [789, { prop6: 111 }]
|
|
859
|
+
* const allValues = await db.values();
|
|
535
860
|
*
|
|
536
|
-
* const
|
|
537
|
-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
871
|
+
* @returns Một phần tử ngẫu nhiên trong array hoặc `null` nếu array rỗng.
|
|
547
872
|
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
873
|
+
* @throws BlackCatError
|
|
874
|
+
* - INVALID_TARGET nếu giá trị tại key không phải array
|
|
550
875
|
*
|
|
551
|
-
*
|
|
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
|
-
|
|
879
|
+
random<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<Maybe<ObjectValue<V, P>>>;
|
|
564
880
|
/**
|
|
565
|
-
*
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
935
|
+
* Kiểm tra có ít nhất một phần tử thỏa điều kiện hay không.
|
|
618
936
|
*
|
|
619
|
-
*
|
|
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
|
|
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
|
|
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
|
-
*
|
|
948
|
+
* Kiểm tra tất cả phần tử có thỏa điều kiện hay không.
|
|
949
|
+
*
|
|
950
|
+
* Hoạt động giống `Array.prototype.every`.
|
|
632
951
|
*
|
|
633
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
967
|
+
* Đường dẫn tới file JSON dùng làm database.
|
|
646
968
|
*
|
|
647
|
-
*
|
|
969
|
+
* Ví 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
|
-
*
|
|
650
|
-
*
|
|
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
|
-
*
|
|
654
|
-
*
|
|
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
|
-
*
|
|
657
|
-
*
|
|
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
|
-
|
|
1055
|
+
all<V>(): Promise<V>;
|
|
660
1056
|
/**
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
1057
|
+
* Ghi toàn bộ dữ liệu database xuống file JSON.
|
|
1058
|
+
*
|
|
1059
|
+
* Method này **không xử lý 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
|
-
|
|
1068
|
+
set<R = any>(data: R): Promise<R>;
|
|
666
1069
|
/**
|
|
667
|
-
*
|
|
1070
|
+
* Xóa toàn bộ database.
|
|
1071
|
+
*
|
|
1072
|
+
* Method này chỉ reset file JSON về `{}`.
|
|
668
1073
|
*
|
|
669
|
-
*
|
|
670
|
-
* @returns {Promise<boolean>} `true` if cleared successfully, `false` otherwise.
|
|
1074
|
+
* @returns `true` nếu reset thành công.
|
|
671
1075
|
*/
|
|
672
|
-
|
|
1076
|
+
delete(): Promise<boolean>;
|
|
673
1077
|
}
|
|
674
1078
|
|
|
675
|
-
|
|
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
|
-
*
|
|
678
|
-
* @type {string}
|
|
1126
|
+
* Object chứa toàn bộ dữ liệu database trong RAM.
|
|
679
1127
|
*/
|
|
680
|
-
|
|
1128
|
+
private store;
|
|
681
1129
|
/**
|
|
682
|
-
*
|
|
683
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
693
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
698
|
-
* @type {boolean}
|
|
1237
|
+
* SQLite database instance.
|
|
699
1238
|
*/
|
|
700
|
-
|
|
1239
|
+
private db;
|
|
701
1240
|
/**
|
|
702
|
-
*
|
|
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
|
-
|
|
1243
|
+
private table;
|
|
707
1244
|
/**
|
|
708
|
-
*
|
|
1245
|
+
* Khởi tạo SQLiteDriver.
|
|
709
1246
|
*
|
|
710
|
-
* @
|
|
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
|
|
1263
|
+
* @template T Kiểu dữ liệu database.
|
|
713
1264
|
*/
|
|
714
|
-
all<
|
|
1265
|
+
all<T = any>(): Promise<T>;
|
|
715
1266
|
/**
|
|
716
|
-
*
|
|
1267
|
+
* Ghi toàn bộ database vào SQLite.
|
|
717
1268
|
*
|
|
718
|
-
*
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
723
|
-
|
|
1288
|
+
* @default "mongodb://localhost:27017"
|
|
1289
|
+
*/
|
|
1290
|
+
mongourl?: string;
|
|
1291
|
+
/**
|
|
1292
|
+
* Tên database MongoDB.
|
|
724
1293
|
*
|
|
725
|
-
* @
|
|
1294
|
+
* @default "database"
|
|
726
1295
|
*/
|
|
727
|
-
|
|
1296
|
+
databaseName?: string;
|
|
728
1297
|
/**
|
|
729
|
-
*
|
|
1298
|
+
* Tên collection lưu dữ liệu JSON.
|
|
730
1299
|
*
|
|
731
|
-
*
|
|
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
|
-
*
|
|
734
|
-
*
|
|
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
|
-
*
|
|
737
|
-
*
|
|
1382
|
+
* ⚠️ Method này **không xử lý key-path**.
|
|
1383
|
+
* Object database đã được xử lý bởi class `Database`.
|
|
738
1384
|
*
|
|
739
|
-
* @
|
|
740
|
-
* @template R The type of data being returned.
|
|
1385
|
+
* @param data Toàn bộ object database
|
|
741
1386
|
*/
|
|
742
|
-
set<
|
|
1387
|
+
set<T = any>(data: T): Promise<T>;
|
|
743
1388
|
/**
|
|
744
|
-
*
|
|
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
|
|
1391
|
+
delete(): Promise<boolean>;
|
|
749
1392
|
}
|
|
750
1393
|
|
|
751
|
-
export { Database, JSONDriver };
|
|
1394
|
+
export { Database, JSONDriver, MemoryDriver, MongoDriver, SQLiteDriver };
|