@warlock.js/cascade 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (506) hide show
  1. package/README.md +97 -0
  2. package/cjs/aggregate/DeselectPipeline.d.ts +10 -0
  3. package/cjs/aggregate/DeselectPipeline.d.ts.map +1 -0
  4. package/cjs/aggregate/DeselectPipeline.js +17 -0
  5. package/cjs/aggregate/DeselectPipeline.js.map +1 -0
  6. package/cjs/aggregate/GeoNearPipeline.d.ts +34 -0
  7. package/cjs/aggregate/GeoNearPipeline.d.ts.map +1 -0
  8. package/cjs/aggregate/GeoNearPipeline.js +44 -0
  9. package/cjs/aggregate/GeoNearPipeline.js.map +1 -0
  10. package/cjs/aggregate/GroupByPipeline.d.ts +12 -0
  11. package/cjs/aggregate/GroupByPipeline.d.ts.map +1 -0
  12. package/cjs/aggregate/GroupByPipeline.js +28 -0
  13. package/cjs/aggregate/GroupByPipeline.js.map +1 -0
  14. package/cjs/aggregate/LimitPipeline.d.ts +10 -0
  15. package/cjs/aggregate/LimitPipeline.d.ts.map +1 -0
  16. package/cjs/aggregate/LimitPipeline.js +14 -0
  17. package/cjs/aggregate/LimitPipeline.js.map +1 -0
  18. package/cjs/aggregate/LookupPipeline.d.ts +19 -0
  19. package/cjs/aggregate/LookupPipeline.d.ts.map +1 -0
  20. package/cjs/aggregate/LookupPipeline.js +19 -0
  21. package/cjs/aggregate/LookupPipeline.js.map +1 -0
  22. package/cjs/aggregate/OrWherePipeline.d.ts +23 -0
  23. package/cjs/aggregate/OrWherePipeline.d.ts.map +1 -0
  24. package/cjs/aggregate/OrWherePipeline.js +42 -0
  25. package/cjs/aggregate/OrWherePipeline.js.map +1 -0
  26. package/cjs/aggregate/SelectPipeline.d.ts +10 -0
  27. package/cjs/aggregate/SelectPipeline.d.ts.map +1 -0
  28. package/cjs/aggregate/SelectPipeline.js +26 -0
  29. package/cjs/aggregate/SelectPipeline.js.map +1 -0
  30. package/cjs/aggregate/SkipPipeline.d.ts +10 -0
  31. package/cjs/aggregate/SkipPipeline.d.ts.map +1 -0
  32. package/cjs/aggregate/SkipPipeline.js +14 -0
  33. package/cjs/aggregate/SkipPipeline.js.map +1 -0
  34. package/cjs/aggregate/SortByPipeline.d.ts +10 -0
  35. package/cjs/aggregate/SortByPipeline.d.ts.map +1 -0
  36. package/cjs/aggregate/SortByPipeline.js +18 -0
  37. package/cjs/aggregate/SortByPipeline.js.map +1 -0
  38. package/cjs/aggregate/SortPipeline.d.ts +11 -0
  39. package/cjs/aggregate/SortPipeline.d.ts.map +1 -0
  40. package/cjs/aggregate/SortPipeline.js +18 -0
  41. package/cjs/aggregate/SortPipeline.js.map +1 -0
  42. package/cjs/aggregate/SortRandomPipeline.d.ts +10 -0
  43. package/cjs/aggregate/SortRandomPipeline.d.ts.map +1 -0
  44. package/cjs/aggregate/SortRandomPipeline.js +16 -0
  45. package/cjs/aggregate/SortRandomPipeline.js.map +1 -0
  46. package/cjs/aggregate/UnwindPipeline.d.ts +14 -0
  47. package/cjs/aggregate/UnwindPipeline.d.ts.map +1 -0
  48. package/cjs/aggregate/UnwindPipeline.js +19 -0
  49. package/cjs/aggregate/UnwindPipeline.js.map +1 -0
  50. package/cjs/aggregate/WhereExpression.d.ts +17 -0
  51. package/cjs/aggregate/WhereExpression.d.ts.map +1 -0
  52. package/cjs/aggregate/WhereExpression.js +149 -0
  53. package/cjs/aggregate/WhereExpression.js.map +1 -0
  54. package/cjs/aggregate/WhereExpressionPipeline.d.ts +10 -0
  55. package/cjs/aggregate/WhereExpressionPipeline.d.ts.map +1 -0
  56. package/cjs/aggregate/WhereExpressionPipeline.js +15 -0
  57. package/cjs/aggregate/WhereExpressionPipeline.js.map +1 -0
  58. package/cjs/aggregate/WherePipeline.d.ts +13 -0
  59. package/cjs/aggregate/WherePipeline.d.ts.map +1 -0
  60. package/cjs/aggregate/WherePipeline.js +12 -0
  61. package/cjs/aggregate/WherePipeline.js.map +1 -0
  62. package/cjs/aggregate/aggregate.d.ts +346 -0
  63. package/cjs/aggregate/aggregate.d.ts.map +1 -0
  64. package/cjs/aggregate/aggregate.js +656 -0
  65. package/cjs/aggregate/aggregate.js.map +1 -0
  66. package/cjs/aggregate/expressions.d.ts +361 -0
  67. package/cjs/aggregate/expressions.d.ts.map +1 -0
  68. package/cjs/aggregate/expressions.js +536 -0
  69. package/cjs/aggregate/expressions.js.map +1 -0
  70. package/cjs/aggregate/index.d.ts +21 -0
  71. package/cjs/aggregate/index.d.ts.map +1 -0
  72. package/cjs/aggregate/parsePipelines.d.ts +5 -0
  73. package/cjs/aggregate/parsePipelines.d.ts.map +1 -0
  74. package/cjs/aggregate/parsePipelines.js +6 -0
  75. package/cjs/aggregate/parsePipelines.js.map +1 -0
  76. package/cjs/aggregate/pipeline.d.ts +31 -0
  77. package/cjs/aggregate/pipeline.d.ts.map +1 -0
  78. package/cjs/aggregate/pipeline.js +42 -0
  79. package/cjs/aggregate/pipeline.js.map +1 -0
  80. package/cjs/aggregate/types.d.ts +8 -0
  81. package/cjs/aggregate/types.d.ts.map +1 -0
  82. package/cjs/aggregate/types.js +31 -0
  83. package/cjs/aggregate/types.js.map +1 -0
  84. package/cjs/blueprint/blueprint.d.ts +154 -0
  85. package/cjs/blueprint/blueprint.d.ts.map +1 -0
  86. package/cjs/blueprint/blueprint.js +328 -0
  87. package/cjs/blueprint/blueprint.js.map +1 -0
  88. package/cjs/blueprint/index.d.ts +3 -0
  89. package/cjs/blueprint/index.d.ts.map +1 -0
  90. package/cjs/blueprint/model-blueprint.d.ts +15 -0
  91. package/cjs/blueprint/model-blueprint.d.ts.map +1 -0
  92. package/cjs/blueprint/model-blueprint.js +16 -0
  93. package/cjs/blueprint/model-blueprint.js.map +1 -0
  94. package/cjs/casts/arrayOf.d.ts +5 -0
  95. package/cjs/casts/arrayOf.d.ts.map +1 -0
  96. package/cjs/casts/arrayOf.js +18 -0
  97. package/cjs/casts/arrayOf.js.map +1 -0
  98. package/cjs/casts/cast-email.d.ts +2 -0
  99. package/cjs/casts/cast-email.d.ts.map +1 -0
  100. package/cjs/casts/cast-email.js +5 -0
  101. package/cjs/casts/cast-email.js.map +1 -0
  102. package/cjs/casts/castModel.d.ts +3 -0
  103. package/cjs/casts/castModel.d.ts.map +1 -0
  104. package/cjs/casts/castModel.js +49 -0
  105. package/cjs/casts/castModel.js.map +1 -0
  106. package/cjs/casts/expiresAfter.d.ts +3 -0
  107. package/cjs/casts/expiresAfter.d.ts.map +1 -0
  108. package/cjs/casts/expiresAfter.js +5 -0
  109. package/cjs/casts/expiresAfter.js.map +1 -0
  110. package/cjs/casts/index.d.ts +9 -0
  111. package/cjs/casts/index.d.ts.map +1 -0
  112. package/cjs/casts/oneOf.d.ts +3 -0
  113. package/cjs/casts/oneOf.d.ts.map +1 -0
  114. package/cjs/casts/oneOf.js +22 -0
  115. package/cjs/casts/oneOf.js.map +1 -0
  116. package/cjs/casts/random-string.d.ts +2 -0
  117. package/cjs/casts/random-string.d.ts.map +1 -0
  118. package/cjs/casts/random-string.js +5 -0
  119. package/cjs/casts/random-string.js.map +1 -0
  120. package/cjs/casts/randomInteger.d.ts +2 -0
  121. package/cjs/casts/randomInteger.d.ts.map +1 -0
  122. package/cjs/casts/randomInteger.js +7 -0
  123. package/cjs/casts/randomInteger.js.map +1 -0
  124. package/cjs/casts/shapedArray.d.ts +26 -0
  125. package/cjs/casts/shapedArray.d.ts.map +1 -0
  126. package/cjs/casts/shapedArray.js +100 -0
  127. package/cjs/casts/shapedArray.js.map +1 -0
  128. package/cjs/config.d.ts +4 -0
  129. package/cjs/config.d.ts.map +1 -0
  130. package/cjs/config.js +10 -0
  131. package/cjs/config.js.map +1 -0
  132. package/cjs/connection.d.ts +44 -0
  133. package/cjs/connection.d.ts.map +1 -0
  134. package/cjs/connection.js +98 -0
  135. package/cjs/connection.js.map +1 -0
  136. package/cjs/console/db-indexes-command.d.ts +3 -0
  137. package/cjs/console/db-indexes-command.d.ts.map +1 -0
  138. package/cjs/console/db-indexes-command.js +18 -0
  139. package/cjs/console/db-indexes-command.js.map +1 -0
  140. package/cjs/console/index.d.ts +4 -0
  141. package/cjs/console/index.d.ts.map +1 -0
  142. package/cjs/console/migrate-command.d.ts +3 -0
  143. package/cjs/console/migrate-command.d.ts.map +1 -0
  144. package/cjs/console/migrate-command.js +14 -0
  145. package/cjs/console/migrate-command.js.map +1 -0
  146. package/cjs/console/register-blueprint-command.d.ts +4 -0
  147. package/cjs/console/register-blueprint-command.d.ts.map +1 -0
  148. package/cjs/console/register-blueprint-command.js +27 -0
  149. package/cjs/console/register-blueprint-command.js.map +1 -0
  150. package/cjs/database.d.ts +49 -0
  151. package/cjs/database.d.ts.map +1 -0
  152. package/cjs/database.js +85 -0
  153. package/cjs/database.js.map +1 -0
  154. package/cjs/index.d.ts +14 -0
  155. package/cjs/index.d.ts.map +1 -0
  156. package/cjs/index.js +1 -0
  157. package/cjs/index.js.map +1 -0
  158. package/cjs/migrate.d.ts +8 -0
  159. package/cjs/migrate.d.ts.map +1 -0
  160. package/cjs/migrate.js +89 -0
  161. package/cjs/migrate.js.map +1 -0
  162. package/cjs/model/ModelAggregate.d.ts +71 -0
  163. package/cjs/model/ModelAggregate.d.ts.map +1 -0
  164. package/cjs/model/ModelAggregate.js +147 -0
  165. package/cjs/model/ModelAggregate.js.map +1 -0
  166. package/cjs/model/ModelSync.d.ts +74 -0
  167. package/cjs/model/ModelSync.d.ts.map +1 -0
  168. package/cjs/model/ModelSync.js +215 -0
  169. package/cjs/model/ModelSync.js.map +1 -0
  170. package/cjs/model/RelationshipWithMany.d.ts +29 -0
  171. package/cjs/model/RelationshipWithMany.d.ts.map +1 -0
  172. package/cjs/model/RelationshipWithMany.js +52 -0
  173. package/cjs/model/RelationshipWithMany.js.map +1 -0
  174. package/cjs/model/base-model.d.ts +148 -0
  175. package/cjs/model/base-model.d.ts.map +1 -0
  176. package/cjs/model/base-model.js +212 -0
  177. package/cjs/model/base-model.js.map +1 -0
  178. package/cjs/model/crud-model.d.ts +117 -0
  179. package/cjs/model/crud-model.d.ts.map +1 -0
  180. package/cjs/model/crud-model.js +346 -0
  181. package/cjs/model/crud-model.js.map +1 -0
  182. package/cjs/model/index.d.ts +11 -0
  183. package/cjs/model/index.d.ts.map +1 -0
  184. package/cjs/model/joinable.d.ts +86 -0
  185. package/cjs/model/joinable.d.ts.map +1 -0
  186. package/cjs/model/joinable.js +155 -0
  187. package/cjs/model/joinable.js.map +1 -0
  188. package/cjs/model/master-mind.d.ts +44 -0
  189. package/cjs/model/master-mind.d.ts.map +1 -0
  190. package/cjs/model/master-mind.js +129 -0
  191. package/cjs/model/master-mind.js.map +1 -0
  192. package/cjs/model/migration-office.d.ts +33 -0
  193. package/cjs/model/migration-office.d.ts.map +1 -0
  194. package/cjs/model/migration-office.js +55 -0
  195. package/cjs/model/migration-office.js.map +1 -0
  196. package/cjs/model/model-events.d.ts +57 -0
  197. package/cjs/model/model-events.d.ts.map +1 -0
  198. package/cjs/model/model-events.js +102 -0
  199. package/cjs/model/model-events.js.map +1 -0
  200. package/cjs/model/model.d.ts +323 -0
  201. package/cjs/model/model.d.ts.map +1 -0
  202. package/cjs/model/model.js +843 -0
  203. package/cjs/model/model.js.map +1 -0
  204. package/cjs/model/relationships.d.ts +69 -0
  205. package/cjs/model/relationships.d.ts.map +1 -0
  206. package/cjs/model/relationships.js +144 -0
  207. package/cjs/model/relationships.js.map +1 -0
  208. package/cjs/model/types.d.ts +113 -0
  209. package/cjs/model/types.d.ts.map +1 -0
  210. package/cjs/model/types.js +11 -0
  211. package/cjs/model/types.js.map +1 -0
  212. package/cjs/models/Log.d.ts +12 -0
  213. package/cjs/models/Log.d.ts.map +1 -0
  214. package/cjs/models/Log.js +16 -0
  215. package/cjs/models/Log.js.map +1 -0
  216. package/cjs/models/index.d.ts +2 -0
  217. package/cjs/models/index.d.ts.map +1 -0
  218. package/cjs/query/index.d.ts +2 -0
  219. package/cjs/query/index.d.ts.map +1 -0
  220. package/cjs/query/query.d.ts +228 -0
  221. package/cjs/query/query.d.ts.map +1 -0
  222. package/cjs/query/query.js +675 -0
  223. package/cjs/query/query.js.map +1 -0
  224. package/cjs/query/types.d.ts +56 -0
  225. package/cjs/query/types.d.ts.map +1 -0
  226. package/cjs/types.d.ts +46 -0
  227. package/cjs/types.d.ts.map +1 -0
  228. package/cjs/utils/DatabaseLog.d.ts +25 -0
  229. package/cjs/utils/DatabaseLog.d.ts.map +1 -0
  230. package/cjs/utils/DatabaseLog.js +41 -0
  231. package/cjs/utils/DatabaseLog.js.map +1 -0
  232. package/cjs/utils/connectToDatabase.d.ts +3 -0
  233. package/cjs/utils/connectToDatabase.d.ts.map +1 -0
  234. package/cjs/utils/connectToDatabase.js +3 -0
  235. package/cjs/utils/connectToDatabase.js.map +1 -0
  236. package/cjs/utils/dropAllDatabaseIndexes.d.ts +2 -0
  237. package/cjs/utils/dropAllDatabaseIndexes.d.ts.map +1 -0
  238. package/cjs/utils/dropAllDatabaseIndexes.js +10 -0
  239. package/cjs/utils/dropAllDatabaseIndexes.js.map +1 -0
  240. package/cjs/utils/index.d.ts +6 -0
  241. package/cjs/utils/index.d.ts.map +1 -0
  242. package/cjs/utils/joinable-proxy.d.ts +3 -0
  243. package/cjs/utils/joinable-proxy.d.ts.map +1 -0
  244. package/cjs/utils/joinable-proxy.js +14 -0
  245. package/cjs/utils/joinable-proxy.js.map +1 -0
  246. package/cjs/utils/listDatabaseIndexes.d.ts +4 -0
  247. package/cjs/utils/listDatabaseIndexes.d.ts.map +1 -0
  248. package/cjs/utils/listDatabaseIndexes.js +101 -0
  249. package/cjs/utils/listDatabaseIndexes.js.map +1 -0
  250. package/cjs/utils/onceConnected.d.ts +2 -0
  251. package/cjs/utils/onceConnected.d.ts.map +1 -0
  252. package/cjs/utils/onceConnected.js +8 -0
  253. package/cjs/utils/onceConnected.js.map +1 -0
  254. package/esm/aggregate/DeselectPipeline.d.ts +10 -0
  255. package/esm/aggregate/DeselectPipeline.d.ts.map +1 -0
  256. package/esm/aggregate/DeselectPipeline.js +17 -0
  257. package/esm/aggregate/DeselectPipeline.js.map +1 -0
  258. package/esm/aggregate/GeoNearPipeline.d.ts +34 -0
  259. package/esm/aggregate/GeoNearPipeline.d.ts.map +1 -0
  260. package/esm/aggregate/GeoNearPipeline.js +44 -0
  261. package/esm/aggregate/GeoNearPipeline.js.map +1 -0
  262. package/esm/aggregate/GroupByPipeline.d.ts +12 -0
  263. package/esm/aggregate/GroupByPipeline.d.ts.map +1 -0
  264. package/esm/aggregate/GroupByPipeline.js +28 -0
  265. package/esm/aggregate/GroupByPipeline.js.map +1 -0
  266. package/esm/aggregate/LimitPipeline.d.ts +10 -0
  267. package/esm/aggregate/LimitPipeline.d.ts.map +1 -0
  268. package/esm/aggregate/LimitPipeline.js +14 -0
  269. package/esm/aggregate/LimitPipeline.js.map +1 -0
  270. package/esm/aggregate/LookupPipeline.d.ts +19 -0
  271. package/esm/aggregate/LookupPipeline.d.ts.map +1 -0
  272. package/esm/aggregate/LookupPipeline.js +19 -0
  273. package/esm/aggregate/LookupPipeline.js.map +1 -0
  274. package/esm/aggregate/OrWherePipeline.d.ts +23 -0
  275. package/esm/aggregate/OrWherePipeline.d.ts.map +1 -0
  276. package/esm/aggregate/OrWherePipeline.js +42 -0
  277. package/esm/aggregate/OrWherePipeline.js.map +1 -0
  278. package/esm/aggregate/SelectPipeline.d.ts +10 -0
  279. package/esm/aggregate/SelectPipeline.d.ts.map +1 -0
  280. package/esm/aggregate/SelectPipeline.js +26 -0
  281. package/esm/aggregate/SelectPipeline.js.map +1 -0
  282. package/esm/aggregate/SkipPipeline.d.ts +10 -0
  283. package/esm/aggregate/SkipPipeline.d.ts.map +1 -0
  284. package/esm/aggregate/SkipPipeline.js +14 -0
  285. package/esm/aggregate/SkipPipeline.js.map +1 -0
  286. package/esm/aggregate/SortByPipeline.d.ts +10 -0
  287. package/esm/aggregate/SortByPipeline.d.ts.map +1 -0
  288. package/esm/aggregate/SortByPipeline.js +18 -0
  289. package/esm/aggregate/SortByPipeline.js.map +1 -0
  290. package/esm/aggregate/SortPipeline.d.ts +11 -0
  291. package/esm/aggregate/SortPipeline.d.ts.map +1 -0
  292. package/esm/aggregate/SortPipeline.js +18 -0
  293. package/esm/aggregate/SortPipeline.js.map +1 -0
  294. package/esm/aggregate/SortRandomPipeline.d.ts +10 -0
  295. package/esm/aggregate/SortRandomPipeline.d.ts.map +1 -0
  296. package/esm/aggregate/SortRandomPipeline.js +16 -0
  297. package/esm/aggregate/SortRandomPipeline.js.map +1 -0
  298. package/esm/aggregate/UnwindPipeline.d.ts +14 -0
  299. package/esm/aggregate/UnwindPipeline.d.ts.map +1 -0
  300. package/esm/aggregate/UnwindPipeline.js +19 -0
  301. package/esm/aggregate/UnwindPipeline.js.map +1 -0
  302. package/esm/aggregate/WhereExpression.d.ts +17 -0
  303. package/esm/aggregate/WhereExpression.d.ts.map +1 -0
  304. package/esm/aggregate/WhereExpression.js +149 -0
  305. package/esm/aggregate/WhereExpression.js.map +1 -0
  306. package/esm/aggregate/WhereExpressionPipeline.d.ts +10 -0
  307. package/esm/aggregate/WhereExpressionPipeline.d.ts.map +1 -0
  308. package/esm/aggregate/WhereExpressionPipeline.js +15 -0
  309. package/esm/aggregate/WhereExpressionPipeline.js.map +1 -0
  310. package/esm/aggregate/WherePipeline.d.ts +13 -0
  311. package/esm/aggregate/WherePipeline.d.ts.map +1 -0
  312. package/esm/aggregate/WherePipeline.js +12 -0
  313. package/esm/aggregate/WherePipeline.js.map +1 -0
  314. package/esm/aggregate/aggregate.d.ts +346 -0
  315. package/esm/aggregate/aggregate.d.ts.map +1 -0
  316. package/esm/aggregate/aggregate.js +656 -0
  317. package/esm/aggregate/aggregate.js.map +1 -0
  318. package/esm/aggregate/expressions.d.ts +361 -0
  319. package/esm/aggregate/expressions.d.ts.map +1 -0
  320. package/esm/aggregate/expressions.js +536 -0
  321. package/esm/aggregate/expressions.js.map +1 -0
  322. package/esm/aggregate/index.d.ts +21 -0
  323. package/esm/aggregate/index.d.ts.map +1 -0
  324. package/esm/aggregate/parsePipelines.d.ts +5 -0
  325. package/esm/aggregate/parsePipelines.d.ts.map +1 -0
  326. package/esm/aggregate/parsePipelines.js +6 -0
  327. package/esm/aggregate/parsePipelines.js.map +1 -0
  328. package/esm/aggregate/pipeline.d.ts +31 -0
  329. package/esm/aggregate/pipeline.d.ts.map +1 -0
  330. package/esm/aggregate/pipeline.js +42 -0
  331. package/esm/aggregate/pipeline.js.map +1 -0
  332. package/esm/aggregate/types.d.ts +8 -0
  333. package/esm/aggregate/types.d.ts.map +1 -0
  334. package/esm/aggregate/types.js +31 -0
  335. package/esm/aggregate/types.js.map +1 -0
  336. package/esm/blueprint/blueprint.d.ts +154 -0
  337. package/esm/blueprint/blueprint.d.ts.map +1 -0
  338. package/esm/blueprint/blueprint.js +328 -0
  339. package/esm/blueprint/blueprint.js.map +1 -0
  340. package/esm/blueprint/index.d.ts +3 -0
  341. package/esm/blueprint/index.d.ts.map +1 -0
  342. package/esm/blueprint/model-blueprint.d.ts +15 -0
  343. package/esm/blueprint/model-blueprint.d.ts.map +1 -0
  344. package/esm/blueprint/model-blueprint.js +16 -0
  345. package/esm/blueprint/model-blueprint.js.map +1 -0
  346. package/esm/casts/arrayOf.d.ts +5 -0
  347. package/esm/casts/arrayOf.d.ts.map +1 -0
  348. package/esm/casts/arrayOf.js +18 -0
  349. package/esm/casts/arrayOf.js.map +1 -0
  350. package/esm/casts/cast-email.d.ts +2 -0
  351. package/esm/casts/cast-email.d.ts.map +1 -0
  352. package/esm/casts/cast-email.js +5 -0
  353. package/esm/casts/cast-email.js.map +1 -0
  354. package/esm/casts/castModel.d.ts +3 -0
  355. package/esm/casts/castModel.d.ts.map +1 -0
  356. package/esm/casts/castModel.js +49 -0
  357. package/esm/casts/castModel.js.map +1 -0
  358. package/esm/casts/expiresAfter.d.ts +3 -0
  359. package/esm/casts/expiresAfter.d.ts.map +1 -0
  360. package/esm/casts/expiresAfter.js +5 -0
  361. package/esm/casts/expiresAfter.js.map +1 -0
  362. package/esm/casts/index.d.ts +9 -0
  363. package/esm/casts/index.d.ts.map +1 -0
  364. package/esm/casts/oneOf.d.ts +3 -0
  365. package/esm/casts/oneOf.d.ts.map +1 -0
  366. package/esm/casts/oneOf.js +22 -0
  367. package/esm/casts/oneOf.js.map +1 -0
  368. package/esm/casts/random-string.d.ts +2 -0
  369. package/esm/casts/random-string.d.ts.map +1 -0
  370. package/esm/casts/random-string.js +5 -0
  371. package/esm/casts/random-string.js.map +1 -0
  372. package/esm/casts/randomInteger.d.ts +2 -0
  373. package/esm/casts/randomInteger.d.ts.map +1 -0
  374. package/esm/casts/randomInteger.js +7 -0
  375. package/esm/casts/randomInteger.js.map +1 -0
  376. package/esm/casts/shapedArray.d.ts +26 -0
  377. package/esm/casts/shapedArray.d.ts.map +1 -0
  378. package/esm/casts/shapedArray.js +100 -0
  379. package/esm/casts/shapedArray.js.map +1 -0
  380. package/esm/config.d.ts +4 -0
  381. package/esm/config.d.ts.map +1 -0
  382. package/esm/config.js +10 -0
  383. package/esm/config.js.map +1 -0
  384. package/esm/connection.d.ts +44 -0
  385. package/esm/connection.d.ts.map +1 -0
  386. package/esm/connection.js +98 -0
  387. package/esm/connection.js.map +1 -0
  388. package/esm/console/db-indexes-command.d.ts +3 -0
  389. package/esm/console/db-indexes-command.d.ts.map +1 -0
  390. package/esm/console/db-indexes-command.js +18 -0
  391. package/esm/console/db-indexes-command.js.map +1 -0
  392. package/esm/console/index.d.ts +4 -0
  393. package/esm/console/index.d.ts.map +1 -0
  394. package/esm/console/migrate-command.d.ts +3 -0
  395. package/esm/console/migrate-command.d.ts.map +1 -0
  396. package/esm/console/migrate-command.js +14 -0
  397. package/esm/console/migrate-command.js.map +1 -0
  398. package/esm/console/register-blueprint-command.d.ts +4 -0
  399. package/esm/console/register-blueprint-command.d.ts.map +1 -0
  400. package/esm/console/register-blueprint-command.js +27 -0
  401. package/esm/console/register-blueprint-command.js.map +1 -0
  402. package/esm/database.d.ts +49 -0
  403. package/esm/database.d.ts.map +1 -0
  404. package/esm/database.js +85 -0
  405. package/esm/database.js.map +1 -0
  406. package/esm/index.d.ts +14 -0
  407. package/esm/index.d.ts.map +1 -0
  408. package/esm/index.js +1 -0
  409. package/esm/index.js.map +1 -0
  410. package/esm/migrate.d.ts +8 -0
  411. package/esm/migrate.d.ts.map +1 -0
  412. package/esm/migrate.js +89 -0
  413. package/esm/migrate.js.map +1 -0
  414. package/esm/model/ModelAggregate.d.ts +71 -0
  415. package/esm/model/ModelAggregate.d.ts.map +1 -0
  416. package/esm/model/ModelAggregate.js +147 -0
  417. package/esm/model/ModelAggregate.js.map +1 -0
  418. package/esm/model/ModelSync.d.ts +74 -0
  419. package/esm/model/ModelSync.d.ts.map +1 -0
  420. package/esm/model/ModelSync.js +215 -0
  421. package/esm/model/ModelSync.js.map +1 -0
  422. package/esm/model/RelationshipWithMany.d.ts +29 -0
  423. package/esm/model/RelationshipWithMany.d.ts.map +1 -0
  424. package/esm/model/RelationshipWithMany.js +52 -0
  425. package/esm/model/RelationshipWithMany.js.map +1 -0
  426. package/esm/model/base-model.d.ts +148 -0
  427. package/esm/model/base-model.d.ts.map +1 -0
  428. package/esm/model/base-model.js +212 -0
  429. package/esm/model/base-model.js.map +1 -0
  430. package/esm/model/crud-model.d.ts +117 -0
  431. package/esm/model/crud-model.d.ts.map +1 -0
  432. package/esm/model/crud-model.js +346 -0
  433. package/esm/model/crud-model.js.map +1 -0
  434. package/esm/model/index.d.ts +11 -0
  435. package/esm/model/index.d.ts.map +1 -0
  436. package/esm/model/joinable.d.ts +86 -0
  437. package/esm/model/joinable.d.ts.map +1 -0
  438. package/esm/model/joinable.js +155 -0
  439. package/esm/model/joinable.js.map +1 -0
  440. package/esm/model/master-mind.d.ts +44 -0
  441. package/esm/model/master-mind.d.ts.map +1 -0
  442. package/esm/model/master-mind.js +129 -0
  443. package/esm/model/master-mind.js.map +1 -0
  444. package/esm/model/migration-office.d.ts +33 -0
  445. package/esm/model/migration-office.d.ts.map +1 -0
  446. package/esm/model/migration-office.js +55 -0
  447. package/esm/model/migration-office.js.map +1 -0
  448. package/esm/model/model-events.d.ts +57 -0
  449. package/esm/model/model-events.d.ts.map +1 -0
  450. package/esm/model/model-events.js +102 -0
  451. package/esm/model/model-events.js.map +1 -0
  452. package/esm/model/model.d.ts +323 -0
  453. package/esm/model/model.d.ts.map +1 -0
  454. package/esm/model/model.js +843 -0
  455. package/esm/model/model.js.map +1 -0
  456. package/esm/model/relationships.d.ts +69 -0
  457. package/esm/model/relationships.d.ts.map +1 -0
  458. package/esm/model/relationships.js +144 -0
  459. package/esm/model/relationships.js.map +1 -0
  460. package/esm/model/types.d.ts +113 -0
  461. package/esm/model/types.d.ts.map +1 -0
  462. package/esm/model/types.js +11 -0
  463. package/esm/model/types.js.map +1 -0
  464. package/esm/models/Log.d.ts +12 -0
  465. package/esm/models/Log.d.ts.map +1 -0
  466. package/esm/models/Log.js +16 -0
  467. package/esm/models/Log.js.map +1 -0
  468. package/esm/models/index.d.ts +2 -0
  469. package/esm/models/index.d.ts.map +1 -0
  470. package/esm/query/index.d.ts +2 -0
  471. package/esm/query/index.d.ts.map +1 -0
  472. package/esm/query/query.d.ts +228 -0
  473. package/esm/query/query.d.ts.map +1 -0
  474. package/esm/query/query.js +675 -0
  475. package/esm/query/query.js.map +1 -0
  476. package/esm/query/types.d.ts +56 -0
  477. package/esm/query/types.d.ts.map +1 -0
  478. package/esm/types.d.ts +46 -0
  479. package/esm/types.d.ts.map +1 -0
  480. package/esm/utils/DatabaseLog.d.ts +25 -0
  481. package/esm/utils/DatabaseLog.d.ts.map +1 -0
  482. package/esm/utils/DatabaseLog.js +41 -0
  483. package/esm/utils/DatabaseLog.js.map +1 -0
  484. package/esm/utils/connectToDatabase.d.ts +3 -0
  485. package/esm/utils/connectToDatabase.d.ts.map +1 -0
  486. package/esm/utils/connectToDatabase.js +3 -0
  487. package/esm/utils/connectToDatabase.js.map +1 -0
  488. package/esm/utils/dropAllDatabaseIndexes.d.ts +2 -0
  489. package/esm/utils/dropAllDatabaseIndexes.d.ts.map +1 -0
  490. package/esm/utils/dropAllDatabaseIndexes.js +10 -0
  491. package/esm/utils/dropAllDatabaseIndexes.js.map +1 -0
  492. package/esm/utils/index.d.ts +6 -0
  493. package/esm/utils/index.d.ts.map +1 -0
  494. package/esm/utils/joinable-proxy.d.ts +3 -0
  495. package/esm/utils/joinable-proxy.d.ts.map +1 -0
  496. package/esm/utils/joinable-proxy.js +14 -0
  497. package/esm/utils/joinable-proxy.js.map +1 -0
  498. package/esm/utils/listDatabaseIndexes.d.ts +4 -0
  499. package/esm/utils/listDatabaseIndexes.d.ts.map +1 -0
  500. package/esm/utils/listDatabaseIndexes.js +101 -0
  501. package/esm/utils/listDatabaseIndexes.js.map +1 -0
  502. package/esm/utils/onceConnected.d.ts +2 -0
  503. package/esm/utils/onceConnected.d.ts.map +1 -0
  504. package/esm/utils/onceConnected.js +8 -0
  505. package/esm/utils/onceConnected.js.map +1 -0
  506. package/package.json +61 -0
@@ -0,0 +1,843 @@
1
+ 'use strict';var reinforcements=require('@mongez/reinforcements'),supportiveIs=require('@mongez/supportive-is'),timeWizard=require('@mongez/time-wizard'),dayjs=require('dayjs'),mongodb=require('mongodb');require('../casts/arrayOf.js');var castModel=require('../casts/castModel.js'),oneOf=require('../casts/oneOf.js');require('../casts/shapedArray.js');var relationships=require('./relationships.js'),types=require('./types.js');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var dayjs__default=/*#__PURE__*/_interopDefault(dayjs);class Model
2
+ // <
3
+ // ModelDocument extends Document = any,
4
+ // ModelSchema extends Schema<ModelDocument> = any,
5
+ // >
6
+ extends relationships.RelationshipModel {
7
+ /**
8
+ * Model Initial Document data
9
+ */
10
+ initialData = {};
11
+ /**
12
+ * Model Document data
13
+ */
14
+ data;
15
+ /**
16
+ * Define Default value data that will be merged with the models' data
17
+ * on the create process
18
+ */
19
+ defaultValue = {};
20
+ /**
21
+ * A flag to determine if the model is being restored
22
+ */
23
+ isRestored = false;
24
+ /**
25
+ * Model casts types
26
+ */
27
+ casts = {};
28
+ /**
29
+ * Set custom casts that will be used to cast the model's data are not related to the current value of the collection's column
30
+ *
31
+ * For example: `name` is not a column in the given data, but it will be concatenation of `firstName` and `lastName`
32
+ */
33
+ customCasts = {};
34
+ /**
35
+ * Guarded fields
36
+ */
37
+ guarded = [];
38
+ /**
39
+ * Fillable fields
40
+ */
41
+ filled = [];
42
+ /**
43
+ * Embedded columns
44
+ */
45
+ embedded = [];
46
+ /**
47
+ * Embed all columns except the given columns
48
+ */
49
+ embedAllExcept = [];
50
+ /**
51
+ * Embed all columns except timestamps and created|updated|deleted by columns
52
+ */
53
+ embedAllExceptTimestampsAndUserColumns = false;
54
+ /**
55
+ * Created at column
56
+ */
57
+ createdAtColumn = "createdAt";
58
+ /**
59
+ * Updated at column
60
+ */
61
+ updatedAtColumn = "updatedAt";
62
+ /**
63
+ * Deleted at column
64
+ */
65
+ deletedAtColumn = "deletedAt";
66
+ /**
67
+ * Created by column
68
+ */
69
+ createdByColumn = "createdBy";
70
+ /**
71
+ * Updated by column
72
+ */
73
+ updatedByColumn = "updatedBy";
74
+ /**
75
+ * Deleted by column
76
+ */
77
+ deletedByColumn = "deletedBy";
78
+ /**
79
+ * Date format
80
+ */
81
+ dateFormat = "DD-MM-YYYY";
82
+ /**
83
+ * A flag to determine if the id is auto generated not added manually
84
+ */
85
+ autoGeneratedId = false;
86
+ /**
87
+ * Is active column
88
+ */
89
+ isActiveColumn = "isActive";
90
+ /**
91
+ * Original data
92
+ */
93
+ originalData = {};
94
+ /**
95
+ * Constructor
96
+ */
97
+ constructor(originalData = {}) {
98
+ super();
99
+ if (originalData instanceof Model) {
100
+ this.originalData = reinforcements.clone(originalData.data);
101
+ }
102
+ else {
103
+ this.originalData = reinforcements.clone(originalData);
104
+ }
105
+ if (typeof originalData?._id === "string") {
106
+ try {
107
+ originalData._id = new mongodb.ObjectId(originalData._id);
108
+ }
109
+ catch (error) {
110
+ originalData._id = new mongodb.ObjectId();
111
+ }
112
+ }
113
+ this.data = reinforcements.clone(this.originalData);
114
+ this.initialData = reinforcements.clone(this.originalData);
115
+ // this is necessary as clone() will generate a new _id for the data
116
+ // so we need to keep the original _id
117
+ if (originalData?._id) {
118
+ this.originalData._id = new mongodb.ObjectId(originalData._id);
119
+ this.data._id = new mongodb.ObjectId(originalData._id);
120
+ this.initialData._id = new mongodb.ObjectId(originalData._id);
121
+ }
122
+ }
123
+ /**
124
+ * Get save columns which are the casts keys
125
+ */
126
+ get castColumns() {
127
+ return Object.keys(this.casts);
128
+ }
129
+ /**
130
+ * Get value from original data
131
+ */
132
+ original(key, defaultValue) {
133
+ return reinforcements.get(this.originalData, key, defaultValue);
134
+ }
135
+ /**
136
+ * Get all data except the guarded fields
137
+ */
138
+ get publicData() {
139
+ return reinforcements.except(this.data, this.guarded);
140
+ }
141
+ /**
142
+ * Get guarded data
143
+ */
144
+ get guardedData() {
145
+ return reinforcements.only(this.data, this.guarded);
146
+ }
147
+ /**
148
+ * Get the model's id
149
+ */
150
+ get id() {
151
+ return this.get("id");
152
+ }
153
+ /**
154
+ * Check if current user is active
155
+ */
156
+ get isActive() {
157
+ return Boolean(this.get(this.isActiveColumn));
158
+ }
159
+ /**
160
+ * Get mongodb id
161
+ */
162
+ get _id() {
163
+ return this.get("_id");
164
+ }
165
+ /**
166
+ * Mark the current model as being restored
167
+ */
168
+ markAsRestored() {
169
+ this.isRestored = true;
170
+ }
171
+ /**
172
+ * Set a column in the model data
173
+ */
174
+ set(column, value) {
175
+ this.data = reinforcements.set(this.data, column, value);
176
+ return this;
177
+ }
178
+ /**
179
+ * Increment the given column by the given value
180
+ */
181
+ increment(column, value = 1) {
182
+ return this.set(column, this.get(column, 0) + value);
183
+ }
184
+ /**
185
+ * Decrement the given column by the given value
186
+ */
187
+ decrement(column, value = 1) {
188
+ return this.set(column, this.get(column, 0) - value);
189
+ }
190
+ /**
191
+ * Get initial value of the given column
192
+ */
193
+ getInitial(column, defaultValue) {
194
+ return reinforcements.get(this.initialData, column, defaultValue);
195
+ }
196
+ /**
197
+ * Get value of the given column
198
+ */
199
+ get(column, defaultValue) {
200
+ return reinforcements.get(this.data, column, defaultValue);
201
+ }
202
+ /**
203
+ * Return the value of the given column as a string
204
+ */
205
+ string(column, defaultValue) {
206
+ return String(this.get(column, defaultValue));
207
+ }
208
+ /**
209
+ * Return the value of the given column as an integer
210
+ */
211
+ int(column, defaultValue) {
212
+ return parseInt(this.get(column, defaultValue));
213
+ }
214
+ /**
215
+ * Return the value of the given column as a float
216
+ */
217
+ float(column, defaultValue) {
218
+ return parseFloat(this.get(column, defaultValue));
219
+ }
220
+ /**
221
+ * Return the value of the given column as a number
222
+ */
223
+ number(column, defaultValue) {
224
+ return Number(this.get(column, defaultValue));
225
+ }
226
+ /**
227
+ * Return the value of the given column as a boolean
228
+ */
229
+ bool(column, defaultValue) {
230
+ return Boolean(this.get(column, defaultValue));
231
+ }
232
+ /**
233
+ * Determine whether the given column exists in the document
234
+ */
235
+ has(column) {
236
+ return reinforcements.get(this.data, column) !== undefined;
237
+ }
238
+ /**
239
+ * Get all columns except the given ones
240
+ */
241
+ except(columns) {
242
+ return reinforcements.except(this.data, columns);
243
+ }
244
+ /**
245
+ * Get only the given columns
246
+ */
247
+ only(columns) {
248
+ return reinforcements.only(this.data, columns);
249
+ }
250
+ /**
251
+ * Get only id
252
+ */
253
+ get onlyId() {
254
+ return this.only(["id"]);
255
+ }
256
+ /**
257
+ * Unset or remove the given columns from the data
258
+ */
259
+ unset(...columns) {
260
+ this.data = reinforcements.except(this.data, columns);
261
+ return this;
262
+ }
263
+ /**
264
+ * Replace the entire document data with the given new data
265
+ */
266
+ replaceWith(data) {
267
+ if (!data.id && this.data.id) {
268
+ data.id = this.data.id;
269
+ }
270
+ if (!data._id && this.data._id) {
271
+ data._id = this.data._id;
272
+ }
273
+ this.data = data;
274
+ return this;
275
+ }
276
+ /**
277
+ * Merge the given documents to current document
278
+ */
279
+ merge(data) {
280
+ this.data = reinforcements.merge(this.data, data);
281
+ return this;
282
+ }
283
+ /**
284
+ * Push the given values to the given column
285
+ * If the given column does not exists, it will be created
286
+ * If the given value exists but not an array it will be ignored
287
+ */
288
+ push(column, ...values) {
289
+ const currentValue = this.get(column);
290
+ if (Array.isArray(currentValue)) {
291
+ this.set(column, [...currentValue, ...values]);
292
+ }
293
+ else if (!currentValue) {
294
+ this.set(column, values);
295
+ }
296
+ return this;
297
+ }
298
+ /**
299
+ * Push the given values to the given column only if not exists
300
+ */
301
+ pushOnce(column, ...values) {
302
+ const currentValue = this.get(column);
303
+ if (Array.isArray(currentValue)) {
304
+ const newValues = Array.from(new Set([...currentValue, ...values]));
305
+ this.set(column, newValues);
306
+ }
307
+ else if (!currentValue) {
308
+ this.set(column, values);
309
+ }
310
+ return this;
311
+ }
312
+ /**
313
+ * Add the given values to the beginning of the given column
314
+ * If the given column does not exists, it will be created
315
+ * If the given value exists but not an array it will be ignored
316
+ */
317
+ unshift(column, ...values) {
318
+ const currentValue = this.get(column);
319
+ if (Array.isArray(currentValue)) {
320
+ this.set(column, [...values, ...currentValue]);
321
+ }
322
+ else if (!currentValue) {
323
+ this.set(column, values);
324
+ }
325
+ return this;
326
+ }
327
+ /**
328
+ * Add the given values to the beginning of the given column only if not exists
329
+ */
330
+ unshiftOnce(column, ...values) {
331
+ const currentValue = this.get(column);
332
+ if (Array.isArray(currentValue)) {
333
+ const newValues = Array.from(new Set([...values, ...currentValue]));
334
+ this.set(column, newValues);
335
+ }
336
+ else if (!currentValue) {
337
+ this.set(column, values);
338
+ }
339
+ return this;
340
+ }
341
+ /**
342
+ * Perform saving operation either by updating or creating a new record in database
343
+ */
344
+ async save(mergedData, { triggerEvents = true, cast = true, forceUpdate = false, } = {}) {
345
+ const saveEvents = [];
346
+ try {
347
+ if (mergedData) {
348
+ this.merge(mergedData);
349
+ }
350
+ let mode = "create";
351
+ let currentModel;
352
+ // check if the data contains the primary id column
353
+ if (!this.isNewModel()) {
354
+ // perform an update operation
355
+ // check if the data has changed
356
+ // if not changed, then do not do anything
357
+ if (cast) {
358
+ await this.castData(forceUpdate);
359
+ }
360
+ if (this.shouldUpdate(this.originalData, this.data) === false) {
361
+ return this;
362
+ }
363
+ currentModel = new this.constructor(this.originalData);
364
+ mode = "update";
365
+ const updatedAtColumn = this.updatedAtColumn;
366
+ if (updatedAtColumn) {
367
+ // updateAtColumn is supposed to be part of the Schema
368
+ this.data[updatedAtColumn] = new Date();
369
+ }
370
+ if (triggerEvents) {
371
+ const selfModelEvents = this.getModelEvents();
372
+ const ModelEvents = this.getBaseModelEvents();
373
+ await this.onSaving();
374
+ await this.onUpdating();
375
+ await selfModelEvents.trigger("updating", this, currentModel);
376
+ await selfModelEvents.trigger("saving", this, currentModel);
377
+ await ModelEvents.trigger("updating", this, currentModel);
378
+ await ModelEvents.trigger("saving", this, currentModel);
379
+ }
380
+ await this.getQuery().replace(this.getCollection(), {
381
+ _id: this.data._id,
382
+ }, this.data);
383
+ if (triggerEvents) {
384
+ const selfModelEvents = this.getModelEvents();
385
+ const ModelEvents = this.getBaseModelEvents();
386
+ saveEvents.push(this.onSaved(), this.onUpdated(), selfModelEvents.trigger("updated", this, currentModel), selfModelEvents.trigger("saved", this, currentModel), ModelEvents.trigger("updated", this, currentModel), ModelEvents.trigger("saved", this, currentModel));
387
+ }
388
+ }
389
+ else {
390
+ let tries = 3;
391
+ while (tries > 0) {
392
+ try {
393
+ // await this.getDatabase().startSessionContext(async ({ session }) => {
394
+ // check for default values and merge it with the data
395
+ await this.checkDefaultValues();
396
+ // if the column does not exist, then create it
397
+ if (!this.data.id || this.autoGeneratedId) {
398
+ this.autoGeneratedId = true;
399
+ await this.generateNextId();
400
+ }
401
+ const now = new Date();
402
+ const createdAtColumn = this.createdAtColumn;
403
+ // if the column does not exist, then create it
404
+ if (this.data[createdAtColumn]) {
405
+ this.data[createdAtColumn] = new Date(this.data[createdAtColumn]);
406
+ }
407
+ else if (createdAtColumn) {
408
+ this.data[createdAtColumn] = now;
409
+ }
410
+ // if the column does not exist, then create it
411
+ const updatedAtColumn = this.updatedAtColumn;
412
+ if (updatedAtColumn) {
413
+ this.data[updatedAtColumn] = now;
414
+ }
415
+ if (cast) {
416
+ await this.castData();
417
+ }
418
+ if (triggerEvents) {
419
+ const selfModelEvents = this.getModelEvents();
420
+ const ModelEvents = this.getBaseModelEvents();
421
+ await this.onSaving();
422
+ await this.onCreating();
423
+ await selfModelEvents.trigger("creating", this);
424
+ await selfModelEvents.trigger("saving", this);
425
+ await ModelEvents.trigger("creating", this);
426
+ await ModelEvents.trigger("saving", this);
427
+ }
428
+ this.data = (await this.getQuery().create(this.getCollection(), this.data));
429
+ if (triggerEvents) {
430
+ const selfModelEvents = this.getModelEvents();
431
+ const ModelEvents = this.getBaseModelEvents();
432
+ saveEvents.push(this.onSaved(), this.onCreated(), selfModelEvents.trigger("created", this), selfModelEvents.trigger("saved", this), ModelEvents.trigger("created", this), ModelEvents.trigger("saved", this));
433
+ }
434
+ break;
435
+ }
436
+ catch (error) {
437
+ console.log(error.message);
438
+ console.log(error.trace);
439
+ // Handle duplicate key error
440
+ if (error instanceof mongodb.MongoServerError && error.code === 11000) {
441
+ if (tries < 2) {
442
+ const duplicateField = error.keyValue;
443
+ const fieldName = Object.keys(duplicateField)[0];
444
+ const errorMessage = `A record with the same ${fieldName} already exists.`;
445
+ if (this.autoGeneratedId) {
446
+ this.unset("id");
447
+ }
448
+ throw new Error(errorMessage);
449
+ }
450
+ else {
451
+ tries--;
452
+ }
453
+ }
454
+ throw error;
455
+ }
456
+ }
457
+ }
458
+ if (!this.data.id)
459
+ return this;
460
+ this.originalData = reinforcements.clone(this.data);
461
+ // @see constructor
462
+ this.originalData._id = this.data._id;
463
+ if (triggerEvents) {
464
+ await Promise.all(saveEvents);
465
+ this.startSyncing(mode, currentModel);
466
+ }
467
+ return this;
468
+ }
469
+ catch (error) {
470
+ console.log("Error in " + this.constructor.name + ".save()");
471
+ console.log(error);
472
+ throw error;
473
+ }
474
+ }
475
+ /**
476
+ * Generate and return next id
477
+ */
478
+ async generateNextId() {
479
+ this.set("id", await this.getStaticProperty("genNextId").bind(this.constructor)());
480
+ return this.id;
481
+ }
482
+ /**
483
+ * Perform saving but without any events triggers
484
+ */
485
+ async silentSaving(mergedData, options) {
486
+ return await this.save(mergedData, {
487
+ triggerEvents: false,
488
+ ...(options || {}),
489
+ });
490
+ }
491
+ /**
492
+ * Determine whether the model should be updated or not
493
+ */
494
+ shouldUpdate(originalData, data) {
495
+ return reinforcements.areEqual(originalData, data) === false;
496
+ }
497
+ /**
498
+ * Triggered before saving the model either by creating or updating
499
+ */
500
+ async onSaving() {
501
+ //
502
+ }
503
+ /**
504
+ * Triggered after saving the model either by creating or updating
505
+ */
506
+ async onSaved() {
507
+ //
508
+ }
509
+ /**
510
+ * Triggered before creating the model
511
+ */
512
+ async onCreating() {
513
+ //
514
+ }
515
+ /**
516
+ * Triggered after creating the model
517
+ */
518
+ async onCreated() {
519
+ //
520
+ }
521
+ /**
522
+ * Triggered before updating the model
523
+ */
524
+ async onUpdating() {
525
+ //
526
+ }
527
+ /**
528
+ * Triggered after updating the model
529
+ */
530
+ async onUpdated() {
531
+ //
532
+ }
533
+ /**
534
+ * Triggered before deleting the model
535
+ */
536
+ async onDeleting() {
537
+ //
538
+ }
539
+ /**
540
+ * Triggered after deleting the model
541
+ */
542
+ async onDeleted() {
543
+ //
544
+ }
545
+ /**
546
+ * Cast data before saving
547
+ */
548
+ async castData(forceUpdate = false) {
549
+ for (const column in this.casts) {
550
+ if (!forceUpdate && !this.isDirty(column)) {
551
+ continue;
552
+ }
553
+ let value = this.get(column);
554
+ if (value === undefined)
555
+ continue;
556
+ const castType = this.casts[column];
557
+ const castValue = async (value) => {
558
+ if (castType.prototype instanceof Model) {
559
+ // if cast type is passed as model class, then get its embedded data
560
+ value = await castModel.castModel(castType)(value);
561
+ }
562
+ else if (castType?.model) {
563
+ // it means the user is passing a custom model embedding i.e Model.embed('embedToProduct') => Product to embed from the getter property
564
+ // embedToProduct
565
+ // @see EmbeddedModel
566
+ value = await castModel.castModel(castType.model, castType.embeddedKey)(value);
567
+ }
568
+ else if (typeof castType === "object") {
569
+ // it means the user is passing an enum object
570
+ value = await oneOf.castEnum(castType)(value);
571
+ }
572
+ else if (value instanceof Model) {
573
+ value = value.embeddedData;
574
+ }
575
+ else if (typeof castType === "function") {
576
+ value = await castType(value, column, this);
577
+ }
578
+ else {
579
+ value = this.castValue(value, castType);
580
+ }
581
+ return value;
582
+ };
583
+ // if the cast type is passed in array
584
+ // it means we just need to pass the value to the first function
585
+ // second argument will be the column name
586
+ // and the third argument will be the model instance
587
+ if (Array.isArray(castType)) {
588
+ value = await castType[0](value, column, this);
589
+ }
590
+ else if (Array.isArray(value) && castType !== "localized") {
591
+ // if cast type is array, then we'll keep the value as it is
592
+ // now we want to add a new validation rule that to check
593
+ // if the value is an array of localized objects
594
+ // if so, then each value in the array should have `localeCode` and `value` keys
595
+ // if so, then it will be cast only to the value key inside each object
596
+ // so the final output will be localeCode and `castValue` of the value key
597
+ if (castType === "array") ;
598
+ else if (value[0]?.localeCode && value[0]?.value) {
599
+ value = await Promise.all(value.map(async (item) => {
600
+ return {
601
+ localeCode: item.localeCode,
602
+ value: await castValue(item.value),
603
+ };
604
+ }));
605
+ }
606
+ else {
607
+ value = await Promise.all(value.map(async (item) => {
608
+ return await castValue(item);
609
+ }));
610
+ }
611
+ }
612
+ else {
613
+ value = await castValue(value);
614
+ }
615
+ if (Array.isArray(value)) {
616
+ value = value.filter(value => value !== null && value !== undefined);
617
+ }
618
+ if (value !== undefined) {
619
+ this.set(column, value);
620
+ }
621
+ else {
622
+ this.unset(column);
623
+ }
624
+ }
625
+ for (const column in this.customCasts) {
626
+ const castType = this.customCasts[column];
627
+ const value = await castType(this, column);
628
+ if (value !== undefined) {
629
+ this.set(column, value);
630
+ }
631
+ else {
632
+ this.unset(column);
633
+ }
634
+ }
635
+ }
636
+ /**
637
+ * Return only the given columns to be used in output
638
+ */
639
+ outputOnly(columns) {
640
+ return this.clone(this.only(columns));
641
+ }
642
+ /**
643
+ * Return all columns except the given columns to be used in output
644
+ */
645
+ outputExcept(columns) {
646
+ return this.clone(this.except(columns));
647
+ }
648
+ /**
649
+ * Cast the given value based on the given cast type
650
+ */
651
+ castValue(value, castType) {
652
+ const isEmptyValue = supportiveIs.isEmpty(value);
653
+ if (typeof value === "object") {
654
+ if (value === null)
655
+ return undefined;
656
+ }
657
+ else if (isEmptyValue)
658
+ return undefined;
659
+ switch (castType) {
660
+ case "string":
661
+ return isEmptyValue ? "" : String(value).trim();
662
+ case "localized":
663
+ // if (isEmptyValue) return [];
664
+ // if (!Array.isArray(value)) return [];
665
+ if (!Array.isArray(value))
666
+ return undefined;
667
+ return value
668
+ .filter(value => !supportiveIs.isEmpty(value) && supportiveIs.isPlainObject(value))
669
+ .map(item => {
670
+ return {
671
+ localeCode: item.localeCode,
672
+ value: item.value,
673
+ };
674
+ });
675
+ case "number":
676
+ // return isEmptyValue ? 0 : Number(value);
677
+ return Number(value);
678
+ case "int":
679
+ case "integer":
680
+ // return isEmptyValue ? 0 : parseInt(value);
681
+ return parseInt(value);
682
+ case "float":
683
+ // return isEmptyValue ? 0 : parseFloat(value);
684
+ return parseFloat(value);
685
+ case "bool":
686
+ case "boolean": {
687
+ // if (isEmptyValue) return false;
688
+ if (value === "true")
689
+ return true;
690
+ if (value === "false" || value === "0" || value === 0)
691
+ return false;
692
+ return Boolean(value);
693
+ }
694
+ case "date": {
695
+ if (dayjs__default.default.isDayjs(value)) {
696
+ value = value.toDate();
697
+ }
698
+ if (value instanceof Date) {
699
+ return timeWizard.toUTC(value);
700
+ }
701
+ return timeWizard.toUTC(new Date(value));
702
+ }
703
+ case "location": {
704
+ // if (isEmptyValue) return null;
705
+ const lat = value?.[0] || value?.lat;
706
+ const lng = value?.[1] || value?.lng;
707
+ const address = value?.address || value?.formattedAddress;
708
+ return {
709
+ type: "Point",
710
+ coordinates: [Number(lat), Number(lng)],
711
+ address,
712
+ };
713
+ }
714
+ case "object": {
715
+ // if (isEmptyValue) return {};
716
+ if (typeof value === "string") {
717
+ try {
718
+ return JSON.parse(value);
719
+ }
720
+ catch (error) {
721
+ return undefined;
722
+ }
723
+ }
724
+ return value;
725
+ }
726
+ case "array": {
727
+ // if (isEmptyValue) return [];
728
+ if (typeof value === "string") {
729
+ return JSON.parse(value);
730
+ }
731
+ return value;
732
+ }
733
+ case "mixed":
734
+ case "any":
735
+ default:
736
+ return value;
737
+ }
738
+ }
739
+ /**
740
+ * Check for default values
741
+ */
742
+ checkDefaultValues() {
743
+ // if default value is empty, then do nothing
744
+ if (supportiveIs.isEmpty(this.defaultValue))
745
+ return;
746
+ const defaultValue = { ...this.defaultValue };
747
+ for (const key in defaultValue) {
748
+ const value = defaultValue[key];
749
+ if (typeof value === "function") {
750
+ defaultValue[key] = value(this);
751
+ }
752
+ }
753
+ // merge the data with default value
754
+ this.data = reinforcements.merge(defaultValue, this.data);
755
+ }
756
+ /**
757
+ * Destroy the model and delete it from database collection
758
+ */
759
+ async destroy() {
760
+ if (!this.data._id)
761
+ return;
762
+ if (this.deletedAtColumn) {
763
+ this.set(this.deletedAtColumn, new Date());
764
+ }
765
+ const deleteStrategy = this.getStaticProperty("deleteStrategy");
766
+ if (deleteStrategy === types.ModelDeleteStrategy.moveToTrash) {
767
+ const collectionName = this.getCollection();
768
+ class Trash extends Model {
769
+ static collection = collectionName + "Trash";
770
+ }
771
+ // we need to wrap the trash collection inside a model class so it get a generated timestamps and id
772
+ Trash.create({
773
+ document: this.data,
774
+ });
775
+ }
776
+ const selfModelEvents = this.getModelEvents();
777
+ const ModelEvents = this.getBaseModelEvents();
778
+ await this.onDeleting();
779
+ await selfModelEvents.trigger("deleting", this);
780
+ await ModelEvents.trigger("deleting", this);
781
+ // the document will be deleted from database collection if the delete strategy is not soft delete
782
+ if (deleteStrategy !== types.ModelDeleteStrategy.softDelete) {
783
+ await this.getQuery().deleteOne(this.getCollection(), {
784
+ _id: this.data._id,
785
+ });
786
+ }
787
+ else if (deleteStrategy === types.ModelDeleteStrategy.softDelete) {
788
+ await this.getQuery().updateOne(this.getCollection(), {
789
+ _id: this.data._id,
790
+ }, this.data);
791
+ }
792
+ this.onDeleted();
793
+ selfModelEvents.trigger("deleted", this);
794
+ ModelEvents.trigger("deleted", this);
795
+ this.syncDestruction();
796
+ }
797
+ /**
798
+ * Determine if the given column is dirty column
799
+ *
800
+ * Dirty columns are columns that their values have been changed from the original data
801
+ */
802
+ isDirty(column) {
803
+ if (!column) {
804
+ return reinforcements.areEqual(reinforcements.clone(this.data), reinforcements.clone(this.originalData)) === false;
805
+ }
806
+ if (this.isNewModel())
807
+ return true;
808
+ const currentValue = reinforcements.get(this.data, column);
809
+ const originalValue = reinforcements.get(this.originalData, column);
810
+ return reinforcements.areEqual(reinforcements.clone(currentValue), reinforcements.clone(originalValue)) === false;
811
+ }
812
+ /**
813
+ * Check if current model is a new model
814
+ */
815
+ isNewModel() {
816
+ return !this.data._id || (this.data._id && this.isRestored);
817
+ }
818
+ /**
819
+ * Get embedded data
820
+ */
821
+ get embeddedData() {
822
+ if (this.embedAllExcept.length > 0) {
823
+ return reinforcements.except(this.data, this.embedAllExcept);
824
+ }
825
+ if (this.embedAllExceptTimestampsAndUserColumns) {
826
+ return reinforcements.except(this.data, [
827
+ this.createdAtColumn,
828
+ this.updatedAtColumn,
829
+ this.deletedAtColumn,
830
+ this.createdByColumn,
831
+ this.updatedByColumn,
832
+ this.deletedByColumn,
833
+ ]);
834
+ }
835
+ return this.embedded.length > 0 ? this.only(this.embedded) : this.data;
836
+ }
837
+ /**
838
+ * Clone the model
839
+ */
840
+ clone(data = this.data) {
841
+ return new this.constructor(reinforcements.clone(data));
842
+ }
843
+ }exports.Model=Model;//# sourceMappingURL=model.js.map