@gravito/dark-matter 1.0.0 → 1.1.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 +213 -5
- package/dist/index.cjs +665 -16
- package/dist/index.d.cts +462 -1
- package/dist/index.d.ts +462 -1
- package/dist/index.js +662 -15
- package/package.json +14 -4
package/dist/index.js
CHANGED
|
@@ -12,6 +12,7 @@ var MongoQueryBuilder = class _MongoQueryBuilder {
|
|
|
12
12
|
sortSpec = {};
|
|
13
13
|
limitCount;
|
|
14
14
|
skipCount;
|
|
15
|
+
softDeleteMode = "exclude";
|
|
15
16
|
// ============================================================================
|
|
16
17
|
// WHERE Clauses
|
|
17
18
|
// ============================================================================
|
|
@@ -607,6 +608,163 @@ var MongoQueryBuilder = class _MongoQueryBuilder {
|
|
|
607
608
|
acknowledged: result.acknowledged
|
|
608
609
|
};
|
|
609
610
|
}
|
|
611
|
+
// ============================================================================
|
|
612
|
+
// Soft Delete Methods
|
|
613
|
+
// ============================================================================
|
|
614
|
+
/**
|
|
615
|
+
* 包含已軟刪除的記錄
|
|
616
|
+
*
|
|
617
|
+
* 查詢時包含所有記錄,不過濾已刪除的文檔
|
|
618
|
+
*
|
|
619
|
+
* @returns 當前查詢建構器實例,支援鏈式調用
|
|
620
|
+
*
|
|
621
|
+
* @example
|
|
622
|
+
* ```typescript
|
|
623
|
+
* const allUsers = await query.withTrashed().get();
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
626
|
+
withTrashed() {
|
|
627
|
+
this.softDeleteMode = "include";
|
|
628
|
+
return this;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* 只查詢已軟刪除的記錄
|
|
632
|
+
*
|
|
633
|
+
* 只返回 deletedAt 不為 null 的文檔
|
|
634
|
+
*
|
|
635
|
+
* @returns 當前查詢建構器實例,支援鏈式調用
|
|
636
|
+
*
|
|
637
|
+
* @example
|
|
638
|
+
* ```typescript
|
|
639
|
+
* const trashedUsers = await query.onlyTrashed().get();
|
|
640
|
+
* ```
|
|
641
|
+
*/
|
|
642
|
+
onlyTrashed() {
|
|
643
|
+
this.softDeleteMode = "only";
|
|
644
|
+
return this;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* 軟刪除單一記錄
|
|
648
|
+
*
|
|
649
|
+
* 設置 deletedAt 為當前時間,而非真正刪除記錄
|
|
650
|
+
*
|
|
651
|
+
* @returns Promise 解析為更新結果
|
|
652
|
+
*
|
|
653
|
+
* @example
|
|
654
|
+
* ```typescript
|
|
655
|
+
* await query.where('_id', userId).softDelete();
|
|
656
|
+
* ```
|
|
657
|
+
*/
|
|
658
|
+
async softDelete() {
|
|
659
|
+
const updateDoc = { $set: { deletedAt: /* @__PURE__ */ new Date() } };
|
|
660
|
+
const result = await this.nativeCollection.updateOne(this.toFilter(), updateDoc, {
|
|
661
|
+
session: this.session
|
|
662
|
+
});
|
|
663
|
+
return {
|
|
664
|
+
matchedCount: result.matchedCount,
|
|
665
|
+
modifiedCount: result.modifiedCount,
|
|
666
|
+
acknowledged: result.acknowledged
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* 批次軟刪除
|
|
671
|
+
*
|
|
672
|
+
* 設置所有符合條件的文檔的 deletedAt 為當前時間
|
|
673
|
+
*
|
|
674
|
+
* @returns Promise 解析為更新結果
|
|
675
|
+
*
|
|
676
|
+
* @example
|
|
677
|
+
* ```typescript
|
|
678
|
+
* await query.where('status', 'inactive').softDeleteMany();
|
|
679
|
+
* ```
|
|
680
|
+
*/
|
|
681
|
+
async softDeleteMany() {
|
|
682
|
+
const updateDoc = { $set: { deletedAt: /* @__PURE__ */ new Date() } };
|
|
683
|
+
const result = await this.nativeCollection.updateMany(this.toFilter(), updateDoc, {
|
|
684
|
+
session: this.session
|
|
685
|
+
});
|
|
686
|
+
return {
|
|
687
|
+
matchedCount: result.matchedCount,
|
|
688
|
+
modifiedCount: result.modifiedCount,
|
|
689
|
+
acknowledged: result.acknowledged
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* 恢復軟刪除的記錄
|
|
694
|
+
*
|
|
695
|
+
* 將 deletedAt 設置為 null,恢復軟刪除的文檔
|
|
696
|
+
*
|
|
697
|
+
* @returns Promise 解析為更新結果
|
|
698
|
+
*
|
|
699
|
+
* @example
|
|
700
|
+
* ```typescript
|
|
701
|
+
* await query.where('_id', userId).restore();
|
|
702
|
+
* ```
|
|
703
|
+
*/
|
|
704
|
+
async restore() {
|
|
705
|
+
const updateDoc = { $set: { deletedAt: null } };
|
|
706
|
+
const result = await this.nativeCollection.updateOne(this.toFilter(), updateDoc, {
|
|
707
|
+
session: this.session
|
|
708
|
+
});
|
|
709
|
+
return {
|
|
710
|
+
matchedCount: result.matchedCount,
|
|
711
|
+
modifiedCount: result.modifiedCount,
|
|
712
|
+
acknowledged: result.acknowledged
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* 批次恢復軟刪除的記錄
|
|
717
|
+
*
|
|
718
|
+
* 將所有符合條件的文檔的 deletedAt 設置為 null
|
|
719
|
+
*
|
|
720
|
+
* @returns Promise 解析為更新結果
|
|
721
|
+
*
|
|
722
|
+
* @example
|
|
723
|
+
* ```typescript
|
|
724
|
+
* await query.onlyTrashed().restoreMany();
|
|
725
|
+
* ```
|
|
726
|
+
*/
|
|
727
|
+
async restoreMany() {
|
|
728
|
+
const updateDoc = { $set: { deletedAt: null } };
|
|
729
|
+
const result = await this.nativeCollection.updateMany(this.toFilter(), updateDoc, {
|
|
730
|
+
session: this.session
|
|
731
|
+
});
|
|
732
|
+
return {
|
|
733
|
+
matchedCount: result.matchedCount,
|
|
734
|
+
modifiedCount: result.modifiedCount,
|
|
735
|
+
acknowledged: result.acknowledged
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* 強制刪除(真正刪除記錄)
|
|
740
|
+
*
|
|
741
|
+
* 永久刪除單一文檔,無法恢復
|
|
742
|
+
*
|
|
743
|
+
* @returns Promise 解析為刪除結果
|
|
744
|
+
*
|
|
745
|
+
* @example
|
|
746
|
+
* ```typescript
|
|
747
|
+
* await query.where('_id', userId).forceDelete();
|
|
748
|
+
* ```
|
|
749
|
+
*/
|
|
750
|
+
async forceDelete() {
|
|
751
|
+
return await this.delete();
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* 批次強制刪除
|
|
755
|
+
*
|
|
756
|
+
* 永久刪除所有符合條件的文檔,無法恢復
|
|
757
|
+
*
|
|
758
|
+
* @returns Promise 解析為刪除結果
|
|
759
|
+
*
|
|
760
|
+
* @example
|
|
761
|
+
* ```typescript
|
|
762
|
+
* await query.where('createdAt', '<', oneYearAgo).forceDeleteMany();
|
|
763
|
+
* ```
|
|
764
|
+
*/
|
|
765
|
+
async forceDeleteMany() {
|
|
766
|
+
return await this.deleteMany();
|
|
767
|
+
}
|
|
610
768
|
/**
|
|
611
769
|
* Executes a bulk write operation.
|
|
612
770
|
*
|
|
@@ -715,17 +873,15 @@ var MongoQueryBuilder = class _MongoQueryBuilder {
|
|
|
715
873
|
toFilter() {
|
|
716
874
|
const hasMainFilters = Object.keys(this.filters).length > 0;
|
|
717
875
|
const hasOrFilters = this.orFilters.length > 0;
|
|
876
|
+
let baseFilter;
|
|
718
877
|
if (!hasOrFilters) {
|
|
719
|
-
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
};
|
|
878
|
+
baseFilter = { ...this.filters };
|
|
879
|
+
} else if (!hasMainFilters) {
|
|
880
|
+
baseFilter = { $or: this.orFilters };
|
|
881
|
+
} else {
|
|
882
|
+
baseFilter = { $or: [this.filters, ...this.orFilters] };
|
|
725
883
|
}
|
|
726
|
-
return
|
|
727
|
-
$or: [this.filters, ...this.orFilters]
|
|
728
|
-
};
|
|
884
|
+
return this.applySoftDeleteFilter(baseFilter);
|
|
729
885
|
}
|
|
730
886
|
/**
|
|
731
887
|
* Creates a deep copy of the query builder.
|
|
@@ -753,6 +909,7 @@ var MongoQueryBuilder = class _MongoQueryBuilder {
|
|
|
753
909
|
cloned.sortSpec = { ...this.sortSpec };
|
|
754
910
|
cloned.limitCount = this.limitCount;
|
|
755
911
|
cloned.skipCount = this.skipCount;
|
|
912
|
+
cloned.softDeleteMode = this.softDeleteMode;
|
|
756
913
|
return cloned;
|
|
757
914
|
}
|
|
758
915
|
// ============================================================================
|
|
@@ -791,6 +948,34 @@ var MongoQueryBuilder = class _MongoQueryBuilder {
|
|
|
791
948
|
}
|
|
792
949
|
return { $set: update };
|
|
793
950
|
}
|
|
951
|
+
/**
|
|
952
|
+
* 應用軟刪除過濾
|
|
953
|
+
*
|
|
954
|
+
* 根據 softDeleteMode 自動過濾已刪除的記錄
|
|
955
|
+
*
|
|
956
|
+
* @param filter - 基礎過濾條件
|
|
957
|
+
* @returns 包含軟刪除過濾的完整過濾條件
|
|
958
|
+
*/
|
|
959
|
+
applySoftDeleteFilter(filter) {
|
|
960
|
+
if (this.softDeleteMode === "include") {
|
|
961
|
+
return filter;
|
|
962
|
+
}
|
|
963
|
+
if (this.softDeleteMode === "only") {
|
|
964
|
+
return {
|
|
965
|
+
...filter,
|
|
966
|
+
deletedAt: { $ne: null }
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
const softDeleteFilter = {
|
|
970
|
+
$or: [{ deletedAt: null }, { deletedAt: { $exists: false } }]
|
|
971
|
+
};
|
|
972
|
+
if (Object.keys(filter).length === 0) {
|
|
973
|
+
return softDeleteFilter;
|
|
974
|
+
}
|
|
975
|
+
return {
|
|
976
|
+
$and: [filter, softDeleteFilter]
|
|
977
|
+
};
|
|
978
|
+
}
|
|
794
979
|
async getObjectId() {
|
|
795
980
|
if (_MongoQueryBuilder.ObjectIdCtor) {
|
|
796
981
|
return _MongoQueryBuilder.ObjectIdCtor;
|
|
@@ -1168,12 +1353,38 @@ var MongoDatabaseWrapper = class {
|
|
|
1168
1353
|
}
|
|
1169
1354
|
await this.db.createCollection(name, createOptions);
|
|
1170
1355
|
}
|
|
1171
|
-
async setValidation(collectionName,
|
|
1356
|
+
async setValidation(collectionName, schema2) {
|
|
1172
1357
|
await this.db.command({
|
|
1173
1358
|
collMod: collectionName,
|
|
1174
|
-
validator:
|
|
1175
|
-
validationLevel:
|
|
1176
|
-
validationAction:
|
|
1359
|
+
validator: schema2.validator,
|
|
1360
|
+
validationLevel: schema2.validationLevel ?? "strict",
|
|
1361
|
+
validationAction: schema2.validationAction ?? "error"
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* 使用 Schema Builder 建立 Collection
|
|
1366
|
+
*
|
|
1367
|
+
* 提供友善的 API 來建立帶有 Schema 驗證的 Collection
|
|
1368
|
+
*
|
|
1369
|
+
* @param name - Collection 名稱
|
|
1370
|
+
* @param schemaBuilder - Schema Builder 實例
|
|
1371
|
+
* @param options - 驗證選項(驗證等級、動作)
|
|
1372
|
+
*
|
|
1373
|
+
* @example
|
|
1374
|
+
* ```typescript
|
|
1375
|
+
* import { schema } from '@gravito/dark-matter'
|
|
1376
|
+
*
|
|
1377
|
+
* const userSchema = schema()
|
|
1378
|
+
* .required('name', 'email')
|
|
1379
|
+
* .string('name')
|
|
1380
|
+
* .string('email')
|
|
1381
|
+
*
|
|
1382
|
+
* await Mongo.database().createCollectionWithSchema('users', userSchema)
|
|
1383
|
+
* ```
|
|
1384
|
+
*/
|
|
1385
|
+
async createCollectionWithSchema(name, schemaBuilder, options) {
|
|
1386
|
+
await this.createCollection(name, {
|
|
1387
|
+
schema: schemaBuilder.toValidationOptions(options)
|
|
1177
1388
|
});
|
|
1178
1389
|
}
|
|
1179
1390
|
};
|
|
@@ -1483,7 +1694,9 @@ var MongoGridFS = class {
|
|
|
1483
1694
|
const reader = source.getReader();
|
|
1484
1695
|
while (true) {
|
|
1485
1696
|
const { done, value } = await reader.read();
|
|
1486
|
-
if (done)
|
|
1697
|
+
if (done) {
|
|
1698
|
+
break;
|
|
1699
|
+
}
|
|
1487
1700
|
uploadStream.write(value);
|
|
1488
1701
|
}
|
|
1489
1702
|
uploadStream.end();
|
|
@@ -1552,6 +1765,190 @@ var MongoGridFS = class {
|
|
|
1552
1765
|
const cursor = this.bucket.find(filter ?? {});
|
|
1553
1766
|
return await cursor.toArray();
|
|
1554
1767
|
}
|
|
1768
|
+
/**
|
|
1769
|
+
* 串流上傳檔案
|
|
1770
|
+
*
|
|
1771
|
+
* 支援進度回調的串流上傳,適合大檔案上傳
|
|
1772
|
+
*
|
|
1773
|
+
* @param stream - ReadableStream 來源
|
|
1774
|
+
* @param options - 上傳選項
|
|
1775
|
+
* @param onProgress - 可選的進度回調函數
|
|
1776
|
+
* @returns Promise 解析為檔案 ID
|
|
1777
|
+
*
|
|
1778
|
+
* @example
|
|
1779
|
+
* ```typescript
|
|
1780
|
+
* const stream = file.stream()
|
|
1781
|
+
* const fileId = await grid.uploadStream(stream, {
|
|
1782
|
+
* filename: 'video.mp4'
|
|
1783
|
+
* }, (progress) => {
|
|
1784
|
+
* console.log(`上傳進度: ${progress.percentage}%`)
|
|
1785
|
+
* })
|
|
1786
|
+
* ```
|
|
1787
|
+
*/
|
|
1788
|
+
async uploadStream(stream, options, onProgress) {
|
|
1789
|
+
await this.ensureBucket();
|
|
1790
|
+
const uploadStream = this.bucket.openUploadStream(options.filename, {
|
|
1791
|
+
chunkSizeBytes: options.chunkSizeBytes,
|
|
1792
|
+
metadata: options.metadata,
|
|
1793
|
+
contentType: options.contentType
|
|
1794
|
+
});
|
|
1795
|
+
let bytesWritten = 0;
|
|
1796
|
+
const reader = stream.getReader();
|
|
1797
|
+
try {
|
|
1798
|
+
while (true) {
|
|
1799
|
+
const { done, value } = await reader.read();
|
|
1800
|
+
if (done) {
|
|
1801
|
+
break;
|
|
1802
|
+
}
|
|
1803
|
+
uploadStream.write(Buffer.from(value));
|
|
1804
|
+
bytesWritten += value.length;
|
|
1805
|
+
if (onProgress) {
|
|
1806
|
+
onProgress({
|
|
1807
|
+
bytesWritten,
|
|
1808
|
+
totalBytes: 0,
|
|
1809
|
+
// 串流無法預知總大小
|
|
1810
|
+
percentage: 0
|
|
1811
|
+
});
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
uploadStream.end();
|
|
1815
|
+
return new Promise((resolve, reject) => {
|
|
1816
|
+
uploadStream.on("finish", () => resolve(uploadStream.id.toString()));
|
|
1817
|
+
uploadStream.on("error", reject);
|
|
1818
|
+
});
|
|
1819
|
+
} catch (error) {
|
|
1820
|
+
uploadStream.abort();
|
|
1821
|
+
throw error;
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
/**
|
|
1825
|
+
* 串流下載檔案
|
|
1826
|
+
*
|
|
1827
|
+
* 返回 ReadableStream 以支援大檔案的串流下載
|
|
1828
|
+
*
|
|
1829
|
+
* @param fileId - 檔案 ID
|
|
1830
|
+
* @returns ReadableStream 提供檔案內容
|
|
1831
|
+
*
|
|
1832
|
+
* @example
|
|
1833
|
+
* ```typescript
|
|
1834
|
+
* const stream = grid.downloadStream(fileId)
|
|
1835
|
+
* const reader = stream.getReader()
|
|
1836
|
+
*
|
|
1837
|
+
* while (true) {
|
|
1838
|
+
* const { done, value } = await reader.read()
|
|
1839
|
+
* if (done) break
|
|
1840
|
+
* // 處理 chunk
|
|
1841
|
+
* }
|
|
1842
|
+
* ```
|
|
1843
|
+
*/
|
|
1844
|
+
downloadStream(fileId) {
|
|
1845
|
+
return new ReadableStream({
|
|
1846
|
+
start: async (controller) => {
|
|
1847
|
+
try {
|
|
1848
|
+
await this.ensureBucket();
|
|
1849
|
+
const { ObjectId } = await import("mongodb");
|
|
1850
|
+
const downloadStream = this.bucket.openDownloadStream(new ObjectId(fileId));
|
|
1851
|
+
downloadStream.on("data", (chunk) => {
|
|
1852
|
+
controller.enqueue(new Uint8Array(chunk));
|
|
1853
|
+
});
|
|
1854
|
+
downloadStream.on("end", () => {
|
|
1855
|
+
controller.close();
|
|
1856
|
+
});
|
|
1857
|
+
downloadStream.on("error", (error) => {
|
|
1858
|
+
controller.error(error);
|
|
1859
|
+
});
|
|
1860
|
+
} catch (error) {
|
|
1861
|
+
controller.error(error);
|
|
1862
|
+
}
|
|
1863
|
+
},
|
|
1864
|
+
cancel: async () => {
|
|
1865
|
+
}
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
/**
|
|
1869
|
+
* 分片上傳大檔案
|
|
1870
|
+
*
|
|
1871
|
+
* 支援進度追蹤的大檔案上傳,自動處理分片
|
|
1872
|
+
*
|
|
1873
|
+
* @param file - Blob 或 File 物件
|
|
1874
|
+
* @param options - 上傳選項
|
|
1875
|
+
* @param onProgress - 可選的進度回調函數
|
|
1876
|
+
* @returns Promise 解析為檔案 ID
|
|
1877
|
+
*
|
|
1878
|
+
* @example
|
|
1879
|
+
* ```typescript
|
|
1880
|
+
* const fileId = await grid.uploadLargeFile(file, {
|
|
1881
|
+
* filename: 'large-video.mp4',
|
|
1882
|
+
* chunkSizeBytes: 255 * 1024 // 255 KB
|
|
1883
|
+
* }, (progress) => {
|
|
1884
|
+
* console.log(`進度: ${progress.percentage}%`)
|
|
1885
|
+
* console.log(`已上傳: ${progress.bytesWritten} / ${progress.totalBytes}`)
|
|
1886
|
+
* })
|
|
1887
|
+
* ```
|
|
1888
|
+
*/
|
|
1889
|
+
async uploadLargeFile(file, options, onProgress) {
|
|
1890
|
+
await this.ensureBucket();
|
|
1891
|
+
const chunkSize = options.chunkSizeBytes ?? 255 * 1024;
|
|
1892
|
+
const totalBytes = file.size;
|
|
1893
|
+
let bytesWritten = 0;
|
|
1894
|
+
const uploadStream = this.bucket.openUploadStream(options.filename, {
|
|
1895
|
+
chunkSizeBytes: chunkSize,
|
|
1896
|
+
metadata: { ...options.metadata, totalSize: totalBytes },
|
|
1897
|
+
contentType: options.contentType
|
|
1898
|
+
});
|
|
1899
|
+
const stream = file.stream();
|
|
1900
|
+
const reader = stream.getReader();
|
|
1901
|
+
try {
|
|
1902
|
+
while (true) {
|
|
1903
|
+
const { done, value } = await reader.read();
|
|
1904
|
+
if (done) {
|
|
1905
|
+
break;
|
|
1906
|
+
}
|
|
1907
|
+
uploadStream.write(Buffer.from(value));
|
|
1908
|
+
bytesWritten += value.length;
|
|
1909
|
+
if (onProgress) {
|
|
1910
|
+
onProgress({
|
|
1911
|
+
bytesWritten,
|
|
1912
|
+
totalBytes,
|
|
1913
|
+
percentage: Math.round(bytesWritten / totalBytes * 100)
|
|
1914
|
+
});
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
uploadStream.end();
|
|
1918
|
+
return new Promise((resolve, reject) => {
|
|
1919
|
+
uploadStream.on("finish", () => resolve(uploadStream.id.toString()));
|
|
1920
|
+
uploadStream.on("error", reject);
|
|
1921
|
+
});
|
|
1922
|
+
} catch (error) {
|
|
1923
|
+
uploadStream.abort();
|
|
1924
|
+
throw error;
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
/**
|
|
1928
|
+
* 取得檔案中繼資料
|
|
1929
|
+
*
|
|
1930
|
+
* 查詢檔案資訊但不下載內容
|
|
1931
|
+
*
|
|
1932
|
+
* @param fileId - 檔案 ID
|
|
1933
|
+
* @returns Promise 解析為檔案中繼資料,找不到時返回 null
|
|
1934
|
+
*
|
|
1935
|
+
* @example
|
|
1936
|
+
* ```typescript
|
|
1937
|
+
* const fileInfo = await grid.findById(fileId)
|
|
1938
|
+
* if (fileInfo) {
|
|
1939
|
+
* console.log(`檔案名稱: ${fileInfo.filename}`)
|
|
1940
|
+
* console.log(`檔案大小: ${fileInfo.length} bytes`)
|
|
1941
|
+
* console.log(`上傳日期: ${fileInfo.uploadDate}`)
|
|
1942
|
+
* }
|
|
1943
|
+
* ```
|
|
1944
|
+
*/
|
|
1945
|
+
async findById(fileId) {
|
|
1946
|
+
await this.ensureBucket();
|
|
1947
|
+
const { ObjectId } = await import("mongodb");
|
|
1948
|
+
const cursor = this.bucket.find({ _id: new ObjectId(fileId) });
|
|
1949
|
+
const files = await cursor.toArray();
|
|
1950
|
+
return files.length > 0 ? files[0] : null;
|
|
1951
|
+
}
|
|
1555
1952
|
async ensureBucket() {
|
|
1556
1953
|
if (!this.bucket) {
|
|
1557
1954
|
throw new Error("GridFS bucket not initialized. Please wait a moment after creation.");
|
|
@@ -1614,6 +2011,254 @@ var MongoPoolMonitor = class {
|
|
|
1614
2011
|
};
|
|
1615
2012
|
}
|
|
1616
2013
|
};
|
|
2014
|
+
|
|
2015
|
+
// src/MongoSchemaBuilder.ts
|
|
2016
|
+
var MongoSchemaBuilder = class _MongoSchemaBuilder {
|
|
2017
|
+
schema = {
|
|
2018
|
+
bsonType: "object",
|
|
2019
|
+
required: [],
|
|
2020
|
+
properties: {}
|
|
2021
|
+
};
|
|
2022
|
+
/**
|
|
2023
|
+
* 定義必填欄位
|
|
2024
|
+
*
|
|
2025
|
+
* @param fields - 必填欄位名稱列表
|
|
2026
|
+
* @returns 當前 Schema Builder 實例
|
|
2027
|
+
*
|
|
2028
|
+
* @example
|
|
2029
|
+
* ```typescript
|
|
2030
|
+
* schema().required('name', 'email', 'age')
|
|
2031
|
+
* ```
|
|
2032
|
+
*/
|
|
2033
|
+
required(...fields) {
|
|
2034
|
+
this.schema.required = [...this.schema.required, ...fields];
|
|
2035
|
+
return this;
|
|
2036
|
+
}
|
|
2037
|
+
/**
|
|
2038
|
+
* 定義字串欄位
|
|
2039
|
+
*
|
|
2040
|
+
* @param field - 欄位名稱
|
|
2041
|
+
* @param options - 字串選項(長度、模式、枚舉等)
|
|
2042
|
+
* @returns 當前 Schema Builder 實例
|
|
2043
|
+
*
|
|
2044
|
+
* @example
|
|
2045
|
+
* ```typescript
|
|
2046
|
+
* schema()
|
|
2047
|
+
* .string('username', { minLength: 3, maxLength: 50 })
|
|
2048
|
+
* .string('email', { pattern: '^.+@.+$' })
|
|
2049
|
+
* .string('status', { enum: ['active', 'inactive'] })
|
|
2050
|
+
* ```
|
|
2051
|
+
*/
|
|
2052
|
+
string(field, options) {
|
|
2053
|
+
const property = { bsonType: "string" };
|
|
2054
|
+
if (options?.minLength !== void 0) {
|
|
2055
|
+
property.minLength = options.minLength;
|
|
2056
|
+
}
|
|
2057
|
+
if (options?.maxLength !== void 0) {
|
|
2058
|
+
property.maxLength = options.maxLength;
|
|
2059
|
+
}
|
|
2060
|
+
if (options?.pattern) {
|
|
2061
|
+
property.pattern = options.pattern;
|
|
2062
|
+
}
|
|
2063
|
+
if (options?.enum) {
|
|
2064
|
+
property.enum = options.enum;
|
|
2065
|
+
}
|
|
2066
|
+
;
|
|
2067
|
+
this.schema.properties[field] = property;
|
|
2068
|
+
return this;
|
|
2069
|
+
}
|
|
2070
|
+
/**
|
|
2071
|
+
* 定義數字欄位
|
|
2072
|
+
*
|
|
2073
|
+
* @param field - 欄位名稱
|
|
2074
|
+
* @param options - 數字選項(最小值、最大值等)
|
|
2075
|
+
* @returns 當前 Schema Builder 實例
|
|
2076
|
+
*
|
|
2077
|
+
* @example
|
|
2078
|
+
* ```typescript
|
|
2079
|
+
* schema()
|
|
2080
|
+
* .number('price', { minimum: 0, maximum: 10000 })
|
|
2081
|
+
* .number('discount', { minimum: 0, maximum: 1, exclusiveMaximum: true })
|
|
2082
|
+
* ```
|
|
2083
|
+
*/
|
|
2084
|
+
number(field, options) {
|
|
2085
|
+
const property = { bsonType: "number" };
|
|
2086
|
+
if (options?.minimum !== void 0) {
|
|
2087
|
+
property.minimum = options.minimum;
|
|
2088
|
+
}
|
|
2089
|
+
if (options?.maximum !== void 0) {
|
|
2090
|
+
property.maximum = options.maximum;
|
|
2091
|
+
}
|
|
2092
|
+
if (options?.exclusiveMinimum) {
|
|
2093
|
+
property.exclusiveMinimum = true;
|
|
2094
|
+
}
|
|
2095
|
+
if (options?.exclusiveMaximum) {
|
|
2096
|
+
property.exclusiveMaximum = true;
|
|
2097
|
+
}
|
|
2098
|
+
;
|
|
2099
|
+
this.schema.properties[field] = property;
|
|
2100
|
+
return this;
|
|
2101
|
+
}
|
|
2102
|
+
/**
|
|
2103
|
+
* 定義整數欄位
|
|
2104
|
+
*
|
|
2105
|
+
* @param field - 欄位名稱
|
|
2106
|
+
* @param options - 整數選項(最小值、最大值)
|
|
2107
|
+
* @returns 當前 Schema Builder 實例
|
|
2108
|
+
*
|
|
2109
|
+
* @example
|
|
2110
|
+
* ```typescript
|
|
2111
|
+
* schema()
|
|
2112
|
+
* .integer('age', { minimum: 0, maximum: 150 })
|
|
2113
|
+
* .integer('count')
|
|
2114
|
+
* ```
|
|
2115
|
+
*/
|
|
2116
|
+
integer(field, options) {
|
|
2117
|
+
const property = { bsonType: "int" };
|
|
2118
|
+
if (options?.minimum !== void 0) {
|
|
2119
|
+
property.minimum = options.minimum;
|
|
2120
|
+
}
|
|
2121
|
+
if (options?.maximum !== void 0) {
|
|
2122
|
+
property.maximum = options.maximum;
|
|
2123
|
+
}
|
|
2124
|
+
;
|
|
2125
|
+
this.schema.properties[field] = property;
|
|
2126
|
+
return this;
|
|
2127
|
+
}
|
|
2128
|
+
/**
|
|
2129
|
+
* 定義布林欄位
|
|
2130
|
+
*
|
|
2131
|
+
* @param field - 欄位名稱
|
|
2132
|
+
* @returns 當前 Schema Builder 實例
|
|
2133
|
+
*
|
|
2134
|
+
* @example
|
|
2135
|
+
* ```typescript
|
|
2136
|
+
* schema().boolean('isActive').boolean('verified')
|
|
2137
|
+
* ```
|
|
2138
|
+
*/
|
|
2139
|
+
boolean(field) {
|
|
2140
|
+
;
|
|
2141
|
+
this.schema.properties[field] = {
|
|
2142
|
+
bsonType: "bool"
|
|
2143
|
+
};
|
|
2144
|
+
return this;
|
|
2145
|
+
}
|
|
2146
|
+
/**
|
|
2147
|
+
* 定義日期欄位
|
|
2148
|
+
*
|
|
2149
|
+
* @param field - 欄位名稱
|
|
2150
|
+
* @returns 當前 Schema Builder 實例
|
|
2151
|
+
*
|
|
2152
|
+
* @example
|
|
2153
|
+
* ```typescript
|
|
2154
|
+
* schema().date('createdAt').date('updatedAt')
|
|
2155
|
+
* ```
|
|
2156
|
+
*/
|
|
2157
|
+
date(field) {
|
|
2158
|
+
;
|
|
2159
|
+
this.schema.properties[field] = {
|
|
2160
|
+
bsonType: "date"
|
|
2161
|
+
};
|
|
2162
|
+
return this;
|
|
2163
|
+
}
|
|
2164
|
+
/**
|
|
2165
|
+
* 定義陣列欄位
|
|
2166
|
+
*
|
|
2167
|
+
* @param field - 欄位名稱
|
|
2168
|
+
* @param itemType - 陣列元素類型
|
|
2169
|
+
* @param options - 陣列選項(長度、唯一性等)
|
|
2170
|
+
* @returns 當前 Schema Builder 實例
|
|
2171
|
+
*
|
|
2172
|
+
* @example
|
|
2173
|
+
* ```typescript
|
|
2174
|
+
* schema()
|
|
2175
|
+
* .array('tags', 'string')
|
|
2176
|
+
* .array('scores', 'number')
|
|
2177
|
+
* .array('roles', 'string', { minItems: 1, uniqueItems: true })
|
|
2178
|
+
* ```
|
|
2179
|
+
*/
|
|
2180
|
+
array(field, itemType, options) {
|
|
2181
|
+
const property = {
|
|
2182
|
+
bsonType: "array",
|
|
2183
|
+
items: { bsonType: itemType }
|
|
2184
|
+
};
|
|
2185
|
+
if (options?.minItems !== void 0) {
|
|
2186
|
+
property.minItems = options.minItems;
|
|
2187
|
+
}
|
|
2188
|
+
if (options?.maxItems !== void 0) {
|
|
2189
|
+
property.maxItems = options.maxItems;
|
|
2190
|
+
}
|
|
2191
|
+
if (options?.uniqueItems) {
|
|
2192
|
+
property.uniqueItems = true;
|
|
2193
|
+
}
|
|
2194
|
+
;
|
|
2195
|
+
this.schema.properties[field] = property;
|
|
2196
|
+
return this;
|
|
2197
|
+
}
|
|
2198
|
+
/**
|
|
2199
|
+
* 定義物件欄位
|
|
2200
|
+
*
|
|
2201
|
+
* @param field - 欄位名稱
|
|
2202
|
+
* @param callback - 巢狀 Schema 建構函數
|
|
2203
|
+
* @returns 當前 Schema Builder 實例
|
|
2204
|
+
*
|
|
2205
|
+
* @example
|
|
2206
|
+
* ```typescript
|
|
2207
|
+
* schema().object('profile', (s) =>
|
|
2208
|
+
* s
|
|
2209
|
+
* .string('bio', { maxLength: 500 })
|
|
2210
|
+
* .string('avatar')
|
|
2211
|
+
* .integer('followers')
|
|
2212
|
+
* )
|
|
2213
|
+
* ```
|
|
2214
|
+
*/
|
|
2215
|
+
object(field, callback) {
|
|
2216
|
+
const nestedBuilder = new _MongoSchemaBuilder();
|
|
2217
|
+
callback(nestedBuilder);
|
|
2218
|
+
this.schema.properties[field] = nestedBuilder.build();
|
|
2219
|
+
return this;
|
|
2220
|
+
}
|
|
2221
|
+
/**
|
|
2222
|
+
* 建構最終的 JSON Schema
|
|
2223
|
+
*
|
|
2224
|
+
* @returns JSON Schema 物件
|
|
2225
|
+
*
|
|
2226
|
+
* @example
|
|
2227
|
+
* ```typescript
|
|
2228
|
+
* const schema = schema()
|
|
2229
|
+
* .required('name')
|
|
2230
|
+
* .string('name')
|
|
2231
|
+
* .build()
|
|
2232
|
+
* ```
|
|
2233
|
+
*/
|
|
2234
|
+
build() {
|
|
2235
|
+
return this.schema;
|
|
2236
|
+
}
|
|
2237
|
+
/**
|
|
2238
|
+
* 轉換為 MongoDB 驗證選項
|
|
2239
|
+
*
|
|
2240
|
+
* @param options - 驗證選項(驗證等級、動作)
|
|
2241
|
+
* @returns MongoDB Schema 驗證選項
|
|
2242
|
+
*
|
|
2243
|
+
* @example
|
|
2244
|
+
* ```typescript
|
|
2245
|
+
* const options = schema()
|
|
2246
|
+
* .required('name')
|
|
2247
|
+
* .string('name')
|
|
2248
|
+
* .toValidationOptions({ validationLevel: 'moderate' })
|
|
2249
|
+
* ```
|
|
2250
|
+
*/
|
|
2251
|
+
toValidationOptions(options) {
|
|
2252
|
+
return {
|
|
2253
|
+
validator: { $jsonSchema: this.schema },
|
|
2254
|
+
validationLevel: options?.validationLevel ?? "strict",
|
|
2255
|
+
validationAction: options?.validationAction ?? "error"
|
|
2256
|
+
};
|
|
2257
|
+
}
|
|
2258
|
+
};
|
|
2259
|
+
function schema() {
|
|
2260
|
+
return new MongoSchemaBuilder();
|
|
2261
|
+
}
|
|
1617
2262
|
export {
|
|
1618
2263
|
Mongo,
|
|
1619
2264
|
MongoAggregateBuilder,
|
|
@@ -1621,5 +2266,7 @@ export {
|
|
|
1621
2266
|
MongoGridFS,
|
|
1622
2267
|
MongoManager,
|
|
1623
2268
|
MongoPoolMonitor,
|
|
1624
|
-
MongoQueryBuilder
|
|
2269
|
+
MongoQueryBuilder,
|
|
2270
|
+
MongoSchemaBuilder,
|
|
2271
|
+
schema
|
|
1625
2272
|
};
|