@tsed/mongoose 7.0.0-alpha.1 → 7.0.0-beta.3

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.
Files changed (244) hide show
  1. package/jest.config.js +14 -0
  2. package/lib/cjs/MongooseModule.js +36 -0
  3. package/lib/cjs/MongooseModule.js.map +1 -0
  4. package/lib/cjs/constants/index.js +24 -0
  5. package/lib/cjs/constants/index.js.map +1 -0
  6. package/lib/cjs/decorators/auto.js +28 -0
  7. package/lib/cjs/decorators/auto.js.map +1 -0
  8. package/lib/cjs/decorators/discriminatorKey.js +14 -0
  9. package/lib/cjs/decorators/discriminatorKey.js.map +1 -0
  10. package/lib/cjs/decorators/dynamicRef.js +46 -0
  11. package/lib/cjs/decorators/dynamicRef.js.map +1 -0
  12. package/lib/cjs/decorators/excludeIndexes.js +28 -0
  13. package/lib/cjs/decorators/excludeIndexes.js.map +1 -0
  14. package/lib/cjs/decorators/expires.js +28 -0
  15. package/lib/cjs/decorators/expires.js.map +1 -0
  16. package/lib/cjs/decorators/immutable.js +30 -0
  17. package/lib/cjs/decorators/immutable.js.map +1 -0
  18. package/lib/cjs/decorators/index.js +30 -0
  19. package/lib/cjs/decorators/index.js.map +1 -0
  20. package/lib/cjs/decorators/indexed.js +26 -0
  21. package/lib/cjs/decorators/indexed.js.map +1 -0
  22. package/lib/cjs/decorators/lowercase.js +28 -0
  23. package/lib/cjs/decorators/lowercase.js.map +1 -0
  24. package/lib/cjs/decorators/model.js +67 -0
  25. package/lib/cjs/decorators/model.js.map +1 -0
  26. package/lib/cjs/decorators/mongooseIndex.js +39 -0
  27. package/lib/cjs/decorators/mongooseIndex.js.map +1 -0
  28. package/lib/cjs/decorators/mongooseIndexes.js +39 -0
  29. package/lib/cjs/decorators/mongooseIndexes.js.map +1 -0
  30. package/lib/cjs/decorators/mongoosePlugin.js +22 -0
  31. package/lib/cjs/decorators/mongoosePlugin.js.map +1 -0
  32. package/lib/cjs/decorators/numberDecimal.js +78 -0
  33. package/lib/cjs/decorators/numberDecimal.js.map +1 -0
  34. package/lib/cjs/decorators/objectID.js +26 -0
  35. package/lib/cjs/decorators/objectID.js.map +1 -0
  36. package/lib/cjs/decorators/postHook.js +26 -0
  37. package/lib/cjs/decorators/postHook.js.map +1 -0
  38. package/lib/cjs/decorators/preHook.js +71 -0
  39. package/lib/cjs/decorators/preHook.js.map +1 -0
  40. package/lib/cjs/decorators/ref.js +75 -0
  41. package/lib/cjs/decorators/ref.js.map +1 -0
  42. package/lib/cjs/decorators/schema.js +32 -0
  43. package/lib/cjs/decorators/schema.js.map +1 -0
  44. package/lib/cjs/decorators/schemaIgnore.js +27 -0
  45. package/lib/cjs/decorators/schemaIgnore.js.map +1 -0
  46. package/lib/cjs/decorators/select.js +28 -0
  47. package/lib/cjs/decorators/select.js.map +1 -0
  48. package/lib/cjs/decorators/sparse.js +30 -0
  49. package/lib/cjs/decorators/sparse.js.map +1 -0
  50. package/lib/cjs/decorators/text.js +29 -0
  51. package/lib/cjs/decorators/text.js.map +1 -0
  52. package/lib/cjs/decorators/trim.js +27 -0
  53. package/lib/cjs/decorators/trim.js.map +1 -0
  54. package/lib/cjs/decorators/unique.js +27 -0
  55. package/lib/cjs/decorators/unique.js.map +1 -0
  56. package/lib/cjs/decorators/uppercase.js +28 -0
  57. package/lib/cjs/decorators/uppercase.js.map +1 -0
  58. package/lib/cjs/decorators/versionKey.js +12 -0
  59. package/lib/cjs/decorators/versionKey.js.map +1 -0
  60. package/lib/cjs/decorators/virtualRef.js +38 -0
  61. package/lib/cjs/decorators/virtualRef.js.map +1 -0
  62. package/lib/cjs/index.js +10 -0
  63. package/lib/cjs/index.js.map +1 -0
  64. package/lib/cjs/interfaces/MongooseConnectionOptions.js +3 -0
  65. package/lib/cjs/interfaces/MongooseConnectionOptions.js.map +1 -0
  66. package/lib/cjs/interfaces/MongooseDocument.js +3 -0
  67. package/lib/cjs/interfaces/MongooseDocument.js.map +1 -0
  68. package/lib/cjs/interfaces/MongooseModel.js +3 -0
  69. package/lib/cjs/interfaces/MongooseModel.js.map +1 -0
  70. package/lib/cjs/interfaces/MongooseModelOptions.js +3 -0
  71. package/lib/cjs/interfaces/MongooseModelOptions.js.map +1 -0
  72. package/lib/cjs/interfaces/MongooseSchemaOptions.js +3 -0
  73. package/lib/cjs/interfaces/MongooseSchemaOptions.js.map +1 -0
  74. package/lib/cjs/interfaces/MongooseSchemaTypes.js +10 -0
  75. package/lib/cjs/interfaces/MongooseSchemaTypes.js.map +1 -0
  76. package/lib/cjs/interfaces/MongooseVirtualRefOptions.js +3 -0
  77. package/lib/cjs/interfaces/MongooseVirtualRefOptions.js.map +1 -0
  78. package/lib/cjs/interfaces/index.js +10 -0
  79. package/lib/cjs/interfaces/index.js.map +1 -0
  80. package/lib/cjs/registries/MongooseModels.js +5 -0
  81. package/lib/cjs/registries/MongooseModels.js.map +1 -0
  82. package/lib/cjs/services/MongooseConnections.js +51 -0
  83. package/lib/cjs/services/MongooseConnections.js.map +1 -0
  84. package/lib/cjs/services/MongooseService.js +73 -0
  85. package/lib/cjs/services/MongooseService.js.map +1 -0
  86. package/lib/cjs/utils/createModel.js +58 -0
  87. package/lib/cjs/utils/createModel.js.map +1 -0
  88. package/lib/cjs/utils/createSchema.js +160 -0
  89. package/lib/cjs/utils/createSchema.js.map +1 -0
  90. package/lib/cjs/utils/index.js +6 -0
  91. package/lib/cjs/utils/index.js.map +1 -0
  92. package/lib/cjs/utils/resolveRefType.js +19 -0
  93. package/lib/cjs/utils/resolveRefType.js.map +1 -0
  94. package/lib/cjs/utils/schemaOptions.js +60 -0
  95. package/lib/cjs/utils/schemaOptions.js.map +1 -0
  96. package/lib/esm/MongooseModule.js +33 -0
  97. package/lib/esm/MongooseModule.js.map +1 -0
  98. package/lib/esm/constants/index.js +21 -0
  99. package/lib/esm/constants/index.js.map +1 -0
  100. package/lib/esm/decorators/auto.js +24 -0
  101. package/lib/esm/decorators/auto.js.map +1 -0
  102. package/lib/esm/decorators/discriminatorKey.js +10 -0
  103. package/lib/esm/decorators/discriminatorKey.js.map +1 -0
  104. package/lib/esm/decorators/dynamicRef.js +42 -0
  105. package/lib/esm/decorators/dynamicRef.js.map +1 -0
  106. package/lib/esm/decorators/excludeIndexes.js +24 -0
  107. package/lib/esm/decorators/excludeIndexes.js.map +1 -0
  108. package/lib/esm/decorators/expires.js +24 -0
  109. package/lib/esm/decorators/expires.js.map +1 -0
  110. package/lib/esm/decorators/immutable.js +26 -0
  111. package/lib/esm/decorators/immutable.js.map +1 -0
  112. package/lib/esm/decorators/index.js +27 -0
  113. package/lib/esm/decorators/index.js.map +1 -0
  114. package/lib/esm/decorators/indexed.js +22 -0
  115. package/lib/esm/decorators/indexed.js.map +1 -0
  116. package/lib/esm/decorators/lowercase.js +24 -0
  117. package/lib/esm/decorators/lowercase.js.map +1 -0
  118. package/lib/esm/decorators/model.js +63 -0
  119. package/lib/esm/decorators/model.js.map +1 -0
  120. package/lib/esm/decorators/mongooseIndex.js +35 -0
  121. package/lib/esm/decorators/mongooseIndex.js.map +1 -0
  122. package/lib/esm/decorators/mongooseIndexes.js +35 -0
  123. package/lib/esm/decorators/mongooseIndexes.js.map +1 -0
  124. package/lib/esm/decorators/mongoosePlugin.js +18 -0
  125. package/lib/esm/decorators/mongoosePlugin.js.map +1 -0
  126. package/lib/esm/decorators/numberDecimal.js +74 -0
  127. package/lib/esm/decorators/numberDecimal.js.map +1 -0
  128. package/lib/esm/decorators/objectID.js +22 -0
  129. package/lib/esm/decorators/objectID.js.map +1 -0
  130. package/lib/esm/decorators/postHook.js +22 -0
  131. package/lib/esm/decorators/postHook.js.map +1 -0
  132. package/lib/esm/decorators/preHook.js +67 -0
  133. package/lib/esm/decorators/preHook.js.map +1 -0
  134. package/lib/esm/decorators/ref.js +71 -0
  135. package/lib/esm/decorators/ref.js.map +1 -0
  136. package/lib/esm/decorators/schema.js +27 -0
  137. package/lib/esm/decorators/schema.js.map +1 -0
  138. package/lib/esm/decorators/schemaIgnore.js +23 -0
  139. package/lib/esm/decorators/schemaIgnore.js.map +1 -0
  140. package/lib/esm/decorators/select.js +24 -0
  141. package/lib/esm/decorators/select.js.map +1 -0
  142. package/lib/esm/decorators/sparse.js +26 -0
  143. package/lib/esm/decorators/sparse.js.map +1 -0
  144. package/lib/esm/decorators/text.js +25 -0
  145. package/lib/esm/decorators/text.js.map +1 -0
  146. package/lib/esm/decorators/trim.js +23 -0
  147. package/lib/esm/decorators/trim.js.map +1 -0
  148. package/lib/esm/decorators/unique.js +23 -0
  149. package/lib/esm/decorators/unique.js.map +1 -0
  150. package/lib/esm/decorators/uppercase.js +24 -0
  151. package/lib/esm/decorators/uppercase.js.map +1 -0
  152. package/lib/esm/decorators/versionKey.js +8 -0
  153. package/lib/esm/decorators/versionKey.js.map +1 -0
  154. package/lib/esm/decorators/virtualRef.js +34 -0
  155. package/lib/esm/decorators/virtualRef.js.map +1 -0
  156. package/lib/esm/index.js +7 -0
  157. package/lib/esm/index.js.map +1 -0
  158. package/lib/esm/interfaces/MongooseConnectionOptions.js +2 -0
  159. package/lib/esm/interfaces/MongooseConnectionOptions.js.map +1 -0
  160. package/lib/esm/interfaces/MongooseDocument.js +2 -0
  161. package/lib/esm/interfaces/MongooseDocument.js.map +1 -0
  162. package/lib/esm/interfaces/MongooseModel.js +2 -0
  163. package/lib/esm/interfaces/MongooseModel.js.map +1 -0
  164. package/lib/esm/interfaces/MongooseModelOptions.js +2 -0
  165. package/lib/esm/interfaces/MongooseModelOptions.js.map +1 -0
  166. package/lib/esm/interfaces/MongooseSchemaOptions.js +2 -0
  167. package/lib/esm/interfaces/MongooseSchemaOptions.js.map +1 -0
  168. package/lib/esm/interfaces/MongooseSchemaTypes.js +7 -0
  169. package/lib/esm/interfaces/MongooseSchemaTypes.js.map +1 -0
  170. package/lib/esm/interfaces/MongooseVirtualRefOptions.js +2 -0
  171. package/lib/esm/interfaces/MongooseVirtualRefOptions.js.map +1 -0
  172. package/lib/esm/interfaces/index.js +7 -0
  173. package/lib/esm/interfaces/index.js.map +1 -0
  174. package/lib/esm/registries/MongooseModels.js +2 -0
  175. package/lib/esm/registries/MongooseModels.js.map +1 -0
  176. package/lib/esm/services/MongooseConnections.js +48 -0
  177. package/lib/esm/services/MongooseConnections.js.map +1 -0
  178. package/lib/esm/services/MongooseService.js +70 -0
  179. package/lib/esm/services/MongooseService.js.map +1 -0
  180. package/lib/esm/utils/createModel.js +52 -0
  181. package/lib/esm/utils/createModel.js.map +1 -0
  182. package/lib/esm/utils/createSchema.js +151 -0
  183. package/lib/esm/utils/createSchema.js.map +1 -0
  184. package/lib/esm/utils/index.js +3 -0
  185. package/lib/esm/utils/index.js.map +1 -0
  186. package/lib/esm/utils/resolveRefType.js +15 -0
  187. package/lib/esm/utils/resolveRefType.js.map +1 -0
  188. package/lib/esm/utils/schemaOptions.js +54 -0
  189. package/lib/esm/utils/schemaOptions.js.map +1 -0
  190. package/lib/{MongooseModule.d.ts → types/MongooseModule.d.ts} +0 -0
  191. package/lib/{constants → types/constants}/index.d.ts +0 -0
  192. package/lib/{decorators → types/decorators}/auto.d.ts +0 -0
  193. package/lib/{decorators → types/decorators}/discriminatorKey.d.ts +0 -0
  194. package/lib/{decorators → types/decorators}/dynamicRef.d.ts +0 -0
  195. package/lib/{decorators → types/decorators}/excludeIndexes.d.ts +0 -0
  196. package/lib/{decorators → types/decorators}/expires.d.ts +0 -0
  197. package/lib/{decorators → types/decorators}/immutable.d.ts +0 -0
  198. package/lib/{decorators → types/decorators}/index.d.ts +0 -0
  199. package/lib/{decorators → types/decorators}/indexed.d.ts +0 -0
  200. package/lib/{decorators → types/decorators}/lowercase.d.ts +0 -0
  201. package/lib/{decorators → types/decorators}/model.d.ts +0 -0
  202. package/lib/{decorators → types/decorators}/mongooseIndex.d.ts +0 -0
  203. package/lib/types/decorators/mongooseIndexes.d.ts +32 -0
  204. package/lib/{decorators → types/decorators}/mongoosePlugin.d.ts +0 -0
  205. package/lib/{decorators → types/decorators}/numberDecimal.d.ts +0 -0
  206. package/lib/{decorators → types/decorators}/objectID.d.ts +0 -0
  207. package/lib/{decorators → types/decorators}/postHook.d.ts +0 -0
  208. package/lib/{decorators → types/decorators}/preHook.d.ts +0 -0
  209. package/lib/{decorators → types/decorators}/ref.d.ts +7 -2
  210. package/lib/{decorators → types/decorators}/schema.d.ts +0 -0
  211. package/lib/{decorators → types/decorators}/schemaIgnore.d.ts +0 -0
  212. package/lib/{decorators → types/decorators}/select.d.ts +0 -0
  213. package/lib/{decorators → types/decorators}/sparse.d.ts +0 -0
  214. package/lib/{decorators → types/decorators}/text.d.ts +0 -0
  215. package/lib/{decorators → types/decorators}/trim.d.ts +0 -0
  216. package/lib/{decorators → types/decorators}/unique.d.ts +0 -0
  217. package/lib/{decorators → types/decorators}/uppercase.d.ts +0 -0
  218. package/lib/{decorators → types/decorators}/versionKey.d.ts +0 -0
  219. package/lib/{decorators → types/decorators}/virtualRef.d.ts +0 -0
  220. package/lib/{index.d.ts → types/index.d.ts} +0 -0
  221. package/lib/{interfaces → types/interfaces}/MongooseConnectionOptions.d.ts +0 -0
  222. package/lib/{interfaces → types/interfaces}/MongooseDocument.d.ts +0 -0
  223. package/lib/{interfaces → types/interfaces}/MongooseModel.d.ts +0 -0
  224. package/lib/{interfaces → types/interfaces}/MongooseModelOptions.d.ts +0 -0
  225. package/lib/{interfaces → types/interfaces}/MongooseSchemaOptions.d.ts +0 -0
  226. package/lib/{interfaces → types/interfaces}/MongooseSchemaTypes.d.ts +0 -0
  227. package/lib/{interfaces → types/interfaces}/MongooseVirtualRefOptions.d.ts +1 -0
  228. package/lib/{interfaces → types/interfaces}/index.d.ts +0 -0
  229. package/lib/{registries → types/registries}/MongooseModels.d.ts +0 -0
  230. package/lib/{services → types/services}/MongooseConnections.d.ts +0 -0
  231. package/lib/{services → types/services}/MongooseService.d.ts +0 -0
  232. package/lib/{utils → types/utils}/createModel.d.ts +2 -1
  233. package/lib/{utils → types/utils}/createSchema.d.ts +0 -0
  234. package/lib/{utils → types/utils}/index.d.ts +0 -0
  235. package/lib/{utils → types/utils}/resolveRefType.d.ts +0 -0
  236. package/lib/{utils → types/utils}/schemaOptions.d.ts +0 -0
  237. package/package.json +25 -21
  238. package/tsconfig.compile.esm.json +23 -0
  239. package/.mocharc.js +0 -5
  240. package/.nycrc +0 -27
  241. package/lib/index.js +0 -1402
  242. package/lib/index.js.map +0 -1
  243. package/lib/index.modern.js +0 -1362
  244. package/lib/index.modern.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseDocument.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseDocument.ts"],"names":[],"mappings":"","sourcesContent":["import {Document} from \"mongoose\";\n\nexport type MongooseDocument<T> = T & Document;\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=MongooseModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseModel.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModel.ts"],"names":[],"mappings":"","sourcesContent":["import {JsonDeserializerOptions} from \"@tsed/json-mapper\";\nimport {Document, Model} from \"mongoose\";\n\n// TODO since v5.11.5 Model require Document with id, See issue https://github.com/Automattic/mongoose/issues/9684\nexport type MongooseMergedDocument<T> = {[K in keyof T]: T[K]};\n\nexport interface MongooseDocumentMethods<T> {\n toClass(): T;\n}\n\nexport interface MongooseModel<T> extends Model<MongooseMergedDocument<Document & T & MongooseDocumentMethods<T>>> {}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=MongooseModelOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseModelOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModelOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {MongooseSchemaOptions} from \"./MongooseSchemaOptions\";\n\nexport interface MongooseModelOptions extends MongooseSchemaOptions {\n name?: string;\n connection?: string;\n collection?: string;\n skipInit?: boolean;\n discriminatorValue?: string;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=MongooseSchemaOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {NativeError, Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: NativeError) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type MongoosePreHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport type MongoosePostHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, error: NativeError, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>, error: NativeError) => Promise<void> | void)\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport interface MongoosePreHook<T = any> {\n method: string | RegExp;\n fn: MongoosePreHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePostHook<T = any> {\n method: string | RegExp;\n fn: MongoosePostHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePluginOptions {\n plugin: (schema: Schema, options?: any) => void;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseIndexOptions {\n fields: object;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseSchemaOptions {\n schemaOptions?: SchemaOptions;\n plugins?: MongoosePluginOptions[];\n indexes?: MongooseIndexOptions[];\n pre?: MongoosePreHook[];\n post?: MongoosePostHook[];\n}\n"]}
@@ -0,0 +1,7 @@
1
+ export var MongooseSchemaTypes;
2
+ (function (MongooseSchemaTypes) {
3
+ MongooseSchemaTypes["OBJECT_ID"] = "ObjectId";
4
+ MongooseSchemaTypes["STRING"] = "String";
5
+ MongooseSchemaTypes["NUMBER"] = "Number";
6
+ })(MongooseSchemaTypes || (MongooseSchemaTypes = {}));
7
+ //# sourceMappingURL=MongooseSchemaTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseSchemaTypes.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaTypes.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,mBAIX;AAJD,WAAY,mBAAmB;IAC7B,6CAAsB,CAAA;IACtB,wCAAiB,CAAA;IACjB,wCAAiB,CAAA;AACnB,CAAC,EAJW,mBAAmB,KAAnB,mBAAmB,QAI9B","sourcesContent":["export enum MongooseSchemaTypes {\n OBJECT_ID = \"ObjectId\",\n STRING = \"String\",\n NUMBER = \"Number\"\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=MongooseVirtualRefOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseVirtualRefOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseVirtualRefOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Type} from \"@tsed/core\";\n\nexport interface MongooseVirtualRefOptions {\n ref?: string | Type<any> | (() => Type<any>);\n foreignField?: string;\n localField?: string;\n justOne?: boolean;\n count?: boolean;\n options?: object;\n}\n"]}
@@ -0,0 +1,7 @@
1
+ export * from "./MongooseConnectionOptions";
2
+ export * from "./MongooseDocument";
3
+ export * from "./MongooseModel";
4
+ export * from "./MongooseModelOptions";
5
+ export * from "./MongooseSchemaOptions";
6
+ export * from "./MongooseVirtualRefOptions";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAUA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC","sourcesContent":["import {MongooseConnectionOptions} from \"./MongooseConnectionOptions\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n mongoose: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[];\n }\n }\n}\n\nexport * from \"./MongooseConnectionOptions\";\nexport * from \"./MongooseDocument\";\nexport * from \"./MongooseModel\";\nexport * from \"./MongooseModelOptions\";\nexport * from \"./MongooseSchemaOptions\";\nexport * from \"./MongooseVirtualRefOptions\";\n"]}
@@ -0,0 +1,2 @@
1
+ export const MongooseModels = new Map();
2
+ //# sourceMappingURL=MongooseModels.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseModels.js","sourceRoot":"","sources":["../../../src/registries/MongooseModels.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,cAAc,GAAsB,IAAI,GAAG,EAAE,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\n\nexport const MongooseModels: Map<string, Type> = new Map();\n"]}
@@ -0,0 +1,48 @@
1
+ import { Configuration, registerProvider } from "@tsed/di";
2
+ import { isArray } from "@tsed/core";
3
+ import { MongooseService } from "../services/MongooseService";
4
+ /**
5
+ * @ignore
6
+ */
7
+ // tslint:disable-next-line:variable-name
8
+ export const MONGOOSE_CONNECTIONS = Symbol.for("MONGOOSE_CONNECTIONS");
9
+ function mapOptions(options) {
10
+ if (!options) {
11
+ return [];
12
+ }
13
+ if (!isArray(options)) {
14
+ const { url, connectionOptions } = options || {};
15
+ return [
16
+ {
17
+ id: "default",
18
+ url,
19
+ connectionOptions
20
+ }
21
+ ];
22
+ }
23
+ return options.map((settings) => {
24
+ return {
25
+ ...settings,
26
+ connectionOptions: settings.connectionOptions
27
+ };
28
+ });
29
+ }
30
+ registerProvider({
31
+ provide: MONGOOSE_CONNECTIONS,
32
+ injectable: false,
33
+ deps: [Configuration, MongooseService],
34
+ async useAsyncFactory(configuration, mongooseService) {
35
+ const settings = mapOptions(configuration.get("mongoose"));
36
+ let isDefault = true;
37
+ for (const current of settings) {
38
+ await mongooseService.connect(current.id, current.url, current.connectionOptions || {
39
+ useCreateIndex: true,
40
+ useNewUrlParser: true,
41
+ useUnifiedTopology: true
42
+ }, isDefault);
43
+ isDefault = false;
44
+ }
45
+ return mongooseService;
46
+ }
47
+ });
48
+ //# sourceMappingURL=MongooseConnections.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseConnections.js","sourceRoot":"","sources":["../../../src/services/MongooseConnections.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,gBAAgB,EAAC,MAAM,UAAU,CAAC;AACzD,OAAO,EAAC,OAAO,EAAC,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAC,eAAe,EAAC,MAAM,6BAA6B,CAAC;AAE5D;;GAEG;AACH,yCAAyC;AACzC,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AAMvE,SAAS,UAAU,CAAC,OAA4E;IAC9F,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,EAAE,CAAC;KACX;IAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QACrB,MAAM,EAAC,GAAG,EAAE,iBAAiB,EAAC,GAAG,OAAO,IAAI,EAAE,CAAC;QAE/C,OAAO;YACL;gBACE,EAAE,EAAE,SAAS;gBACb,GAAG;gBACH,iBAAiB;aAClB;SACF,CAAC;KACH;IAED,OAAQ,OAAuC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC/D,OAAO;YACL,GAAG,QAAQ;YACX,iBAAiB,EAAE,QAAQ,CAAC,iBAAiB;SAC9C,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,gBAAgB,CAAC;IACf,OAAO,EAAE,oBAAoB;IAC7B,UAAU,EAAE,KAAK;IACjB,IAAI,EAAE,CAAC,aAAa,EAAE,eAAe,CAAC;IACtC,KAAK,CAAC,eAAe,CAAC,aAA4B,EAAE,eAAgC;QAClF,MAAM,QAAQ,GAAG,UAAU,CAAC,aAAa,CAAC,GAAG,CAA0D,UAAU,CAAC,CAAC,CAAC;QACpH,IAAI,SAAS,GAAG,IAAI,CAAC;QAErB,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,eAAe,CAAC,OAAO,CAC3B,OAAO,CAAC,EAAE,EACV,OAAO,CAAC,GAAG,EACX,OAAO,CAAC,iBAAiB,IAAI;gBAC3B,cAAc,EAAE,IAAI;gBACpB,eAAe,EAAE,IAAI;gBACrB,kBAAkB,EAAE,IAAI;aACzB,EACD,SAAS,CACV,CAAC;YAEF,SAAS,GAAG,KAAK,CAAC;SACnB;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {Configuration, registerProvider} from \"@tsed/di\";\nimport {isArray} from \"@tsed/core\";\nimport {MongooseConnectionOptions} from \"../interfaces\";\nimport {MongooseService} from \"../services/MongooseService\";\n\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nexport const MONGOOSE_CONNECTIONS = Symbol.for(\"MONGOOSE_CONNECTIONS\");\n/**\n * @ignore\n */\nexport type MONGOOSE_CONNECTIONS = MongooseService;\n\nfunction mapOptions(options: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[]): MongooseConnectionOptions[] {\n if (!options) {\n return [];\n }\n\n if (!isArray(options)) {\n const {url, connectionOptions} = options || {};\n\n return [\n {\n id: \"default\",\n url,\n connectionOptions\n }\n ];\n }\n\n return (options as MongooseConnectionOptions[]).map((settings) => {\n return {\n ...settings,\n connectionOptions: settings.connectionOptions\n };\n });\n}\n\nregisterProvider({\n provide: MONGOOSE_CONNECTIONS,\n injectable: false,\n deps: [Configuration, MongooseService],\n async useAsyncFactory(configuration: Configuration, mongooseService: MongooseService) {\n const settings = mapOptions(configuration.get<MongooseConnectionOptions | MongooseConnectionOptions[]>(\"mongoose\"));\n let isDefault = true;\n\n for (const current of settings) {\n await mongooseService.connect(\n current.id,\n current.url,\n current.connectionOptions || {\n useCreateIndex: true,\n useNewUrlParser: true,\n useUnifiedTopology: true\n },\n isDefault\n );\n\n isDefault = false;\n }\n\n return mongooseService;\n }\n});\n"]}
@@ -0,0 +1,70 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Inject, Injectable } from "@tsed/di";
3
+ import { Logger } from "@tsed/logger";
4
+ import Mongoose from "mongoose";
5
+ // istanbul ignore next
6
+ function asPromise(c) {
7
+ return c && c.asPromise ? c.asPromise() : c;
8
+ }
9
+ let MongooseService = class MongooseService {
10
+ constructor() {
11
+ this.connections = new Map();
12
+ this.defaultConnection = "default";
13
+ }
14
+ /**
15
+ *
16
+ * @returns {Promise<"mongoose".Connection>}
17
+ */
18
+ async connect(id, url, connectionOptions, isDefault = false) {
19
+ if (this.has(id)) {
20
+ return await this.get(id);
21
+ }
22
+ this.logger.info(`Connect to mongo database: ${id}`);
23
+ this.logger.debug(`Url: ${url}`);
24
+ this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);
25
+ try {
26
+ const connection = await asPromise(Mongoose.createConnection(url, connectionOptions));
27
+ this.connections.set(id, connection);
28
+ if (id === "default" || isDefault) {
29
+ this.defaultConnection = id;
30
+ }
31
+ return connection;
32
+ }
33
+ catch (er) {
34
+ /* istanbul ignore next */
35
+ this.logger.error(er);
36
+ /* istanbul ignore next */
37
+ process.exit();
38
+ }
39
+ }
40
+ /**
41
+ *
42
+ * @returns {"mongoose".Connection}
43
+ */
44
+ get(id) {
45
+ return this.connections.get(id || this.defaultConnection);
46
+ }
47
+ /**
48
+ *
49
+ * @param {string} id
50
+ * @returns {boolean}
51
+ */
52
+ has(id) {
53
+ return this.connections.has(id || this.defaultConnection);
54
+ }
55
+ async closeConnections() {
56
+ for (const [id, connection] of this.connections.entries()) {
57
+ await connection.close();
58
+ this.connections.delete(id);
59
+ }
60
+ }
61
+ };
62
+ __decorate([
63
+ Inject(),
64
+ __metadata("design:type", Logger)
65
+ ], MongooseService.prototype, "logger", void 0);
66
+ MongooseService = __decorate([
67
+ Injectable()
68
+ ], MongooseService);
69
+ export { MongooseService };
70
+ //# sourceMappingURL=MongooseService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseService.js","sourceRoot":"","sources":["../../../src/services/MongooseService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,QAAQ,MAAM,UAAU,CAAC;AAGhC,uBAAuB;AACvB,SAAS,SAAS,CAAC,CAAM;IACvB,OAAO,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9C,CAAC;AAGD,IAAa,eAAe,GAA5B,MAAa,eAAe;IAA5B;QACW,gBAAW,GAAqC,IAAI,GAAG,EAAE,CAAC;QAC3D,sBAAiB,GAAW,SAAS,CAAC;IA0DhD,CAAC;IArDC;;;OAGG;IACH,KAAK,CAAC,OAAO,CAAC,EAAU,EAAE,GAAW,EAAE,iBAAiC,EAAE,SAAS,GAAG,KAAK;QACzF,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAChB,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,CAAE,CAAC;SAC5B;QAED,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,8BAA8B,EAAE,EAAE,CAAC,CAAC;QACrD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,GAAG,EAAE,CAAC,CAAC;QACjC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC;QAEnE,IAAI;YACF,MAAM,UAAU,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,gBAAgB,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC;YACtF,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;YAErC,IAAI,EAAE,KAAK,SAAS,IAAI,SAAS,EAAE;gBACjC,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;aAC7B;YAED,OAAO,UAAU,CAAC;SACnB;QAAC,OAAO,EAAE,EAAE;YACX,0BAA0B;YAC1B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACtB,0BAA0B;YAC1B,OAAO,CAAC,IAAI,EAAE,CAAC;SAChB;IACH,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,KAAK,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE;YACzD,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SAC7B;IACH,CAAC;CACF,CAAA;AAvDC;IADC,MAAM,EAAE;8BACD,MAAM;+CAAC;AALJ,eAAe;IAD3B,UAAU,EAAE;GACA,eAAe,CA4D3B;SA5DY,eAAe","sourcesContent":["import {Inject, Injectable} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport Mongoose from \"mongoose\";\nimport {ConnectOptions} from \"mongoose\";\n\n// istanbul ignore next\nfunction asPromise(c: any) {\n return c && c.asPromise ? c.asPromise() : c;\n}\n\n@Injectable()\nexport class MongooseService {\n readonly connections: Map<string, Mongoose.Connection> = new Map();\n private defaultConnection: string = \"default\";\n\n @Inject()\n logger: Logger;\n\n /**\n *\n * @returns {Promise<\"mongoose\".Connection>}\n */\n async connect(id: string, url: string, connectionOptions: ConnectOptions, isDefault = false): Promise<any> {\n if (this.has(id)) {\n return await this.get(id)!;\n }\n\n this.logger.info(`Connect to mongo database: ${id}`);\n this.logger.debug(`Url: ${url}`);\n this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);\n\n try {\n const connection = await asPromise(Mongoose.createConnection(url, connectionOptions));\n this.connections.set(id, connection);\n\n if (id === \"default\" || isDefault) {\n this.defaultConnection = id;\n }\n\n return connection;\n } catch (er) {\n /* istanbul ignore next */\n this.logger.error(er);\n /* istanbul ignore next */\n process.exit();\n }\n }\n\n /**\n *\n * @returns {\"mongoose\".Connection}\n */\n get(id?: string): Mongoose.Connection | undefined {\n return this.connections.get(id || this.defaultConnection);\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id?: string): boolean {\n return this.connections.has(id || this.defaultConnection);\n }\n\n async closeConnections() {\n for (const [id, connection] of this.connections.entries()) {\n await connection.close();\n this.connections.delete(id);\n }\n }\n}\n"]}
@@ -0,0 +1,52 @@
1
+ import { ancestorsOf, nameOf, Store } from "@tsed/core";
2
+ import mongoose from "mongoose";
3
+ import { MONGOOSE_MODEL, MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA_OPTIONS } from "../constants";
4
+ import { MongooseModels } from "../registries/MongooseModels";
5
+ import { getSchemaToken } from "./createSchema";
6
+ export function getModelToken(target, options) {
7
+ const { collectionName, token } = getSchemaToken(target, options);
8
+ Store.from(target).set(MONGOOSE_MODEL_NAME, collectionName);
9
+ MongooseModels.set(collectionName, target);
10
+ return { token, collectionName };
11
+ }
12
+ /**
13
+ * Create an instance of mongoose.model from a class.
14
+ *
15
+ * @param {Type<any>} target Class attached to the schema and model.
16
+ * @param {"mongoose".Schema} schema Schema that will be attached to the model.
17
+ * @param name model name
18
+ * @param collection (optional, induced from model name)
19
+ * @param skipInit whether to skip initialization (defaults to false)
20
+ * @param connection
21
+ * @param discriminatorValue
22
+ * @returns {Model<T extends Document>}
23
+ */
24
+ export function createModel(target, schema, name = nameOf(target), collection, skipInit, connection, discriminatorValue) {
25
+ // if ancestor has a discriminatorKey we need to link model with anscestor schema
26
+ const ancestor = ancestorsOf(target).find((ancestor) => {
27
+ const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};
28
+ return !!options.discriminatorKey;
29
+ });
30
+ if (ancestor && ancestor !== target) {
31
+ const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL);
32
+ if (ancestorModel) {
33
+ const discriminatorName = discriminatorValue || name;
34
+ if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {
35
+ return ancestorModel.discriminators[discriminatorName];
36
+ }
37
+ else {
38
+ return ancestorModel.discriminator(discriminatorName, schema);
39
+ }
40
+ }
41
+ }
42
+ /* istanbul ignore else */
43
+ if (connection) {
44
+ const model = connection.model(name, schema, collection);
45
+ Store.from(target).set(MONGOOSE_MODEL, model);
46
+ return model;
47
+ }
48
+ const model = mongoose.model(name, schema, collection, skipInit);
49
+ Store.from(target).set(MONGOOSE_MODEL, model);
50
+ return model;
51
+ }
52
+ //# sourceMappingURL=createModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../../src/utils/createModel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAO,MAAM,YAAY,CAAC;AAC5D,OAAO,QAA8C,MAAM,UAAU,CAAC;AACtE,OAAO,EAAC,cAAc,EAAE,mBAAmB,EAAE,uBAAuB,EAAC,MAAM,cAAc,CAAC;AAC1F,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAC,cAAc,EAAC,MAAM,gBAAgB,CAAC;AAE9C,MAAM,UAAU,aAAa,CAAC,MA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/D;SACF;KACF;IAED,0BAA0B;IAC1B,IAAI,UAAU,EAAE;QACd,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;QACzD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;QAC9C,OAAO,KAAK,CAAC;KACd;IAED,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;IACjE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IAC9C,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {ancestorsOf, nameOf, Store, Type} from \"@tsed/core\";\nimport mongoose, {Connection, Model as MongooseModel} from \"mongoose\";\nimport {MONGOOSE_MODEL, MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA_OPTIONS} from \"../constants\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\nimport {getSchemaToken} from \"./createSchema\";\n\nexport function getModelToken(target: Type<any>, options: any) {\n const {collectionName, token} = getSchemaToken(target, options);\n\n Store.from(target).set(MONGOOSE_MODEL_NAME, collectionName);\n MongooseModels.set(collectionName, target);\n\n return {token, collectionName};\n}\n\n/**\n * Create an instance of mongoose.model from a class.\n *\n * @param {Type<any>} target Class attached to the schema and model.\n * @param {\"mongoose\".Schema} schema Schema that will be attached to the model.\n * @param name model name\n * @param collection (optional, induced from model name)\n * @param skipInit whether to skip initialization (defaults to false)\n * @param connection\n * @param discriminatorValue\n * @returns {Model<T extends Document>}\n */\nexport function createModel<T>(\n target: any,\n schema: mongoose.Schema,\n name: string = nameOf(target),\n collection?: string,\n skipInit?: boolean,\n connection?: Connection,\n discriminatorValue?: string\n) {\n // if ancestor has a discriminatorKey we need to link model with anscestor schema\n const ancestor = ancestorsOf(target).find((ancestor) => {\n const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n return !!options.discriminatorKey;\n });\n\n if (ancestor && ancestor !== target) {\n const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL) as MongooseModel<typeof target> | undefined;\n if (ancestorModel) {\n const discriminatorName = discriminatorValue || name;\n if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {\n return ancestorModel.discriminators[discriminatorName];\n } else {\n return ancestorModel.discriminator(discriminatorName, schema);\n }\n }\n }\n\n /* istanbul ignore else */\n if (connection) {\n const model = connection.model(name, schema, collection);\n Store.from(target).set(MONGOOSE_MODEL, model);\n return model;\n }\n\n const model = mongoose.model(name, schema, collection, skipInit);\n Store.from(target).set(MONGOOSE_MODEL, model);\n return model;\n}\n"]}
@@ -0,0 +1,151 @@
1
+ import { cleanObject, nameOf, Store } from "@tsed/core";
2
+ import { deserialize, serialize } from "@tsed/json-mapper";
3
+ import { getProperties } from "@tsed/schema";
4
+ import { pascalCase } from "change-case";
5
+ import mongoose from "mongoose";
6
+ import { MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS } from "../constants";
7
+ import { resolveRefType } from "./resolveRefType";
8
+ import { schemaOptions } from "./schemaOptions";
9
+ const MONGOOSE_RESERVED_KEYS = ["_id"];
10
+ /**
11
+ * @ignore
12
+ */
13
+ function setUpSchema({ schema, virtuals }, options) {
14
+ const mongooseSchema = new mongoose.Schema(schema, options);
15
+ for (const [key, options] of virtuals.entries()) {
16
+ mongooseSchema.virtual(key, options);
17
+ }
18
+ return mongooseSchema;
19
+ }
20
+ /**
21
+ * @ignore
22
+ */
23
+ function isVirtualRef(target) {
24
+ return !!(target.ref && target.localField && target.foreignField);
25
+ }
26
+ function hasVersionField(schema, versionKey) {
27
+ // Check if versioning was disabled explicitly
28
+ if (!versionKey)
29
+ return false;
30
+ // Check for alternative version field in schema
31
+ return versionKey in schema.paths;
32
+ }
33
+ export function createSchema(target, options = {}) {
34
+ var _a;
35
+ const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};
36
+ options.schemaOptions = { ...options.schemaOptions, ...schemaOptionsFromStore };
37
+ const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);
38
+ schemaOptions(target, options);
39
+ const outputVersionKey = hasVersionField(schema, (_a = options.schemaOptions) === null || _a === void 0 ? void 0 : _a.versionKey);
40
+ schema.methods.toClass = function toClass() {
41
+ return deserialize(this.toObject({
42
+ versionKey: outputVersionKey,
43
+ flattenMaps: true
44
+ }), {
45
+ type: target,
46
+ useAlias: false,
47
+ additionalProperties: true,
48
+ groups: false
49
+ });
50
+ };
51
+ schema.methods.toJSON = function toJSON(options) {
52
+ return serialize(this.toClass(), options);
53
+ };
54
+ schema.loadClass(target);
55
+ Store.from(target).set(MONGOOSE_SCHEMA, schema);
56
+ return schema;
57
+ }
58
+ /**
59
+ * Get a schema already created. If the schema doesn't exists in registry, it'll be created.
60
+ * @param target
61
+ * @param options
62
+ */
63
+ export function getSchema(target, options = {}) {
64
+ const store = Store.from(target);
65
+ if (!store.has(MONGOOSE_SCHEMA)) {
66
+ createSchema(target, options);
67
+ }
68
+ return store.get(MONGOOSE_SCHEMA);
69
+ }
70
+ export function getSchemaToken(target, options) {
71
+ const collectionName = options.name || nameOf(target);
72
+ const token = Symbol.for(pascalCase(`${collectionName}Schema`));
73
+ return { token, collectionName };
74
+ }
75
+ /**
76
+ * @ignore
77
+ */
78
+ export function buildMongooseSchema(target) {
79
+ const properties = getProperties(target, { withIgnoredProps: true, mongoose: true, groups: false });
80
+ const schema = { schema: {}, virtuals: new Map() };
81
+ properties.forEach((propertyMetadata, key) => {
82
+ if (MONGOOSE_RESERVED_KEYS.includes(key)) {
83
+ return;
84
+ }
85
+ // Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.
86
+ const schemaTypeOptions = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};
87
+ if (schemaTypeOptions.schemaIgnore) {
88
+ return;
89
+ }
90
+ if (schemaTypeOptions.ref) {
91
+ schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref);
92
+ }
93
+ if (isVirtualRef(schemaTypeOptions)) {
94
+ schema.virtuals.set(key, schemaTypeOptions);
95
+ return;
96
+ }
97
+ schema.schema[key] = createSchemaTypeOptions(propertyMetadata);
98
+ });
99
+ return schema;
100
+ }
101
+ /**
102
+ * @ignore
103
+ */
104
+ export function createSchemaTypeOptions(propEntity) {
105
+ const key = propEntity.propertyKey;
106
+ const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};
107
+ let schemaTypeOptions = {
108
+ required: propEntity.required
109
+ ? function () {
110
+ return propEntity.isRequired(this[key]);
111
+ }
112
+ : false
113
+ };
114
+ if (!propEntity.isClass) {
115
+ const jsonSchema = propEntity.itemSchema.toJSON();
116
+ const { minimum: min, maximum: max, minLength: minlength, maxLength: maxlength } = jsonSchema;
117
+ let match = jsonSchema.pattern;
118
+ if (match) {
119
+ match = new RegExp(match);
120
+ }
121
+ schemaTypeOptions = {
122
+ ...schemaTypeOptions,
123
+ type: propEntity.type,
124
+ match: match,
125
+ min,
126
+ max,
127
+ minlength,
128
+ maxlength,
129
+ enum: jsonSchema["enum"],
130
+ default: jsonSchema["default"]
131
+ };
132
+ }
133
+ else if (!rawMongooseSchema.ref) {
134
+ // References are handled by the final merge
135
+ schemaTypeOptions = { ...schemaTypeOptions, type: getSchema(propEntity.type) };
136
+ }
137
+ schemaTypeOptions = cleanObject({ ...schemaTypeOptions, ...rawMongooseSchema });
138
+ if (propEntity.isCollection) {
139
+ if (propEntity.isArray) {
140
+ return [schemaTypeOptions];
141
+ }
142
+ // Can be a Map or a Set;
143
+ // Mongoose implements only Map;
144
+ if (propEntity.collectionType !== Map) {
145
+ throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);
146
+ }
147
+ return { type: Map, of: schemaTypeOptions };
148
+ }
149
+ return schemaTypeOptions;
150
+ }
151
+ //# sour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AK,EAAE,KAAe;YACtB,GAAG;YACH,GAAG;YACH,SAAS;YACT,SAAS;YACT,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC;YACxB,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC;SAC/B,CAAC;KACH;SAAM,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE;QACjC,4CAA4C;QAC5C,iBAAiB,GAAG,EAAC,GAAG,iBAAiB,EAAE,IAAI,EAAE,SAAS,CAAC,UAAU,CAAC,IAAI,CAAQ,EAAC,CAAC;KACrF;IAED,iBAAiB,GAAG,WAAW,CAAC,EAAC,GAAG,iBAAiB,EAAE,GAAG,iBAAiB,EAAC,CAAC,CAAC;IAE9E,IAAI,UAAU,CAAC,YAAY,EAAE;QAC3B,IAAI,UAAU,CAAC,OAAO,EAAE;YACtB,OAAO,CAAC,iBAAiB,CAAC,CAAC;SAC5B;QACD,yBAAyB;QACzB,gCAAgC;QAChC,IAAI,UAAU,CAAC,cAAc,KAAK,GAAG,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,4BAA4B,MAAM,CAAC,UAAU,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC;SACpG;QAED,OAAQ,EAAC,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,iBAAiB,EAAqC,CAAC;KAChF;IAED,OAAO,iBAAiB,CAAC;AAC3B,CAAC","sourcesContent":["import {cleanObject, nameOf, Store, Type} from \"@tsed/core\";\nimport {deserialize, serialize} from \"@tsed/json-mapper\";\nimport {getProperties, JsonEntityStore} from \"@tsed/schema\";\nimport {pascalCase} from \"change-case\";\nimport mongoose, {Schema, SchemaDefinition, SchemaOptions, SchemaTypeOptions} from \"mongoose\";\nimport {MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS} from \"../constants\";\nimport {MongooseSchemaOptions} from \"../interfaces\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\nimport {resolveRefType} from \"./resolveRefType\";\nimport {schemaOptions} from \"./schemaOptions\";\n\nconst MONGOOSE_RESERVED_KEYS = [\"_id\"];\n\nexport interface MongooseSchemaMetadata {\n schema: SchemaDefinition;\n virtuals: Map<string, MongooseVirtualRefOptions>;\n}\n\n/**\n * @ignore\n */\nfunction setUpSchema({schema, virtuals}: MongooseSchemaMetadata, options?: SchemaOptions) {\n const mongooseSchema = new mongoose.Schema(schema, options);\n\n for (const [key, options] of virtuals.entries()) {\n mongooseSchema.virtual(key, options);\n }\n\n return mongooseSchema;\n}\n\n/**\n * @ignore\n */\nfunction isVirtualRef(target: Partial<MongooseVirtualRefOptions>): target is MongooseVirtualRefOptions {\n return !!(target.ref && target.localField && target.foreignField);\n}\n\nfunction hasVersionField(schema: mongoose.Schema, versionKey?: string | boolean) {\n // Check if versioning was disabled explicitly\n if (!versionKey) return false;\n\n // Check for alternative version field in schema\n return (versionKey as string) in schema.paths;\n}\n\nexport function createSchema(target: Type<any>, options: MongooseSchemaOptions = {}): mongoose.Schema {\n const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n options.schemaOptions = {...options.schemaOptions, ...schemaOptionsFromStore};\n\n const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);\n\n schemaOptions(target, options);\n\n const outputVersionKey = hasVersionField(schema, options.schemaOptions?.versionKey);\n\n schema.methods.toClass = function toClass() {\n return deserialize(\n this.toObject({\n versionKey: outputVersionKey,\n flattenMaps: true\n }),\n {\n type: target,\n useAlias: false,\n additionalProperties: true,\n groups: false\n }\n );\n };\n\n schema.methods.toJSON = function toJSON(options?: any) {\n return serialize((this as any).toClass(), options);\n };\n\n schema.loadClass(target);\n\n Store.from(target).set(MONGOOSE_SCHEMA, schema);\n\n return schema;\n}\n\n/**\n * Get a schema already created. If the schema doesn't exists in registry, it'll be created.\n * @param target\n * @param options\n */\nexport function getSchema(target: Type<any>, options: MongooseSchemaOptions = {}): Schema {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA)) {\n createSchema(target, options);\n }\n\n return store.get(MONGOOSE_SCHEMA);\n}\n\nexport function getSchemaToken(target: Type<any>, options?: any) {\n const collectionName = options.name || nameOf(target);\n const token = Symbol.for(pascalCase(`${collectionName}Schema`));\n\n return {token, collectionName};\n}\n\n/**\n * @ignore\n */\nexport function buildMongooseSchema(target: any): MongooseSchemaMetadata {\n const properties = getProperties(target, {withIgnoredProps: true, mongoose: true, groups: false});\n const schema: MongooseSchemaMetadata = {schema: {}, virtuals: new Map()};\n\n properties.forEach((propertyMetadata, key) => {\n if (MONGOOSE_RESERVED_KEYS.includes(key as string)) {\n return;\n }\n\n // Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.\n const schemaTypeOptions: any = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};\n\n if (schemaTypeOptions.schemaIgnore) {\n return;\n }\n\n if (schemaTypeOptions.ref) {\n schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref as any);\n }\n\n if (isVirtualRef(schemaTypeOptions)) {\n schema.virtuals.set(key as string, schemaTypeOptions);\n\n return;\n }\n\n schema.schema[key as string] = createSchemaTypeOptions(propertyMetadata);\n });\n\n return schema;\n}\n\n/**\n * @ignore\n */\nexport function createSchemaTypeOptions<T = any>(propEntity: JsonEntityStore): SchemaTypeOptions<T> | SchemaTypeOptions<T>[] {\n const key = propEntity.propertyKey;\n const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};\n\n let schemaTypeOptions: SchemaTypeOptions<T> = {\n required: propEntity.required\n ? function () {\n return propEntity.isRequired(this[key]);\n }\n : false\n };\n\n if (!propEntity.isClass) {\n const jsonSchema = propEntity.itemSchema.toJSON();\n const {minimum: min, maximum: max, minLength: minlength, maxLength: maxlength} = jsonSchema;\n\n let match: string | RegExp = jsonSchema.pattern;\n if (match) {\n match = new RegExp(match);\n }\n\n schemaTypeOptions = {\n ...schemaTypeOptions,\n type: propEntity.type,\n match: match as RegExp,\n min,\n max,\n minlength,\n maxlength,\n enum: jsonSchema[\"enum\"],\n default: jsonSchema[\"default\"]\n };\n } else if (!rawMongooseSchema.ref) {\n // References are handled by the final merge\n schemaTypeOptions = {...schemaTypeOptions, type: getSchema(propEntity.type) as any};\n }\n\n schemaTypeOptions = cleanObject({...schemaTypeOptions, ...rawMongooseSchema});\n\n if (propEntity.isCollection) {\n if (propEntity.isArray) {\n return [schemaTypeOptions];\n }\n // Can be a Map or a Set;\n // Mongoose implements only Map;\n if (propEntity.collectionType !== Map) {\n throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);\n }\n\n return ({type: Map, of: schemaTypeOptions} as unknown) as SchemaTypeOptions<T>;\n }\n\n return schemaTypeOptions;\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export * from "./createSchema";
2
+ export * from "./createModel";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC","sourcesContent":["export * from \"./createSchema\";\nexport * from \"./createModel\";\n"]}
@@ -0,0 +1,15 @@
1
+ import { isClass, isString, nameOf, Store } from "@tsed/core";
2
+ import { MONGOOSE_MODEL_NAME } from "../constants";
3
+ /**
4
+ * @ignore
5
+ */
6
+ export function resolveRefType(type) {
7
+ if (isString(type)) {
8
+ return type;
9
+ }
10
+ if (!isClass(type)) {
11
+ type = type();
12
+ }
13
+ return Store.from(type).get(MONGOOSE_MODEL_NAME) || nameOf(type);
14
+ }
15
+ //# sourceMappingURL=resolveRefType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolveRefType.js","sourceRoot":"","sources":["../../../src/utils/resolveRefType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAO,MAAM,YAAY,CAAC;AAClE,OAAO,EAAC,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAEjD;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,IAA4C;IACzE,IAAI,QAAQ,CAAC,IAAI,CAAC,EAAE;QAClB,OAAO,IAAI,CAAC;KACb;IAED,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QAClB,IAAI,GAAI,IAAY,EAAE,CAAC;KACxB;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,mBAAmB,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import {isClass, isString, nameOf, Store, Type} from \"@tsed/core\";\nimport {MONGOOSE_MODEL_NAME} from \"../constants\";\n\n/**\n * @ignore\n */\nexport function resolveRefType(type: string | (() => Type<any>) | Type<any>): string {\n if (isString(type)) {\n return type;\n }\n\n if (!isClass(type)) {\n type = (type as any)();\n }\n\n return Store.from(type).get(MONGOOSE_MODEL_NAME) || nameOf(type);\n}\n"]}
@@ -0,0 +1,54 @@
1
+ import { deepMerge, Store } from "@tsed/core";
2
+ import { MONGOOSE_SCHEMA_OPTIONS } from "../constants";
3
+ /**
4
+ * @ignore
5
+ */
6
+ export function schemaOptions(target, options) {
7
+ const store = Store.from(target);
8
+ if (!store.has(MONGOOSE_SCHEMA_OPTIONS)) {
9
+ store.set(MONGOOSE_SCHEMA_OPTIONS, {});
10
+ }
11
+ if (options) {
12
+ store.set(MONGOOSE_SCHEMA_OPTIONS, deepMerge(store.get(MONGOOSE_SCHEMA_OPTIONS), options));
13
+ }
14
+ return store.get(MONGOOSE_SCHEMA_OPTIONS);
15
+ }
16
+ function mapHookArgs(hook) {
17
+ return [hook.method, hook.options || hook.fn, hook.options ? hook.fn : undefined].filter(Boolean);
18
+ }
19
+ /**
20
+ * @ignore
21
+ */
22
+ export function buildPreHook(fn) {
23
+ return fn.length === 2
24
+ ? function (next) {
25
+ return fn(this, next);
26
+ }
27
+ : function (next, options) {
28
+ return fn(this, next, options);
29
+ };
30
+ }
31
+ /**
32
+ * @ignore
33
+ */
34
+ export function applySchemaOptions(schema, options) {
35
+ if (options.plugins) {
36
+ options.plugins.forEach((item) => schema.plugin(item.plugin, item.options));
37
+ }
38
+ if (options.indexes) {
39
+ options.indexes.forEach((item) => schema.index(item.fields, item.options));
40
+ }
41
+ if (options.pre) {
42
+ options.pre.forEach((item) => {
43
+ item = {
44
+ ...item,
45
+ fn: buildPreHook(item.fn)
46
+ };
47
+ schema.pre(...mapHookArgs(item));
48
+ });
49
+ }
50
+ if (options.post) {
51
+ options.post.forEach((item) => schema.post(...mapHookArgs(item)));
52
+ }
53
+ }
54
+ //# sourceMappingURL=schemaOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemaOptions.js","sourceRoot":"","sources":["../../../src/utils/schemaOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,SAAS,EAAE,KAAK,EAAC,MAAM,YAAY,CAAC;AAEzD,OAAO,EAAC,uBAAuB,EAAC,MAAM,cAAc,CAAC;AAGrD;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,MAAW,EAAE,OAA+B;IACxE,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAEjC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,EAAE;QACvC,KAAK,CAAC,GAAG,CAAC,uBAAuB,EAAE,EAAE,CAAC,CAAC;KACxC;IAED,IAAI,OAAO,EAAE;QACX,KAAK,CAAC,GAAG,CAAC,uBAAuB,EAAE,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;KAC5F;IAED,OAAO,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;AAC5C,CAAC;AAED,SAAS,WAAW,CAAC,IAAwC;IAC3D,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,OAA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AAC,IAAI,EAAE;QAChB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAE,MAAM,CAAC,IAAY,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KAC5E;AACH,CAAC","sourcesContent":["import {cleanObject, deepMerge, Store} from \"@tsed/core\";\nimport {Schema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA_OPTIONS} from \"../constants\";\nimport {MongooseNextCB, MongoosePostHook, MongoosePreHook, MongooseSchemaOptions} from \"../interfaces\";\n\n/**\n * @ignore\n */\nexport function schemaOptions(target: any, options?: MongooseSchemaOptions) {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA_OPTIONS)) {\n store.set(MONGOOSE_SCHEMA_OPTIONS, {});\n }\n\n if (options) {\n store.set(MONGOOSE_SCHEMA_OPTIONS, deepMerge(store.get(MONGOOSE_SCHEMA_OPTIONS), options));\n }\n\n return store.get(MONGOOSE_SCHEMA_OPTIONS);\n}\n\nfunction mapHookArgs(hook: MongoosePreHook | MongoosePostHook): [string | RegExp, Function] {\n return [hook.method, hook.options || hook.fn, hook.options ? hook.fn : undefined].filter(Boolean) as any;\n}\n\n/**\n * @ignore\n */\nexport function buildPreHook(fn: Function) {\n return fn.length === 2\n ? function (next: MongooseNextCB) {\n return fn(this, next);\n }\n : function (next: MongooseNextCB, options: any) {\n return fn(this, next, options);\n };\n}\n\n/**\n * @ignore\n */\nexport function applySchemaOptions(schema: Schema, options: MongooseSchemaOptions) {\n if (options.plugins) {\n options.plugins.forEach((item) => schema.plugin(item.plugin, item.options));\n }\n\n if (options.indexes) {\n options.indexes.forEach((item) => schema.index(item.fields, item.options));\n }\n\n if (options.pre) {\n options.pre.forEach((item) => {\n item = {\n ...item,\n fn: buildPreHook(item.fn)\n };\n\n (schema.pre as any)(...mapHookArgs(item));\n });\n }\n\n if (options.post) {\n options.post.forEach((item) => (schema.post as any)(...mapHookArgs(item)));\n }\n}\n"]}
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Calls schema.index() to define multiple indexes (most likely compound) for the schema.
3
+ *
4
+ * ### Example
5
+ *
6
+ * ```typescript
7
+ * @Model()
8
+ * @MongooseIndexes([{fields: {first: 1, second: 1}, options:{unique: 1}}, {fields: {first: 1, third: 1}, options:{unique: 1}}])
9
+ * export class EventModel {
10
+ *
11
+ * @Property()
12
+ * first: string;
13
+ *
14
+ * @Property()
15
+ * second: string;
16
+ *
17
+ * @Property()
18
+ * third: string;
19
+ *
20
+ * }
21
+ * ```
22
+ *
23
+ * @param indexes - define multiple mongoose indexes
24
+ * @returns {Function}
25
+ * @decorator
26
+ * @mongoose
27
+ * @class
28
+ */
29
+ export declare function MongooseIndexes(indexes: Array<{
30
+ fields: object;
31
+ options?: any;
32
+ }>): Function;