@nocobase/database 0.7.0-alpha.34 → 0.7.0-alpha.57

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 (280) hide show
  1. package/lib/collection-importer.js +101 -67
  2. package/lib/collection.js +339 -210
  3. package/lib/database.js +488 -304
  4. package/lib/fields/array-field.js +45 -25
  5. package/lib/fields/belongs-to-field.js +101 -54
  6. package/lib/fields/belongs-to-many-field.js +98 -53
  7. package/lib/fields/boolean-field.js +24 -9
  8. package/lib/fields/context-field.js +77 -45
  9. package/lib/fields/date-field.js +24 -9
  10. package/lib/fields/field.js +114 -75
  11. package/lib/fields/has-inverse-field.js +4 -2
  12. package/lib/fields/has-many-field.js +105 -56
  13. package/lib/fields/has-one-field.js +105 -54
  14. package/lib/fields/index.js +277 -33
  15. package/lib/fields/json-field.js +36 -16
  16. package/lib/fields/number-field.js +53 -26
  17. package/lib/fields/password-field.js +118 -73
  18. package/lib/fields/radio-field.js +75 -47
  19. package/lib/fields/relation-field.js +41 -28
  20. package/lib/fields/sort-field.js +165 -89
  21. package/lib/fields/string-field.js +24 -9
  22. package/lib/fields/text-field.js +24 -9
  23. package/lib/fields/time-field.js +24 -9
  24. package/lib/fields/uid-field.js +57 -28
  25. package/lib/fields/uuid-field.js +36 -12
  26. package/lib/fields/virtual-field.js +24 -9
  27. package/lib/filter-parser.js +288 -179
  28. package/lib/index.js +210 -29
  29. package/lib/magic-attribute-model.js +123 -71
  30. package/lib/mock-database.js +68 -34
  31. package/lib/model-hook.js +101 -59
  32. package/lib/model.js +116 -81
  33. package/lib/operators/array.js +136 -96
  34. package/lib/operators/association.js +30 -14
  35. package/lib/operators/date.js +78 -34
  36. package/lib/operators/empty.js +113 -75
  37. package/lib/operators/index.js +15 -3
  38. package/lib/operators/ne.js +27 -12
  39. package/lib/operators/notIn.js +27 -12
  40. package/lib/operators/string.js +56 -35
  41. package/lib/operators/utils.js +18 -10
  42. package/lib/options-parser.js +323 -215
  43. package/lib/playground.js +66 -53
  44. package/lib/relation-repository/belongs-to-many-repository.js +281 -198
  45. package/lib/relation-repository/belongs-to-repository.js +10 -6
  46. package/lib/relation-repository/hasmany-repository.js +168 -121
  47. package/lib/relation-repository/hasone-repository.js +10 -6
  48. package/lib/relation-repository/multiple-relation-repository.js +263 -148
  49. package/lib/relation-repository/relation-repository.js +155 -97
  50. package/lib/relation-repository/single-relation-repository.js +138 -99
  51. package/lib/relation-repository/types.js +4 -2
  52. package/lib/repository.js +465 -291
  53. package/lib/transaction-decorator.js +80 -67
  54. package/lib/update-associations.js +525 -321
  55. package/lib/update-guard.js +160 -117
  56. package/package.json +3 -8
  57. package/src/database.ts +9 -3
  58. package/src/mock-database.ts +1 -1
  59. package/esm/collection-importer.d.ts +0 -7
  60. package/esm/collection-importer.js +0 -49
  61. package/esm/collection-importer.js.map +0 -1
  62. package/esm/collection.d.ts +0 -73
  63. package/esm/collection.js +0 -224
  64. package/esm/collection.js.map +0 -1
  65. package/esm/database.d.ts +0 -104
  66. package/esm/database.js +0 -305
  67. package/esm/database.js.map +0 -1
  68. package/esm/fields/array-field.d.ts +0 -11
  69. package/esm/fields/array-field.js +0 -26
  70. package/esm/fields/array-field.js.map +0 -1
  71. package/esm/fields/belongs-to-field.d.ts +0 -12
  72. package/esm/fields/belongs-to-field.js +0 -57
  73. package/esm/fields/belongs-to-field.js.map +0 -1
  74. package/esm/fields/belongs-to-many-field.d.ts +0 -11
  75. package/esm/fields/belongs-to-many-field.js +0 -55
  76. package/esm/fields/belongs-to-many-field.js.map +0 -1
  77. package/esm/fields/boolean-field.d.ts +0 -8
  78. package/esm/fields/boolean-field.js +0 -8
  79. package/esm/fields/boolean-field.js.map +0 -1
  80. package/esm/fields/context-field.d.ts +0 -13
  81. package/esm/fields/context-field.js +0 -43
  82. package/esm/fields/context-field.js.map +0 -1
  83. package/esm/fields/date-field.d.ts +0 -8
  84. package/esm/fields/date-field.js +0 -8
  85. package/esm/fields/date-field.js.map +0 -1
  86. package/esm/fields/field.d.ts +0 -37
  87. package/esm/fields/field.js +0 -74
  88. package/esm/fields/field.js.map +0 -1
  89. package/esm/fields/has-inverse-field.d.ts +0 -4
  90. package/esm/fields/has-inverse-field.js +0 -2
  91. package/esm/fields/has-inverse-field.js.map +0 -1
  92. package/esm/fields/has-many-field.d.ts +0 -64
  93. package/esm/fields/has-many-field.js +0 -58
  94. package/esm/fields/has-many-field.js.map +0 -1
  95. package/esm/fields/has-one-field.d.ts +0 -64
  96. package/esm/fields/has-one-field.js +0 -57
  97. package/esm/fields/has-one-field.js.map +0 -1
  98. package/esm/fields/index.d.ts +0 -42
  99. package/esm/fields/index.js +0 -22
  100. package/esm/fields/index.js.map +0 -1
  101. package/esm/fields/json-field.d.ts +0 -14
  102. package/esm/fields/json-field.js +0 -17
  103. package/esm/fields/json-field.js.map +0 -1
  104. package/esm/fields/number-field.d.ts +0 -32
  105. package/esm/fields/number-field.js +0 -28
  106. package/esm/fields/number-field.js.map +0 -1
  107. package/esm/fields/password-field.d.ts +0 -21
  108. package/esm/fields/password-field.js +0 -71
  109. package/esm/fields/password-field.js.map +0 -1
  110. package/esm/fields/radio-field.d.ts +0 -14
  111. package/esm/fields/radio-field.js +0 -49
  112. package/esm/fields/radio-field.js.map +0 -1
  113. package/esm/fields/relation-field.d.ts +0 -20
  114. package/esm/fields/relation-field.js +0 -27
  115. package/esm/fields/relation-field.js.map +0 -1
  116. package/esm/fields/sort-field.d.ts +0 -16
  117. package/esm/fields/sort-field.js +0 -90
  118. package/esm/fields/sort-field.js.map +0 -1
  119. package/esm/fields/string-field.d.ts +0 -8
  120. package/esm/fields/string-field.js +0 -8
  121. package/esm/fields/string-field.js.map +0 -1
  122. package/esm/fields/text-field.d.ts +0 -8
  123. package/esm/fields/text-field.js +0 -8
  124. package/esm/fields/text-field.js.map +0 -1
  125. package/esm/fields/time-field.d.ts +0 -8
  126. package/esm/fields/time-field.js +0 -8
  127. package/esm/fields/time-field.js.map +0 -1
  128. package/esm/fields/uid-field.d.ts +0 -10
  129. package/esm/fields/uid-field.js +0 -27
  130. package/esm/fields/uid-field.js.map +0 -1
  131. package/esm/fields/uuid-field.d.ts +0 -9
  132. package/esm/fields/uuid-field.js +0 -11
  133. package/esm/fields/uuid-field.js.map +0 -1
  134. package/esm/fields/virtual-field.d.ts +0 -8
  135. package/esm/fields/virtual-field.js +0 -8
  136. package/esm/fields/virtual-field.js.map +0 -1
  137. package/esm/filter-parser.d.ts +0 -27
  138. package/esm/filter-parser.js +0 -185
  139. package/esm/filter-parser.js.map +0 -1
  140. package/esm/index.d.ts +0 -15
  141. package/esm/index.js +0 -16
  142. package/esm/index.js.map +0 -1
  143. package/esm/magic-attribute-model.d.ts +0 -7
  144. package/esm/magic-attribute-model.js +0 -70
  145. package/esm/magic-attribute-model.js.map +0 -1
  146. package/esm/mock-database.d.ts +0 -22
  147. package/esm/mock-database.js +0 -34
  148. package/esm/mock-database.js.map +0 -1
  149. package/esm/model-hook.d.ts +0 -12
  150. package/esm/model-hook.js +0 -60
  151. package/esm/model-hook.js.map +0 -1
  152. package/esm/model.d.ts +0 -15
  153. package/esm/model.js +0 -80
  154. package/esm/model.js.map +0 -1
  155. package/esm/operators/array.d.ts +0 -26
  156. package/esm/operators/array.js +0 -105
  157. package/esm/operators/array.js.map +0 -1
  158. package/esm/operators/association.d.ts +0 -10
  159. package/esm/operators/association.js +0 -14
  160. package/esm/operators/association.js.map +0 -1
  161. package/esm/operators/date.d.ts +0 -34
  162. package/esm/operators/date.js +0 -35
  163. package/esm/operators/date.js.map +0 -1
  164. package/esm/operators/empty.d.ts +0 -28
  165. package/esm/operators/empty.js +0 -58
  166. package/esm/operators/empty.js.map +0 -1
  167. package/esm/operators/index.d.ts +0 -2
  168. package/esm/operators/index.js +0 -2
  169. package/esm/operators/index.js.map +0 -1
  170. package/esm/operators/ne.d.ts +0 -10
  171. package/esm/operators/ne.js +0 -12
  172. package/esm/operators/ne.js.map +0 -1
  173. package/esm/operators/notIn.d.ts +0 -10
  174. package/esm/operators/notIn.js +0 -12
  175. package/esm/operators/notIn.js.map +0 -1
  176. package/esm/operators/string.d.ts +0 -21
  177. package/esm/operators/string.js +0 -35
  178. package/esm/operators/string.js.map +0 -1
  179. package/esm/operators/utils.d.ts +0 -4
  180. package/esm/operators/utils.js +0 -11
  181. package/esm/operators/utils.js.map +0 -1
  182. package/esm/options-parser.d.ts +0 -31
  183. package/esm/options-parser.js +0 -225
  184. package/esm/options-parser.js.map +0 -1
  185. package/esm/playground.d.ts +0 -1
  186. package/esm/playground.js +0 -53
  187. package/esm/playground.js.map +0 -1
  188. package/esm/relation-repository/belongs-to-many-repository.d.ts +0 -36
  189. package/esm/relation-repository/belongs-to-many-repository.js +0 -199
  190. package/esm/relation-repository/belongs-to-many-repository.js.map +0 -1
  191. package/esm/relation-repository/belongs-to-repository.d.ts +0 -17
  192. package/esm/relation-repository/belongs-to-repository.js +0 -4
  193. package/esm/relation-repository/belongs-to-repository.js.map +0 -1
  194. package/esm/relation-repository/hasmany-repository.d.ts +0 -23
  195. package/esm/relation-repository/hasmany-repository.js +0 -125
  196. package/esm/relation-repository/hasmany-repository.js.map +0 -1
  197. package/esm/relation-repository/hasone-repository.d.ts +0 -17
  198. package/esm/relation-repository/hasone-repository.js +0 -4
  199. package/esm/relation-repository/hasone-repository.js.map +0 -1
  200. package/esm/relation-repository/multiple-relation-repository.d.ts +0 -23
  201. package/esm/relation-repository/multiple-relation-repository.js +0 -149
  202. package/esm/relation-repository/multiple-relation-repository.js.map +0 -1
  203. package/esm/relation-repository/relation-repository.d.ts +0 -32
  204. package/esm/relation-repository/relation-repository.js +0 -95
  205. package/esm/relation-repository/relation-repository.js.map +0 -1
  206. package/esm/relation-repository/single-relation-repository.d.ts +0 -23
  207. package/esm/relation-repository/single-relation-repository.js +0 -96
  208. package/esm/relation-repository/single-relation-repository.js.map +0 -1
  209. package/esm/relation-repository/types.d.ts +0 -7
  210. package/esm/relation-repository/types.js +0 -2
  211. package/esm/relation-repository/types.js.map +0 -1
  212. package/esm/repository.d.ts +0 -165
  213. package/esm/repository.js +0 -276
  214. package/esm/repository.js.map +0 -1
  215. package/esm/transaction-decorator.d.ts +0 -1
  216. package/esm/transaction-decorator.js +0 -63
  217. package/esm/transaction-decorator.js.map +0 -1
  218. package/esm/update-associations.d.ts +0 -60
  219. package/esm/update-associations.js +0 -362
  220. package/esm/update-associations.js.map +0 -1
  221. package/esm/update-guard.d.ts +0 -26
  222. package/esm/update-guard.js +0 -122
  223. package/esm/update-guard.js.map +0 -1
  224. package/lib/collection-importer.js.map +0 -1
  225. package/lib/collection.js.map +0 -1
  226. package/lib/database.js.map +0 -1
  227. package/lib/fields/array-field.js.map +0 -1
  228. package/lib/fields/belongs-to-field.js.map +0 -1
  229. package/lib/fields/belongs-to-many-field.js.map +0 -1
  230. package/lib/fields/boolean-field.js.map +0 -1
  231. package/lib/fields/context-field.js.map +0 -1
  232. package/lib/fields/date-field.js.map +0 -1
  233. package/lib/fields/field.js.map +0 -1
  234. package/lib/fields/has-inverse-field.js.map +0 -1
  235. package/lib/fields/has-many-field.js.map +0 -1
  236. package/lib/fields/has-one-field.js.map +0 -1
  237. package/lib/fields/index.js.map +0 -1
  238. package/lib/fields/json-field.js.map +0 -1
  239. package/lib/fields/number-field.js.map +0 -1
  240. package/lib/fields/password-field.js.map +0 -1
  241. package/lib/fields/radio-field.js.map +0 -1
  242. package/lib/fields/relation-field.js.map +0 -1
  243. package/lib/fields/sort-field.js.map +0 -1
  244. package/lib/fields/string-field.js.map +0 -1
  245. package/lib/fields/text-field.js.map +0 -1
  246. package/lib/fields/time-field.js.map +0 -1
  247. package/lib/fields/uid-field.js.map +0 -1
  248. package/lib/fields/uuid-field.js.map +0 -1
  249. package/lib/fields/virtual-field.js.map +0 -1
  250. package/lib/filter-parser.js.map +0 -1
  251. package/lib/index.js.map +0 -1
  252. package/lib/magic-attribute-model.js.map +0 -1
  253. package/lib/mock-database.js.map +0 -1
  254. package/lib/model-hook.js.map +0 -1
  255. package/lib/model.js.map +0 -1
  256. package/lib/operators/array.js.map +0 -1
  257. package/lib/operators/association.js.map +0 -1
  258. package/lib/operators/date.js.map +0 -1
  259. package/lib/operators/empty.js.map +0 -1
  260. package/lib/operators/index.js.map +0 -1
  261. package/lib/operators/ne.js.map +0 -1
  262. package/lib/operators/notIn.js.map +0 -1
  263. package/lib/operators/string.js.map +0 -1
  264. package/lib/operators/utils.js.map +0 -1
  265. package/lib/options-parser.js.map +0 -1
  266. package/lib/playground.js.map +0 -1
  267. package/lib/relation-repository/belongs-to-many-repository.js.map +0 -1
  268. package/lib/relation-repository/belongs-to-repository.js.map +0 -1
  269. package/lib/relation-repository/hasmany-repository.js.map +0 -1
  270. package/lib/relation-repository/hasone-repository.js.map +0 -1
  271. package/lib/relation-repository/multiple-relation-repository.js.map +0 -1
  272. package/lib/relation-repository/relation-repository.js.map +0 -1
  273. package/lib/relation-repository/single-relation-repository.js.map +0 -1
  274. package/lib/relation-repository/types.js.map +0 -1
  275. package/lib/repository.js.map +0 -1
  276. package/lib/transaction-decorator.js.map +0 -1
  277. package/lib/update-associations.js.map +0 -1
  278. package/lib/update-guard.js.map +0 -1
  279. package/tsconfig.build.json +0 -9
  280. package/tsconfig.json +0 -5
@@ -1,10 +0,0 @@
1
- import { Op } from 'sequelize';
2
- declare const _default: {
3
- $ne(val: any, ctx: any): {
4
- [Op.or]: {
5
- [Op.ne]: any;
6
- [Op.is]: any;
7
- };
8
- };
9
- };
10
- export default _default;
@@ -1,12 +0,0 @@
1
- import { Op } from 'sequelize';
2
- export default {
3
- $ne(val, ctx) {
4
- return {
5
- [Op.or]: {
6
- [Op.ne]: val,
7
- [Op.is]: null,
8
- },
9
- };
10
- },
11
- };
12
- //# sourceMappingURL=ne.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ne.js","sourceRoot":"","sources":["../../src/operators/ne.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AAE/B,eAAe;IACb,GAAG,CAAC,GAAG,EAAE,GAAG;QACV,OAAO;YACL,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE;gBACP,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG;gBACZ,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI;aACd;SACF,CAAC;IACJ,CAAC;CACF,CAAC","sourcesContent":["import { Op } from 'sequelize';\n\nexport default {\n $ne(val, ctx) {\n return {\n [Op.or]: {\n [Op.ne]: val,\n [Op.is]: null,\n },\n };\n },\n};\n"]}
@@ -1,10 +0,0 @@
1
- import { Op } from 'sequelize';
2
- declare const _default: {
3
- $notIn(val: any, ctx: any): {
4
- [Op.or]: {
5
- [Op.notIn]: any;
6
- [Op.is]: any;
7
- };
8
- };
9
- };
10
- export default _default;
@@ -1,12 +0,0 @@
1
- import { Op } from 'sequelize';
2
- export default {
3
- $notIn(val, ctx) {
4
- return {
5
- [Op.or]: {
6
- [Op.notIn]: val,
7
- [Op.is]: null,
8
- },
9
- };
10
- },
11
- };
12
- //# sourceMappingURL=notIn.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"notIn.js","sourceRoot":"","sources":["../../src/operators/notIn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AAE/B,eAAe;IACb,MAAM,CAAC,GAAG,EAAE,GAAG;QACb,OAAO;YACL,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE;gBACP,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,GAAG;gBACf,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI;aACd;SACF,CAAC;IACJ,CAAC;CACF,CAAC","sourcesContent":["import { Op } from 'sequelize';\n\nexport default {\n $notIn(val, ctx) {\n return {\n [Op.or]: {\n [Op.notIn]: val,\n [Op.is]: null,\n },\n };\n },\n};\n"]}
@@ -1,21 +0,0 @@
1
- declare const _default: {
2
- $includes(value: any, ctx: any): {
3
- [x: symbol]: string;
4
- };
5
- $notIncludes(value: any, ctx: any): {
6
- [x: symbol]: string;
7
- };
8
- $startsWith(value: any, ctx: any): {
9
- [x: symbol]: string;
10
- };
11
- $notStartsWith(value: any, ctx: any): {
12
- [x: symbol]: string;
13
- };
14
- $endWith(value: any, ctx: any): {
15
- [x: symbol]: string;
16
- };
17
- $notEndWith(value: any, ctx: any): {
18
- [x: symbol]: string;
19
- };
20
- };
21
- export default _default;
@@ -1,35 +0,0 @@
1
- import { isPg } from './utils';
2
- import { Op } from 'sequelize';
3
- export default {
4
- $includes(value, ctx) {
5
- return {
6
- [isPg(ctx) ? Op.iLike : Op.like]: `%${value}%`,
7
- };
8
- },
9
- $notIncludes(value, ctx) {
10
- return {
11
- [isPg(ctx) ? Op.notILike : Op.notLike]: `%${value}%`,
12
- };
13
- },
14
- $startsWith(value, ctx) {
15
- return {
16
- [isPg(ctx) ? Op.iLike : Op.like]: `${value}%`,
17
- };
18
- },
19
- $notStartsWith(value, ctx) {
20
- return {
21
- [isPg(ctx) ? Op.notILike : Op.notLike]: `${value}%`,
22
- };
23
- },
24
- $endWith(value, ctx) {
25
- return {
26
- [isPg(ctx) ? Op.iLike : Op.like]: `%${value}`,
27
- };
28
- },
29
- $notEndWith(value, ctx) {
30
- return {
31
- [isPg(ctx) ? Op.notILike : Op.notLike]: `%${value}`,
32
- };
33
- },
34
- };
35
- //# sourceMappingURL=string.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"string.js","sourceRoot":"","sources":["../../src/operators/string.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AAE/B,eAAe;IACb,SAAS,CAAC,KAAK,EAAE,GAAG;QAClB,OAAO;YACL,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,KAAK,GAAG;SAC/C,CAAC;IACJ,CAAC;IAED,YAAY,CAAC,KAAK,EAAE,GAAG;QACrB,OAAO;YACL,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,IAAI,KAAK,GAAG;SACrD,CAAC;IACJ,CAAC;IAED,WAAW,CAAC,KAAK,EAAE,GAAG;QACpB,OAAO;YACL,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,GAAG;SAC9C,CAAC;IACJ,CAAC;IAED,cAAc,CAAC,KAAK,EAAE,GAAG;QACvB,OAAO;YACL,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,GAAG,KAAK,GAAG;SACpD,CAAC;IACJ,CAAC;IAED,QAAQ,CAAC,KAAK,EAAE,GAAG;QACjB,OAAO;YACL,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,KAAK,EAAE;SAC9C,CAAC;IACJ,CAAC;IAED,WAAW,CAAC,KAAK,EAAE,GAAG;QACpB,OAAO;YACL,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,IAAI,KAAK,EAAE;SACpD,CAAC;IACJ,CAAC;CACF,CAAC","sourcesContent":["import { isPg } from './utils';\nimport { Op } from 'sequelize';\n\nexport default {\n $includes(value, ctx) {\n return {\n [isPg(ctx) ? Op.iLike : Op.like]: `%${value}%`,\n };\n },\n\n $notIncludes(value, ctx) {\n return {\n [isPg(ctx) ? Op.notILike : Op.notLike]: `%${value}%`,\n };\n },\n\n $startsWith(value, ctx) {\n return {\n [isPg(ctx) ? Op.iLike : Op.like]: `${value}%`,\n };\n },\n\n $notStartsWith(value, ctx) {\n return {\n [isPg(ctx) ? Op.notILike : Op.notLike]: `${value}%`,\n };\n },\n\n $endWith(value, ctx) {\n return {\n [isPg(ctx) ? Op.iLike : Op.like]: `%${value}`,\n };\n },\n\n $notEndWith(value, ctx) {\n return {\n [isPg(ctx) ? Op.notILike : Op.notLike]: `%${value}`,\n };\n },\n};\n"]}
@@ -1,4 +0,0 @@
1
- declare const getDialect: (ctx: any) => any;
2
- declare const isPg: (ctx: any) => boolean;
3
- declare const isMySQL: (ctx: any) => boolean;
4
- export { getDialect, isPg, isMySQL };
@@ -1,11 +0,0 @@
1
- const getDialect = (ctx) => {
2
- return ctx.db.sequelize.getDialect();
3
- };
4
- const isPg = (ctx) => {
5
- return getDialect(ctx) === 'postgres';
6
- };
7
- const isMySQL = (ctx) => {
8
- return getDialect(ctx) === 'mysql';
9
- };
10
- export { getDialect, isPg, isMySQL };
11
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/operators/utils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,GAAG,CAAC,GAAG,EAAE,EAAE;IACzB,OAAO,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC;AACvC,CAAC,CAAC;AAEF,MAAM,IAAI,GAAG,CAAC,GAAG,EAAE,EAAE;IACnB,OAAO,UAAU,CAAC,GAAG,CAAC,KAAK,UAAU,CAAC;AACxC,CAAC,CAAC;AAEF,MAAM,OAAO,GAAG,CAAC,GAAG,EAAE,EAAE;IACtB,OAAO,UAAU,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC;AACrC,CAAC,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC","sourcesContent":["const getDialect = (ctx) => {\n return ctx.db.sequelize.getDialect();\n};\n\nconst isPg = (ctx) => {\n return getDialect(ctx) === 'postgres';\n};\n\nconst isMySQL = (ctx) => {\n return getDialect(ctx) === 'mysql';\n};\n\nexport { getDialect, isPg, isMySQL };\n"]}
@@ -1,31 +0,0 @@
1
- import { ModelCtor } from 'sequelize';
2
- import { Collection } from './collection';
3
- import { Database } from './database';
4
- import FilterParser from './filter-parser';
5
- import { Appends, Except, FindOptions } from './repository';
6
- interface OptionsParserContext {
7
- collection: Collection;
8
- targetKey?: string;
9
- }
10
- export declare class OptionsParser {
11
- options: FindOptions;
12
- database: Database;
13
- collection: Collection;
14
- model: ModelCtor<any>;
15
- filterParser: FilterParser;
16
- context: OptionsParserContext;
17
- constructor(options: FindOptions, context: OptionsParserContext);
18
- isAssociation(key: string): boolean;
19
- isAssociationPath(path: string): boolean;
20
- toSequelizeParams(): any;
21
- /**
22
- * parser sort options
23
- * @param filterParams
24
- * @protected
25
- */
26
- protected parseSort(filterParams: any): any;
27
- protected parseFields(filterParams: any): any;
28
- protected parseExcept(except: Except, filterParams: any): any;
29
- protected parseAppends(appends: Appends, filterParams: any): any;
30
- }
31
- export {};
@@ -1,225 +0,0 @@
1
- import { Op } from 'sequelize';
2
- import FilterParser from './filter-parser';
3
- const debug = require('debug')('noco-database');
4
- export class OptionsParser {
5
- constructor(options, context) {
6
- const { collection } = context;
7
- this.collection = collection;
8
- this.model = collection.model;
9
- this.options = options;
10
- this.database = collection.context.database;
11
- this.filterParser = new FilterParser(options === null || options === void 0 ? void 0 : options.filter, {
12
- collection,
13
- app: {
14
- ctx: options === null || options === void 0 ? void 0 : options.context,
15
- },
16
- });
17
- this.context = context;
18
- }
19
- isAssociation(key) {
20
- return this.model.associations[key] !== undefined;
21
- }
22
- isAssociationPath(path) {
23
- return this.isAssociation(path.split('.')[0]);
24
- }
25
- toSequelizeParams() {
26
- var _a;
27
- const queryParams = this.filterParser.toSequelizeParams();
28
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.filterByTk) {
29
- queryParams.where = {
30
- [Op.and]: [
31
- queryParams.where,
32
- {
33
- [this.context.targetKey || this.collection.filterTargetKey]: this.options.filterByTk,
34
- },
35
- ],
36
- };
37
- }
38
- return this.parseSort(this.parseFields(queryParams));
39
- }
40
- /**
41
- * parser sort options
42
- * @param filterParams
43
- * @protected
44
- */
45
- parseSort(filterParams) {
46
- var _a;
47
- let sort = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.sort) || [];
48
- if (typeof sort === 'string') {
49
- sort = sort.split(',');
50
- }
51
- const orderParams = sort.map((sortKey) => {
52
- const direction = sortKey.startsWith('-') ? 'DESC' : 'ASC';
53
- const sortField = sortKey.replace('-', '').split('.');
54
- // handle sort by association
55
- if (sortField.length > 1) {
56
- let associationModel = this.model;
57
- for (let i = 0; i < sortField.length - 1; i++) {
58
- const associationKey = sortField[i];
59
- sortField[i] = associationModel.associations[associationKey].target;
60
- associationModel = sortField[i];
61
- }
62
- }
63
- sortField.push(direction);
64
- return sortField;
65
- });
66
- if (orderParams.length > 0) {
67
- return Object.assign({ order: orderParams }, filterParams);
68
- }
69
- return filterParams;
70
- }
71
- parseFields(filterParams) {
72
- var _a, _b, _c;
73
- const appends = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.appends) || [];
74
- const except = [];
75
- let attributes = {
76
- include: [],
77
- exclude: [],
78
- }; // out put all fields by default
79
- if ((_b = this.options) === null || _b === void 0 ? void 0 : _b.fields) {
80
- // 将fields拆分为 attributes 和 appends
81
- for (const field of this.options.fields) {
82
- if (this.isAssociationPath(field)) {
83
- // field is association field
84
- appends.push(field);
85
- }
86
- else {
87
- // field is model attribute, change attributes to array type
88
- if (!Array.isArray(attributes))
89
- attributes = [];
90
- attributes.push(field);
91
- }
92
- }
93
- }
94
- if ((_c = this.options) === null || _c === void 0 ? void 0 : _c.except) {
95
- for (const exceptKey of this.options.except) {
96
- if (this.isAssociationPath(exceptKey)) {
97
- // except association field
98
- except.push(exceptKey);
99
- }
100
- else {
101
- // if attributes is array form, ignore except
102
- if (Array.isArray(attributes))
103
- continue;
104
- attributes.exclude.push(exceptKey);
105
- }
106
- }
107
- }
108
- return Object.assign({ attributes }, this.parseExcept(except, this.parseAppends(appends, filterParams)));
109
- }
110
- parseExcept(except, filterParams) {
111
- if (!except)
112
- return filterParams;
113
- const setExcept = (queryParams, except) => {
114
- // split exceptKey to path form
115
- // posts.comments.content => ['posts', 'comments', 'content']
116
- // then set except on include attributes
117
- const exceptPath = except.split('.');
118
- const association = exceptPath[0];
119
- const lastLevel = exceptPath.length <= 2;
120
- let existIncludeIndex = queryParams['include'].findIndex((include) => include['association'] == association);
121
- if (existIncludeIndex == -1) {
122
- // if include not exists, ignore this except
123
- return;
124
- }
125
- if (lastLevel) {
126
- // if it not have exclude form
127
- if (Array.isArray(queryParams['include'][existIncludeIndex]['attributes'])) {
128
- return;
129
- }
130
- else {
131
- if (!queryParams['include'][existIncludeIndex]['attributes']['exclude']) {
132
- queryParams['include'][existIncludeIndex]['attributes']['exclude'] = [];
133
- }
134
- queryParams['include'][existIncludeIndex]['attributes']['exclude'].push(exceptPath[1]);
135
- }
136
- }
137
- else {
138
- setExcept(queryParams['include'][existIncludeIndex], exceptPath.filter((_, index) => index !== 0).join('.'));
139
- }
140
- };
141
- for (const exceptKey of except) {
142
- setExcept(filterParams, exceptKey);
143
- }
144
- return filterParams;
145
- }
146
- parseAppends(appends, filterParams) {
147
- if (!appends)
148
- return filterParams;
149
- /**
150
- * set include params
151
- * @param includeRoot
152
- * @param appends
153
- */
154
- const setInclude = (model, queryParams, append) => {
155
- const appendFields = append.split('.');
156
- const appendAssociation = appendFields[0];
157
- const associations = model.associations;
158
- // if append length less or equal 2
159
- // example:
160
- // appends: ['posts']
161
- // appends: ['posts.title']
162
- // All of these can be seen as last level
163
- let lastLevel = false;
164
- if (appendFields.length == 1) {
165
- lastLevel = true;
166
- }
167
- if (appendFields.length == 2) {
168
- const associationModel = associations[appendFields[0]].target;
169
- if (associationModel.rawAttributes[appendFields[1]]) {
170
- lastLevel = true;
171
- }
172
- }
173
- // find association index
174
- if (queryParams['include'] == undefined) {
175
- queryParams['include'] = [];
176
- }
177
- let existIncludeIndex = queryParams['include'].findIndex((include) => include['association'] == appendAssociation);
178
- // if association not exist, create it
179
- if (existIncludeIndex == -1) {
180
- // association not exists
181
- queryParams['include'].push({
182
- association: appendAssociation,
183
- });
184
- existIncludeIndex = queryParams['include'].length - 1;
185
- }
186
- // end appends
187
- // without nests association
188
- if (lastLevel) {
189
- // get exist association attributes
190
- let attributes = queryParams['include'][existIncludeIndex]['attributes'] || {
191
- include: [], // all fields are output by default
192
- };
193
- // if need set attribute
194
- if (appendFields.length == 2) {
195
- if (!Array.isArray(attributes)) {
196
- attributes = [];
197
- }
198
- const attributeName = appendFields[1];
199
- // push field to it
200
- attributes.push(attributeName);
201
- }
202
- else {
203
- // if attributes is empty array, change it to object
204
- if (Array.isArray(attributes) && attributes.length == 0) {
205
- attributes = {
206
- include: [],
207
- };
208
- }
209
- }
210
- // set new attributes
211
- queryParams['include'][existIncludeIndex] = Object.assign(Object.assign({}, queryParams['include'][existIncludeIndex]), { attributes });
212
- }
213
- else {
214
- setInclude(model.associations[queryParams['include'][existIncludeIndex].association].target, queryParams['include'][existIncludeIndex], appendFields.filter((_, index) => index !== 0).join('.'));
215
- }
216
- };
217
- // handle every appends
218
- for (const append of appends) {
219
- setInclude(this.model, filterParams, append);
220
- }
221
- debug('filter params: %o', filterParams);
222
- return filterParams;
223
- }
224
- }
225
- //# sourceMappingURL=options-parser.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"options-parser.js","sourceRoot":"","sources":["../src/options-parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,EAAE,EAAE,MAAM,WAAW,CAAC;AAGhE,OAAO,YAAY,MAAM,iBAAiB,CAAC;AAG3C,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,eAAe,CAAC,CAAC;A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CAAC,iBAAiB,CAAC,KAC5C,UAAU,GACX,CAAC;aACH;iBAAM;gBACL,UAAU,CACR,KAAK,CAAC,YAAY,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,iBAAiB,CAAC,CAAC,WAAW,CAAC,CAAC,MAAM,EAChF,WAAW,CAAC,SAAS,CAAC,CAAC,iBAAiB,CAAC,EACzC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CACzD,CAAC;aACH;QACH,CAAC,CAAC;QAEF,uBAAuB;QACvB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;YAC5B,UAAU,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,EAAE,MAAM,CAAC,CAAC;SAC9C;QAED,KAAK,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;QACzC,OAAO,YAAY,CAAC;IACtB,CAAC;CACF","sourcesContent":["import { FindAttributeOptions, ModelCtor, Op } from 'sequelize';\nimport { Collection } from './collection';\nimport { Database } from './database';\nimport FilterParser from './filter-parser';\nimport { Appends, Except, FindOptions } from './repository';\n\nconst debug = require('debug')('noco-database');\n\ninterface OptionsParserContext {\n collection: Collection;\n targetKey?: string;\n}\n\nexport class OptionsParser {\n options: FindOptions;\n database: Database;\n collection: Collection;\n model: ModelCtor<any>;\n filterParser: FilterParser;\n context: OptionsParserContext;\n\n constructor(options: FindOptions, context: OptionsParserContext) {\n const { collection } = context;\n\n this.collection = collection;\n this.model = collection.model;\n this.options = options;\n this.database = collection.context.database;\n this.filterParser = new FilterParser(options?.filter, {\n collection,\n app: {\n ctx: options?.context,\n },\n });\n this.context = context;\n }\n\n isAssociation(key: string) {\n return this.model.associations[key] !== undefined;\n }\n\n isAssociationPath(path: string) {\n return this.isAssociation(path.split('.')[0]);\n }\n\n toSequelizeParams() {\n const queryParams = this.filterParser.toSequelizeParams();\n\n if (this.options?.filterByTk) {\n queryParams.where = {\n [Op.and]: [\n queryParams.where,\n {\n [this.context.targetKey || this.collection.filterTargetKey]: this.options.filterByTk,\n },\n ],\n };\n }\n\n return this.parseSort(this.parseFields(queryParams));\n }\n\n /**\n * parser sort options\n * @param filterParams\n * @protected\n */\n protected parseSort(filterParams) {\n let sort = this.options?.sort || [];\n if (typeof sort === 'string') {\n sort = sort.split(',');\n }\n const orderParams = sort.map((sortKey: string) => {\n const direction = sortKey.startsWith('-') ? 'DESC' : 'ASC';\n const sortField: Array<any> = sortKey.replace('-', '').split('.');\n\n // handle sort by association\n if (sortField.length > 1) {\n let associationModel = this.model;\n\n for (let i = 0; i < sortField.length - 1; i++) {\n const associationKey = sortField[i];\n sortField[i] = associationModel.associations[associationKey].target;\n associationModel = sortField[i];\n }\n }\n\n sortField.push(direction);\n return sortField;\n });\n\n if (orderParams.length > 0) {\n return {\n order: orderParams,\n ...filterParams,\n };\n }\n\n return filterParams;\n }\n\n protected parseFields(filterParams: any) {\n const appends = this.options?.appends || [];\n const except = [];\n\n let attributes: FindAttributeOptions = {\n include: [],\n exclude: [],\n }; // out put all fields by default\n\n if (this.options?.fields) {\n // 将fields拆分为 attributes 和 appends\n for (const field of this.options.fields) {\n if (this.isAssociationPath(field)) {\n // field is association field\n appends.push(field);\n } else {\n // field is model attribute, change attributes to array type\n if (!Array.isArray(attributes)) attributes = [];\n\n attributes.push(field);\n }\n }\n }\n\n if (this.options?.except) {\n for (const exceptKey of this.options.except) {\n if (this.isAssociationPath(exceptKey)) {\n // except association field\n except.push(exceptKey);\n } else {\n // if attributes is array form, ignore except\n if (Array.isArray(attributes)) continue;\n attributes.exclude.push(exceptKey);\n }\n }\n }\n\n return {\n attributes,\n ...this.parseExcept(except, this.parseAppends(appends, filterParams)),\n };\n }\n\n protected parseExcept(except: Except, filterParams: any) {\n if (!except) return filterParams;\n const setExcept = (queryParams: any, except: string) => {\n // split exceptKey to path form\n // posts.comments.content => ['posts', 'comments', 'content']\n // then set except on include attributes\n const exceptPath = except.split('.');\n const association = exceptPath[0];\n const lastLevel = exceptPath.length <= 2;\n\n let existIncludeIndex = queryParams['include'].findIndex((include) => include['association'] == association);\n\n if (existIncludeIndex == -1) {\n // if include not exists, ignore this except\n return;\n }\n\n if (lastLevel) {\n // if it not have exclude form\n if (Array.isArray(queryParams['include'][existIncludeIndex]['attributes'])) {\n return;\n } else {\n if (!queryParams['include'][existIncludeIndex]['attributes']['exclude']) {\n queryParams['include'][existIncludeIndex]['attributes']['exclude'] = [];\n }\n\n queryParams['include'][existIncludeIndex]['attributes']['exclude'].push(exceptPath[1]);\n }\n } else {\n setExcept(queryParams['include'][existIncludeIndex], exceptPath.filter((_, index) => index !== 0).join('.'));\n }\n };\n\n for (const exceptKey of except) {\n setExcept(filterParams, exceptKey);\n }\n\n return filterParams;\n }\n\n protected parseAppends(appends: Appends, filterParams: any) {\n if (!appends) return filterParams;\n\n /**\n * set include params\n * @param includeRoot\n * @param appends\n */\n const setInclude = (model: ModelCtor<any>, queryParams: any, append: string) => {\n const appendFields = append.split('.');\n const appendAssociation = appendFields[0];\n\n const associations = model.associations;\n\n // if append length less or equal 2\n // example:\n // appends: ['posts']\n // appends: ['posts.title']\n // All of these can be seen as last level\n let lastLevel: boolean = false;\n\n if (appendFields.length == 1) {\n lastLevel = true;\n }\n\n if (appendFields.length == 2) {\n const associationModel = associations[appendFields[0]].target;\n if (associationModel.rawAttributes[appendFields[1]]) {\n lastLevel = true;\n }\n }\n\n // find association index\n if (queryParams['include'] == undefined) {\n queryParams['include'] = [];\n }\n\n let existIncludeIndex = queryParams['include'].findIndex(\n (include) => include['association'] == appendAssociation,\n );\n\n // if association not exist, create it\n if (existIncludeIndex == -1) {\n // association not exists\n queryParams['include'].push({\n association: appendAssociation,\n });\n\n existIncludeIndex = queryParams['include'].length - 1;\n }\n\n // end appends\n // without nests association\n if (lastLevel) {\n // get exist association attributes\n let attributes = queryParams['include'][existIncludeIndex]['attributes'] || {\n include: [], // all fields are output by default\n };\n\n // if need set attribute\n if (appendFields.length == 2) {\n if (!Array.isArray(attributes)) {\n attributes = [];\n }\n\n const attributeName = appendFields[1];\n\n // push field to it\n attributes.push(attributeName);\n } else {\n // if attributes is empty array, change it to object\n if (Array.isArray(attributes) && attributes.length == 0) {\n attributes = {\n include: [],\n };\n }\n }\n\n // set new attributes\n queryParams['include'][existIncludeIndex] = {\n ...queryParams['include'][existIncludeIndex],\n attributes,\n };\n } else {\n setInclude(\n model.associations[queryParams['include'][existIncludeIndex].association].target,\n queryParams['include'][existIncludeIndex],\n appendFields.filter((_, index) => index !== 0).join('.'),\n );\n }\n };\n\n // handle every appends\n for (const append of appends) {\n setInclude(this.model, filterParams, append);\n }\n\n debug('filter params: %o', filterParams);\n return filterParams;\n }\n}\n"]}
@@ -1 +0,0 @@
1
- export {};
package/esm/playground.js DELETED
@@ -1,53 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import { Database } from './database';
11
- const db = new Database({
12
- dialect: 'sqlite',
13
- dialectModule: require('sqlite3'),
14
- storage: ':memory:',
15
- });
16
- (() => __awaiter(void 0, void 0, void 0, function* () {
17
- const User = db.collection({
18
- name: 'users',
19
- fields: [{ type: 'string', name: 'name' }],
20
- });
21
- const Post = db.collection({
22
- name: 'posts',
23
- fields: [
24
- { type: 'string', name: 'title' },
25
- {
26
- type: 'belongsTo',
27
- name: 'user',
28
- },
29
- ],
30
- });
31
- yield db.sync();
32
- const repository = User.repository;
33
- yield repository.createMany({
34
- records: [
35
- { name: 'u1', posts: [{ title: 'u1t1' }] },
36
- { name: 'u2', posts: [{ title: 'u2t1' }] },
37
- { name: 'u3', posts: [{ title: 'u3t1' }] },
38
- ],
39
- });
40
- const Model = User.model;
41
- const user = yield Model.findOne({
42
- subQuery: false,
43
- where: {
44
- '$posts.title$': 'u1t1',
45
- },
46
- include: { association: 'posts', attributes: [] },
47
- attributes: {
48
- include: [],
49
- },
50
- });
51
- console.log(user.toJSON());
52
- }))();
53
- //# sourceMappingURL=playground.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"playground.js","sourceRoot":"","sources":["../src/playground.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC;IACtB,OAAO,EAAE,QAAQ;IACjB,aAAa,EAAE,OAAO,CAAC,SAAS,CAAC;IACjC,OAAO,EAAE,UAAU;CACpB,CAAC,CAAC;AAEH,CAAC,GAAS,EAAE;IACV,MAAM,IAAI,GAAG,EAAE,CAAC,UAAU,CAAC;QACzB,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;KAC3C,CAAC,CAAC;IAEH,MAAM,IAAI,GAAG,EAAE,CAAC,UAAU,CAAC;QACzB,IAAI,EAAE,OAAO;QACb,MAAM,EAAE;YACN,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE;YACjC;gBACE,IAAI,EAAE,WAAW;gBACjB,IAAI,EAAE,MAAM;aACb;SACF;KACF,CAAC,CAAC;IAEH,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC;IAEhB,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;IAEnC,MAAM,UAAU,CAAC,UAAU,CAAC;QAC1B,OAAO,EAAE;YACP,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE;YAC1C,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE;YAC1C,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE;SAC3C;KACF,CAAC,CAAC;IAEH,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;IACzB,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC;QAC/B,QAAQ,EAAE,KAAK;QACf,KAAK,EAAE;YACL,eAAe,EAAE,MAAM;SACxB;QACD,OAAO,EAAE,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE;QACjD,UAAU,EAAE;YACV,OAAO,EAAE,EAAE;SACZ;KACF,CAAC,CAAC;IAEH,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;AAC7B,CAAC,CAAA,CAAC,EAAE,CAAC","sourcesContent":["import { Database } from './database';\nimport FilterParser from './filter-parser';\n\nconst db = new Database({\n dialect: 'sqlite',\n dialectModule: require('sqlite3'),\n storage: ':memory:',\n});\n\n(async () => {\n const User = db.collection({\n name: 'users',\n fields: [{ type: 'string', name: 'name' }],\n });\n\n const Post = db.collection({\n name: 'posts',\n fields: [\n { type: 'string', name: 'title' },\n {\n type: 'belongsTo',\n name: 'user',\n },\n ],\n });\n\n await db.sync();\n\n const repository = User.repository;\n\n await repository.createMany({\n records: [\n { name: 'u1', posts: [{ title: 'u1t1' }] },\n { name: 'u2', posts: [{ title: 'u2t1' }] },\n { name: 'u3', posts: [{ title: 'u3t1' }] },\n ],\n });\n\n const Model = User.model;\n const user = await Model.findOne({\n subQuery: false,\n where: {\n '$posts.title$': 'u1t1',\n },\n include: { association: 'posts', attributes: [] },\n attributes: {\n include: [],\n },\n });\n\n console.log(user.toJSON());\n})();\n"]}
@@ -1,36 +0,0 @@
1
- import { Transaction } from 'sequelize';
2
- import { Model } from '../model';
3
- import { CreateOptions, DestroyOptions, FindOptions, TargetKey, UpdateOptions } from '../repository';
4
- import { FindAndCountOptions, FindOneOptions, MultipleRelationRepository } from './multiple-relation-repository';
5
- import { AssociatedOptions, PrimaryKeyWithThroughValues } from './types';
6
- declare type CreateBelongsToManyOptions = CreateOptions;
7
- interface IBelongsToManyRepository<M extends Model> {
8
- find(options?: FindOptions): Promise<M[]>;
9
- findAndCount(options?: FindAndCountOptions): Promise<[M[], number]>;
10
- findOne(options?: FindOneOptions): Promise<M>;
11
- create(options?: CreateBelongsToManyOptions): Promise<M>;
12
- update(options?: UpdateOptions): Promise<M>;
13
- destroy(options?: number | string | number[] | string[] | DestroyOptions): Promise<Boolean>;
14
- set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
15
- add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
16
- remove(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
17
- toggle(options: TargetKey | {
18
- pk?: TargetKey;
19
- transaction?: Transaction;
20
- }): Promise<void>;
21
- }
22
- export declare class BelongsToManyRepository extends MultipleRelationRepository implements IBelongsToManyRepository<any> {
23
- create(options?: CreateBelongsToManyOptions): Promise<any>;
24
- destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<Boolean>;
25
- protected setTargets(call: 'add' | 'set', options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
26
- add(options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
27
- set(options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
28
- toggle(options: TargetKey | {
29
- tk?: TargetKey;
30
- transaction?: Transaction;
31
- }): Promise<void>;
32
- extendFindOptions(findOptions: any): any;
33
- throughName(): any;
34
- throughModel(): any;
35
- }
36
- export {};