@nocobase/database 0.5.0-alpha.38 → 0.7.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +50 -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
@@ -0,0 +1,412 @@
1
+ import { Collection } from '../collection';
2
+ import { Database } from '../database';
3
+ import { updateAssociation, updateAssociations } from '../update-associations';
4
+ import { mockDatabase } from './';
5
+
6
+ describe('update associations', () => {
7
+ describe('belongsTo', () => {
8
+ let db: Database;
9
+ beforeEach(async () => {
10
+ db = mockDatabase();
11
+ });
12
+
13
+ afterEach(async () => {
14
+ await db.close();
15
+ });
16
+
17
+ it('post.user', async () => {
18
+ const User = db.collection<{ id: string; name: string }, { name: string }>({
19
+ name: 'users',
20
+ fields: [{ type: 'string', name: 'name' }],
21
+ });
22
+
23
+ const Post = db.collection({
24
+ name: 'posts',
25
+ fields: [
26
+ { type: 'string', name: 'name' },
27
+ { type: 'belongsTo', name: 'user' },
28
+ ],
29
+ });
30
+
31
+ await db.sync();
32
+
33
+ const user = await User.model.create({ name: 'user1' });
34
+ const post1 = await Post.model.create({ name: 'post1' });
35
+
36
+ await updateAssociations(post1, {
37
+ user,
38
+ });
39
+
40
+ expect(post1.toJSON()).toMatchObject({
41
+ id: 1,
42
+ name: 'post1',
43
+ userId: 1,
44
+ user: {
45
+ id: 1,
46
+ name: 'user1',
47
+ },
48
+ });
49
+
50
+ const post2 = await Post.model.create({ name: 'post2' });
51
+ await updateAssociations(post2, {
52
+ user: user.getDataValue('id'),
53
+ });
54
+
55
+ expect(post2.toJSON()).toMatchObject({
56
+ id: 2,
57
+ name: 'post2',
58
+ userId: 1,
59
+ });
60
+
61
+ const post3 = await Post.model.create({ name: 'post3' });
62
+ await updateAssociations(post3, {
63
+ user: {
64
+ name: 'user3',
65
+ },
66
+ });
67
+
68
+ expect(post3.toJSON()).toMatchObject({
69
+ id: 3,
70
+ name: 'post3',
71
+ userId: 2,
72
+ user: {
73
+ id: 2,
74
+ name: 'user3',
75
+ },
76
+ });
77
+
78
+ const post4 = await Post.model.create({ name: 'post4' });
79
+ await updateAssociations(post4, {
80
+ user: {
81
+ id: user.getDataValue('id'),
82
+ name: 'user4',
83
+ },
84
+ });
85
+
86
+ expect(post4.toJSON()).toMatchObject({
87
+ id: 4,
88
+ name: 'post4',
89
+ userId: 1,
90
+ user: {
91
+ id: 1,
92
+ name: 'user1',
93
+ },
94
+ });
95
+ });
96
+ });
97
+
98
+ describe('hasMany', () => {
99
+ let db: Database;
100
+ let User: Collection;
101
+ let Post: Collection;
102
+ beforeEach(async () => {
103
+ db = mockDatabase();
104
+ User = db.collection({
105
+ name: 'users',
106
+ fields: [
107
+ { type: 'string', name: 'name' },
108
+ { type: 'hasMany', name: 'posts' },
109
+ ],
110
+ });
111
+ Post = db.collection({
112
+ name: 'posts',
113
+ fields: [{ type: 'string', name: 'name' }],
114
+ });
115
+ await db.sync();
116
+ });
117
+ afterEach(async () => {
118
+ await db.close();
119
+ });
120
+ it('user.posts', async () => {
121
+ const user1 = await User.model.create<any>({ name: 'user1' });
122
+ await updateAssociations(user1, {
123
+ posts: {
124
+ name: 'post1',
125
+ },
126
+ });
127
+ expect(user1.toJSON()).toMatchObject({
128
+ name: 'user1',
129
+ posts: [
130
+ {
131
+ name: 'post1',
132
+ userId: user1.id,
133
+ },
134
+ ],
135
+ });
136
+ });
137
+ it('user.posts', async () => {
138
+ const user1 = await User.model.create<any>({ name: 'user1' });
139
+ await updateAssociations(user1, {
140
+ posts: [
141
+ {
142
+ name: 'post1',
143
+ },
144
+ ],
145
+ });
146
+ expect(user1.toJSON()).toMatchObject({
147
+ name: 'user1',
148
+ posts: [
149
+ {
150
+ name: 'post1',
151
+ userId: user1.id,
152
+ },
153
+ ],
154
+ });
155
+ });
156
+ it('user.posts', async () => {
157
+ const user1 = await User.model.create<any>({ name: 'user1' });
158
+ const post1 = await Post.model.create<any>({ name: 'post1' });
159
+ await updateAssociations(user1, {
160
+ posts: post1.id,
161
+ });
162
+ expect(user1.toJSON()).toMatchObject({
163
+ name: 'user1',
164
+ });
165
+ const post11 = await Post.model.findByPk(post1.id);
166
+ expect(post11.toJSON()).toMatchObject({
167
+ userId: user1.id,
168
+ });
169
+ });
170
+ it('user.posts', async () => {
171
+ const user1 = await User.model.create<any>({ name: 'user1' });
172
+ const post1 = await Post.model.create<any>({ name: 'post1' });
173
+ await updateAssociations(user1, {
174
+ posts: post1,
175
+ });
176
+ console.log(JSON.stringify(user1, null, 2));
177
+ expect(user1.toJSON()).toMatchObject({
178
+ name: 'user1',
179
+ });
180
+ const post11 = await Post.model.findByPk(post1.id);
181
+ expect(post11.toJSON()).toMatchObject({
182
+ userId: user1.id,
183
+ });
184
+ });
185
+ it('user.posts', async () => {
186
+ const user1 = await User.model.create<any>({ name: 'user1' });
187
+ const post1 = await Post.model.create<any>({ name: 'post1' });
188
+ await updateAssociations(user1, {
189
+ posts: {
190
+ id: post1.id,
191
+ name: 'post111',
192
+ },
193
+ });
194
+ console.log(JSON.stringify(user1, null, 2));
195
+ expect(user1.toJSON()).toMatchObject({
196
+ name: 'user1',
197
+ });
198
+ const post11 = await Post.model.findByPk(post1.id);
199
+ expect(post11.toJSON()).toMatchObject({
200
+ userId: user1.id,
201
+ name: 'post1',
202
+ });
203
+ });
204
+ it('user.posts', async () => {
205
+ const user1 = await User.model.create<any>({ name: 'user1' });
206
+ const post1 = await Post.model.create<any>({ name: 'post1' });
207
+ const post2 = await Post.model.create<any>({ name: 'post2' });
208
+ const post3 = await Post.model.create<any>({ name: 'post3' });
209
+ await updateAssociations(user1, {
210
+ posts: [
211
+ {
212
+ id: post1.id,
213
+ name: 'post111',
214
+ },
215
+ post2.id,
216
+ post3,
217
+ ],
218
+ });
219
+ console.log(JSON.stringify(user1, null, 2));
220
+ expect(user1.toJSON()).toMatchObject({
221
+ name: 'user1',
222
+ });
223
+ const post11 = await Post.model.findByPk(post1.id);
224
+ expect(post11.toJSON()).toMatchObject({
225
+ userId: user1.id,
226
+ name: 'post1',
227
+ });
228
+ const post22 = await Post.model.findByPk(post2.id);
229
+ expect(post22.toJSON()).toMatchObject({
230
+ userId: user1.id,
231
+ name: 'post2',
232
+ });
233
+ const post33 = await Post.model.findByPk(post3.id);
234
+ expect(post33.toJSON()).toMatchObject({
235
+ userId: user1.id,
236
+ name: 'post3',
237
+ });
238
+ });
239
+ });
240
+
241
+ describe('nested', () => {
242
+ let db: Database;
243
+ let User: Collection;
244
+ let Post: Collection;
245
+ let Comment: Collection;
246
+
247
+ beforeEach(async () => {
248
+ db = mockDatabase();
249
+ User = db.collection({
250
+ name: 'users',
251
+ fields: [
252
+ { type: 'string', name: 'name' },
253
+ { type: 'hasMany', name: 'posts' },
254
+ ],
255
+ });
256
+ Post = db.collection({
257
+ name: 'posts',
258
+ fields: [
259
+ { type: 'string', name: 'name' },
260
+ { type: 'belongsTo', name: 'user' },
261
+ { type: 'hasMany', name: 'comments' },
262
+ ],
263
+ });
264
+ Comment = db.collection({
265
+ name: 'comments',
266
+ fields: [
267
+ { type: 'string', name: 'name' },
268
+ { type: 'belongsTo', name: 'post' },
269
+ ],
270
+ });
271
+ await db.sync();
272
+ });
273
+
274
+ afterEach(async () => {
275
+ await db.close();
276
+ });
277
+
278
+ test('create many with nested associations', async () => {
279
+ await User.repository.createMany({
280
+ records: [
281
+ {
282
+ name: 'u1',
283
+ posts: [
284
+ {
285
+ name: 'u1p1',
286
+ comments: [
287
+ {
288
+ name: 'u1p1c1',
289
+ },
290
+ ],
291
+ },
292
+ ],
293
+ },
294
+ {
295
+ name: 'u2',
296
+ posts: [
297
+ {
298
+ name: 'u2p1',
299
+ comments: [
300
+ {
301
+ name: 'u2p1c1',
302
+ },
303
+ ],
304
+ },
305
+ ],
306
+ },
307
+ ],
308
+ });
309
+ });
310
+
311
+ it('nested', async () => {
312
+ const user = await User.model.create({ name: 'user1' });
313
+ await updateAssociations(user, {
314
+ posts: [
315
+ {
316
+ name: 'post1',
317
+ comments: [
318
+ {
319
+ name: 'comment1',
320
+ },
321
+ {
322
+ name: 'comment12',
323
+ },
324
+ ],
325
+ },
326
+ {
327
+ name: 'post2',
328
+ comments: [
329
+ {
330
+ name: 'comment21',
331
+ },
332
+ {
333
+ name: 'comment22',
334
+ },
335
+ ],
336
+ },
337
+ ],
338
+ });
339
+
340
+ const post1 = await Post.model.findOne({
341
+ where: { name: 'post1' },
342
+ });
343
+
344
+ const comment1 = await Comment.model.findOne({
345
+ where: { name: 'comment1' },
346
+ });
347
+
348
+ expect(post1).toMatchObject({
349
+ userId: user.get('id'),
350
+ });
351
+
352
+ expect(comment1).toMatchObject({
353
+ postId: post1.get('id'),
354
+ });
355
+ });
356
+ });
357
+
358
+ describe('belongsToMany', () => {
359
+ let db;
360
+ let Post;
361
+ let Tag;
362
+ let PostTag;
363
+
364
+ beforeEach(async () => {
365
+ db = mockDatabase();
366
+ PostTag = db.collection({
367
+ name: 'posts_tags',
368
+ fields: [{ type: 'string', name: 'tagged_at' }],
369
+ });
370
+ Post = db.collection({
371
+ name: 'posts',
372
+ fields: [
373
+ { type: 'belongsToMany', name: 'tags', through: 'posts_tags' },
374
+ { type: 'string', name: 'title' },
375
+ ],
376
+ });
377
+
378
+ Tag = db.collection({
379
+ name: 'tags',
380
+ fields: [
381
+ { type: 'belongsToMany', name: 'posts', through: 'posts_tags' },
382
+ { type: 'string', name: 'name' },
383
+ ],
384
+ });
385
+
386
+ await db.sync();
387
+ });
388
+
389
+ afterEach(async () => {
390
+ await db.close();
391
+ });
392
+ test('set through value', async () => {
393
+ const p1 = await Post.repository.create({
394
+ values: {
395
+ title: 'hello',
396
+ tags: [
397
+ {
398
+ name: 't1',
399
+ posts_tags: {
400
+ tagged_at: '123',
401
+ },
402
+ },
403
+ { name: 't2' },
404
+ ],
405
+ },
406
+ });
407
+
408
+ const t1 = (await p1.getTags())[0];
409
+ expect(t1.posts_tags.tagged_at).toEqual('123');
410
+ });
411
+ });
412
+ });