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