@nocobase/database 0.5.0-alpha.38 → 0.7.0-alpha.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 (458) hide show
  1. package/LICENSE +201 -21
  2. package/esm/collection-importer.d.ts +7 -0
  3. package/esm/collection-importer.js +49 -0
  4. package/esm/collection-importer.js.map +1 -0
  5. package/esm/collection.d.ts +73 -0
  6. package/esm/collection.js +224 -0
  7. package/esm/collection.js.map +1 -0
  8. package/esm/database.d.ts +101 -0
  9. package/esm/database.js +275 -0
  10. package/esm/database.js.map +1 -0
  11. package/esm/fields/array-field.d.ts +11 -0
  12. package/esm/fields/array-field.js +26 -0
  13. package/esm/fields/array-field.js.map +1 -0
  14. package/esm/fields/belongs-to-field.d.ts +12 -0
  15. package/esm/fields/belongs-to-field.js +57 -0
  16. package/esm/fields/belongs-to-field.js.map +1 -0
  17. package/esm/fields/belongs-to-many-field.d.ts +11 -0
  18. package/esm/fields/belongs-to-many-field.js +55 -0
  19. package/esm/fields/belongs-to-many-field.js.map +1 -0
  20. package/esm/fields/boolean-field.d.ts +8 -0
  21. package/esm/fields/boolean-field.js +8 -0
  22. package/esm/fields/boolean-field.js.map +1 -0
  23. package/esm/fields/context-field.d.ts +13 -0
  24. package/esm/fields/context-field.js +43 -0
  25. package/esm/fields/context-field.js.map +1 -0
  26. package/esm/fields/date-field.d.ts +8 -0
  27. package/esm/fields/date-field.js +8 -0
  28. package/esm/fields/date-field.js.map +1 -0
  29. package/esm/fields/field.d.ts +37 -0
  30. package/esm/fields/field.js +74 -0
  31. package/esm/fields/field.js.map +1 -0
  32. package/esm/fields/has-inverse-field.d.ts +4 -0
  33. package/esm/fields/has-inverse-field.js +2 -0
  34. package/esm/fields/has-inverse-field.js.map +1 -0
  35. package/esm/fields/has-many-field.d.ts +64 -0
  36. package/esm/fields/has-many-field.js +58 -0
  37. package/esm/fields/has-many-field.js.map +1 -0
  38. package/esm/fields/has-one-field.d.ts +64 -0
  39. package/esm/fields/has-one-field.js +57 -0
  40. package/esm/fields/has-one-field.js.map +1 -0
  41. package/esm/fields/index.d.ts +40 -0
  42. package/esm/fields/index.js +21 -0
  43. package/esm/fields/index.js.map +1 -0
  44. package/esm/fields/json-field.d.ts +14 -0
  45. package/esm/fields/json-field.js +17 -0
  46. package/esm/fields/json-field.js.map +1 -0
  47. package/esm/fields/number-field.d.ts +32 -0
  48. package/esm/fields/number-field.js +28 -0
  49. package/esm/fields/number-field.js.map +1 -0
  50. package/esm/fields/password-field.d.ts +21 -0
  51. package/esm/fields/password-field.js +71 -0
  52. package/esm/fields/password-field.js.map +1 -0
  53. package/esm/fields/radio-field.d.ts +14 -0
  54. package/esm/fields/radio-field.js +49 -0
  55. package/esm/fields/radio-field.js.map +1 -0
  56. package/esm/fields/relation-field.d.ts +20 -0
  57. package/esm/fields/relation-field.js +27 -0
  58. package/esm/fields/relation-field.js.map +1 -0
  59. package/esm/fields/sort-field.d.ts +16 -0
  60. package/esm/fields/sort-field.js +90 -0
  61. package/esm/fields/sort-field.js.map +1 -0
  62. package/esm/fields/string-field.d.ts +8 -0
  63. package/esm/fields/string-field.js +8 -0
  64. package/esm/fields/string-field.js.map +1 -0
  65. package/esm/fields/text-field.d.ts +8 -0
  66. package/esm/fields/text-field.js +8 -0
  67. package/esm/fields/text-field.js.map +1 -0
  68. package/esm/fields/time-field.d.ts +8 -0
  69. package/esm/fields/time-field.js +8 -0
  70. package/esm/fields/time-field.js.map +1 -0
  71. package/esm/fields/uid-field.d.ts +10 -0
  72. package/esm/fields/uid-field.js +27 -0
  73. package/esm/fields/uid-field.js.map +1 -0
  74. package/esm/fields/virtual-field.d.ts +8 -0
  75. package/esm/fields/virtual-field.js +8 -0
  76. package/esm/fields/virtual-field.js.map +1 -0
  77. package/esm/filter-parser.d.ts +27 -0
  78. package/esm/filter-parser.js +185 -0
  79. package/esm/filter-parser.js.map +1 -0
  80. package/esm/index.d.ts +15 -0
  81. package/esm/index.js +16 -0
  82. package/esm/index.js.map +1 -0
  83. package/esm/magic-attribute-model.d.ts +7 -0
  84. package/esm/magic-attribute-model.js +70 -0
  85. package/esm/magic-attribute-model.js.map +1 -0
  86. package/esm/mock-database.d.ts +22 -0
  87. package/esm/mock-database.js +34 -0
  88. package/esm/mock-database.js.map +1 -0
  89. package/esm/model-hook.d.ts +12 -0
  90. package/esm/model-hook.js +61 -0
  91. package/esm/model-hook.js.map +1 -0
  92. package/esm/model.d.ts +15 -0
  93. package/esm/model.js +80 -0
  94. package/esm/model.js.map +1 -0
  95. package/esm/operators/array.d.ts +26 -0
  96. package/esm/operators/array.js +105 -0
  97. package/esm/operators/array.js.map +1 -0
  98. package/esm/operators/association.d.ts +10 -0
  99. package/esm/operators/association.js +14 -0
  100. package/esm/operators/association.js.map +1 -0
  101. package/esm/operators/date.d.ts +34 -0
  102. package/esm/operators/date.js +35 -0
  103. package/esm/operators/date.js.map +1 -0
  104. package/esm/operators/empty.d.ts +28 -0
  105. package/esm/operators/empty.js +58 -0
  106. package/esm/operators/empty.js.map +1 -0
  107. package/esm/operators/index.d.ts +2 -0
  108. package/esm/operators/index.js +2 -0
  109. package/esm/operators/index.js.map +1 -0
  110. package/esm/operators/ne.d.ts +10 -0
  111. package/esm/operators/ne.js +12 -0
  112. package/esm/operators/ne.js.map +1 -0
  113. package/esm/operators/string.d.ts +21 -0
  114. package/esm/operators/string.js +35 -0
  115. package/esm/operators/string.js.map +1 -0
  116. package/esm/operators/utils.d.ts +4 -0
  117. package/esm/operators/utils.js +11 -0
  118. package/esm/operators/utils.js.map +1 -0
  119. package/esm/options-parser.d.ts +31 -0
  120. package/esm/options-parser.js +225 -0
  121. package/esm/options-parser.js.map +1 -0
  122. package/esm/playground.d.ts +1 -0
  123. package/esm/playground.js +53 -0
  124. package/esm/playground.js.map +1 -0
  125. package/esm/relation-repository/belongs-to-many-repository.d.ts +36 -0
  126. package/esm/relation-repository/belongs-to-many-repository.js +199 -0
  127. package/esm/relation-repository/belongs-to-many-repository.js.map +1 -0
  128. package/esm/relation-repository/belongs-to-repository.d.ts +17 -0
  129. package/esm/relation-repository/belongs-to-repository.js +4 -0
  130. package/esm/relation-repository/belongs-to-repository.js.map +1 -0
  131. package/esm/relation-repository/hasmany-repository.d.ts +23 -0
  132. package/esm/relation-repository/hasmany-repository.js +125 -0
  133. package/esm/relation-repository/hasmany-repository.js.map +1 -0
  134. package/esm/relation-repository/hasone-repository.d.ts +17 -0
  135. package/esm/relation-repository/hasone-repository.js +4 -0
  136. package/esm/relation-repository/hasone-repository.js.map +1 -0
  137. package/esm/relation-repository/multiple-relation-repository.d.ts +23 -0
  138. package/esm/relation-repository/multiple-relation-repository.js +149 -0
  139. package/esm/relation-repository/multiple-relation-repository.js.map +1 -0
  140. package/esm/relation-repository/relation-repository.d.ts +32 -0
  141. package/esm/relation-repository/relation-repository.js +93 -0
  142. package/esm/relation-repository/relation-repository.js.map +1 -0
  143. package/esm/relation-repository/single-relation-repository.d.ts +23 -0
  144. package/esm/relation-repository/single-relation-repository.js +96 -0
  145. package/esm/relation-repository/single-relation-repository.js.map +1 -0
  146. package/esm/relation-repository/types.d.ts +7 -0
  147. package/esm/relation-repository/types.js +2 -0
  148. package/esm/relation-repository/types.js.map +1 -0
  149. package/esm/repository.d.ts +165 -0
  150. package/esm/repository.js +276 -0
  151. package/esm/repository.js.map +1 -0
  152. package/esm/transaction-decorator.d.ts +1 -0
  153. package/esm/transaction-decorator.js +63 -0
  154. package/esm/transaction-decorator.js.map +1 -0
  155. package/esm/update-associations.d.ts +60 -0
  156. package/esm/update-associations.js +362 -0
  157. package/esm/update-associations.js.map +1 -0
  158. package/esm/update-guard.d.ts +26 -0
  159. package/esm/update-guard.js +122 -0
  160. package/esm/update-guard.js.map +1 -0
  161. package/lib/collection-importer.d.ts +7 -0
  162. package/lib/collection-importer.js +75 -0
  163. package/lib/collection-importer.js.map +1 -0
  164. package/lib/collection.d.ts +73 -0
  165. package/lib/collection.js +231 -0
  166. package/lib/collection.js.map +1 -0
  167. package/lib/database.d.ts +93 -43
  168. package/lib/database.js +277 -471
  169. package/lib/database.js.map +1 -1
  170. package/lib/fields/array-field.d.ts +11 -0
  171. package/lib/fields/array-field.js +30 -0
  172. package/lib/fields/array-field.js.map +1 -0
  173. package/lib/fields/belongs-to-field.d.ts +12 -0
  174. package/lib/fields/belongs-to-field.js +61 -0
  175. package/lib/fields/belongs-to-field.js.map +1 -0
  176. package/lib/fields/belongs-to-many-field.d.ts +11 -0
  177. package/lib/fields/belongs-to-many-field.js +59 -0
  178. package/lib/fields/belongs-to-many-field.js.map +1 -0
  179. package/lib/fields/boolean-field.d.ts +8 -0
  180. package/lib/fields/boolean-field.js +12 -0
  181. package/lib/fields/boolean-field.js.map +1 -0
  182. package/lib/fields/context-field.d.ts +13 -0
  183. package/lib/fields/context-field.js +47 -0
  184. package/lib/fields/context-field.js.map +1 -0
  185. package/lib/fields/date-field.d.ts +8 -0
  186. package/lib/fields/date-field.js +12 -0
  187. package/lib/fields/date-field.js.map +1 -0
  188. package/lib/fields/field.d.ts +37 -0
  189. package/lib/fields/field.js +81 -0
  190. package/lib/fields/field.js.map +1 -0
  191. package/lib/fields/has-inverse-field.d.ts +4 -0
  192. package/lib/fields/has-inverse-field.js +3 -0
  193. package/lib/fields/has-inverse-field.js.map +1 -0
  194. package/lib/fields/has-many-field.d.ts +64 -0
  195. package/lib/fields/has-many-field.js +62 -0
  196. package/lib/fields/has-many-field.js.map +1 -0
  197. package/lib/fields/has-one-field.d.ts +64 -0
  198. package/lib/fields/has-one-field.js +61 -0
  199. package/lib/fields/has-one-field.js.map +1 -0
  200. package/lib/fields/index.d.ts +40 -10
  201. package/lib/fields/index.js +31 -138
  202. package/lib/fields/index.js.map +1 -1
  203. package/lib/fields/json-field.d.ts +14 -0
  204. package/lib/fields/json-field.js +22 -0
  205. package/lib/fields/json-field.js.map +1 -0
  206. package/lib/fields/number-field.d.ts +32 -0
  207. package/lib/fields/number-field.js +36 -0
  208. package/lib/fields/number-field.js.map +1 -0
  209. package/lib/fields/password-field.d.ts +21 -0
  210. package/lib/fields/password-field.js +78 -0
  211. package/lib/fields/password-field.js.map +1 -0
  212. package/lib/fields/radio-field.d.ts +14 -0
  213. package/lib/fields/radio-field.js +53 -0
  214. package/lib/fields/radio-field.js.map +1 -0
  215. package/lib/fields/relation-field.d.ts +20 -0
  216. package/lib/fields/relation-field.js +31 -0
  217. package/lib/fields/relation-field.js.map +1 -0
  218. package/lib/fields/sort-field.d.ts +16 -0
  219. package/lib/fields/sort-field.js +94 -0
  220. package/lib/fields/sort-field.js.map +1 -0
  221. package/lib/fields/string-field.d.ts +8 -0
  222. package/lib/fields/string-field.js +12 -0
  223. package/lib/fields/string-field.js.map +1 -0
  224. package/lib/fields/text-field.d.ts +8 -0
  225. package/lib/fields/text-field.js +12 -0
  226. package/lib/fields/text-field.js.map +1 -0
  227. package/lib/fields/time-field.d.ts +8 -0
  228. package/lib/fields/time-field.js +12 -0
  229. package/lib/fields/time-field.js.map +1 -0
  230. package/lib/fields/uid-field.d.ts +10 -0
  231. package/lib/fields/uid-field.js +31 -0
  232. package/lib/fields/uid-field.js.map +1 -0
  233. package/lib/fields/virtual-field.d.ts +8 -0
  234. package/lib/fields/virtual-field.js +12 -0
  235. package/lib/fields/virtual-field.js.map +1 -0
  236. package/lib/filter-parser.d.ts +27 -0
  237. package/lib/filter-parser.js +191 -0
  238. package/lib/filter-parser.js.map +1 -0
  239. package/lib/index.d.ts +13 -6
  240. package/lib/index.js +27 -60
  241. package/lib/index.js.map +1 -1
  242. package/lib/magic-attribute-model.d.ts +7 -0
  243. package/lib/magic-attribute-model.js +77 -0
  244. package/lib/magic-attribute-model.js.map +1 -0
  245. package/lib/mock-database.d.ts +22 -0
  246. package/lib/mock-database.js +40 -0
  247. package/lib/mock-database.js.map +1 -0
  248. package/lib/model-hook.d.ts +12 -0
  249. package/lib/model-hook.js +68 -0
  250. package/lib/model-hook.js.map +1 -0
  251. package/lib/model.d.ts +11 -45
  252. package/lib/model.js +76 -452
  253. package/lib/model.js.map +1 -1
  254. package/lib/operators/array.d.ts +26 -0
  255. package/lib/operators/array.js +107 -0
  256. package/lib/operators/array.js.map +1 -0
  257. package/lib/operators/association.d.ts +10 -0
  258. package/lib/operators/association.js +16 -0
  259. package/lib/operators/association.js.map +1 -0
  260. package/lib/operators/date.d.ts +34 -0
  261. package/lib/operators/date.js +40 -0
  262. package/lib/operators/date.js.map +1 -0
  263. package/lib/operators/empty.d.ts +28 -0
  264. package/lib/operators/empty.js +82 -0
  265. package/lib/operators/empty.js.map +1 -0
  266. package/lib/operators/index.d.ts +2 -0
  267. package/lib/operators/index.js +4 -0
  268. package/lib/operators/index.js.map +1 -0
  269. package/lib/operators/ne.d.ts +10 -0
  270. package/lib/operators/ne.js +14 -0
  271. package/lib/operators/ne.js.map +1 -0
  272. package/lib/operators/string.d.ts +21 -0
  273. package/lib/operators/string.js +37 -0
  274. package/lib/operators/string.js.map +1 -0
  275. package/lib/operators/utils.d.ts +4 -0
  276. package/lib/operators/utils.js +16 -0
  277. package/lib/operators/utils.js.map +1 -0
  278. package/lib/options-parser.d.ts +31 -0
  279. package/lib/options-parser.js +232 -0
  280. package/lib/options-parser.js.map +1 -0
  281. package/lib/playground.d.ts +1 -0
  282. package/lib/playground.js +55 -0
  283. package/lib/playground.js.map +1 -0
  284. package/lib/relation-repository/belongs-to-many-repository.d.ts +36 -0
  285. package/lib/relation-repository/belongs-to-many-repository.js +206 -0
  286. package/lib/relation-repository/belongs-to-many-repository.js.map +1 -0
  287. package/lib/relation-repository/belongs-to-repository.d.ts +17 -0
  288. package/lib/relation-repository/belongs-to-repository.js +8 -0
  289. package/lib/relation-repository/belongs-to-repository.js.map +1 -0
  290. package/lib/relation-repository/hasmany-repository.d.ts +23 -0
  291. package/lib/relation-repository/hasmany-repository.js +129 -0
  292. package/lib/relation-repository/hasmany-repository.js.map +1 -0
  293. package/lib/relation-repository/hasone-repository.d.ts +17 -0
  294. package/lib/relation-repository/hasone-repository.js +8 -0
  295. package/lib/relation-repository/hasone-repository.js.map +1 -0
  296. package/lib/relation-repository/multiple-relation-repository.d.ts +23 -0
  297. package/lib/relation-repository/multiple-relation-repository.js +153 -0
  298. package/lib/relation-repository/multiple-relation-repository.js.map +1 -0
  299. package/lib/relation-repository/relation-repository.d.ts +32 -0
  300. package/lib/relation-repository/relation-repository.js +100 -0
  301. package/lib/relation-repository/relation-repository.js.map +1 -0
  302. package/lib/relation-repository/single-relation-repository.d.ts +23 -0
  303. package/lib/relation-repository/single-relation-repository.js +103 -0
  304. package/lib/relation-repository/single-relation-repository.js.map +1 -0
  305. package/lib/relation-repository/types.d.ts +7 -0
  306. package/lib/relation-repository/types.js +3 -0
  307. package/lib/relation-repository/types.js.map +1 -0
  308. package/lib/repository.d.ts +165 -0
  309. package/lib/repository.js +302 -0
  310. package/lib/repository.js.map +1 -0
  311. package/lib/transaction-decorator.d.ts +1 -0
  312. package/lib/transaction-decorator.js +70 -0
  313. package/lib/transaction-decorator.js.map +1 -0
  314. package/lib/update-associations.d.ts +60 -0
  315. package/lib/update-associations.js +374 -0
  316. package/lib/update-associations.js.map +1 -0
  317. package/lib/update-guard.d.ts +26 -0
  318. package/lib/update-guard.js +129 -0
  319. package/lib/update-guard.js.map +1 -0
  320. package/package.json +17 -5
  321. package/src/__tests__/collection-importer.test.ts +21 -0
  322. package/src/__tests__/collection.sortable.test.ts +65 -0
  323. package/src/__tests__/collection.test.ts +218 -0
  324. package/src/__tests__/database.import.test.ts +33 -0
  325. package/src/__tests__/database.test.ts +229 -0
  326. package/src/__tests__/field-options/hidden.test.ts +302 -0
  327. package/src/__tests__/field-options/sort-by.test.ts +220 -0
  328. package/src/__tests__/fields/belongs-to-field.test.ts +162 -0
  329. package/src/__tests__/fields/belongs-to-many-field.test.ts +61 -0
  330. package/src/__tests__/fields/context-field.test.ts +140 -0
  331. package/src/__tests__/fields/has-many-field.test.ts +152 -0
  332. package/src/__tests__/fields/has-one-field.test.ts +67 -0
  333. package/src/__tests__/fields/password-field.test.ts +30 -0
  334. package/src/__tests__/fields/sort-field.test.ts +133 -0
  335. package/src/__tests__/fields/string-field.test.ts +77 -0
  336. package/src/__tests__/filter-parser.test.ts +104 -0
  337. package/src/__tests__/fixtures/c0/a.ts +6 -0
  338. package/src/__tests__/fixtures/c1/b.ts +6 -0
  339. package/src/__tests__/fixtures/c2/a.ts +6 -0
  340. package/src/__tests__/fixtures/collections/delay-extend.ts +6 -0
  341. package/src/__tests__/fixtures/collections/delay-extend2.ts +6 -0
  342. package/src/__tests__/fixtures/collections/extend.ts +6 -0
  343. package/src/__tests__/fixtures/collections/extend2.ts +6 -0
  344. package/src/__tests__/fixtures/collections/posts.ts +4 -0
  345. package/src/__tests__/fixtures/collections/tags.js +4 -0
  346. package/src/__tests__/fixtures/collections/test.jpg +0 -0
  347. package/src/__tests__/fixtures/collections/user.json +9 -0
  348. package/src/__tests__/index.ts +1 -0
  349. package/src/__tests__/magic-attribute-model.test.ts +103 -0
  350. package/src/__tests__/model.test.ts +60 -0
  351. package/src/__tests__/operator/array-operator.test.ts +268 -0
  352. package/src/__tests__/operator/association-operator.test.ts +263 -0
  353. package/src/__tests__/operator/date-operator.test.ts +165 -0
  354. package/src/__tests__/operator/empty-operator.test.ts +77 -0
  355. package/src/__tests__/operator/ne.test.ts +33 -0
  356. package/src/__tests__/operator/string-operator.test.ts +62 -0
  357. package/src/__tests__/option-parser.test.ts +185 -0
  358. package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +697 -0
  359. package/src/__tests__/relation-repository/has-many-repository.test.ts +414 -0
  360. package/src/__tests__/relation-repository/hasone-repository.test.ts +77 -0
  361. package/src/__tests__/repository/count.test.ts +180 -0
  362. package/src/__tests__/repository/create.test.ts +163 -0
  363. package/src/__tests__/repository/destroy.test.ts +196 -0
  364. package/src/__tests__/repository/find.test.ts +247 -0
  365. package/src/__tests__/repository/update.test.ts +60 -0
  366. package/src/__tests__/repository.test.ts +438 -0
  367. package/src/__tests__/update-associations.test.ts +412 -0
  368. package/src/__tests__/update-guard.test.ts +376 -0
  369. package/src/collection-importer.ts +49 -0
  370. package/src/collection.ts +282 -0
  371. package/src/database.ts +340 -0
  372. package/src/fields/array-field.ts +35 -0
  373. package/src/fields/belongs-to-field.ts +76 -0
  374. package/src/fields/belongs-to-many-field.ts +77 -0
  375. package/src/fields/boolean-field.ts +12 -0
  376. package/src/fields/context-field.ts +45 -0
  377. package/src/fields/date-field.ts +12 -0
  378. package/src/fields/field.ts +105 -0
  379. package/src/fields/has-inverse-field.ts +5 -0
  380. package/src/fields/has-many-field.ts +143 -0
  381. package/src/fields/has-one-field.ts +136 -0
  382. package/src/fields/index.ts +72 -0
  383. package/src/fields/json-field.ts +25 -0
  384. package/src/fields/number-field.ts +52 -0
  385. package/src/fields/password-field.ts +72 -0
  386. package/src/fields/radio-field.ts +50 -0
  387. package/src/fields/relation-field.ts +37 -0
  388. package/src/fields/sort-field.ts +96 -0
  389. package/src/fields/string-field.ts +12 -0
  390. package/src/fields/text-field.ts +12 -0
  391. package/src/fields/time-field.ts +12 -0
  392. package/src/fields/uid-field.ts +24 -0
  393. package/src/fields/virtual-field.ts +12 -0
  394. package/src/filter-parser.ts +243 -0
  395. package/src/index.ts +16 -0
  396. package/src/magic-attribute-model.ts +62 -0
  397. package/src/mock-database.ts +42 -0
  398. package/src/model-hook.ts +69 -0
  399. package/src/model.ts +114 -0
  400. package/src/operators/array.ts +145 -0
  401. package/src/operators/association.ts +14 -0
  402. package/src/operators/date.ts +41 -0
  403. package/src/operators/empty.ts +75 -0
  404. package/src/operators/index.ts +8 -0
  405. package/src/operators/ne.ts +12 -0
  406. package/src/operators/string.ts +40 -0
  407. package/src/operators/utils.ts +13 -0
  408. package/src/options-parser.ts +285 -0
  409. package/src/playground.ts +52 -0
  410. package/src/relation-repository/belongs-to-many-repository.ts +240 -0
  411. package/src/relation-repository/belongs-to-repository.ts +23 -0
  412. package/src/relation-repository/hasmany-repository.ts +145 -0
  413. package/src/relation-repository/hasone-repository.ts +23 -0
  414. package/src/relation-repository/multiple-relation-repository.ts +198 -0
  415. package/src/relation-repository/relation-repository.ts +114 -0
  416. package/src/relation-repository/single-relation-repository.ts +99 -0
  417. package/src/relation-repository/types.ts +15 -0
  418. package/src/repository.ts +478 -0
  419. package/src/transaction-decorator.ts +58 -0
  420. package/src/update-associations.ts +478 -0
  421. package/src/update-guard.ts +167 -0
  422. package/tsconfig.build.json +9 -0
  423. package/tsconfig.json +5 -0
  424. package/examples/index.ts +0 -125
  425. package/examples/plugins/db-driven/index.ts +0 -25
  426. package/examples/plugins/db-driven/tables/fields.ts +0 -78
  427. package/examples/plugins/db-driven/tables/tables.ts +0 -53
  428. package/examples/tables/bar.js +0 -26
  429. package/examples/tables/comments.ts +0 -19
  430. package/examples/tables/foo.json +0 -3
  431. package/examples/tables/posts.ts +0 -28
  432. package/examples/tables/profiles.ts +0 -23
  433. package/examples/tables/tags.ts +0 -15
  434. package/examples/tables/users.ts +0 -34
  435. package/lib/database.d.ts.map +0 -1
  436. package/lib/fields/field-types.d.ts +0 -419
  437. package/lib/fields/field-types.d.ts.map +0 -1
  438. package/lib/fields/field-types.js +0 -1222
  439. package/lib/fields/field-types.js.map +0 -1
  440. package/lib/fields/index.d.ts.map +0 -1
  441. package/lib/fields/option-types.d.ts +0 -105
  442. package/lib/fields/option-types.d.ts.map +0 -1
  443. package/lib/fields/option-types.js +0 -18
  444. package/lib/fields/option-types.js.map +0 -1
  445. package/lib/index.d.ts.map +0 -1
  446. package/lib/model.d.ts.map +0 -1
  447. package/lib/op.d.ts +0 -45
  448. package/lib/op.d.ts.map +0 -1
  449. package/lib/op.js +0 -225
  450. package/lib/op.js.map +0 -1
  451. package/lib/table.d.ts +0 -56
  452. package/lib/table.d.ts.map +0 -1
  453. package/lib/table.js +0 -456
  454. package/lib/table.js.map +0 -1
  455. package/lib/utils.d.ts +0 -26
  456. package/lib/utils.d.ts.map +0 -1
  457. package/lib/utils.js +0 -438
  458. package/lib/utils.js.map +0 -1
@@ -1,1222 +0,0 @@
1
- "use strict";
2
-
3
- function _react() {
4
- const data = _interopRequireDefault(require("react"));
5
-
6
- _react = function _react() {
7
- return data;
8
- };
9
-
10
- return data;
11
- }
12
-
13
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
-
15
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
16
-
17
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
18
-
19
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
20
-
21
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
22
-
23
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
24
-
25
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
26
-
27
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
28
-
29
- var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
30
- function adopt(value) {
31
- return value instanceof P ? value : new P(function (resolve) {
32
- resolve(value);
33
- });
34
- }
35
-
36
- return new (P || (P = Promise))(function (resolve, reject) {
37
- function fulfilled(value) {
38
- try {
39
- step(generator.next(value));
40
- } catch (e) {
41
- reject(e);
42
- }
43
- }
44
-
45
- function rejected(value) {
46
- try {
47
- step(generator["throw"](value));
48
- } catch (e) {
49
- reject(e);
50
- }
51
- }
52
-
53
- function step(result) {
54
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
55
- }
56
-
57
- step((generator = generator.apply(thisArg, _arguments || [])).next());
58
- });
59
- };
60
-
61
- var __rest = void 0 && (void 0).__rest || function (s, e) {
62
- var t = {};
63
-
64
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
65
-
66
- if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
67
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
68
- }
69
- return t;
70
- };
71
-
72
- var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
73
- return mod && mod.__esModule ? mod : {
74
- "default": mod
75
- };
76
- };
77
-
78
- Object.defineProperty(exports, "__esModule", {
79
- value: true
80
- });
81
- exports.Radio = exports.SORT = exports.BELONGSTOMANY = exports.BELONGSTO = exports.HASMANY = exports.HASONE = exports.Relation = exports.UUIDV4 = exports.UID = exports.UUID = exports.JSONB = exports.JSON = exports.ARRAY = exports.PASSWORD = exports.FORMULA = exports.REFERENCE = exports.VIRTUAL = exports.DATEONLY = exports.DATE = exports.TIME = exports.TEXT = exports.STRING = exports.REAL = exports.DECIMAL = exports.DOUBLE = exports.FLOAT = exports.INTEGER = exports.NUMBER = exports.BOOLEAN = exports.Column = exports.Field = void 0;
82
-
83
- const sequelize_1 = require("sequelize");
84
-
85
- const lodash_1 = require("lodash");
86
-
87
- const bcrypt_1 = __importDefault(require("bcrypt"));
88
-
89
- const _1 = require(".");
90
-
91
- const model_1 = __importDefault(require("../model"));
92
-
93
- const utils_1 = require("../utils");
94
-
95
- class Field {
96
- constructor(options, context) {
97
- const type = options.type;
98
- this.options = Object.assign({}, options);
99
- this.context = context;
100
- }
101
-
102
- isMultipleColumns() {
103
- return false;
104
- }
105
-
106
- getType() {
107
- return this.options.type;
108
- }
109
-
110
- getOptions() {
111
- return this.options;
112
- }
113
-
114
- }
115
-
116
- exports.Field = Field;
117
-
118
- class Column extends Field {
119
- getDataType() {
120
- const type = this.options.type;
121
- const dataType = (0, _1.getDataTypeKey)(type);
122
-
123
- if (sequelize_1.DataTypes[dataType]) {
124
- return sequelize_1.DataTypes[dataType];
125
- }
126
-
127
- return sequelize_1.DataTypes[this.constructor.name.toUpperCase()];
128
- }
129
-
130
- getDataTypeInstance(options = {}) {
131
- const dataType = this.getDataType();
132
- Object.keys(options).forEach(key => options[key] === undefined && delete options[key]);
133
- return Object.keys(options).length > 0 ? dataType(options) : dataType;
134
- }
135
-
136
- getAttributeOptions() {
137
- return Object.assign(Object.assign({}, this.options), {
138
- type: this.getDataTypeInstance()
139
- });
140
- }
141
-
142
- }
143
-
144
- exports.Column = Column;
145
-
146
- class BOOLEAN extends Column {}
147
-
148
- exports.BOOLEAN = BOOLEAN;
149
-
150
- class NUMBER extends Column {}
151
-
152
- exports.NUMBER = NUMBER;
153
-
154
- class INTEGER extends NUMBER {
155
- getDataType() {
156
- const type = this.options.type;
157
- const dataType = (0, _1.getDataTypeKey)(type);
158
- return {
159
- INT: sequelize_1.DataTypes.INTEGER,
160
- INTEGER: sequelize_1.DataTypes.INTEGER,
161
- TINYINT: sequelize_1.DataTypes.TINYINT,
162
- TINYINTEGER: sequelize_1.DataTypes.TINYINT,
163
- SMALLINT: sequelize_1.DataTypes.SMALLINT,
164
- SMALLINTEGER: sequelize_1.DataTypes.SMALLINT,
165
- MEDIUMINT: sequelize_1.DataTypes.MEDIUMINT,
166
- MEDIUMINTEGER: sequelize_1.DataTypes.MEDIUMINT,
167
- BIGINT: sequelize_1.DataTypes.BIGINT,
168
- BIGINTEGER: sequelize_1.DataTypes.BIGINT
169
- }[dataType] || sequelize_1.DataTypes.INTEGER;
170
- }
171
-
172
- getAttributeOptions() {
173
- const _a = this.options,
174
- length = _a.length,
175
- zerofill = _a.zerofill,
176
- unsigned = _a.unsigned,
177
- restOptions = __rest(_a, ["length", "zerofill", "unsigned"]);
178
-
179
- return Object.assign(Object.assign({}, restOptions), {
180
- type: this.getDataTypeInstance({
181
- length,
182
- zerofill,
183
- unsigned
184
- })
185
- });
186
- }
187
-
188
- }
189
-
190
- exports.INTEGER = INTEGER;
191
-
192
- class FLOAT extends NUMBER {
193
- getAttributeOptions() {
194
- const _a = this.options,
195
- length = _a.length,
196
- decimals = _a.decimals,
197
- restOptions = __rest(_a, ["length", "decimals"]);
198
-
199
- return Object.assign(Object.assign({}, restOptions), {
200
- type: this.getDataTypeInstance({
201
- length,
202
- decimals
203
- })
204
- });
205
- }
206
-
207
- }
208
-
209
- exports.FLOAT = FLOAT;
210
-
211
- class DOUBLE extends NUMBER {
212
- getAttributeOptions() {
213
- const _a = this.options,
214
- length = _a.length,
215
- decimals = _a.decimals,
216
- restOptions = __rest(_a, ["length", "decimals"]);
217
-
218
- return Object.assign(Object.assign({}, restOptions), {
219
- type: this.getDataTypeInstance({
220
- length,
221
- decimals
222
- })
223
- });
224
- }
225
-
226
- }
227
-
228
- exports.DOUBLE = DOUBLE;
229
-
230
- class DECIMAL extends NUMBER {
231
- getAttributeOptions() {
232
- const _a = this.options,
233
- precision = _a.precision,
234
- scale = _a.scale,
235
- restOptions = __rest(_a, ["precision", "scale"]);
236
-
237
- return Object.assign(Object.assign({}, restOptions), {
238
- type: this.getDataTypeInstance({
239
- precision,
240
- scale
241
- })
242
- });
243
- }
244
-
245
- }
246
-
247
- exports.DECIMAL = DECIMAL;
248
-
249
- class REAL extends NUMBER {
250
- getAttributeOptions() {
251
- const _a = this.options,
252
- length = _a.length,
253
- decimals = _a.decimals,
254
- restOptions = __rest(_a, ["length", "decimals"]);
255
-
256
- return Object.assign(Object.assign({}, restOptions), {
257
- type: this.getDataTypeInstance({
258
- length,
259
- decimals
260
- })
261
- });
262
- }
263
-
264
- }
265
-
266
- exports.REAL = REAL;
267
-
268
- class STRING extends Column {
269
- getAttributeOptions() {
270
- const _a = this.options,
271
- length = _a.length,
272
- binary = _a.binary,
273
- restOptions = __rest(_a, ["length", "binary"]);
274
-
275
- return Object.assign(Object.assign({}, restOptions), {
276
- type: this.getDataTypeInstance({
277
- length,
278
- binary
279
- })
280
- });
281
- }
282
-
283
- }
284
-
285
- exports.STRING = STRING;
286
-
287
- class TEXT extends Column {
288
- getDataTypeInstance(options = {}) {
289
- const database = this.context.database;
290
- const dataType = this.getDataType();
291
- Object.keys(options).forEach(key => options[key] === undefined && delete options[key]);
292
-
293
- if (database.sequelize.getDialect() === 'postgres') {
294
- return Object.keys(options).length > 0 ? dataType() : dataType;
295
- }
296
-
297
- return Object.keys(options).length > 0 ? dataType(options) : dataType;
298
- }
299
-
300
- getAttributeOptions() {
301
- const _a = this.options,
302
- length = _a.length,
303
- restOptions = __rest(_a, ["length"]);
304
-
305
- return Object.assign(Object.assign({}, restOptions), {
306
- type: this.getDataTypeInstance({
307
- length
308
- })
309
- });
310
- }
311
-
312
- }
313
-
314
- exports.TEXT = TEXT;
315
-
316
- class TIME extends Column {}
317
-
318
- exports.TIME = TIME;
319
-
320
- class DATE extends Column {
321
- getAttributeOptions() {
322
- const _a = this.options,
323
- length = _a.length,
324
- restOptions = __rest(_a, ["length"]);
325
-
326
- return Object.assign(Object.assign({}, restOptions), {
327
- type: this.getDataTypeInstance({
328
- length
329
- })
330
- });
331
- }
332
-
333
- }
334
-
335
- exports.DATE = DATE;
336
-
337
- class DATEONLY extends Column {}
338
-
339
- exports.DATEONLY = DATEONLY;
340
-
341
- class VIRTUAL extends Column {}
342
-
343
- exports.VIRTUAL = VIRTUAL;
344
-
345
- class REFERENCE extends VIRTUAL {
346
- getDataType() {
347
- return sequelize_1.DataTypes.VIRTUAL;
348
- }
349
-
350
- getAttributeOptions() {
351
- const _a = this.options,
352
- source = _a.source,
353
- dataIndex = _a.dataIndex,
354
- restOptions = __rest(_a, ["source", "dataIndex"]);
355
-
356
- return Object.assign(Object.assign({}, restOptions), {
357
- type: this.getDataTypeInstance({
358
- source,
359
- dataIndex
360
- }),
361
-
362
- get() {
363
- return (0, lodash_1.get)(this[source], dataIndex);
364
- }
365
-
366
- });
367
- }
368
-
369
- }
370
-
371
- exports.REFERENCE = REFERENCE;
372
-
373
- class FORMULA extends VIRTUAL {
374
- getDataType() {
375
- return sequelize_1.DataTypes.VIRTUAL;
376
- }
377
-
378
- getAttributeOptions() {
379
- const sourceTable = this.context.sourceTable;
380
-
381
- const _a = this.options,
382
- formula = _a.formula,
383
- _a$format = _a.format,
384
- format = _a$format === void 0 ? 'string' : _a$format,
385
- restOptions = __rest(_a, ["formula", "format"]);
386
-
387
- return Object.assign(Object.assign({}, restOptions), {
388
- type: this.getDataTypeInstance({
389
- formula
390
- }),
391
-
392
- get() {
393
- const fields = sourceTable.getFields();
394
- const data = {};
395
-
396
- var _iterator = _createForOfIteratorHelper(fields),
397
- _step;
398
-
399
- try {
400
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
401
- const _step$value = _slicedToArray(_step.value, 2),
402
- name = _step$value[0],
403
- field = _step$value[1];
404
-
405
- if (['formula', 'virtual'].indexOf(field.getType().toLowerCase()) === -1) {
406
- data[name] = this.getDataValue(name);
407
- }
408
- }
409
- } catch (err) {
410
- _iterator.e(err);
411
- } finally {
412
- _iterator.f();
413
- }
414
-
415
- try {
416
- const compiled = (0, lodash_1.template)(formula, {
417
- interpolate: /{{([\s\S]+?)}}/g
418
- });
419
- const value = compiled(data);
420
- return format === 'number' ? (0, lodash_1.toNumber)(value) : value;
421
- } catch (error) {
422
- console.log(error);
423
- return;
424
- }
425
- }
426
-
427
- });
428
- }
429
-
430
- }
431
-
432
- exports.FORMULA = FORMULA;
433
-
434
- class PASSWORD extends STRING {
435
- getDataType() {
436
- return sequelize_1.DataTypes.STRING;
437
- }
438
-
439
- constructor(options, context) {
440
- super(options, context);
441
- const database = context.database,
442
- sourceTable = context.sourceTable;
443
- const name = sourceTable.getName();
444
- database.on(`${name}.beforeCreate`, PASSWORD.hash.bind(this));
445
- database.on(`${name}.beforeUpdate`, PASSWORD.hash.bind(this));
446
- }
447
-
448
- static hash(model) {
449
- return __awaiter(this, void 0, void 0, function* () {
450
- const name = this.options.name;
451
-
452
- if (!model.changed(name)) {
453
- return;
454
- }
455
-
456
- const value = model.get(name);
457
-
458
- if (value) {
459
- if (value.startsWith('$2b$10$') && value.length === 60) {
460
- return;
461
- }
462
-
463
- const hash = yield bcrypt_1.default.hash(value, 10);
464
- model.set(name, hash);
465
- } else {
466
- model.set(name, null);
467
- }
468
- });
469
- }
470
-
471
- static verify(value, hash) {
472
- return __awaiter(this, void 0, void 0, function* () {
473
- return yield bcrypt_1.default.compare(value, hash);
474
- });
475
- }
476
-
477
- }
478
-
479
- exports.PASSWORD = PASSWORD;
480
-
481
- class ARRAY extends Column {
482
- getDataType() {
483
- return sequelize_1.DataTypes.JSONB;
484
- }
485
-
486
- getAttributeOptions() {
487
- const _a = this.options,
488
- items = _a.items,
489
- restOptions = __rest(_a, ["items"]);
490
-
491
- return Object.assign(Object.assign({}, restOptions), {
492
- type: this.getDataTypeInstance({
493
- items
494
- })
495
- });
496
- }
497
-
498
- }
499
-
500
- exports.ARRAY = ARRAY;
501
-
502
- class JSON extends Column {
503
- getDataType() {
504
- return sequelize_1.DataTypes.JSON;
505
- }
506
-
507
- }
508
-
509
- exports.JSON = JSON;
510
-
511
- class JSONB extends Column {
512
- getDataType() {
513
- if (this.context.database.sequelize.getDialect() === 'postgres') {
514
- return sequelize_1.DataTypes.JSONB;
515
- }
516
-
517
- return sequelize_1.DataTypes.JSON;
518
- }
519
-
520
- }
521
-
522
- exports.JSONB = JSONB;
523
-
524
- class UUID extends Column {
525
- getDataType() {
526
- return sequelize_1.DataTypes.UUID;
527
- }
528
-
529
- }
530
-
531
- exports.UUID = UUID;
532
-
533
- class UID extends Column {
534
- constructor(options, context) {
535
- super(options, context);
536
- const sourceTable = context.sourceTable,
537
- database = context.database;
538
- const name = options.name,
539
- _options$prefix = options.prefix,
540
- prefix = _options$prefix === void 0 ? '' : _options$prefix;
541
- database.on(`${sourceTable.getName()}.beforeCreate`, model => {
542
- if (!model.get(name)) {
543
- model.set(name, `${prefix}${(0, utils_1.uid)()}`);
544
- }
545
- });
546
- }
547
-
548
- getDataType() {
549
- return sequelize_1.DataTypes.STRING;
550
- }
551
-
552
- }
553
-
554
- exports.UID = UID;
555
-
556
- class UUIDV4 extends Column {
557
- getDataType() {
558
- return sequelize_1.DataTypes.UUIDV4;
559
- }
560
-
561
- }
562
-
563
- exports.UUIDV4 = UUIDV4;
564
-
565
- class Relation extends Field {
566
- targetTableInit() {
567
- const _this$options = this.options,
568
- target = _this$options.target,
569
- _this$options$fields = _this$options.fields,
570
- fields = _this$options$fields === void 0 ? [] : _this$options$fields;
571
- const children = fields.concat(this.options.children || []);
572
-
573
- if (target && children.length) {
574
- this.context.database.table({
575
- name: target,
576
- fields: children
577
- });
578
- }
579
- }
580
-
581
- getAssociationType() {
582
- if (this instanceof HASONE) {
583
- return 'hasOne';
584
- }
585
-
586
- if (this instanceof HASMANY) {
587
- return 'hasMany';
588
- }
589
-
590
- if (this instanceof BELONGSTO) {
591
- return 'belongsTo';
592
- }
593
-
594
- if (this instanceof BELONGSTOMANY) {
595
- return 'belongsToMany';
596
- }
597
- }
598
-
599
- getTarget() {
600
- const _this$options2 = this.options,
601
- target = _this$options2.target,
602
- name = _this$options2.name;
603
-
604
- if (target) {
605
- return target;
606
- }
607
-
608
- if (this instanceof HASMANY) {
609
- return name;
610
- }
611
-
612
- if (this instanceof BELONGSTOMANY) {
613
- return name;
614
- }
615
-
616
- return sequelize_1.Utils.pluralize(name);
617
- }
618
-
619
- getTargetModel() {
620
- const name = this.options.name;
621
- const sourceTable = this.context.sourceTable;
622
- return sourceTable.getModel().associations[name].target;
623
- }
624
-
625
- getAccessors() {
626
- const name = this.options.name;
627
- const sourceTable = this.context.sourceTable;
628
- return sourceTable.getModel().associations[name].accessors;
629
- }
630
-
631
- getAssociationOptions() {
632
- const _a = this.options,
633
- name = _a.name,
634
- restOptions = __rest(_a, ["name"]);
635
-
636
- return Object.assign({
637
- as: name
638
- }, restOptions);
639
- }
640
-
641
- getAssociationArguments() {
642
- return {
643
- target: this.getTarget(),
644
- type: this.getAssociationType(),
645
- options: this.getAssociationOptions()
646
- };
647
- }
648
-
649
- }
650
-
651
- exports.Relation = Relation;
652
-
653
- class HasOneOrMany extends Relation {
654
- constructor(options, context) {
655
- const sourceTable = context.sourceTable;
656
- let foreignKey = options.foreignKey,
657
- sourceKey = options.sourceKey;
658
- const SourceModel = sourceTable.getModel();
659
-
660
- if (!sourceKey) {
661
- sourceKey = SourceModel.primaryKeyAttribute;
662
- }
663
-
664
- if (!SourceModel.rawAttributes[sourceKey]) {
665
- sourceTable.addField({
666
- type: 'integer',
667
- name: sourceKey,
668
- unique: true
669
- });
670
- }
671
-
672
- if (!foreignKey) {
673
- foreignKey = sequelize_1.Utils.underscoredIf(sequelize_1.Utils.camelize([SourceModel.options.name.singular, sourceKey].join('_')), SourceModel.options.underscored);
674
- }
675
-
676
- super(Object.assign({
677
- sourceKey,
678
- foreignKey
679
- }, options), context);
680
- }
681
-
682
- }
683
-
684
- class HASONE extends HasOneOrMany {
685
- constructor(options, context) {
686
- let name = options.name,
687
- target = options.target;
688
-
689
- if (!target) {
690
- target = sequelize_1.Utils.pluralize(name);
691
- }
692
-
693
- super(Object.assign({
694
- target
695
- }, options), context);
696
- this.targetTableInit();
697
- }
698
-
699
- getAccessors() {
700
- return super.getAccessors();
701
- }
702
-
703
- getAssociationOptions() {
704
- const _a = this.options,
705
- name = _a.name,
706
- restOptions = __rest(_a, ["name"]);
707
-
708
- return Object.assign({
709
- as: name
710
- }, restOptions);
711
- }
712
-
713
- }
714
-
715
- exports.HASONE = HASONE;
716
-
717
- class HASMANY extends HasOneOrMany {
718
- constructor(options, context) {
719
- let name = options.name,
720
- target = options.target;
721
-
722
- if (!target) {
723
- target = name;
724
- }
725
-
726
- super(Object.assign({
727
- target
728
- }, options), context);
729
- this.targetTableInit();
730
- }
731
-
732
- getAssociationOptions() {
733
- const _a = this.options,
734
- name = _a.name,
735
- restOptions = __rest(_a, ["name"]);
736
-
737
- return Object.assign({
738
- as: name
739
- }, restOptions);
740
- }
741
-
742
- }
743
-
744
- exports.HASMANY = HASMANY;
745
-
746
- class BELONGSTO extends Relation {
747
- constructor(options, context) {
748
- let name = options.name,
749
- target = options.target;
750
-
751
- if (!target) {
752
- target = sequelize_1.Utils.pluralize(name);
753
- }
754
-
755
- super(Object.assign({
756
- target
757
- }, options), context);
758
- this.targetTableInit();
759
- this.updateOptionsAfterTargetModelBeDefined();
760
- }
761
-
762
- getAccessors() {
763
- return super.getAccessors();
764
- }
765
-
766
- updateOptionsAfterTargetModelBeDefined() {
767
- let _this$options3 = this.options,
768
- name = _this$options3.name,
769
- target = _this$options3.target,
770
- targetKey = _this$options3.targetKey,
771
- foreignKey = _this$options3.foreignKey;
772
- const database = this.context.database;
773
- const TargetModel = database.getModel(target);
774
-
775
- if (!TargetModel) {
776
- return;
777
- }
778
-
779
- if (!targetKey) {
780
- targetKey = TargetModel.primaryKeyAttribute;
781
- this.options.targetKey = targetKey;
782
- }
783
-
784
- if (!foreignKey) {
785
- foreignKey = sequelize_1.Utils.underscoredIf(sequelize_1.Utils.camelize([name, targetKey].join('_')), TargetModel.options.underscored);
786
- this.options.foreignKey = foreignKey;
787
- }
788
- }
789
-
790
- getAssociationOptions() {
791
- const _a = this.options,
792
- name = _a.name,
793
- restOptions = __rest(_a, ["name"]);
794
-
795
- return Object.assign({
796
- as: name
797
- }, restOptions);
798
- }
799
-
800
- }
801
-
802
- exports.BELONGSTO = BELONGSTO;
803
-
804
- class BELONGSTOMANY extends Relation {
805
- constructor(options, context) {
806
- let name = options.name,
807
- target = options.target,
808
- through = options.through,
809
- sourceKey = options.sourceKey,
810
- foreignKey = options.foreignKey,
811
- targetKey = options.targetKey,
812
- otherKey = options.otherKey;
813
- const database = context.database,
814
- sourceTable = context.sourceTable;
815
- const SourceModel = sourceTable.getModel();
816
-
817
- if (!target) {
818
- target = name;
819
- }
820
-
821
- if (!through) {
822
- through = sequelize_1.Utils.underscoredIf(sequelize_1.Utils.camelize([SourceModel.name, target].map(name => name.toLowerCase()).sort().join('_')), SourceModel.options.underscored);
823
- }
824
-
825
- if (!sourceKey) {
826
- sourceKey = SourceModel.primaryKeyAttribute;
827
- }
828
-
829
- if (!foreignKey) {
830
- foreignKey = sequelize_1.Utils.underscoredIf(sequelize_1.Utils.camelize([SourceModel.options.name.singular, sourceKey].join('_')), SourceModel.options.underscored);
831
- }
832
-
833
- super(Object.assign({
834
- target,
835
- through,
836
- sourceKey,
837
- foreignKey
838
- }, options), context);
839
- this.targetTableInit();
840
- this.updateOptionsAfterTargetModelBeDefined();
841
- database.throughTables.set(this.getThroughName(), [sourceTable.getName(), target]);
842
- }
843
-
844
- getAccessors() {
845
- return super.getAccessors();
846
- }
847
-
848
- updateOptionsAfterTargetModelBeDefined() {
849
- const database = this.context.database;
850
- let _this$options4 = this.options,
851
- target = _this$options4.target,
852
- targetKey = _this$options4.targetKey,
853
- otherKey = _this$options4.otherKey;
854
- const TargetModel = database.getModel(target);
855
-
856
- if (!TargetModel) {
857
- return;
858
- }
859
-
860
- if (!targetKey) {
861
- targetKey = TargetModel.primaryKeyAttribute;
862
- this.options.targetKey = targetKey;
863
- }
864
-
865
- if (!otherKey) {
866
- otherKey = sequelize_1.Utils.underscoredIf(sequelize_1.Utils.camelize([TargetModel.options.name.singular, targetKey].join('_')), TargetModel.options.underscored);
867
- this.options.otherKey = otherKey;
868
- }
869
- }
870
-
871
- getThroughName() {
872
- return this.options.through;
873
- }
874
-
875
- getThroughModel() {
876
- const _this$options5 = this.options,
877
- through = _this$options5.through,
878
- target = _this$options5.target;
879
- const _this$context = this.context,
880
- database = _this$context.database,
881
- sourceTable = _this$context.sourceTable;
882
- const throughName = this.getThroughName();
883
-
884
- if (database.sequelize.isDefined(throughName)) {
885
- return database.getModel(throughName);
886
- }
887
-
888
- class ThroughModel extends model_1.default {}
889
-
890
- ThroughModel.init({}, {
891
- modelName: throughName,
892
- tableName: throughName,
893
- sequelize: database.sequelize,
894
- indexes: [],
895
- underscored: true
896
- });
897
- return ThroughModel;
898
- }
899
-
900
- getAssociationOptions() {
901
- const _a = this.options,
902
- name = _a.name,
903
- restOptions = __rest(_a, ["name"]);
904
-
905
- return Object.assign({
906
- as: name,
907
- through: this.getThroughModel()
908
- }, restOptions);
909
- }
910
-
911
- }
912
-
913
- exports.BELONGSTOMANY = BELONGSTOMANY;
914
-
915
- class SORT extends NUMBER {
916
- constructor(options, context) {
917
- super(options, context);
918
- const database = context.database,
919
- sourceTable = context.sourceTable;
920
- const name = sourceTable.getName();
921
- database.on(`${name}.beforeCreate`, SORT.beforeCreateHook.bind(this));
922
- database.on(`${name}.beforeBulkCreate`, SORT.beforeBulkCreateHook.bind(this));
923
- }
924
-
925
- static beforeCreateHook(model, options) {
926
- return __awaiter(this, void 0, void 0, function* () {
927
- const _this$options6 = this.options,
928
- name = _this$options6.name,
929
- _this$options6$scope = _this$options6.scope,
930
- scope = _this$options6$scope === void 0 ? [] : _this$options6$scope;
931
-
932
- if ((0, utils_1.isNumber)(model.get(name))) {
933
- return;
934
- }
935
-
936
- const extremum = yield this.getNextValue(Object.assign(Object.assign({}, options), {
937
- where: model.getValuesByFieldNames(scope)
938
- }));
939
- model.set(name, extremum);
940
- });
941
- }
942
-
943
- static beforeBulkCreateHook(models, options) {
944
- return __awaiter(this, void 0, void 0, function* () {
945
- const transaction = options.transaction;
946
- const _this$options7 = this.options,
947
- name = _this$options7.name,
948
- _this$options7$scope = _this$options7.scope,
949
- scope = _this$options7$scope === void 0 ? [] : _this$options7$scope,
950
- _this$options7$next = _this$options7.next,
951
- next = _this$options7$next === void 0 ? 'max' : _this$options7$next;
952
-
953
- if (!scope.length) {
954
- const extremum = yield this.getNextValue({
955
- where: {},
956
- transaction
957
- });
958
- models.forEach((model, i) => {
959
- model.setDataValue(name, extremum + i * (next === 'max' ? 1 : -1));
960
- });
961
- return;
962
- }
963
-
964
- const groups = new Map();
965
- yield models.reduce((promise, model) => promise.then(() => __awaiter(this, void 0, void 0, function* () {
966
- const where = model.getValuesByFieldNames(scope);
967
- let extremum;
968
- let combo;
969
-
970
- var _iterator2 = _createForOfIteratorHelper(groups.keys()),
971
- _step2;
972
-
973
- try {
974
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
975
- combo = _step2.value;
976
-
977
- if ((0, utils_1.whereCompare)(combo, where)) {
978
- extremum = groups.get(combo) + (next === 'max' ? 1 : -1);
979
- break;
980
- }
981
- }
982
- } catch (err) {
983
- _iterator2.e(err);
984
- } finally {
985
- _iterator2.f();
986
- }
987
-
988
- if (typeof extremum === 'undefined') {
989
- extremum = yield this.getNextValue({
990
- where,
991
- transaction
992
- });
993
- combo = where;
994
- }
995
-
996
- const nextValue = extremum;
997
- model.setDataValue(name, nextValue);
998
- groups.set(combo, nextValue);
999
- })), Promise.resolve());
1000
- });
1001
- }
1002
-
1003
- getDataType() {
1004
- return sequelize_1.DataTypes.INTEGER;
1005
- }
1006
-
1007
- getNextValue({
1008
- where,
1009
- transaction,
1010
- next: n = 'max'
1011
- }) {
1012
- return __awaiter(this, void 0, void 0, function* () {
1013
- const table = this.context.sourceTable;
1014
- const Model = table.getModel();
1015
- const _this$options8 = this.options,
1016
- name = _this$options8.name,
1017
- _this$options8$next = _this$options8.next,
1018
- next = _this$options8$next === void 0 ? n : _this$options8$next;
1019
- const extremum = (yield Model[next](name, {
1020
- where,
1021
- transaction
1022
- })) || 0;
1023
- return extremum + (next === 'max' ? 1 : -1);
1024
- });
1025
- }
1026
-
1027
- }
1028
-
1029
- exports.SORT = SORT;
1030
-
1031
- class Radio extends BOOLEAN {
1032
- constructor(_a, context) {
1033
- var type = _a.type,
1034
- options = __rest(_a, ["type"]);
1035
-
1036
- super(Object.assign(Object.assign({}, options), {
1037
- type: 'radio'
1038
- }), context);
1039
- const database = context.database,
1040
- sourceTable = context.sourceTable;
1041
- const name = sourceTable.getName();
1042
- database.on(`${name}.beforeCreate`, Radio.beforeCreateHook.bind(this));
1043
- database.on(`${name}.beforeUpdate`, Radio.beforeUpdateHook.bind(this));
1044
- database.on(`${name}.beforeBulkCreate`, Radio.beforeBulkCreateHook.bind(this));
1045
- }
1046
-
1047
- static beforeCreateHook(model, options) {
1048
- return __awaiter(this, void 0, void 0, function* () {
1049
- const _this$options9 = this.options,
1050
- name = _this$options9.name,
1051
- _this$options9$defaul = _this$options9.defaultValue,
1052
- defaultValue = _this$options9$defaul === void 0 ? false : _this$options9$defaul,
1053
- _this$options9$scope = _this$options9.scope,
1054
- scope = _this$options9$scope === void 0 ? [] : _this$options9$scope;
1055
- const transaction = options.transaction;
1056
- const value = model.get(name) || defaultValue;
1057
- model.set(name, value);
1058
-
1059
- if (value) {
1060
- const where = model.getValuesByFieldNames(scope);
1061
- yield this.setOthers({
1062
- where,
1063
- transaction
1064
- });
1065
- }
1066
- });
1067
- }
1068
-
1069
- static beforeUpdateHook(model, options) {
1070
- return __awaiter(this, void 0, void 0, function* () {
1071
- const _this$options10 = this.options,
1072
- name = _this$options10.name,
1073
- _this$options10$scope = _this$options10.scope,
1074
- scope = _this$options10$scope === void 0 ? [] : _this$options10$scope;
1075
- const transaction = options.transaction,
1076
- association = options.association;
1077
-
1078
- if (model.changed(name) && model.get(name) && !association) {
1079
- const where = model.getValuesByFieldNames(scope);
1080
- const primaryKeyAttribute = model.constructor.primaryKeyAttribute;
1081
- where[primaryKeyAttribute] = {
1082
- [sequelize_1.Op.ne]: model.get(primaryKeyAttribute)
1083
- };
1084
- yield this.setOthers({
1085
- where,
1086
- transaction
1087
- });
1088
- }
1089
- });
1090
- }
1091
-
1092
- static beforeBulkCreateHook(models, options) {
1093
- return __awaiter(this, void 0, void 0, function* () {
1094
- const _this$options$scope = this.options.scope,
1095
- scope = _this$options$scope === void 0 ? [] : _this$options$scope;
1096
- const transaction = options.transaction;
1097
-
1098
- if (!scope.length) {
1099
- yield this.makeGroup(models, {
1100
- transaction
1101
- });
1102
- return;
1103
- }
1104
-
1105
- const groups = new Map();
1106
- models.forEach(model => {
1107
- const where = model.getValuesByFieldNames(scope);
1108
- let combo;
1109
- let group;
1110
-
1111
- var _iterator3 = _createForOfIteratorHelper(groups.keys()),
1112
- _step3;
1113
-
1114
- try {
1115
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
1116
- combo = _step3.value;
1117
-
1118
- if ((0, utils_1.whereCompare)(combo, where)) {
1119
- group = groups.get(combo);
1120
- break;
1121
- }
1122
- }
1123
- } catch (err) {
1124
- _iterator3.e(err);
1125
- } finally {
1126
- _iterator3.f();
1127
- }
1128
-
1129
- if (!group) {
1130
- group = [];
1131
- groups.set(where, group);
1132
- }
1133
-
1134
- group.push(model);
1135
- });
1136
-
1137
- var _iterator4 = _createForOfIteratorHelper(groups),
1138
- _step4;
1139
-
1140
- try {
1141
- for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
1142
- const _step4$value = _slicedToArray(_step4.value, 2),
1143
- where = _step4$value[0],
1144
- group = _step4$value[1];
1145
-
1146
- yield this.makeGroup(group, {
1147
- where,
1148
- transaction
1149
- });
1150
- }
1151
- } catch (err) {
1152
- _iterator4.e(err);
1153
- } finally {
1154
- _iterator4.f();
1155
- }
1156
- });
1157
- }
1158
-
1159
- getDataType() {
1160
- return sequelize_1.DataTypes.BOOLEAN;
1161
- }
1162
-
1163
- setOthers({
1164
- where = {},
1165
- transaction
1166
- }) {
1167
- return __awaiter(this, void 0, void 0, function* () {
1168
- const name = this.options.name;
1169
- const table = this.context.sourceTable;
1170
- const Model = table.getModel();
1171
- yield Model.update({
1172
- [name]: false
1173
- }, {
1174
- where: Object.assign(Object.assign({}, where), {
1175
- [name]: true
1176
- }),
1177
- transaction,
1178
- hooks: false
1179
- });
1180
- });
1181
- }
1182
-
1183
- makeGroup(models, {
1184
- where = {},
1185
- transaction
1186
- }) {
1187
- return __awaiter(this, void 0, void 0, function* () {
1188
- const _this$options11 = this.options,
1189
- name = _this$options11.name,
1190
- _this$options11$defau = _this$options11.defaultValue,
1191
- defaultValue = _this$options11$defau === void 0 ? false : _this$options11$defau;
1192
- let lastTrue;
1193
- let lastNull;
1194
- models.forEach(model => {
1195
- const value = model.get(name);
1196
-
1197
- if (value) {
1198
- lastTrue = model;
1199
- } else if (value == null) {
1200
- lastNull = model;
1201
- }
1202
-
1203
- model.set(name, false);
1204
- });
1205
-
1206
- if (lastTrue) {
1207
- lastTrue.set(name, true);
1208
- } else if (defaultValue && lastNull) {
1209
- lastNull.set(name, true);
1210
- }
1211
-
1212
- yield this.setOthers({
1213
- where,
1214
- transaction
1215
- });
1216
- });
1217
- }
1218
-
1219
- }
1220
-
1221
- exports.Radio = Radio;
1222
- //# sourceMappingURL=field-types.js.map