@tsed/mongoose 7.0.0-alpha.1 → 7.0.0-beta.10
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/jest.config.js +14 -0
- package/lib/cjs/MongooseModule.js +36 -0
- package/lib/cjs/MongooseModule.js.map +1 -0
- package/lib/cjs/constants/constants.js +24 -0
- package/lib/cjs/constants/constants.js.map +1 -0
- package/lib/cjs/decorators/auto.js +28 -0
- package/lib/cjs/decorators/auto.js.map +1 -0
- package/lib/cjs/decorators/discriminatorKey.js +14 -0
- package/lib/cjs/decorators/discriminatorKey.js.map +1 -0
- package/lib/cjs/decorators/dynamicRef.js +65 -0
- package/lib/cjs/decorators/dynamicRef.js.map +1 -0
- package/lib/cjs/decorators/excludeIndexes.js +28 -0
- package/lib/cjs/decorators/excludeIndexes.js.map +1 -0
- package/lib/cjs/decorators/expires.js +28 -0
- package/lib/cjs/decorators/expires.js.map +1 -0
- package/lib/cjs/decorators/immutable.js +30 -0
- package/lib/cjs/decorators/immutable.js.map +1 -0
- package/lib/cjs/decorators/indexed.js +26 -0
- package/lib/cjs/decorators/indexed.js.map +1 -0
- package/lib/cjs/decorators/lowercase.js +28 -0
- package/lib/cjs/decorators/lowercase.js.map +1 -0
- package/lib/cjs/decorators/model.js +68 -0
- package/lib/cjs/decorators/model.js.map +1 -0
- package/lib/cjs/decorators/mongooseIndex.js +39 -0
- package/lib/cjs/decorators/mongooseIndex.js.map +1 -0
- package/lib/cjs/decorators/mongooseIndexes.js +39 -0
- package/lib/cjs/decorators/mongooseIndexes.js.map +1 -0
- package/lib/cjs/decorators/mongoosePlugin.js +22 -0
- package/lib/cjs/decorators/mongoosePlugin.js.map +1 -0
- package/lib/cjs/decorators/numberDecimal.js +78 -0
- package/lib/cjs/decorators/numberDecimal.js.map +1 -0
- package/lib/cjs/decorators/objectID.js +26 -0
- package/lib/cjs/decorators/objectID.js.map +1 -0
- package/lib/cjs/decorators/postHook.js +26 -0
- package/lib/cjs/decorators/postHook.js.map +1 -0
- package/lib/cjs/decorators/preHook.js +71 -0
- package/lib/cjs/decorators/preHook.js.map +1 -0
- package/lib/cjs/decorators/ref.js +75 -0
- package/lib/cjs/decorators/ref.js.map +1 -0
- package/lib/cjs/decorators/schema.js +32 -0
- package/lib/cjs/decorators/schema.js.map +1 -0
- package/lib/cjs/decorators/schemaIgnore.js +27 -0
- package/lib/cjs/decorators/schemaIgnore.js.map +1 -0
- package/lib/cjs/decorators/select.js +28 -0
- package/lib/cjs/decorators/select.js.map +1 -0
- package/lib/cjs/decorators/sparse.js +30 -0
- package/lib/cjs/decorators/sparse.js.map +1 -0
- package/lib/cjs/decorators/text.js +29 -0
- package/lib/cjs/decorators/text.js.map +1 -0
- package/lib/cjs/decorators/trim.js +27 -0
- package/lib/cjs/decorators/trim.js.map +1 -0
- package/lib/cjs/decorators/unique.js +27 -0
- package/lib/cjs/decorators/unique.js.map +1 -0
- package/lib/cjs/decorators/uppercase.js +28 -0
- package/lib/cjs/decorators/uppercase.js.map +1 -0
- package/lib/cjs/decorators/versionKey.js +12 -0
- package/lib/cjs/decorators/versionKey.js.map +1 -0
- package/lib/cjs/decorators/virtualRef.js +38 -0
- package/lib/cjs/decorators/virtualRef.js.map +1 -0
- package/lib/cjs/index.js +51 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/interfaces/MongooseConnectionOptions.js +3 -0
- package/lib/cjs/interfaces/MongooseConnectionOptions.js.map +1 -0
- package/lib/cjs/interfaces/MongooseDocument.js +3 -0
- package/lib/cjs/interfaces/MongooseDocument.js.map +1 -0
- package/lib/cjs/interfaces/MongooseModel.js +3 -0
- package/lib/cjs/interfaces/MongooseModel.js.map +1 -0
- package/lib/cjs/interfaces/MongooseModelOptions.js +3 -0
- package/lib/cjs/interfaces/MongooseModelOptions.js.map +1 -0
- package/lib/cjs/interfaces/MongooseSchemaOptions.js +3 -0
- package/lib/cjs/interfaces/MongooseSchemaOptions.js.map +1 -0
- package/lib/cjs/interfaces/MongooseSchemaTypes.js +10 -0
- package/lib/cjs/interfaces/MongooseSchemaTypes.js.map +1 -0
- package/lib/cjs/interfaces/MongooseVirtualRefOptions.js +3 -0
- package/lib/cjs/interfaces/MongooseVirtualRefOptions.js.map +1 -0
- package/lib/cjs/interfaces/interfaces.js +3 -0
- package/lib/cjs/interfaces/interfaces.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/registries/MongooseModels.js +5 -0
- package/lib/cjs/registries/MongooseModels.js.map +1 -0
- package/lib/cjs/services/MongooseConnections.js +47 -0
- package/lib/cjs/services/MongooseConnections.js.map +1 -0
- package/lib/cjs/services/MongooseService.js +73 -0
- package/lib/cjs/services/MongooseService.js.map +1 -0
- package/lib/cjs/utils/createModel.js +54 -0
- package/lib/cjs/utils/createModel.js.map +1 -0
- package/lib/cjs/utils/createSchema.js +160 -0
- package/lib/cjs/utils/createSchema.js.map +1 -0
- package/lib/cjs/utils/resolveRefType.js +19 -0
- package/lib/cjs/utils/resolveRefType.js.map +1 -0
- package/lib/cjs/utils/schemaOptions.js +60 -0
- package/lib/cjs/utils/schemaOptions.js.map +1 -0
- package/lib/esm/MongooseModule.js +33 -0
- package/lib/esm/MongooseModule.js.map +1 -0
- package/lib/esm/constants/constants.js +21 -0
- package/lib/esm/constants/constants.js.map +1 -0
- package/lib/esm/decorators/auto.js +24 -0
- package/lib/esm/decorators/auto.js.map +1 -0
- package/lib/esm/decorators/discriminatorKey.js +10 -0
- package/lib/esm/decorators/discriminatorKey.js.map +1 -0
- package/lib/esm/decorators/dynamicRef.js +61 -0
- package/lib/esm/decorators/dynamicRef.js.map +1 -0
- package/lib/esm/decorators/excludeIndexes.js +24 -0
- package/lib/esm/decorators/excludeIndexes.js.map +1 -0
- package/lib/esm/decorators/expires.js +24 -0
- package/lib/esm/decorators/expires.js.map +1 -0
- package/lib/esm/decorators/immutable.js +26 -0
- package/lib/esm/decorators/immutable.js.map +1 -0
- package/lib/esm/decorators/indexed.js +22 -0
- package/lib/esm/decorators/indexed.js.map +1 -0
- package/lib/esm/decorators/lowercase.js +24 -0
- package/lib/esm/decorators/lowercase.js.map +1 -0
- package/lib/esm/decorators/model.js +64 -0
- package/lib/esm/decorators/model.js.map +1 -0
- package/lib/esm/decorators/mongooseIndex.js +35 -0
- package/lib/esm/decorators/mongooseIndex.js.map +1 -0
- package/lib/esm/decorators/mongooseIndexes.js +35 -0
- package/lib/esm/decorators/mongooseIndexes.js.map +1 -0
- package/lib/esm/decorators/mongoosePlugin.js +18 -0
- package/lib/esm/decorators/mongoosePlugin.js.map +1 -0
- package/lib/esm/decorators/numberDecimal.js +74 -0
- package/lib/esm/decorators/numberDecimal.js.map +1 -0
- package/lib/esm/decorators/objectID.js +22 -0
- package/lib/esm/decorators/objectID.js.map +1 -0
- package/lib/esm/decorators/postHook.js +22 -0
- package/lib/esm/decorators/postHook.js.map +1 -0
- package/lib/esm/decorators/preHook.js +67 -0
- package/lib/esm/decorators/preHook.js.map +1 -0
- package/lib/esm/decorators/ref.js +71 -0
- package/lib/esm/decorators/ref.js.map +1 -0
- package/lib/esm/decorators/schema.js +27 -0
- package/lib/esm/decorators/schema.js.map +1 -0
- package/lib/esm/decorators/schemaIgnore.js +23 -0
- package/lib/esm/decorators/schemaIgnore.js.map +1 -0
- package/lib/esm/decorators/select.js +24 -0
- package/lib/esm/decorators/select.js.map +1 -0
- package/lib/esm/decorators/sparse.js +26 -0
- package/lib/esm/decorators/sparse.js.map +1 -0
- package/lib/esm/decorators/text.js +25 -0
- package/lib/esm/decorators/text.js.map +1 -0
- package/lib/esm/decorators/trim.js +23 -0
- package/lib/esm/decorators/trim.js.map +1 -0
- package/lib/esm/decorators/unique.js +23 -0
- package/lib/esm/decorators/unique.js.map +1 -0
- package/lib/esm/decorators/uppercase.js +24 -0
- package/lib/esm/decorators/uppercase.js.map +1 -0
- package/lib/esm/decorators/versionKey.js +8 -0
- package/lib/esm/decorators/versionKey.js.map +1 -0
- package/lib/esm/decorators/virtualRef.js +34 -0
- package/lib/esm/decorators/virtualRef.js.map +1 -0
- package/lib/esm/index.js +48 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/MongooseConnectionOptions.js +2 -0
- package/lib/esm/interfaces/MongooseConnectionOptions.js.map +1 -0
- package/lib/esm/interfaces/MongooseDocument.js +2 -0
- package/lib/esm/interfaces/MongooseDocument.js.map +1 -0
- package/lib/esm/interfaces/MongooseModel.js +2 -0
- package/lib/esm/interfaces/MongooseModel.js.map +1 -0
- package/lib/esm/interfaces/MongooseModelOptions.js +2 -0
- package/lib/esm/interfaces/MongooseModelOptions.js.map +1 -0
- package/lib/esm/interfaces/MongooseSchemaOptions.js +2 -0
- package/lib/esm/interfaces/MongooseSchemaOptions.js.map +1 -0
- package/lib/esm/interfaces/MongooseSchemaTypes.js +7 -0
- package/lib/esm/interfaces/MongooseSchemaTypes.js.map +1 -0
- package/lib/esm/interfaces/MongooseVirtualRefOptions.js +2 -0
- package/lib/esm/interfaces/MongooseVirtualRefOptions.js.map +1 -0
- package/lib/esm/interfaces/interfaces.js +2 -0
- package/lib/esm/interfaces/interfaces.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/registries/MongooseModels.js +2 -0
- package/lib/esm/registries/MongooseModels.js.map +1 -0
- package/lib/esm/services/MongooseConnections.js +44 -0
- package/lib/esm/services/MongooseConnections.js.map +1 -0
- package/lib/esm/services/MongooseService.js +70 -0
- package/lib/esm/services/MongooseService.js.map +1 -0
- package/lib/esm/utils/createModel.js +48 -0
- package/lib/esm/utils/createModel.js.map +1 -0
- package/lib/esm/utils/createSchema.js +151 -0
- package/lib/esm/utils/createSchema.js.map +1 -0
- package/lib/esm/utils/resolveRefType.js +15 -0
- package/lib/esm/utils/resolveRefType.js.map +1 -0
- package/lib/esm/utils/schemaOptions.js +54 -0
- package/lib/esm/utils/schemaOptions.js.map +1 -0
- package/lib/{MongooseModule.d.ts → types/MongooseModule.d.ts} +0 -0
- package/lib/{constants/index.d.ts → types/constants/constants.d.ts} +0 -0
- package/lib/{decorators → types/decorators}/auto.d.ts +0 -0
- package/lib/{decorators → types/decorators}/discriminatorKey.d.ts +0 -0
- package/lib/{decorators → types/decorators}/dynamicRef.d.ts +4 -2
- package/lib/{decorators → types/decorators}/excludeIndexes.d.ts +0 -0
- package/lib/{decorators → types/decorators}/expires.d.ts +0 -0
- package/lib/{decorators → types/decorators}/immutable.d.ts +0 -0
- package/lib/{decorators → types/decorators}/indexed.d.ts +0 -0
- package/lib/{decorators → types/decorators}/lowercase.d.ts +0 -0
- package/lib/{decorators → types/decorators}/model.d.ts +0 -0
- package/lib/{decorators → types/decorators}/mongooseIndex.d.ts +0 -0
- package/lib/types/decorators/mongooseIndexes.d.ts +32 -0
- package/lib/{decorators → types/decorators}/mongoosePlugin.d.ts +0 -0
- package/lib/{decorators → types/decorators}/numberDecimal.d.ts +0 -0
- package/lib/{decorators → types/decorators}/objectID.d.ts +0 -0
- package/lib/{decorators → types/decorators}/postHook.d.ts +1 -1
- package/lib/{decorators → types/decorators}/preHook.d.ts +1 -1
- package/lib/{decorators → types/decorators}/ref.d.ts +7 -2
- package/lib/{decorators → types/decorators}/schema.d.ts +1 -1
- package/lib/{decorators → types/decorators}/schemaIgnore.d.ts +0 -0
- package/lib/{decorators → types/decorators}/select.d.ts +0 -0
- package/lib/{decorators → types/decorators}/sparse.d.ts +0 -0
- package/lib/{decorators → types/decorators}/text.d.ts +0 -0
- package/lib/{decorators → types/decorators}/trim.d.ts +0 -0
- package/lib/{decorators → types/decorators}/unique.d.ts +0 -0
- package/lib/{decorators → types/decorators}/uppercase.d.ts +0 -0
- package/lib/{decorators → types/decorators}/versionKey.d.ts +0 -0
- package/lib/{decorators → types/decorators}/virtualRef.d.ts +0 -4
- package/lib/types/index.d.ts +47 -0
- package/lib/{interfaces → types/interfaces}/MongooseConnectionOptions.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/MongooseDocument.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/MongooseModel.d.ts +1 -2
- package/lib/{interfaces → types/interfaces}/MongooseModelOptions.d.ts +1 -1
- package/lib/{interfaces → types/interfaces}/MongooseSchemaOptions.d.ts +1 -1
- package/lib/{interfaces → types/interfaces}/MongooseSchemaTypes.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/MongooseVirtualRefOptions.d.ts +1 -0
- package/lib/{interfaces/index.d.ts → types/interfaces/interfaces.d.ts} +0 -6
- package/lib/{registries → types/registries}/MongooseModels.d.ts +0 -0
- package/lib/{services → types/services}/MongooseConnections.d.ts +0 -0
- package/lib/{services → types/services}/MongooseService.d.ts +0 -0
- package/lib/{utils → types/utils}/createModel.d.ts +3 -2
- package/lib/{utils → types/utils}/createSchema.d.ts +1 -1
- package/lib/{utils → types/utils}/resolveRefType.d.ts +0 -0
- package/lib/{utils → types/utils}/schemaOptions.d.ts +1 -1
- package/package.json +29 -23
- package/readme.md +124 -134
- package/tsconfig.compile.esm.json +13 -0
- package/.mocharc.js +0 -5
- package/.nycrc +0 -27
- package/lib/decorators/index.d.ts +0 -26
- package/lib/index.d.ts +0 -6
- package/lib/index.js +0 -1402
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -1362
- package/lib/index.modern.js.map +0 -1
- package/lib/utils/index.d.ts +0 -2
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { Formats } from "@tsed/ajv";
|
|
3
|
+
import { isNumber, isString, StoreMerge, useDecorators } from "@tsed/core";
|
|
4
|
+
import { OnDeserialize, OnSerialize } from "@tsed/json-mapper";
|
|
5
|
+
import { Example, Format, Property } from "@tsed/schema";
|
|
6
|
+
import { Types, Schema as MongooseSchema } from "mongoose";
|
|
7
|
+
import { MONGOOSE_SCHEMA } from "../constants/constants.js";
|
|
8
|
+
let DecimalFormat = class DecimalFormat {
|
|
9
|
+
validate(num) {
|
|
10
|
+
return isString(num) || isNumber(num);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
DecimalFormat = __decorate([
|
|
14
|
+
Formats("decimal", { type: "number" })
|
|
15
|
+
], DecimalFormat);
|
|
16
|
+
export { DecimalFormat };
|
|
17
|
+
function isDecimal(value) {
|
|
18
|
+
return value && value._bsontype === "Decimal128";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Tell Mongoose whether to define an Decimal128 property.
|
|
22
|
+
* Will be serialized as `number` with format as `decimal`.
|
|
23
|
+
* ### Example
|
|
24
|
+
*
|
|
25
|
+
* ```typescript
|
|
26
|
+
* @Model()
|
|
27
|
+
* export class PriceModel {
|
|
28
|
+
* @NumberDecimal()
|
|
29
|
+
* price: Decimal128;
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
* Optionally using custom decimal type, such as `Big` from big.js
|
|
33
|
+
* ```typescript
|
|
34
|
+
* @Model()
|
|
35
|
+
* export class PriceModel {
|
|
36
|
+
* @NumberDecimal(Big)
|
|
37
|
+
* price: Big;
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
* @param type Optional decimal type constructor
|
|
41
|
+
* @decorator
|
|
42
|
+
* @mongoose
|
|
43
|
+
* @schema
|
|
44
|
+
*/
|
|
45
|
+
export function NumberDecimal(type) {
|
|
46
|
+
const schema = {
|
|
47
|
+
type: MongooseSchema.Types.Decimal128
|
|
48
|
+
};
|
|
49
|
+
if (type) {
|
|
50
|
+
// Define property getter to convert Decimal128 to custom type
|
|
51
|
+
schema.get = (value) => {
|
|
52
|
+
return isDecimal(value) ? new type(value) : value;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return useDecorators(Property(Number), Format("decimal"), Example(12.34), StoreMerge(MONGOOSE_SCHEMA, schema),
|
|
56
|
+
// Deserialize number value from JSON to Decimal128
|
|
57
|
+
OnDeserialize((value) => {
|
|
58
|
+
if (value === undefined) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (type) {
|
|
62
|
+
return new type(value);
|
|
63
|
+
}
|
|
64
|
+
if (isString(value)) {
|
|
65
|
+
return Types.Decimal128.fromString(value);
|
|
66
|
+
}
|
|
67
|
+
return Types.Decimal128.fromString(`${value}`);
|
|
68
|
+
}),
|
|
69
|
+
// Serialize decimal value to floating point number
|
|
70
|
+
OnSerialize((value, ctx) => {
|
|
71
|
+
return value && Number(value);
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=numberDecimal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numberDecimal.js","sourceRoot":"","sources":["../../../src/decorators/numberDecimal.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,OAAO,EAAiB,MAAM,WAAW,CAAC;AAClD,OAAO,EAAC,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACzE,OAAO,EAAC,aAAa,EAAE,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;AACvD,OAAO,EAAC,KAAK,EAAE,MAAM,IAAI,cAAc,EAAoB,MAAM,UAAU,CAAC;AAC5E,OAAO,EAAC,eAAe,EAAC,MAAM,wBAAwB,CAAC;AAGvD,IAAa,aAAa,GAA1B,MAAa,aAAa;IACxB,QAAQ,CAAC,GAAoB;QAC3B,OAAO,QAAQ,CAAC,GAAG,CAAC,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC;IACxC,CAAC;CACF,CAAA;AAJY,aAAa;IADzB,OAAO,CAAC,SAAS,EAAE,EAAC,IAAI,EAAE,QAAQ,EAAC,CAAC;GACxB,aAAa,CAIzB;SAJY,aAAa;AAM1B,SAAS,SAAS,CAAC,KAA+B;IAChD,OAAO,KAAK,IAAI,KAAK,CAAC,SAAS,KAAK,YAAY,CAAC;AACnD,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,aAAa,CAAC,IAAU;IACtC,MAAM,MAAM,GAAkC;QAC5C,IAAI,EAAE,cAAc,CAAC,KAAK,CAAC,UAAU;KACtC,CAAC;IAEF,IAAI,IAAI,EAAE;QACR,8DAA8D;QAC9D,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,EAAE,EAAE;YACrB,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QACpD,CAAC,CAAC;KACH;IAED,OAAO,aAAa,CAClB,QAAQ,CAAC,MAAM,CAAC,EAChB,MAAM,CAAC,SAAS,CAAC,EACjB,OAAO,CAAC,KAAK,CAAC,EACd,UAAU,CAAC,eAAe,EAAE,MAAM,CAAC;IAEnC,mDAAmD;IACnD,aAAa,CAAC,CAAC,KAAK,EAAE,EAAE;QACtB,IAAI,KAAK,KAAK,SAAS,EAAE;YACvB,OAAO,SAAS,CAAC;SAClB;QACD,IAAI,IAAI,EAAE;YACR,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;SACxB;QACD,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE;YACnB,OAAO,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;SAC3C;QACD,OAAO,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,KAAK,EAAE,CAAC,CAAC;IACjD,CAAC,CAAC;IAEF,mDAAmD;IACnD,WAAW,CAAC,CAAC,KAAU,EAAE,GAAG,EAAE,EAAE;QAC9B,OAAO,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC,CACH,CAAC;AACJ,CAAC","sourcesContent":["import {Formats, FormatsMethods} from \"@tsed/ajv\";\nimport {isNumber, isString, StoreMerge, useDecorators} from \"@tsed/core\";\nimport {OnDeserialize, OnSerialize} from \"@tsed/json-mapper\";\nimport {Example, Format, Property} from \"@tsed/schema\";\nimport {Types, Schema as MongooseSchema, SchemaTypeOptions} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants/constants\";\n\n@Formats(\"decimal\", {type: \"number\"})\nexport class DecimalFormat implements FormatsMethods<string | number> {\n validate(num: string | number): boolean {\n return isString(num) || isNumber(num);\n }\n}\n\nfunction isDecimal(value: undefined | number | any) {\n return value && value._bsontype === \"Decimal128\";\n}\n\n/**\n * Tell Mongoose whether to define an Decimal128 property.\n * Will be serialized as `number` with format as `decimal`.\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class PriceModel {\n * @NumberDecimal()\n * price: Decimal128;\n * }\n * ```\n * Optionally using custom decimal type, such as `Big` from big.js\n * ```typescript\n * @Model()\n * export class PriceModel {\n * @NumberDecimal(Big)\n * price: Big;\n * }\n * ```\n * @param type Optional decimal type constructor\n * @decorator\n * @mongoose\n * @schema\n */\nexport function NumberDecimal(type?: any) {\n const schema: SchemaTypeOptions<Decimal128> = {\n type: MongooseSchema.Types.Decimal128\n };\n\n if (type) {\n // Define property getter to convert Decimal128 to custom type\n schema.get = (value) => {\n return isDecimal(value) ? new type(value) : value;\n };\n }\n\n return useDecorators(\n Property(Number),\n Format(\"decimal\"),\n Example(12.34),\n StoreMerge(MONGOOSE_SCHEMA, schema),\n\n // Deserialize number value from JSON to Decimal128\n OnDeserialize((value) => {\n if (value === undefined) {\n return undefined;\n }\n if (type) {\n return new type(value);\n }\n if (isString(value)) {\n return Types.Decimal128.fromString(value);\n }\n return Types.Decimal128.fromString(`${value}`);\n }),\n\n // Serialize decimal value to floating point number\n OnSerialize((value: any, ctx) => {\n return value && Number(value);\n })\n );\n}\n\nexport type Decimal128 = Types.Decimal128;\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useDecorators } from "@tsed/core";
|
|
2
|
+
import { Description, Example, Name, Pattern } from "@tsed/schema";
|
|
3
|
+
/**
|
|
4
|
+
* Tell Mongoose whether to define an ObjectId property.
|
|
5
|
+
* ### Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @Model()
|
|
9
|
+
* export class EventModel {
|
|
10
|
+
* @ObjectId('id')
|
|
11
|
+
* _id: string;
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
* @param name
|
|
15
|
+
* @decorator
|
|
16
|
+
* @mongoose
|
|
17
|
+
* @schema
|
|
18
|
+
*/
|
|
19
|
+
export function ObjectID(name) {
|
|
20
|
+
return useDecorators(name && Name(name), Pattern(/^[0-9a-fA-F]{24}$/), Description("Mongoose ObjectId"), Example("5ce7ad3028890bd71749d477"));
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=objectID.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objectID.js","sourceRoot":"","sources":["../../../src/decorators/objectID.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,YAAY,CAAC;AACzC,OAAO,EAAC,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAC,MAAM,cAAc,CAAC;AAGjE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAa;IACpC,OAAO,aAAa,CAClB,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,EAClB,OAAO,CAAC,mBAAmB,CAAC,EAC5B,WAAW,CAAC,mBAAmB,CAAC,EAChC,OAAO,CAAC,0BAA0B,CAAC,CACpC,CAAC;AACJ,CAAC","sourcesContent":["import {useDecorators} from \"@tsed/core\";\nimport {Description, Example, Name, Pattern} from \"@tsed/schema\";\nimport {Types} from \"mongoose\";\n\n/**\n * Tell Mongoose whether to define an ObjectId property.\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @ObjectId('id')\n * _id: string;\n * }\n * ```\n * @param name\n * @decorator\n * @mongoose\n * @schema\n */\nexport function ObjectID(name?: string) {\n return useDecorators(\n name && Name(name),\n Pattern(/^[0-9a-fA-F]{24}$/),\n Description(\"Mongoose ObjectId\"),\n Example(\"5ce7ad3028890bd71749d477\")\n );\n}\n\nexport type ObjectID = Types.ObjectId;\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { decoratorTypeOf, DecoratorTypes } from "@tsed/core";
|
|
2
|
+
import { schemaOptions } from "../utils/schemaOptions.js";
|
|
3
|
+
export function PostHook(method, ...params) {
|
|
4
|
+
return ((...args) => {
|
|
5
|
+
let options = params[1];
|
|
6
|
+
let fn = params[0];
|
|
7
|
+
if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {
|
|
8
|
+
options = params[0];
|
|
9
|
+
fn = args[0][args[1]].bind(args[0]);
|
|
10
|
+
}
|
|
11
|
+
schemaOptions(args[0], {
|
|
12
|
+
post: [
|
|
13
|
+
{
|
|
14
|
+
method,
|
|
15
|
+
fn,
|
|
16
|
+
options
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=postHook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postHook.js","sourceRoot":"","sources":["../../../src/decorators/postHook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,cAAc,EAAwB,MAAM,YAAY,CAAC;AAElF,OAAO,EAAC,aAAa,EAAC,MAAM,wBAAwB,CAAC;AAgDrD,MAAM,UAAU,QAAQ,CAAU,MAAc,EAAE,GAAG,MAAa;IAChE,OAAO,CAAC,CAAC,GAAG,IAAW,EAAE,EAAE;QACzB,IAAI,OAAO,GAAwB,MAAM,CAAC,CAAC,CAAC,CAAC;QAC7C,IAAI,EAAE,GAA0B,MAAM,CAAC,CAAC,CAAC,CAAC;QAE1C,IAAI,eAAe,CAAC,IAAI,CAAC,KAAK,cAAc,CAAC,UAAU,EAAE;YACvD,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;YACpB,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;SACrC;QAED,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;YACrB,IAAI,EAAE;gBACJ;oBACE,MAAM;oBACN,EAAE;oBACF,OAAO;iBACR;aACF;SACF,CAAC,CAAC;IACL,CAAC,CAAQ,CAAC;AACZ,CAAC","sourcesContent":["import {decoratorTypeOf, DecoratorTypes, StaticMethodDecorator} from \"@tsed/core\";\nimport {MongooseHookOptions, MongoosePostHookCB} from \"../interfaces/MongooseSchemaOptions\";\nimport {schemaOptions} from \"../utils/schemaOptions\";\n\n/**\n * We can simply attach a `@PostHook` decorator to your model class and\n * define the hook function like you normally would in Mongoose.\n *\n * ```typescript\n * import {Ignore, Required} from \"@tsed/common\";\n * import {PostHook, Model} from \"@tsed/mongoose\";\n *\n * @Model()\n * @PostHook(\"save\", (car: CarModel) => {\n * if (car.topSpeedInKmH > 300) {\n * console.log(car.model, 'is fast!');\n * }\n * })\n * export class CarModel {\n * @Ignore()\n * _id: string;\n *\n * @Required()\n * model: string;\n *\n * @Required()\n * isFast: boolean;\n *\n * // or Prehook on static method\n * @PostHook(\"save\")\n * static postSave(car: CarModel) {\n * if (car.topSpeedInKmH > 300) {\n * console.log(car.model, 'is fast!');\n * }\n * }\n * }\n * ```\n *\n * This will execute the post-save hook each time a `CarModel` document is saved.\n *\n * @param {string} method\n * @param fn\n * @returns {Function}\n * @decorator\n * @mongoose\n * @class\n */\nexport function PostHook<T = any>(method: string, fn: MongoosePostHookCB<T>): ClassDecorator;\nexport function PostHook<T = any>(method: string, fn: MongoosePostHookCB<T>, options: MongooseHookOptions): ClassDecorator;\nexport function PostHook<T = any>(method: string, options: MongooseHookOptions): StaticMethodDecorator;\nexport function PostHook<T = any>(method: string, ...params: any[]): Function {\n return ((...args: any[]) => {\n let options: MongooseHookOptions = params[1];\n let fn: MongoosePostHookCB<T> = params[0];\n\n if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {\n options = params[0];\n fn = args[0][args[1]].bind(args[0]);\n }\n\n schemaOptions(args[0], {\n post: [\n {\n method,\n fn,\n options\n }\n ]\n });\n }) as any;\n}\n"]}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { decoratorTypeOf, DecoratorTypes } from "@tsed/core";
|
|
2
|
+
import { schemaOptions } from "../utils/schemaOptions.js";
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* We can simply attach a `@PreHook` decorator to your model class and
|
|
6
|
+
* define the hook function like you normally would in Mongoose.
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import {Ignore, Required} from "@tsed/common";
|
|
10
|
+
* import {PreHook, Model} from "@tsed/mongoose";
|
|
11
|
+
*
|
|
12
|
+
* @Model()
|
|
13
|
+
* @PreHook("save", (car: CarModel, next) => {
|
|
14
|
+
* if (car.model === 'Tesla') {
|
|
15
|
+
* car.isFast = true;
|
|
16
|
+
* }
|
|
17
|
+
* next();
|
|
18
|
+
*})
|
|
19
|
+
* export class CarModel {
|
|
20
|
+
*
|
|
21
|
+
* @Ignore()
|
|
22
|
+
* _id: string;
|
|
23
|
+
*
|
|
24
|
+
* @Required()
|
|
25
|
+
* model: string;
|
|
26
|
+
*
|
|
27
|
+
* @Required()
|
|
28
|
+
* isFast: boolean;
|
|
29
|
+
*
|
|
30
|
+
* // or Prehook on static method
|
|
31
|
+
* @PreHook("save")
|
|
32
|
+
* static preSave(car: CarModel, next) {
|
|
33
|
+
* if (car.model === 'Tesla') {
|
|
34
|
+
* car.isFast = true;
|
|
35
|
+
* }
|
|
36
|
+
* next();
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* This will execute the pre-save hook each time a `CarModel` document is saved.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} method
|
|
44
|
+
* @param fn
|
|
45
|
+
* @param options
|
|
46
|
+
* @returns {Function}
|
|
47
|
+
* @decorator
|
|
48
|
+
* @class
|
|
49
|
+
*/
|
|
50
|
+
export function PreHook(method, fn, options) {
|
|
51
|
+
return (...args) => {
|
|
52
|
+
if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {
|
|
53
|
+
options = fn;
|
|
54
|
+
fn = args[0][args[1]].bind(args[0]);
|
|
55
|
+
}
|
|
56
|
+
schemaOptions(args[0], {
|
|
57
|
+
pre: [
|
|
58
|
+
{
|
|
59
|
+
method,
|
|
60
|
+
fn: fn,
|
|
61
|
+
options
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=preHook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preHook.js","sourceRoot":"","sources":["../../../src/decorators/preHook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,cAAc,EAAC,MAAM,YAAY,CAAC;AAE3D,OAAO,EAAC,aAAa,EAAC,MAAM,wBAAwB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,MAAM,UAAU,OAAO,CAAU,MAAc,EAAE,EAA+C,EAAE,OAA6B;IAC7H,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE;QACxB,IAAI,eAAe,CAAC,IAAI,CAAC,KAAK,cAAc,CAAC,UAAU,EAAE;YACvD,OAAO,GAAG,EAAyB,CAAC;YACpC,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;SACrC;QAED,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;YACrB,GAAG,EAAE;gBACH;oBACE,MAAM;oBACN,EAAE,EAAE,EAA0B;oBAC9B,OAAO;iBACR;aACF;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {decoratorTypeOf, DecoratorTypes} from \"@tsed/core\";\nimport {MongooseHookOptions, MongoosePreHookCB} from \"../interfaces/MongooseSchemaOptions\";\nimport {schemaOptions} from \"../utils/schemaOptions\";\n\n/**\n *\n * We can simply attach a `@PreHook` decorator to your model class and\n * define the hook function like you normally would in Mongoose.\n *\n * ```typescript\n * import {Ignore, Required} from \"@tsed/common\";\n * import {PreHook, Model} from \"@tsed/mongoose\";\n *\n * @Model()\n * @PreHook(\"save\", (car: CarModel, next) => {\n * if (car.model === 'Tesla') {\n * car.isFast = true;\n * }\n * next();\n *})\n * export class CarModel {\n *\n * @Ignore()\n * _id: string;\n *\n * @Required()\n * model: string;\n *\n * @Required()\n * isFast: boolean;\n *\n * // or Prehook on static method\n * @PreHook(\"save\")\n * static preSave(car: CarModel, next) {\n * if (car.model === 'Tesla') {\n * car.isFast = true;\n * }\n * next();\n * }\n * }\n * ```\n *\n * This will execute the pre-save hook each time a `CarModel` document is saved.\n *\n * @param {string} method\n * @param fn\n * @param options\n * @returns {Function}\n * @decorator\n * @class\n */\nexport function PreHook<T = any>(method: string, fn?: MongoosePreHookCB<T> | MongooseHookOptions, options?: MongooseHookOptions): Function {\n return (...args: any[]) => {\n if (decoratorTypeOf(args) === DecoratorTypes.METHOD_STC) {\n options = fn as MongooseHookOptions;\n fn = args[0][args[1]].bind(args[0]);\n }\n\n schemaOptions(args[0], {\n pre: [\n {\n method,\n fn: fn as MongoosePreHookCB<T>,\n options\n }\n ]\n });\n };\n}\n"]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { isArrowFn, isObject, isString, StoreMerge, useDecorators } from "@tsed/core";
|
|
2
|
+
import { OnDeserialize, OnSerialize, serialize } from "@tsed/json-mapper";
|
|
3
|
+
import { ForwardGroups, JsonEntityFn, lazyRef, matchGroups, OneOf, Property, string } from "@tsed/schema";
|
|
4
|
+
import { Schema as MongooseSchema } from "mongoose";
|
|
5
|
+
import { MONGOOSE_SCHEMA } from "../constants/constants.js";
|
|
6
|
+
import { MongooseSchemaTypes } from "../interfaces/MongooseSchemaTypes.js";
|
|
7
|
+
import { MongooseModels } from "../registries/MongooseModels.js";
|
|
8
|
+
function isRef(value) {
|
|
9
|
+
return (value && value._bsontype) || isString(value);
|
|
10
|
+
}
|
|
11
|
+
function PopulateGroups(populatedGroups) {
|
|
12
|
+
return useDecorators(ForwardGroups(true), JsonEntityFn((store) => {
|
|
13
|
+
store.schema.$hooks.on("oneOf", (obj, givenGroups) => {
|
|
14
|
+
if (matchGroups(populatedGroups, givenGroups)) {
|
|
15
|
+
return obj.filter((x) => x.type === "string"); // keep the object id;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return obj.filter((x) => x.type !== "string"); // keep the ref definition
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Define a property as mongoose reference to other Model (decorated with @Model).
|
|
25
|
+
*
|
|
26
|
+
* ### Example
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
*
|
|
30
|
+
* @Model()
|
|
31
|
+
* class FooModel {
|
|
32
|
+
*
|
|
33
|
+
* @Ref(Foo2Model)
|
|
34
|
+
* field: Ref<Foo2Model>
|
|
35
|
+
*
|
|
36
|
+
* @Ref(Foo2Model)
|
|
37
|
+
* list: Ref<Foo2Model>[]
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* @Model()
|
|
41
|
+
* class Foo2Model {
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @param model
|
|
46
|
+
* @param options
|
|
47
|
+
* @returns {Function}
|
|
48
|
+
* @decorator
|
|
49
|
+
* @mongoose
|
|
50
|
+
* @property
|
|
51
|
+
*/
|
|
52
|
+
export function Ref(model, options = MongooseSchemaTypes.OBJECT_ID) {
|
|
53
|
+
const getType = () => (isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model);
|
|
54
|
+
const populatedGroups = (isObject(options) && options.populatedGroups) || [];
|
|
55
|
+
return useDecorators(Property(Object), StoreMerge(MONGOOSE_SCHEMA, {
|
|
56
|
+
type: MongooseSchema.Types[isObject(options) ? options.type || MongooseSchemaTypes.OBJECT_ID : options],
|
|
57
|
+
ref: model
|
|
58
|
+
}), OnDeserialize((value) => {
|
|
59
|
+
if (isRef(value)) {
|
|
60
|
+
return value.toString();
|
|
61
|
+
}
|
|
62
|
+
return value;
|
|
63
|
+
}), OnSerialize((value, ctx) => {
|
|
64
|
+
if (isRef(value)) {
|
|
65
|
+
return value.toString();
|
|
66
|
+
}
|
|
67
|
+
const type = getType();
|
|
68
|
+
return serialize(value, { ...ctx, type });
|
|
69
|
+
}), OneOf(string().example("5ce7ad3028890bd71749d477").description("Mongoose Ref ObjectId"), lazyRef(getType)), populatedGroups.length && PopulateGroups(populatedGroups));
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=ref.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ref.js","sourceRoot":"","sources":["../../../src/decorators/ref.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAQ,aAAa,EAAC,MAAM,YAAY,CAAC;AAC1F,OAAO,EAAC,aAAa,EAAE,WAAW,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AACxE,OAAO,EAAC,aAAa,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAC,MAAM,cAAc,CAAC;AACxG,OAAO,EAAC,MAAM,IAAI,cAAc,EAAC,MAAM,UAAU,CAAC;AAClD,OAAO,EAAC,eAAe,EAAC,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAC,mBAAmB,EAAC,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAO5D,SAAS,KAAK,CAAC,KAA+B;IAC5C,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,SAAS,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,cAAc,CAAC,eAAyB;IAC/C,OAAO,aAAa,CAClB,aAAa,CAAC,IAAI,CAAC,EACnB,YAAY,CAAC,CAAC,KAAK,EAAE,EAAE;QACrB,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAU,EAAE,WAAqB,EAAE,EAAE;YACpE,IAAI,WAAW,CAAC,eAAe,EAAE,WAAW,CAAC,EAAE;gBAC7C,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,sBAAsB;aACtE;iBAAM;gBACL,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,0BAA0B;aAC1E;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CACH,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,GAAG,CACjB,KAAkC,EAClC,UAA4C,mBAAmB,CAAC,SAAS;IAEzE,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IACzG,MAAM,eAAe,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,CAAC;IAE7E,OAAO,aAAa,CAClB,QAAQ,CAAC,MAAM,CAAC,EAChB,UAAU,CAAC,eAAe,EAAE;QAC1B,IAAI,EAAE,cAAc,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,IAAI,mBAAmB,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;QACvG,GAAG,EAAE,KAAK;KACX,CAAC,EACF,aAAa,CAAC,CAAC,KAAK,EAAE,EAAE;QACtB,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QAED,OAAO,KAAK,CAAC;IACf,CAAC,CAAC,EACF,WAAW,CAAC,CAAC,KAAU,EAAE,GAAG,EAAE,EAAE;QAC9B,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QACD,MAAM,IAAI,GAAG,OAAO,EAAE,CAAC;QAEvB,OAAO,SAAS,CAAC,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,IAAI,EAAC,CAAC,CAAC;IAC1C,CAAC,CAAC,EACF,KAAK,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,EAC1G,eAAe,CAAC,MAAM,IAAI,cAAc,CAAC,eAAe,CAAC,CACrC,CAAC;AACzB,CAAC","sourcesContent":["import {isArrowFn, isObject, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {ForwardGroups, JsonEntityFn, lazyRef, matchGroups, OneOf, Property, string} from \"@tsed/schema\";\nimport {Schema as MongooseSchema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {MongooseSchemaTypes} from \"../interfaces/MongooseSchemaTypes\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\ninterface RefOptions {\n type?: MongooseSchemaTypes;\n populatedGroups?: string[];\n}\n\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\n}\n\nfunction PopulateGroups(populatedGroups: string[]) {\n return useDecorators(\n ForwardGroups(true),\n JsonEntityFn((store) => {\n store.schema.$hooks.on(\"oneOf\", (obj: any[], givenGroups: string[]) => {\n if (matchGroups(populatedGroups, givenGroups)) {\n return obj.filter((x) => x.type === \"string\"); // keep the object id;\n } else {\n return obj.filter((x) => x.type !== \"string\"); // keep the ref definition\n }\n });\n })\n );\n}\n\n/**\n * Define a property as mongoose reference to other Model (decorated with @Model).\n *\n * ### Example\n *\n * ```typescript\n *\n * @Model()\n * class FooModel {\n *\n * @Ref(Foo2Model)\n * field: Ref<Foo2Model>\n *\n * @Ref(Foo2Model)\n * list: Ref<Foo2Model>[]\n * }\n *\n * @Model()\n * class Foo2Model {\n * }\n * ```\n *\n * @param model\n * @param options\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Ref(\n model: string | (() => Type) | any,\n options: RefOptions | MongooseSchemaTypes = MongooseSchemaTypes.OBJECT_ID\n): PropertyDecorator {\n const getType = () => (isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model);\n const populatedGroups = (isObject(options) && options.populatedGroups) || [];\n\n return useDecorators(\n Property(Object),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types[isObject(options) ? options.type || MongooseSchemaTypes.OBJECT_ID : options],\n ref: model\n }),\n OnDeserialize((value) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return value;\n }),\n OnSerialize((value: any, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n const type = getType();\n\n return serialize(value, {...ctx, type});\n }),\n OneOf(string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), lazyRef(getType)),\n populatedGroups.length && PopulateGroups(populatedGroups)\n ) as PropertyDecorator;\n}\n\nexport type Ref<T> = T | string;\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { decoratorTypeOf, StoreMerge, useDecorators } from "@tsed/core";
|
|
2
|
+
import { registerProvider } from "@tsed/di";
|
|
3
|
+
import { Property } from "@tsed/schema";
|
|
4
|
+
import { MONGOOSE_SCHEMA } from "../constants/constants.js";
|
|
5
|
+
import { getSchema, getSchemaToken } from "../utils/createSchema.js";
|
|
6
|
+
export function Schema(options = {}) {
|
|
7
|
+
return (...parameters) => {
|
|
8
|
+
switch (decoratorTypeOf(parameters)) {
|
|
9
|
+
case "property":
|
|
10
|
+
return useDecorators(Property(), StoreMerge(MONGOOSE_SCHEMA, options))(parameters[0], parameters[1], parameters[2]);
|
|
11
|
+
case "class":
|
|
12
|
+
const { token } = getSchemaToken(parameters[0], options);
|
|
13
|
+
registerProvider({
|
|
14
|
+
provide: token,
|
|
15
|
+
deps: [],
|
|
16
|
+
useFactory() {
|
|
17
|
+
return getSchema(parameters[0], options);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export function MongooseSchema(options = {}) {
|
|
25
|
+
return Schema(options);
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/decorators/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,UAAU,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACtE,OAAO,EAAC,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAC,QAAQ,EAAC,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAC,eAAe,EAAC,MAAM,wBAAwB,CAAC;AAEvD,OAAO,EAAC,SAAS,EAAE,cAAc,EAAC,MAAM,uBAAuB,CAAC;AA4BhE,MAAM,UAAU,MAAM,CAAC,UAA0D,EAAE;IACjF,OAAO,CAAC,GAAG,UAAiB,EAAE,EAAE;QAC9B,QAAQ,eAAe,CAAC,UAAU,CAAC,EAAE;YACnC,KAAK,UAAU;gBACb,OAAO,aAAa,CAAC,QAAQ,EAAE,EAAE,UAAU,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;YAEtH,KAAK,OAAO;gBACV,MAAM,EAAC,KAAK,EAAC,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;gBAEvD,gBAAgB,CAAC;oBACf,OAAO,EAAE,KAAK;oBACd,IAAI,EAAE,EAAE;oBACR,UAAU;wBACR,OAAO,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,OAAc,CAAC,CAAC;oBAClD,CAAC;iBACF,CAAC,CAAC;gBACH,MAAM;SACT;IACH,CAAC,CAAC;AACJ,CAAC;AA4BD,MAAM,UAAU,cAAc,CAAC,UAA0D,EAAE;IACzF,OAAO,MAAM,CAAC,OAAc,CAAC,CAAC;AAChC,CAAC","sourcesContent":["import {decoratorTypeOf, StoreMerge, useDecorators} from \"@tsed/core\";\nimport {registerProvider} from \"@tsed/di\";\nimport {Property} from \"@tsed/schema\";\nimport {SchemaTypeOptions} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {MongooseSchemaOptions} from \"../interfaces/MongooseSchemaOptions\";\nimport {getSchema, getSchemaToken} from \"../utils/createSchema\";\n\n/**\n * Define a class as a Mongoose Schema ready to be used to compose other schemes and models.\n *\n * ### Example\n *\n * ```typescript\n * @MongooseSchema()\n * export class EventSchema {\n * @Property()\n * field: string;\n * }\n * ```\n *\n * ### Options\n *\n * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.\n *\n * @param {MongooseSchemaOptions | undefined} options\n * @returns {(target: any) => void}\n * @decorator\n * @mongoose\n * @property\n * @class\n */\nexport function Schema(options?: MongooseSchemaOptions): (target: any) => void;\nexport function Schema(definition: SchemaTypeOptions<any>): Function;\nexport function Schema(options: MongooseSchemaOptions | SchemaTypeOptions<any> = {}) {\n return (...parameters: any[]) => {\n switch (decoratorTypeOf(parameters)) {\n case \"property\":\n return useDecorators(Property(), StoreMerge(MONGOOSE_SCHEMA, options))(parameters[0], parameters[1], parameters[2]);\n\n case \"class\":\n const {token} = getSchemaToken(parameters[0], options);\n\n registerProvider({\n provide: token,\n deps: [],\n useFactory() {\n return getSchema(parameters[0], options as any);\n }\n });\n break;\n }\n };\n}\n\n/**\n * Define a class as a Mongoose Schema ready to be used to compose other schemes and models.\n *\n * ### Example\n *\n * ```typescript\n * @MongooseSchema()\n * export class EventSchema {\n * @Property()\n * field: string;\n * }\n * ```\n *\n * ### Options\n *\n * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.\n *\n * @param {MongooseSchemaOptions | undefined} options\n * @returns {(target: any) => void}\n * @decorator\n * @mongoose\n * @property\n * @class\n */\nexport function MongooseSchema(options?: MongooseSchemaOptions): (target: any) => void;\nexport function MongooseSchema(definition: SchemaTypeOptions<any>): Function;\nexport function MongooseSchema(options: MongooseSchemaOptions | SchemaTypeOptions<any> = {}) {\n return Schema(options as any);\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MongooseSchema } from "./schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Do not apply this property to schema (create virtual property)
|
|
4
|
+
*
|
|
5
|
+
* ### Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @Model()
|
|
9
|
+
* @SchemaIgnore()
|
|
10
|
+
* @Property()
|
|
11
|
+
* kind: string;
|
|
12
|
+
*
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @returns {Function}
|
|
16
|
+
* @decorator
|
|
17
|
+
* @mongoose
|
|
18
|
+
* @class
|
|
19
|
+
*/
|
|
20
|
+
export function SchemaIgnore() {
|
|
21
|
+
return MongooseSchema({ schemaIgnore: true });
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=schemaIgnore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaIgnore.js","sourceRoot":"","sources":["../../../src/decorators/schemaIgnore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;GAiBG;AAEH,MAAM,UAAU,YAAY;IAC1B,OAAO,cAAc,CAAC,EAAC,YAAY,EAAE,IAAI,EAAQ,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * Do not apply this property to schema (create virtual property)\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * @SchemaIgnore()\n * @Property()\n * kind: string;\n *\n * ```\n *\n * @returns {Function}\n * @decorator\n * @mongoose\n * @class\n */\n\nexport function SchemaIgnore(): Function {\n return MongooseSchema({schemaIgnore: true} as any);\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { MongooseSchema } from "./schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Tell Mongoose to set default select() behavior for this path.
|
|
4
|
+
*
|
|
5
|
+
* ### Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @Model()
|
|
9
|
+
* export class EventModel {
|
|
10
|
+
* @Select()
|
|
11
|
+
* field: string;
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param {boolean | any} select
|
|
16
|
+
* @returns {Function}
|
|
17
|
+
* @decorator
|
|
18
|
+
* @mongoose
|
|
19
|
+
* @property
|
|
20
|
+
*/
|
|
21
|
+
export function Select(select = true) {
|
|
22
|
+
return MongooseSchema({ select });
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=select.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"select.js","sourceRoot":"","sources":["../../../src/decorators/select.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,MAAM,CAAC,SAAwB,IAAI;IACjD,OAAO,cAAc,CAAC,EAAC,MAAM,EAAC,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * Tell Mongoose to set default select() behavior for this path.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Select()\n * field: string;\n * }\n * ```\n *\n * @param {boolean | any} select\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Select(select: boolean | any = true) {\n return MongooseSchema({select});\n}\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MongooseSchema } from "./schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
|
|
4
|
+
* disallow changes to this path once the document is saved to the database for the first time. Read more
|
|
5
|
+
* about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
|
|
6
|
+
*
|
|
7
|
+
* ### Example
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @Model()
|
|
11
|
+
* export class EventModel {
|
|
12
|
+
* @Sparse()
|
|
13
|
+
* field: string;
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @param {boolean | number} sparse
|
|
18
|
+
* @returns {Function}
|
|
19
|
+
* @decorator
|
|
20
|
+
* @mongoose
|
|
21
|
+
* @property
|
|
22
|
+
*/
|
|
23
|
+
export function Sparse(sparse = true) {
|
|
24
|
+
return MongooseSchema({ sparse });
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=sparse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sparse.js","sourceRoot":"","sources":["../../../src/decorators/sparse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,MAAM,CAAC,SAA2B,IAAI;IACpD,OAAO,cAAc,CAAC,EAAC,MAAM,EAAQ,CAAC,CAAC;AACzC,CAAC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will\n * disallow changes to this path once the document is saved to the database for the first time. Read more\n * about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Sparse()\n * field: string;\n * }\n * ```\n *\n * @param {boolean | number} sparse\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Sparse(sparse: boolean | number = true): PropertyDecorator {\n return MongooseSchema({sparse} as any);\n}\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { MongooseSchema } from "./schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
|
|
4
|
+
* will build a text index on this path.
|
|
5
|
+
*
|
|
6
|
+
* ### Example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* @Model()
|
|
10
|
+
* export class EventModel {
|
|
11
|
+
* @Text()
|
|
12
|
+
* field: string;
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @param {boolean | number | any} text
|
|
17
|
+
* @returns {Function}
|
|
18
|
+
* @decorator
|
|
19
|
+
* @mongoose
|
|
20
|
+
* @property
|
|
21
|
+
*/
|
|
22
|
+
export function Text(text = true) {
|
|
23
|
+
return MongooseSchema({ text });
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../../src/decorators/text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,IAAI,CAAC,OAA+B,IAAI;IACtD,OAAO,cAAc,CAAC,EAAC,IAAI,EAAQ,CAAC,CAAC;AACvC,CAAC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose\n * will build a text index on this path.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Text()\n * field: string;\n * }\n * ```\n *\n * @param {boolean | number | any} text\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Text(text: boolean | number | any = true): PropertyDecorator {\n return MongooseSchema({text} as any);\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MongooseSchema } from "./schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Tell Mongoose to make the property trimmable.
|
|
4
|
+
*
|
|
5
|
+
* ### Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @Model()
|
|
9
|
+
* export class EventModel {
|
|
10
|
+
* @Trim()
|
|
11
|
+
* name: string;
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @returns {Function}
|
|
16
|
+
* @decorator
|
|
17
|
+
* @mongoose
|
|
18
|
+
* @property
|
|
19
|
+
*/
|
|
20
|
+
export function Trim() {
|
|
21
|
+
return MongooseSchema({ trim: true });
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=trim.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trim.js","sourceRoot":"","sources":["../../../src/decorators/trim.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,IAAI;IAClB,OAAO,cAAc,CAAC,EAAC,IAAI,EAAE,IAAI,EAAC,CAAC,CAAC;AACtC,CAAC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * Tell Mongoose to make the property trimmable.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Trim()\n * name: string;\n * }\n * ```\n *\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Trim() {\n return MongooseSchema({trim: true});\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MongooseSchema } from "./schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Tell Mongoose to ensure a unique index is created for this path.
|
|
4
|
+
*
|
|
5
|
+
* ### Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @Model()
|
|
9
|
+
* export class EventModel {
|
|
10
|
+
* @Unique()
|
|
11
|
+
* index: string;
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param {boolean | any} unique
|
|
16
|
+
* @returns {Function}
|
|
17
|
+
* @decorator
|
|
18
|
+
* @mongoose
|
|
19
|
+
*/
|
|
20
|
+
export function Unique(unique = true) {
|
|
21
|
+
return MongooseSchema({ unique });
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=unique.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unique.js","sourceRoot":"","sources":["../../../src/decorators/unique.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,MAAM,CAAC,SAAwB,IAAI;IACjD,OAAO,cAAc,CAAC,EAAC,MAAM,EAAC,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * Tell Mongoose to ensure a unique index is created for this path.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Unique()\n * index: string;\n * }\n * ```\n *\n * @param {boolean | any} unique\n * @returns {Function}\n * @decorator\n * @mongoose\n */\nexport function Unique(unique: boolean | any = true) {\n return MongooseSchema({unique});\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { MongooseSchema } from "./schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in String#toUpperCase().
|
|
4
|
+
*
|
|
5
|
+
* ### Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @Model()
|
|
9
|
+
* export class EventModel {
|
|
10
|
+
* @Uppercase()
|
|
11
|
+
* field: string;
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param {boolean} uppercase
|
|
16
|
+
* @returns {Function}
|
|
17
|
+
* @decorator
|
|
18
|
+
* @mongoose
|
|
19
|
+
* @property
|
|
20
|
+
*/
|
|
21
|
+
export function Uppercase(uppercase = true) {
|
|
22
|
+
return MongooseSchema({ uppercase });
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=uppercase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uppercase.js","sourceRoot":"","sources":["../../../src/decorators/uppercase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,SAAS,CAAC,YAAqB,IAAI;IACjD,OAAO,cAAc,CAAC,EAAC,SAAS,EAAQ,CAAC,CAAC;AAC5C,CAAC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in String#toUpperCase().\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Uppercase()\n * field: string;\n * }\n * ```\n *\n * @param {boolean} uppercase\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Uppercase(uppercase: boolean = true): PropertyDecorator {\n return MongooseSchema({uppercase} as any);\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Store } from "@tsed/core";
|
|
2
|
+
import { MONGOOSE_SCHEMA_OPTIONS } from "../constants/constants.js";
|
|
3
|
+
export function VersionKey() {
|
|
4
|
+
return (target, propertyKey) => {
|
|
5
|
+
Store.from(target).merge(MONGOOSE_SCHEMA_OPTIONS, { versionKey: propertyKey });
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=versionKey.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"versionKey.js","sourceRoot":"","sources":["../../../src/decorators/versionKey.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AACjC,OAAO,EAAC,uBAAuB,EAAC,MAAM,wBAAwB,CAAC;AAE/D,MAAM,UAAU,UAAU;IACxB,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,EAAE;QAC1C,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,uBAAuB,EAAE,EAAC,UAAU,EAAE,WAAW,EAAC,CAAC,CAAC;IAC/E,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {Store} from \"@tsed/core\";\nimport {MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\n\nexport function VersionKey(): PropertyDecorator {\n return (target: any, propertyKey: string) => {\n Store.from(target).merge(MONGOOSE_SCHEMA_OPTIONS, {versionKey: propertyKey});\n };\n}\n"]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isPlainObject, isString, Store, StoreMerge, useDecorators } from "@tsed/core";
|
|
2
|
+
import { CollectionOf, Property } from "@tsed/schema";
|
|
3
|
+
import { MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA } from "../constants/constants.js";
|
|
4
|
+
import { MongooseModels } from "../registries/MongooseModels.js";
|
|
5
|
+
function getRef(opts) {
|
|
6
|
+
const ref = opts.ref;
|
|
7
|
+
return isString(ref) ? ref : Store.from(ref).get(MONGOOSE_MODEL_NAME);
|
|
8
|
+
}
|
|
9
|
+
function getType(opts) {
|
|
10
|
+
const ref = opts.ref;
|
|
11
|
+
return !isString(ref) ? ref : MongooseModels.get(ref) || (() => MongooseModels.get(ref) || Object);
|
|
12
|
+
}
|
|
13
|
+
function getInitialOpts(options, foreignField) {
|
|
14
|
+
return isPlainObject(options) ? options : { ref: options, foreignField };
|
|
15
|
+
}
|
|
16
|
+
function mapToSchema(opts) {
|
|
17
|
+
const ref = getRef(opts);
|
|
18
|
+
const schema = {
|
|
19
|
+
ref,
|
|
20
|
+
localField: opts.localField || "_id",
|
|
21
|
+
foreignField: opts.foreignField,
|
|
22
|
+
justOne: opts.justOne || false,
|
|
23
|
+
count: opts.count || false,
|
|
24
|
+
options: opts.options
|
|
25
|
+
};
|
|
26
|
+
return schema;
|
|
27
|
+
}
|
|
28
|
+
export function VirtualRef(options, foreignField) {
|
|
29
|
+
const opts = getInitialOpts(options, foreignField);
|
|
30
|
+
const schema = mapToSchema(opts);
|
|
31
|
+
const type = getType(opts);
|
|
32
|
+
return useDecorators(StoreMerge(MONGOOSE_SCHEMA, schema), schema.count ? Property(Number) : type && (schema.justOne ? Property(type) : CollectionOf(type)));
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=virtualRef.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"virtualRef.js","sourceRoot":"","sources":["../../../src/decorators/virtualRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACrF,OAAO,EAAC,YAAY,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;AACpD,OAAO,EAAC,mBAAmB,EAAE,eAAe,EAAC,MAAM,wBAAwB,CAAC;AAE5E,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAE5D,SAAS,MAAM,CAAC,IAAS;IACvB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC;IAErB,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC;AACxE,CAAC;AAED,SAAS,OAAO,CAAC,IAAS;IACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC;IACrB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,CAAC;AACrG,CAAC;AAED,SAAS,cAAc,CAAC,OAA2C,EAAE,YAAqB;IACxF,OAAO,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAC,GAAG,EAAE,OAAc,EAAE,YAAY,EAAC,CAAC;AAChF,CAAC;AAED,SAAS,WAAW,CAAC,IAAS;IAC5B,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAEzB,MAAM,MAAM,GAAQ;QAClB,GAAG;QACH,UAAU,EAAE,IAAI,CAAC,UAAU,IAAI,KAAK;QACpC,YAAY,EAAE,IAAI,CAAC,YAAY;QAC/B,OAAO,EAAE,IAAI,CAAC,OAAO,IAAI,KAAK;QAC9B,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,KAAK;QAC1B,OAAO,EAAE,IAAI,CAAC,OAAO;KACtB,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AAsBD,MAAM,UAAU,UAAU,CAAC,OAA2C,EAAE,YAAqB;IAC3F,MAAM,IAAI,GAAG,cAAc,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;IACnD,MAAM,MAAM,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE3B,OAAO,aAAa,CAClB,UAAU,CAAC,eAAe,EAAE,MAAM,CAAC,EACnC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CACjG,CAAC;AACJ,CAAC","sourcesContent":["import {isPlainObject, isString, Store, StoreMerge, useDecorators} from \"@tsed/core\";\nimport {CollectionOf, Property} from \"@tsed/schema\";\nimport {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\nfunction getRef(opts: any) {\n const ref = opts.ref;\n\n return isString(ref) ? ref : Store.from(ref).get(MONGOOSE_MODEL_NAME);\n}\n\nfunction getType(opts: any) {\n const ref = opts.ref;\n return !isString(ref) ? ref : MongooseModels.get(ref) || (() => MongooseModels.get(ref) || Object);\n}\n\nfunction getInitialOpts(options: string | MongooseVirtualRefOptions, foreignField?: string): any {\n return isPlainObject(options) ? options : {ref: options as any, foreignField};\n}\n\nfunction mapToSchema(opts: any) {\n const ref = getRef(opts);\n\n const schema: any = {\n ref,\n localField: opts.localField || \"_id\",\n foreignField: opts.foreignField,\n justOne: opts.justOne || false,\n count: opts.count || false,\n options: opts.options\n };\n\n return schema;\n}\n\n/**\n * Define a property as mongoose virtual reference to other Model (decorated with @Model).\n *\n * ::: warning\n * To avoid circular dependencies, do not use the virtual reference model in\n * anything except a type declaration. Using the virtual reference model will prevent\n * typescript transpiler from stripping away the import statement and cause a circular\n * import in node.\n * :::\n *\n * @param ref\n * @param foreignField\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function VirtualRef(ref: string, foreignField: string): Function;\nexport function VirtualRef(options: MongooseVirtualRefOptions): Function;\nexport function VirtualRef(options: string | MongooseVirtualRefOptions, foreignField?: string): Function;\nexport function VirtualRef(options: string | MongooseVirtualRefOptions, foreignField?: string): Function {\n const opts = getInitialOpts(options, foreignField);\n const schema = mapToSchema(opts);\n const type = getType(opts);\n\n return useDecorators(\n StoreMerge(MONGOOSE_SCHEMA, schema),\n schema.count ? Property(Number) : type && (schema.justOne ? Property(type) : CollectionOf(type))\n );\n}\n\nexport type VirtualRef<T> = T | null;\nexport type VirtualRefs<T> = T[];\n"]}
|