blackcat.js-database 1.0.3 → 1.0.5
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 +14 -4
- package/dist/index.d.mts +141 -13
- package/dist/index.d.ts +141 -13
- package/dist/index.js +202 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +202 -60
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -349,10 +349,20 @@ import { Database, MongoDriver } from "blackcat.js-database";
|
|
|
349
349
|
|
|
350
350
|
const database = new Database({
|
|
351
351
|
driver: new MongoDriver({
|
|
352
|
-
mongourl: "
|
|
353
|
-
databaseName: "
|
|
354
|
-
collectionName: "
|
|
355
|
-
|
|
352
|
+
mongourl: "mongourl",
|
|
353
|
+
databaseName: "BlackCat-Club",
|
|
354
|
+
collectionName: "database-test",
|
|
355
|
+
onLoad: ({ db, collection, databaseName }) => {
|
|
356
|
+
console.log(`✅ Đã kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
357
|
+
console.log(`📦 Bộ sưu tập: ${collection.collectionName}`);
|
|
358
|
+
// Ví dụ advanced:
|
|
359
|
+
// db.command({ ping: 1 })
|
|
360
|
+
},
|
|
361
|
+
onError: ({ error, databaseName }) => {
|
|
362
|
+
console.error(`❌ Không thể kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
363
|
+
console.error(error);
|
|
364
|
+
}
|
|
365
|
+
}),
|
|
356
366
|
});
|
|
357
367
|
```
|
|
358
368
|
---
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { MongoClient, Db, Collection } from 'mongodb';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Interface định nghĩa contract cho các storage driver
|
|
3
5
|
* được sử dụng bởi class `Database`.
|
|
@@ -369,7 +371,7 @@ type FirstObjectKey<TKey extends ObjectPath<string, any>> = TKey extends `${infe
|
|
|
369
371
|
* // "user.profile"
|
|
370
372
|
* // "user.profile.name"
|
|
371
373
|
*/
|
|
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]
|
|
374
|
+
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 ? NonNullable<T[TKey]> extends Array<any> ? TKey : NonNullable<T[TKey]> extends Record<string, any> ? `${TKey}` | `${TKey}.${ObjectPath<NonNullable<T[TKey]>>}` : TKey : never;
|
|
373
375
|
/**
|
|
374
376
|
* Trích xuất kiểu dữ liệu của giá trị tại một object path.
|
|
375
377
|
* @hidden
|
|
@@ -663,25 +665,57 @@ declare class Database<V = any> {
|
|
|
663
665
|
*/
|
|
664
666
|
deleteMany<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, query: Partial<ArrayElement<ObjectValue<V, P>>>): Promise<number>;
|
|
665
667
|
/**
|
|
666
|
-
* Cập nhật
|
|
668
|
+
* Cập nhật dữ liệu trong database theo đường dẫn key.
|
|
667
669
|
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
+
* Hàm này sẽ tìm đến vị trí của `key` trong database và gán `value` vào đó.
|
|
671
|
+
* Nếu `value` và giá trị hiện tại tại key đều là object thì sẽ thực hiện
|
|
672
|
+
* **shallow merge** thay vì ghi đè toàn bộ object.
|
|
670
673
|
*
|
|
671
|
-
*
|
|
672
|
-
* thì chúng sẽ được tự động tạo.
|
|
674
|
+
* Điều này giúp tránh việc làm mất các thuộc tính đã tồn tại.
|
|
673
675
|
*
|
|
674
|
-
* @template P
|
|
676
|
+
* @template P - Đường dẫn key hợp lệ trong object database.
|
|
675
677
|
*
|
|
676
|
-
* @param key Đường dẫn
|
|
677
|
-
*
|
|
678
|
+
* @param key - Đường dẫn tới giá trị cần cập nhật.
|
|
679
|
+
* Hỗ trợ nested path dạng `"a.b.c"`.
|
|
678
680
|
*
|
|
679
|
-
* @
|
|
681
|
+
* @param value - Giá trị mới sẽ được cập nhật tại key.
|
|
682
|
+
*
|
|
683
|
+
* @returns
|
|
684
|
+
* Trả về giá trị cuối cùng sau khi cập nhật.
|
|
680
685
|
*
|
|
681
686
|
* @example
|
|
687
|
+
* Database ban đầu:
|
|
688
|
+
* ```json
|
|
689
|
+
* {
|
|
690
|
+
* "123": {
|
|
691
|
+
* "guildID": "123",
|
|
692
|
+
* "guildName": "BlackCat",
|
|
693
|
+
* "history": []
|
|
694
|
+
* }
|
|
695
|
+
* }
|
|
696
|
+
* ```
|
|
697
|
+
*
|
|
698
|
+
* Cập nhật:
|
|
682
699
|
* ```ts
|
|
683
|
-
* await db.update("
|
|
700
|
+
* await db.update("123", {
|
|
701
|
+
* history: [1,2,3]
|
|
702
|
+
* });
|
|
703
|
+
* ```
|
|
704
|
+
*
|
|
705
|
+
* Kết quả:
|
|
706
|
+
* ```json
|
|
707
|
+
* {
|
|
708
|
+
* "123": {
|
|
709
|
+
* "guildID": "123",
|
|
710
|
+
* "guildName": "BlackCat",
|
|
711
|
+
* "history": [1,2,3]
|
|
712
|
+
* }
|
|
713
|
+
* }
|
|
684
714
|
* ```
|
|
715
|
+
*
|
|
716
|
+
* @throws {BlackCatError}
|
|
717
|
+
* - `REQUIRED_PARAMETER_MISSING` nếu key không được cung cấp
|
|
718
|
+
* - `INVALID_TYPE` nếu key không phải string
|
|
685
719
|
*/
|
|
686
720
|
update<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>;
|
|
687
721
|
/**
|
|
@@ -1278,6 +1312,53 @@ declare class SQLiteDriver implements DatabaseDriver {
|
|
|
1278
1312
|
delete(): Promise<boolean>;
|
|
1279
1313
|
}
|
|
1280
1314
|
|
|
1315
|
+
/**
|
|
1316
|
+
* Context được truyền vào callback `onLoad` của MongoDriver.
|
|
1317
|
+
* @hidden
|
|
1318
|
+
*/
|
|
1319
|
+
interface MongoDriverOnLoadContext {
|
|
1320
|
+
/**
|
|
1321
|
+
* Instance MongoClient đã kết nối.
|
|
1322
|
+
*/
|
|
1323
|
+
client: MongoClient;
|
|
1324
|
+
/**
|
|
1325
|
+
* Database hiện tại.
|
|
1326
|
+
*/
|
|
1327
|
+
db: Db;
|
|
1328
|
+
/**
|
|
1329
|
+
* Collection đang được sử dụng.
|
|
1330
|
+
*/
|
|
1331
|
+
collection: Collection<DatabaseDocument>;
|
|
1332
|
+
/**
|
|
1333
|
+
* Tên database.
|
|
1334
|
+
*/
|
|
1335
|
+
databaseName: string;
|
|
1336
|
+
/**
|
|
1337
|
+
* Tên collection.
|
|
1338
|
+
*/
|
|
1339
|
+
collectionName: string;
|
|
1340
|
+
}
|
|
1341
|
+
/**
|
|
1342
|
+
* Context được truyền vào callback `onError` của MongoDriver.
|
|
1343
|
+
* @hidden
|
|
1344
|
+
*/
|
|
1345
|
+
interface MongoDriverOnErrorContext {
|
|
1346
|
+
/**
|
|
1347
|
+
* Lỗi phát sinh.
|
|
1348
|
+
*
|
|
1349
|
+
* Có thể là bất kỳ kiểu nào (`unknown`),
|
|
1350
|
+
* nên cần type guard hoặc cast khi xử lý.
|
|
1351
|
+
*/
|
|
1352
|
+
error: unknown;
|
|
1353
|
+
/**
|
|
1354
|
+
* Tên database tại thời điểm xảy ra lỗi.
|
|
1355
|
+
*/
|
|
1356
|
+
databaseName: string;
|
|
1357
|
+
/**
|
|
1358
|
+
* Tên collection tại thời điểm xảy ra lỗi.
|
|
1359
|
+
*/
|
|
1360
|
+
collectionName: string;
|
|
1361
|
+
}
|
|
1281
1362
|
/**
|
|
1282
1363
|
* Cấu hình cho MongoDriver.
|
|
1283
1364
|
*/
|
|
@@ -1300,6 +1381,21 @@ interface MongoDriverOptions {
|
|
|
1300
1381
|
* @default "json_store"
|
|
1301
1382
|
*/
|
|
1302
1383
|
collectionName?: string;
|
|
1384
|
+
/**
|
|
1385
|
+
* Callback khi kết nối thành công.
|
|
1386
|
+
*/
|
|
1387
|
+
onLoad?: (ctx: MongoDriverOnLoadContext) => any;
|
|
1388
|
+
/**
|
|
1389
|
+
* Callback khi xảy ra lỗi.
|
|
1390
|
+
*/
|
|
1391
|
+
onError?: (ctx: MongoDriverOnErrorContext) => any;
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* Schema document dùng để lưu trữ database trong MongoDB.
|
|
1395
|
+
*/
|
|
1396
|
+
interface DatabaseDocument<V = any> {
|
|
1397
|
+
_id: string;
|
|
1398
|
+
data: V;
|
|
1303
1399
|
}
|
|
1304
1400
|
/**
|
|
1305
1401
|
* Driver lưu trữ dữ liệu bằng MongoDB.
|
|
@@ -1322,11 +1418,21 @@ interface MongoDriverOptions {
|
|
|
1322
1418
|
*
|
|
1323
1419
|
* @example
|
|
1324
1420
|
* ```ts
|
|
1421
|
+
* import { Database, MongoDriver } from "blackcat.js-database";
|
|
1422
|
+
*
|
|
1325
1423
|
* const database = new Database({
|
|
1326
1424
|
* driver: new MongoDriver({
|
|
1327
1425
|
* mongourl: "mongodb://localhost:27017",
|
|
1328
1426
|
* databaseName: "mydb",
|
|
1329
|
-
* collectionName: "store"
|
|
1427
|
+
* collectionName: "store",
|
|
1428
|
+
* onLoad: ({ db, collection, databaseName }) => {
|
|
1429
|
+
* console.log(`✅ Đã kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
1430
|
+
* console.log(`📦 Bộ sưu tập: ${collection.collectionName}`);
|
|
1431
|
+
* },
|
|
1432
|
+
* onError: ({ error, databaseName }) => {
|
|
1433
|
+
* console.error(`❌ Không thể kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
1434
|
+
* console.error(error);
|
|
1435
|
+
* }
|
|
1330
1436
|
* })
|
|
1331
1437
|
* });
|
|
1332
1438
|
* ```
|
|
@@ -1352,6 +1458,18 @@ declare class MongoDriver implements DatabaseDriver {
|
|
|
1352
1458
|
* Tên collection MongoDB.
|
|
1353
1459
|
*/
|
|
1354
1460
|
private collectionName;
|
|
1461
|
+
/**
|
|
1462
|
+
* Callback khi kết nối thành công.
|
|
1463
|
+
*/
|
|
1464
|
+
onLoad?: (ctx: MongoDriverOnLoadContext) => any;
|
|
1465
|
+
/**
|
|
1466
|
+
* Callback khi xảy ra lỗi.
|
|
1467
|
+
*/
|
|
1468
|
+
onError?: (ctx: MongoDriverOnErrorContext) => any;
|
|
1469
|
+
/**
|
|
1470
|
+
* Trạng thái đã gọi onLoad chưa (tránh spam)
|
|
1471
|
+
*/
|
|
1472
|
+
private isLoaded;
|
|
1355
1473
|
/**
|
|
1356
1474
|
* Khởi tạo MongoDriver.
|
|
1357
1475
|
*
|
|
@@ -1359,11 +1477,21 @@ declare class MongoDriver implements DatabaseDriver {
|
|
|
1359
1477
|
*
|
|
1360
1478
|
* @example
|
|
1361
1479
|
* ```ts
|
|
1480
|
+
* import { Database, MongoDriver } from "blackcat.js-database";
|
|
1481
|
+
*
|
|
1362
1482
|
* const database = new Database({
|
|
1363
1483
|
* driver: new MongoDriver({
|
|
1364
1484
|
* mongourl: "mongodb://localhost:27017",
|
|
1365
1485
|
* databaseName: "mydb",
|
|
1366
|
-
* collectionName: "store"
|
|
1486
|
+
* collectionName: "store",
|
|
1487
|
+
* onLoad: ({ db, collection, databaseName }) => {
|
|
1488
|
+
* console.log(`✅ Đã kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
1489
|
+
* console.log(`📦 Bộ sưu tập: ${collection.collectionName}`);
|
|
1490
|
+
* },
|
|
1491
|
+
* onError: ({ error, databaseName }) => {
|
|
1492
|
+
* console.error(`❌ Không thể kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
1493
|
+
* console.error(error);
|
|
1494
|
+
* }
|
|
1367
1495
|
* })
|
|
1368
1496
|
* });
|
|
1369
1497
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { MongoClient, Db, Collection } from 'mongodb';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Interface định nghĩa contract cho các storage driver
|
|
3
5
|
* được sử dụng bởi class `Database`.
|
|
@@ -369,7 +371,7 @@ type FirstObjectKey<TKey extends ObjectPath<string, any>> = TKey extends `${infe
|
|
|
369
371
|
* // "user.profile"
|
|
370
372
|
* // "user.profile.name"
|
|
371
373
|
*/
|
|
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]
|
|
374
|
+
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 ? NonNullable<T[TKey]> extends Array<any> ? TKey : NonNullable<T[TKey]> extends Record<string, any> ? `${TKey}` | `${TKey}.${ObjectPath<NonNullable<T[TKey]>>}` : TKey : never;
|
|
373
375
|
/**
|
|
374
376
|
* Trích xuất kiểu dữ liệu của giá trị tại một object path.
|
|
375
377
|
* @hidden
|
|
@@ -663,25 +665,57 @@ declare class Database<V = any> {
|
|
|
663
665
|
*/
|
|
664
666
|
deleteMany<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, query: Partial<ArrayElement<ObjectValue<V, P>>>): Promise<number>;
|
|
665
667
|
/**
|
|
666
|
-
* Cập nhật
|
|
668
|
+
* Cập nhật dữ liệu trong database theo đường dẫn key.
|
|
667
669
|
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
+
* Hàm này sẽ tìm đến vị trí của `key` trong database và gán `value` vào đó.
|
|
671
|
+
* Nếu `value` và giá trị hiện tại tại key đều là object thì sẽ thực hiện
|
|
672
|
+
* **shallow merge** thay vì ghi đè toàn bộ object.
|
|
670
673
|
*
|
|
671
|
-
*
|
|
672
|
-
* thì chúng sẽ được tự động tạo.
|
|
674
|
+
* Điều này giúp tránh việc làm mất các thuộc tính đã tồn tại.
|
|
673
675
|
*
|
|
674
|
-
* @template P
|
|
676
|
+
* @template P - Đường dẫn key hợp lệ trong object database.
|
|
675
677
|
*
|
|
676
|
-
* @param key Đường dẫn
|
|
677
|
-
*
|
|
678
|
+
* @param key - Đường dẫn tới giá trị cần cập nhật.
|
|
679
|
+
* Hỗ trợ nested path dạng `"a.b.c"`.
|
|
678
680
|
*
|
|
679
|
-
* @
|
|
681
|
+
* @param value - Giá trị mới sẽ được cập nhật tại key.
|
|
682
|
+
*
|
|
683
|
+
* @returns
|
|
684
|
+
* Trả về giá trị cuối cùng sau khi cập nhật.
|
|
680
685
|
*
|
|
681
686
|
* @example
|
|
687
|
+
* Database ban đầu:
|
|
688
|
+
* ```json
|
|
689
|
+
* {
|
|
690
|
+
* "123": {
|
|
691
|
+
* "guildID": "123",
|
|
692
|
+
* "guildName": "BlackCat",
|
|
693
|
+
* "history": []
|
|
694
|
+
* }
|
|
695
|
+
* }
|
|
696
|
+
* ```
|
|
697
|
+
*
|
|
698
|
+
* Cập nhật:
|
|
682
699
|
* ```ts
|
|
683
|
-
* await db.update("
|
|
700
|
+
* await db.update("123", {
|
|
701
|
+
* history: [1,2,3]
|
|
702
|
+
* });
|
|
703
|
+
* ```
|
|
704
|
+
*
|
|
705
|
+
* Kết quả:
|
|
706
|
+
* ```json
|
|
707
|
+
* {
|
|
708
|
+
* "123": {
|
|
709
|
+
* "guildID": "123",
|
|
710
|
+
* "guildName": "BlackCat",
|
|
711
|
+
* "history": [1,2,3]
|
|
712
|
+
* }
|
|
713
|
+
* }
|
|
684
714
|
* ```
|
|
715
|
+
*
|
|
716
|
+
* @throws {BlackCatError}
|
|
717
|
+
* - `REQUIRED_PARAMETER_MISSING` nếu key không được cung cấp
|
|
718
|
+
* - `INVALID_TYPE` nếu key không phải string
|
|
685
719
|
*/
|
|
686
720
|
update<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>;
|
|
687
721
|
/**
|
|
@@ -1278,6 +1312,53 @@ declare class SQLiteDriver implements DatabaseDriver {
|
|
|
1278
1312
|
delete(): Promise<boolean>;
|
|
1279
1313
|
}
|
|
1280
1314
|
|
|
1315
|
+
/**
|
|
1316
|
+
* Context được truyền vào callback `onLoad` của MongoDriver.
|
|
1317
|
+
* @hidden
|
|
1318
|
+
*/
|
|
1319
|
+
interface MongoDriverOnLoadContext {
|
|
1320
|
+
/**
|
|
1321
|
+
* Instance MongoClient đã kết nối.
|
|
1322
|
+
*/
|
|
1323
|
+
client: MongoClient;
|
|
1324
|
+
/**
|
|
1325
|
+
* Database hiện tại.
|
|
1326
|
+
*/
|
|
1327
|
+
db: Db;
|
|
1328
|
+
/**
|
|
1329
|
+
* Collection đang được sử dụng.
|
|
1330
|
+
*/
|
|
1331
|
+
collection: Collection<DatabaseDocument>;
|
|
1332
|
+
/**
|
|
1333
|
+
* Tên database.
|
|
1334
|
+
*/
|
|
1335
|
+
databaseName: string;
|
|
1336
|
+
/**
|
|
1337
|
+
* Tên collection.
|
|
1338
|
+
*/
|
|
1339
|
+
collectionName: string;
|
|
1340
|
+
}
|
|
1341
|
+
/**
|
|
1342
|
+
* Context được truyền vào callback `onError` của MongoDriver.
|
|
1343
|
+
* @hidden
|
|
1344
|
+
*/
|
|
1345
|
+
interface MongoDriverOnErrorContext {
|
|
1346
|
+
/**
|
|
1347
|
+
* Lỗi phát sinh.
|
|
1348
|
+
*
|
|
1349
|
+
* Có thể là bất kỳ kiểu nào (`unknown`),
|
|
1350
|
+
* nên cần type guard hoặc cast khi xử lý.
|
|
1351
|
+
*/
|
|
1352
|
+
error: unknown;
|
|
1353
|
+
/**
|
|
1354
|
+
* Tên database tại thời điểm xảy ra lỗi.
|
|
1355
|
+
*/
|
|
1356
|
+
databaseName: string;
|
|
1357
|
+
/**
|
|
1358
|
+
* Tên collection tại thời điểm xảy ra lỗi.
|
|
1359
|
+
*/
|
|
1360
|
+
collectionName: string;
|
|
1361
|
+
}
|
|
1281
1362
|
/**
|
|
1282
1363
|
* Cấu hình cho MongoDriver.
|
|
1283
1364
|
*/
|
|
@@ -1300,6 +1381,21 @@ interface MongoDriverOptions {
|
|
|
1300
1381
|
* @default "json_store"
|
|
1301
1382
|
*/
|
|
1302
1383
|
collectionName?: string;
|
|
1384
|
+
/**
|
|
1385
|
+
* Callback khi kết nối thành công.
|
|
1386
|
+
*/
|
|
1387
|
+
onLoad?: (ctx: MongoDriverOnLoadContext) => any;
|
|
1388
|
+
/**
|
|
1389
|
+
* Callback khi xảy ra lỗi.
|
|
1390
|
+
*/
|
|
1391
|
+
onError?: (ctx: MongoDriverOnErrorContext) => any;
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* Schema document dùng để lưu trữ database trong MongoDB.
|
|
1395
|
+
*/
|
|
1396
|
+
interface DatabaseDocument<V = any> {
|
|
1397
|
+
_id: string;
|
|
1398
|
+
data: V;
|
|
1303
1399
|
}
|
|
1304
1400
|
/**
|
|
1305
1401
|
* Driver lưu trữ dữ liệu bằng MongoDB.
|
|
@@ -1322,11 +1418,21 @@ interface MongoDriverOptions {
|
|
|
1322
1418
|
*
|
|
1323
1419
|
* @example
|
|
1324
1420
|
* ```ts
|
|
1421
|
+
* import { Database, MongoDriver } from "blackcat.js-database";
|
|
1422
|
+
*
|
|
1325
1423
|
* const database = new Database({
|
|
1326
1424
|
* driver: new MongoDriver({
|
|
1327
1425
|
* mongourl: "mongodb://localhost:27017",
|
|
1328
1426
|
* databaseName: "mydb",
|
|
1329
|
-
* collectionName: "store"
|
|
1427
|
+
* collectionName: "store",
|
|
1428
|
+
* onLoad: ({ db, collection, databaseName }) => {
|
|
1429
|
+
* console.log(`✅ Đã kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
1430
|
+
* console.log(`📦 Bộ sưu tập: ${collection.collectionName}`);
|
|
1431
|
+
* },
|
|
1432
|
+
* onError: ({ error, databaseName }) => {
|
|
1433
|
+
* console.error(`❌ Không thể kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
1434
|
+
* console.error(error);
|
|
1435
|
+
* }
|
|
1330
1436
|
* })
|
|
1331
1437
|
* });
|
|
1332
1438
|
* ```
|
|
@@ -1352,6 +1458,18 @@ declare class MongoDriver implements DatabaseDriver {
|
|
|
1352
1458
|
* Tên collection MongoDB.
|
|
1353
1459
|
*/
|
|
1354
1460
|
private collectionName;
|
|
1461
|
+
/**
|
|
1462
|
+
* Callback khi kết nối thành công.
|
|
1463
|
+
*/
|
|
1464
|
+
onLoad?: (ctx: MongoDriverOnLoadContext) => any;
|
|
1465
|
+
/**
|
|
1466
|
+
* Callback khi xảy ra lỗi.
|
|
1467
|
+
*/
|
|
1468
|
+
onError?: (ctx: MongoDriverOnErrorContext) => any;
|
|
1469
|
+
/**
|
|
1470
|
+
* Trạng thái đã gọi onLoad chưa (tránh spam)
|
|
1471
|
+
*/
|
|
1472
|
+
private isLoaded;
|
|
1355
1473
|
/**
|
|
1356
1474
|
* Khởi tạo MongoDriver.
|
|
1357
1475
|
*
|
|
@@ -1359,11 +1477,21 @@ declare class MongoDriver implements DatabaseDriver {
|
|
|
1359
1477
|
*
|
|
1360
1478
|
* @example
|
|
1361
1479
|
* ```ts
|
|
1480
|
+
* import { Database, MongoDriver } from "blackcat.js-database";
|
|
1481
|
+
*
|
|
1362
1482
|
* const database = new Database({
|
|
1363
1483
|
* driver: new MongoDriver({
|
|
1364
1484
|
* mongourl: "mongodb://localhost:27017",
|
|
1365
1485
|
* databaseName: "mydb",
|
|
1366
|
-
* collectionName: "store"
|
|
1486
|
+
* collectionName: "store",
|
|
1487
|
+
* onLoad: ({ db, collection, databaseName }) => {
|
|
1488
|
+
* console.log(`✅ Đã kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
1489
|
+
* console.log(`📦 Bộ sưu tập: ${collection.collectionName}`);
|
|
1490
|
+
* },
|
|
1491
|
+
* onError: ({ error, databaseName }) => {
|
|
1492
|
+
* console.error(`❌ Không thể kết nối với cơ sở dữ liệu: ${databaseName}`);
|
|
1493
|
+
* console.error(error);
|
|
1494
|
+
* }
|
|
1367
1495
|
* })
|
|
1368
1496
|
* });
|
|
1369
1497
|
*/
|