@strapi/database 5.12.1 → 5.12.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (321) hide show
  1. package/dist/connection.js +43 -0
  2. package/dist/connection.js.map +1 -0
  3. package/dist/connection.mjs +41 -0
  4. package/dist/connection.mjs.map +1 -0
  5. package/dist/dialects/dialect.js +54 -0
  6. package/dist/dialects/dialect.js.map +1 -0
  7. package/dist/dialects/dialect.mjs +52 -0
  8. package/dist/dialects/dialect.mjs.map +1 -0
  9. package/dist/dialects/index.js +44 -0
  10. package/dist/dialects/index.js.map +1 -0
  11. package/dist/dialects/index.mjs +42 -0
  12. package/dist/dialects/index.mjs.map +1 -0
  13. package/dist/dialects/mysql/constants.js +8 -0
  14. package/dist/dialects/mysql/constants.js.map +1 -0
  15. package/dist/dialects/mysql/constants.mjs +5 -0
  16. package/dist/dialects/mysql/constants.mjs.map +1 -0
  17. package/dist/dialects/mysql/database-inspector.js +35 -0
  18. package/dist/dialects/mysql/database-inspector.js.map +1 -0
  19. package/dist/dialects/mysql/database-inspector.mjs +33 -0
  20. package/dist/dialects/mysql/database-inspector.mjs.map +1 -0
  21. package/dist/dialects/mysql/index.js +75 -0
  22. package/dist/dialects/mysql/index.js.map +1 -0
  23. package/dist/dialects/mysql/index.mjs +73 -0
  24. package/dist/dialects/mysql/index.mjs.map +1 -0
  25. package/dist/dialects/mysql/schema-inspector.js +297 -0
  26. package/dist/dialects/mysql/schema-inspector.js.map +1 -0
  27. package/dist/dialects/mysql/schema-inspector.mjs +295 -0
  28. package/dist/dialects/mysql/schema-inspector.mjs.map +1 -0
  29. package/dist/dialects/postgresql/index.js +62 -0
  30. package/dist/dialects/postgresql/index.js.map +1 -0
  31. package/dist/dialects/postgresql/index.mjs +60 -0
  32. package/dist/dialects/postgresql/index.mjs.map +1 -0
  33. package/dist/dialects/postgresql/schema-inspector.js +316 -0
  34. package/dist/dialects/postgresql/schema-inspector.js.map +1 -0
  35. package/dist/dialects/postgresql/schema-inspector.mjs +314 -0
  36. package/dist/dialects/postgresql/schema-inspector.mjs.map +1 -0
  37. package/dist/dialects/sqlite/index.js +82 -0
  38. package/dist/dialects/sqlite/index.js.map +1 -0
  39. package/dist/dialects/sqlite/index.mjs +80 -0
  40. package/dist/dialects/sqlite/index.mjs.map +1 -0
  41. package/dist/dialects/sqlite/schema-inspector.js +211 -0
  42. package/dist/dialects/sqlite/schema-inspector.js.map +1 -0
  43. package/dist/dialects/sqlite/schema-inspector.mjs +209 -0
  44. package/dist/dialects/sqlite/schema-inspector.mjs.map +1 -0
  45. package/dist/entity-manager/entity-repository.js +139 -0
  46. package/dist/entity-manager/entity-repository.js.map +1 -0
  47. package/dist/entity-manager/entity-repository.mjs +137 -0
  48. package/dist/entity-manager/entity-repository.mjs.map +1 -0
  49. package/dist/entity-manager/index.js +1186 -0
  50. package/dist/entity-manager/index.js.map +1 -0
  51. package/dist/entity-manager/index.mjs +1184 -0
  52. package/dist/entity-manager/index.mjs.map +1 -0
  53. package/dist/entity-manager/morph-relations.js +73 -0
  54. package/dist/entity-manager/morph-relations.js.map +1 -0
  55. package/dist/entity-manager/morph-relations.mjs +69 -0
  56. package/dist/entity-manager/morph-relations.mjs.map +1 -0
  57. package/dist/entity-manager/regular-relations.js +247 -0
  58. package/dist/entity-manager/regular-relations.js.map +1 -0
  59. package/dist/entity-manager/regular-relations.mjs +242 -0
  60. package/dist/entity-manager/regular-relations.mjs.map +1 -0
  61. package/dist/entity-manager/relations-orderer.js +221 -0
  62. package/dist/entity-manager/relations-orderer.js.map +1 -0
  63. package/dist/entity-manager/relations-orderer.mjs +218 -0
  64. package/dist/entity-manager/relations-orderer.mjs.map +1 -0
  65. package/dist/errors/database.js +13 -0
  66. package/dist/errors/database.js.map +1 -0
  67. package/dist/errors/database.mjs +11 -0
  68. package/dist/errors/database.mjs.map +1 -0
  69. package/dist/errors/index.js +18 -0
  70. package/dist/errors/index.js.map +1 -0
  71. package/dist/errors/index.mjs +7 -0
  72. package/dist/errors/index.mjs.map +1 -0
  73. package/dist/errors/invalid-date.js +13 -0
  74. package/dist/errors/invalid-date.js.map +1 -0
  75. package/dist/errors/invalid-date.mjs +11 -0
  76. package/dist/errors/invalid-date.mjs.map +1 -0
  77. package/dist/errors/invalid-datetime.js +13 -0
  78. package/dist/errors/invalid-datetime.js.map +1 -0
  79. package/dist/errors/invalid-datetime.mjs +11 -0
  80. package/dist/errors/invalid-datetime.mjs.map +1 -0
  81. package/dist/errors/invalid-relation.js +13 -0
  82. package/dist/errors/invalid-relation.js.map +1 -0
  83. package/dist/errors/invalid-relation.mjs +11 -0
  84. package/dist/errors/invalid-relation.mjs.map +1 -0
  85. package/dist/errors/invalid-time.js +13 -0
  86. package/dist/errors/invalid-time.js.map +1 -0
  87. package/dist/errors/invalid-time.mjs +11 -0
  88. package/dist/errors/invalid-time.mjs.map +1 -0
  89. package/dist/errors/not-null.js +17 -0
  90. package/dist/errors/not-null.js.map +1 -0
  91. package/dist/errors/not-null.mjs +15 -0
  92. package/dist/errors/not-null.mjs.map +1 -0
  93. package/dist/fields/biginteger.js +9 -0
  94. package/dist/fields/biginteger.js.map +1 -0
  95. package/dist/fields/biginteger.mjs +7 -0
  96. package/dist/fields/biginteger.mjs.map +1 -0
  97. package/dist/fields/boolean.js +48 -0
  98. package/dist/fields/boolean.js.map +1 -0
  99. package/dist/fields/boolean.mjs +46 -0
  100. package/dist/fields/boolean.mjs.map +1 -0
  101. package/dist/fields/date.js +16 -0
  102. package/dist/fields/date.js.map +1 -0
  103. package/dist/fields/date.mjs +14 -0
  104. package/dist/fields/date.mjs.map +1 -0
  105. package/dist/fields/datetime.js +37 -0
  106. package/dist/fields/datetime.js.map +1 -0
  107. package/dist/fields/datetime.mjs +16 -0
  108. package/dist/fields/datetime.mjs.map +1 -0
  109. package/dist/fields/field.js +16 -0
  110. package/dist/fields/field.js.map +1 -0
  111. package/dist/fields/field.mjs +14 -0
  112. package/dist/fields/field.mjs.map +1 -0
  113. package/dist/fields/index.js +45 -0
  114. package/dist/fields/index.js.map +1 -0
  115. package/dist/fields/index.mjs +43 -0
  116. package/dist/fields/index.mjs.map +1 -0
  117. package/dist/fields/json.js +36 -0
  118. package/dist/fields/json.js.map +1 -0
  119. package/dist/fields/json.mjs +34 -0
  120. package/dist/fields/json.mjs.map +1 -0
  121. package/dist/fields/number.js +20 -0
  122. package/dist/fields/number.js.map +1 -0
  123. package/dist/fields/number.mjs +18 -0
  124. package/dist/fields/number.mjs.map +1 -0
  125. package/dist/fields/shared/parsers.js +91 -0
  126. package/dist/fields/shared/parsers.js.map +1 -0
  127. package/dist/fields/shared/parsers.mjs +68 -0
  128. package/dist/fields/shared/parsers.mjs.map +1 -0
  129. package/dist/fields/string.js +16 -0
  130. package/dist/fields/string.js.map +1 -0
  131. package/dist/fields/string.mjs +14 -0
  132. package/dist/fields/string.mjs.map +1 -0
  133. package/dist/fields/time.js +17 -0
  134. package/dist/fields/time.js.map +1 -0
  135. package/dist/fields/time.mjs +15 -0
  136. package/dist/fields/time.mjs.map +1 -0
  137. package/dist/fields/timestamp.js +37 -0
  138. package/dist/fields/timestamp.js.map +1 -0
  139. package/dist/fields/timestamp.mjs +16 -0
  140. package/dist/fields/timestamp.mjs.map +1 -0
  141. package/dist/index.js +33 -8569
  142. package/dist/index.js.map +1 -1
  143. package/dist/index.mjs +16 -8532
  144. package/dist/index.mjs.map +1 -1
  145. package/dist/lifecycles/index.js +73 -0
  146. package/dist/lifecycles/index.js.map +1 -0
  147. package/dist/lifecycles/index.mjs +71 -0
  148. package/dist/lifecycles/index.mjs.map +1 -0
  149. package/dist/lifecycles/subscribers/index.js +10 -0
  150. package/dist/lifecycles/subscribers/index.js.map +1 -0
  151. package/dist/lifecycles/subscribers/index.mjs +8 -0
  152. package/dist/lifecycles/subscribers/index.mjs.map +1 -0
  153. package/dist/lifecycles/subscribers/models-lifecycles.js +13 -0
  154. package/dist/lifecycles/subscribers/models-lifecycles.js.map +1 -0
  155. package/dist/lifecycles/subscribers/models-lifecycles.mjs +11 -0
  156. package/dist/lifecycles/subscribers/models-lifecycles.mjs.map +1 -0
  157. package/dist/lifecycles/subscribers/timestamps.js +55 -0
  158. package/dist/lifecycles/subscribers/timestamps.js.map +1 -0
  159. package/dist/lifecycles/subscribers/timestamps.mjs +53 -0
  160. package/dist/lifecycles/subscribers/timestamps.mjs.map +1 -0
  161. package/dist/metadata/index.js +24 -0
  162. package/dist/metadata/index.js.map +1 -0
  163. package/dist/metadata/index.mjs +16 -0
  164. package/dist/metadata/index.mjs.map +1 -0
  165. package/dist/metadata/metadata.js +100 -0
  166. package/dist/metadata/metadata.js.map +1 -0
  167. package/dist/metadata/metadata.mjs +98 -0
  168. package/dist/metadata/metadata.mjs.map +1 -0
  169. package/dist/metadata/relations.js +545 -0
  170. package/dist/metadata/relations.js.map +1 -0
  171. package/dist/metadata/relations.mjs +536 -0
  172. package/dist/metadata/relations.mjs.map +1 -0
  173. package/dist/migrations/common.js +8 -0
  174. package/dist/migrations/common.js.map +1 -0
  175. package/dist/migrations/common.mjs +6 -0
  176. package/dist/migrations/common.mjs.map +1 -0
  177. package/dist/migrations/index.js +39 -0
  178. package/dist/migrations/index.js.map +1 -0
  179. package/dist/migrations/index.mjs +37 -0
  180. package/dist/migrations/index.mjs.map +1 -0
  181. package/dist/migrations/internal-migrations/5.0.0-01-convert-identifiers-long-than-max-length.js +179 -0
  182. package/dist/migrations/internal-migrations/5.0.0-01-convert-identifiers-long-than-max-length.js.map +1 -0
  183. package/dist/migrations/internal-migrations/5.0.0-01-convert-identifiers-long-than-max-length.mjs +177 -0
  184. package/dist/migrations/internal-migrations/5.0.0-01-convert-identifiers-long-than-max-length.mjs.map +1 -0
  185. package/dist/migrations/internal-migrations/5.0.0-02-document-id.js +125 -0
  186. package/dist/migrations/internal-migrations/5.0.0-02-document-id.js.map +1 -0
  187. package/dist/migrations/internal-migrations/5.0.0-02-document-id.mjs +123 -0
  188. package/dist/migrations/internal-migrations/5.0.0-02-document-id.mjs.map +1 -0
  189. package/dist/migrations/internal-migrations/5.0.0-03-locale.js +41 -0
  190. package/dist/migrations/internal-migrations/5.0.0-03-locale.js.map +1 -0
  191. package/dist/migrations/internal-migrations/5.0.0-03-locale.mjs +39 -0
  192. package/dist/migrations/internal-migrations/5.0.0-03-locale.mjs.map +1 -0
  193. package/dist/migrations/internal-migrations/5.0.0-04-published-at.js +45 -0
  194. package/dist/migrations/internal-migrations/5.0.0-04-published-at.js.map +1 -0
  195. package/dist/migrations/internal-migrations/5.0.0-04-published-at.mjs +43 -0
  196. package/dist/migrations/internal-migrations/5.0.0-04-published-at.mjs.map +1 -0
  197. package/dist/migrations/internal-migrations/5.0.0-05-drop-slug-unique-index.js +43 -0
  198. package/dist/migrations/internal-migrations/5.0.0-05-drop-slug-unique-index.js.map +1 -0
  199. package/dist/migrations/internal-migrations/5.0.0-05-drop-slug-unique-index.mjs +41 -0
  200. package/dist/migrations/internal-migrations/5.0.0-05-drop-slug-unique-index.mjs.map +1 -0
  201. package/dist/migrations/internal-migrations/index.js +26 -0
  202. package/dist/migrations/internal-migrations/index.js.map +1 -0
  203. package/dist/migrations/internal-migrations/index.mjs +24 -0
  204. package/dist/migrations/internal-migrations/index.mjs.map +1 -0
  205. package/dist/migrations/internal.js +63 -0
  206. package/dist/migrations/internal.js.map +1 -0
  207. package/dist/migrations/internal.mjs +61 -0
  208. package/dist/migrations/internal.mjs.map +1 -0
  209. package/dist/migrations/logger.js +24 -0
  210. package/dist/migrations/logger.js.map +1 -0
  211. package/dist/migrations/logger.mjs +22 -0
  212. package/dist/migrations/logger.mjs.map +1 -0
  213. package/dist/migrations/storage.js +39 -0
  214. package/dist/migrations/storage.js.map +1 -0
  215. package/dist/migrations/storage.mjs +37 -0
  216. package/dist/migrations/storage.mjs.map +1 -0
  217. package/dist/migrations/users.js +87 -0
  218. package/dist/migrations/users.js.map +1 -0
  219. package/dist/migrations/users.mjs +85 -0
  220. package/dist/migrations/users.mjs.map +1 -0
  221. package/dist/query/helpers/join.js +127 -0
  222. package/dist/query/helpers/join.js.map +1 -0
  223. package/dist/query/helpers/join.mjs +122 -0
  224. package/dist/query/helpers/join.mjs.map +1 -0
  225. package/dist/query/helpers/order-by.js +167 -0
  226. package/dist/query/helpers/order-by.js.map +1 -0
  227. package/dist/query/helpers/order-by.mjs +163 -0
  228. package/dist/query/helpers/order-by.mjs.map +1 -0
  229. package/dist/query/helpers/populate/apply.js +592 -0
  230. package/dist/query/helpers/populate/apply.js.map +1 -0
  231. package/dist/query/helpers/populate/apply.mjs +590 -0
  232. package/dist/query/helpers/populate/apply.mjs.map +1 -0
  233. package/dist/query/helpers/populate/process.js +92 -0
  234. package/dist/query/helpers/populate/process.js.map +1 -0
  235. package/dist/query/helpers/populate/process.mjs +90 -0
  236. package/dist/query/helpers/populate/process.mjs.map +1 -0
  237. package/dist/query/helpers/search.js +67 -0
  238. package/dist/query/helpers/search.js.map +1 -0
  239. package/dist/query/helpers/search.mjs +65 -0
  240. package/dist/query/helpers/search.mjs.map +1 -0
  241. package/dist/query/helpers/streams/readable.js +131 -0
  242. package/dist/query/helpers/streams/readable.js.map +1 -0
  243. package/dist/query/helpers/streams/readable.mjs +129 -0
  244. package/dist/query/helpers/streams/readable.mjs.map +1 -0
  245. package/dist/query/helpers/transform.js +77 -0
  246. package/dist/query/helpers/transform.js.map +1 -0
  247. package/dist/query/helpers/transform.mjs +73 -0
  248. package/dist/query/helpers/transform.mjs.map +1 -0
  249. package/dist/query/helpers/where.js +372 -0
  250. package/dist/query/helpers/where.js.map +1 -0
  251. package/dist/query/helpers/where.mjs +369 -0
  252. package/dist/query/helpers/where.mjs.map +1 -0
  253. package/dist/query/query-builder.js +507 -0
  254. package/dist/query/query-builder.js.map +1 -0
  255. package/dist/query/query-builder.mjs +505 -0
  256. package/dist/query/query-builder.mjs.map +1 -0
  257. package/dist/repairs/index.js +13 -0
  258. package/dist/repairs/index.js.map +1 -0
  259. package/dist/repairs/index.mjs +11 -0
  260. package/dist/repairs/index.mjs.map +1 -0
  261. package/dist/repairs/operations/remove-orphan-morph-types.js +54 -0
  262. package/dist/repairs/operations/remove-orphan-morph-types.js.map +1 -0
  263. package/dist/repairs/operations/remove-orphan-morph-types.mjs +52 -0
  264. package/dist/repairs/operations/remove-orphan-morph-types.mjs.map +1 -0
  265. package/dist/schema/builder.js +354 -0
  266. package/dist/schema/builder.js.map +1 -0
  267. package/dist/schema/builder.mjs +352 -0
  268. package/dist/schema/builder.mjs.map +1 -0
  269. package/dist/schema/diff.js +379 -0
  270. package/dist/schema/diff.js.map +1 -0
  271. package/dist/schema/diff.mjs +377 -0
  272. package/dist/schema/diff.mjs.map +1 -0
  273. package/dist/schema/index.js +93 -0
  274. package/dist/schema/index.js.map +1 -0
  275. package/dist/schema/index.mjs +91 -0
  276. package/dist/schema/index.mjs.map +1 -0
  277. package/dist/schema/schema.js +266 -0
  278. package/dist/schema/schema.js.map +1 -0
  279. package/dist/schema/schema.mjs +264 -0
  280. package/dist/schema/schema.mjs.map +1 -0
  281. package/dist/schema/storage.js +58 -0
  282. package/dist/schema/storage.js.map +1 -0
  283. package/dist/schema/storage.mjs +56 -0
  284. package/dist/schema/storage.mjs.map +1 -0
  285. package/dist/transaction-context.js +65 -0
  286. package/dist/transaction-context.js.map +1 -0
  287. package/dist/transaction-context.mjs +63 -0
  288. package/dist/transaction-context.mjs.map +1 -0
  289. package/dist/utils/async-curry.js +19 -0
  290. package/dist/utils/async-curry.js.map +1 -0
  291. package/dist/utils/async-curry.mjs +17 -0
  292. package/dist/utils/async-curry.mjs.map +1 -0
  293. package/dist/utils/identifiers/hash.js +30 -0
  294. package/dist/utils/identifiers/hash.js.map +1 -0
  295. package/dist/utils/identifiers/hash.mjs +28 -0
  296. package/dist/utils/identifiers/hash.mjs.map +1 -0
  297. package/dist/utils/identifiers/index.js +414 -0
  298. package/dist/utils/identifiers/index.js.map +1 -0
  299. package/dist/utils/identifiers/index.mjs +411 -0
  300. package/dist/utils/identifiers/index.mjs.map +1 -0
  301. package/dist/utils/knex.js +21 -0
  302. package/dist/utils/knex.js.map +1 -0
  303. package/dist/utils/knex.mjs +18 -0
  304. package/dist/utils/knex.mjs.map +1 -0
  305. package/dist/utils/types.js +51 -0
  306. package/dist/utils/types.js.map +1 -0
  307. package/dist/utils/types.mjs +44 -0
  308. package/dist/utils/types.mjs.map +1 -0
  309. package/dist/validations/index.js +12 -0
  310. package/dist/validations/index.js.map +1 -0
  311. package/dist/validations/index.mjs +10 -0
  312. package/dist/validations/index.mjs.map +1 -0
  313. package/dist/validations/relations/bidirectional.js +64 -0
  314. package/dist/validations/relations/bidirectional.js.map +1 -0
  315. package/dist/validations/relations/bidirectional.mjs +62 -0
  316. package/dist/validations/relations/bidirectional.mjs.map +1 -0
  317. package/dist/validations/relations/index.js +13 -0
  318. package/dist/validations/relations/index.js.map +1 -0
  319. package/dist/validations/relations/index.mjs +11 -0
  320. package/dist/validations/relations/index.mjs.map +1 -0
  321. package/package.json +4 -4
@@ -0,0 +1,221 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash/fp');
4
+ var _$1 = require('lodash');
5
+ var invalidRelation = require('../errors/invalid-relation.js');
6
+
7
+ /**
8
+ * When connecting relations, the order you connect them matters.
9
+ *
10
+ * Example, if you connect the following relations:
11
+ * { id: 5, position: { before: 1 } }
12
+ * { id: 1, position: { before: 2 } }
13
+ * { id: 2, position: { end: true } }
14
+ *
15
+ * Going through the connect array, id 5 has to be connected before id 1,
16
+ * so the order of id5 = id1 - 1. But the order value of id 1 is unknown.
17
+ * The only way to know the order of id 1 is to connect it first.
18
+ *
19
+ * This function makes sure the relations are connected in the right order:
20
+ * { id: 2, position: { end: true } }
21
+ * { id: 1, position: { before: 2 } }
22
+ * { id: 5, position: { before: 1 } }
23
+ *
24
+ */ const sortConnectArray = (connectArr, initialArr = [], strictSort = true)=>{
25
+ const sortedConnect = [];
26
+ // Boolean to know if we have to recalculate the order of the relations
27
+ let needsSorting = false;
28
+ // Map to validate if relation is already in sortedConnect or DB.
29
+ const relationInInitialArray = initialArr.reduce((acc, rel)=>({
30
+ ...acc,
31
+ [rel.id]: true
32
+ }), {});
33
+ // Map to store the first index where a relation id is connected
34
+ const mappedRelations = connectArr.reduce((mapper, relation)=>{
35
+ const adjacentRelId = relation.position?.before || relation.position?.after;
36
+ if (!adjacentRelId || !relationInInitialArray[adjacentRelId] && !mapper[adjacentRelId]) {
37
+ needsSorting = true;
38
+ }
39
+ /**
40
+ * We do not allow duplicate relations to be connected, so we need to check for uniqueness with components
41
+ * Note that the id here includes the uid for polymorphic relations
42
+ *
43
+ * So for normal relations, the same id means the same relation
44
+ * For component relations, it means the unique combo of (id, component name)
45
+ */ // Check if there's an existing relation with this id
46
+ const existingRelation = mapper[relation.id];
47
+ // Check if existing relation has a component or not
48
+ const hasNoComponent = existingRelation && !('__component' in existingRelation);
49
+ // Check if the existing relation has the same component as the new relation
50
+ const hasSameComponent = existingRelation && existingRelation.__component === relation.__component;
51
+ // If we have an existing relation that is not unique (no component or same component) we won't accept it
52
+ if (existingRelation && (hasNoComponent || hasSameComponent)) {
53
+ throw new invalidRelation(`The relation with id ${relation.id} is already connected. ` + 'You cannot connect the same relation twice.');
54
+ }
55
+ return {
56
+ [relation.id]: {
57
+ ...relation,
58
+ computed: false
59
+ },
60
+ ...mapper
61
+ };
62
+ }, {});
63
+ // If we don't need to sort the connect array, we can return it as is
64
+ if (!needsSorting) return connectArr;
65
+ // Recursively compute in which order the relation should be connected
66
+ const computeRelation = (relation, relationsSeenInBranch)=>{
67
+ const adjacentRelId = relation.position?.before || relation.position?.after;
68
+ const adjacentRelation = mappedRelations[adjacentRelId];
69
+ // If the relation has already been seen in the current branch,
70
+ // it means there is a circular reference
71
+ if (adjacentRelId && relationsSeenInBranch[adjacentRelId]) {
72
+ throw new invalidRelation('A circular reference was found in the connect array. ' + 'One relation is trying to connect before/after another one that is trying to connect before/after it');
73
+ }
74
+ // This relation has already been computed
75
+ if (mappedRelations[relation.id]?.computed) {
76
+ return;
77
+ }
78
+ mappedRelations[relation.id].computed = true;
79
+ // Relation does not have a before or after attribute or is in the initial array
80
+ if (!adjacentRelId || relationInInitialArray[adjacentRelId]) {
81
+ sortedConnect.push(relation);
82
+ return;
83
+ }
84
+ // Look if id is referenced elsewhere in the array
85
+ if (mappedRelations[adjacentRelId]) {
86
+ computeRelation(adjacentRelation, {
87
+ ...relationsSeenInBranch,
88
+ [relation.id]: true
89
+ });
90
+ sortedConnect.push(relation);
91
+ } else if (strictSort) {
92
+ // If we reach this point, it means that the adjacent relation is not in the connect array
93
+ // and it is not in the database.
94
+ throw new invalidRelation(`There was a problem connecting relation with id ${relation.id} at position ${JSON.stringify(relation.position)}. The relation with id ${adjacentRelId} needs to be connected first.`);
95
+ } else {
96
+ // We are in non-strict mode so we can push the relation.
97
+ sortedConnect.push({
98
+ id: relation.id,
99
+ position: {
100
+ end: true
101
+ }
102
+ });
103
+ }
104
+ };
105
+ // Iterate over connectArr and populate sortedConnect
106
+ connectArr.forEach((relation)=>computeRelation(relation, {}));
107
+ return sortedConnect;
108
+ };
109
+ /**
110
+ * Responsible for calculating the relations order when connecting them.
111
+ *
112
+ * The connect method takes an array of relations with positional attributes:
113
+ * - before: the id of the relation to connect before
114
+ * - after: the id of the relation to connect after
115
+ * - end: it should be at the end
116
+ * - start: it should be at the start
117
+ *
118
+ * Example:
119
+ * - Having a connect array like:
120
+ * [ { id: 4, before: 2 }, { id: 4, before: 3}, {id: 5, before: 4} ]
121
+ * - With the initial relations:
122
+ * [ { id: 2, order: 4 }, { id: 3, order: 10 } ]
123
+ * - Step by step, going through the connect array, the array of relations would be:
124
+ * [ { id: 4, order: 3.5 }, { id: 2, order: 4 }, { id: 3, order: 10 } ]
125
+ * [ { id: 2, order: 4 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]
126
+ * [ { id: 2, order: 4 }, { id: 5, order: 3.5 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]
127
+ * - The final step would be to recalculate fractional order values.
128
+ * [ { id: 2, order: 4 }, { id: 5, order: 3.33 }, { id: 4, order: 3.66 }, { id: 3, order: 10 } ]
129
+ *
130
+ * @param {Array<*>} initArr - array of relations to initialize the class with
131
+ * @param {string} idColumn - the column name of the id
132
+ * @param {string} orderColumn - the column name of the order
133
+ * @param {boolean} strict - if true, will throw an error if a relation is connected adjacent to
134
+ * another one that does not exist
135
+ * @return {*}
136
+ */ const relationsOrderer = (initArr, idColumn, orderColumn, strict)=>{
137
+ const computedRelations = _.castArray(initArr ?? []).map((r)=>({
138
+ init: true,
139
+ id: r[idColumn],
140
+ order: Number(r[orderColumn]) || 1
141
+ }));
142
+ const maxOrder = _.maxBy('order', computedRelations)?.order || 0;
143
+ const findRelation = (id)=>{
144
+ const idx = computedRelations.findIndex((r)=>r.id === id);
145
+ return {
146
+ idx,
147
+ relation: computedRelations[idx]
148
+ };
149
+ };
150
+ const removeRelation = (r)=>{
151
+ const { idx } = findRelation(r.id);
152
+ if (idx >= 0) {
153
+ computedRelations.splice(idx, 1);
154
+ }
155
+ };
156
+ const insertRelation = (r)=>{
157
+ let idx;
158
+ if (r.position?.before) {
159
+ const { idx: _idx, relation } = findRelation(r.position.before);
160
+ if (relation.init) {
161
+ r.order = relation.order - 0.5;
162
+ } else {
163
+ r.order = relation.order;
164
+ }
165
+ idx = _idx;
166
+ } else if (r.position?.after) {
167
+ const { idx: _idx, relation } = findRelation(r.position.after);
168
+ if (relation.init) {
169
+ r.order = relation.order + 0.5;
170
+ } else {
171
+ r.order = relation.order;
172
+ }
173
+ idx = _idx + 1;
174
+ } else if (r.position?.start) {
175
+ r.order = 0.5;
176
+ idx = 0;
177
+ } else {
178
+ r.order = maxOrder + 0.5;
179
+ idx = computedRelations.length;
180
+ }
181
+ // Insert the relation in the array
182
+ computedRelations.splice(idx, 0, r);
183
+ };
184
+ return {
185
+ disconnect (relations) {
186
+ _.castArray(relations).forEach((relation)=>{
187
+ removeRelation(relation);
188
+ });
189
+ return this;
190
+ },
191
+ connect (relations) {
192
+ sortConnectArray(_.castArray(relations), computedRelations, strict).forEach((relation)=>{
193
+ this.disconnect(relation);
194
+ try {
195
+ insertRelation(relation);
196
+ } catch (err) {
197
+ throw new Error(`There was a problem connecting relation with id ${relation.id} at position ${JSON.stringify(relation.position)}. The list of connect relations is not valid`);
198
+ }
199
+ });
200
+ return this;
201
+ },
202
+ get () {
203
+ return computedRelations;
204
+ },
205
+ /**
206
+ * Get a map between the relation id and its order
207
+ */ getOrderMap () {
208
+ return _$1(computedRelations).groupBy('order').reduce((acc, relations)=>{
209
+ if (relations[0]?.init) return acc;
210
+ relations.forEach((relation, idx)=>{
211
+ acc[relation.id] = Math.floor(relation.order) + (idx + 1) / (relations.length + 1);
212
+ });
213
+ return acc;
214
+ }, {});
215
+ }
216
+ };
217
+ };
218
+
219
+ exports.relationsOrderer = relationsOrderer;
220
+ exports.sortConnectArray = sortConnectArray;
221
+ //# sourceMappingURL=relations-orderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relations-orderer.js","sources":["../../src/entity-manager/relations-orderer.ts"],"sourcesContent":["import { castArray, maxBy } from 'lodash/fp';\nimport _ from 'lodash';\n\nimport { InvalidRelationError } from '../errors';\nimport type { ID } from '../types';\n\ninterface Link {\n id: ID;\n position?: { before?: ID; after?: ID; start?: true; end?: true };\n order?: number;\n __component?: string;\n}\n\ninterface OrderedLink extends Link {\n init?: boolean;\n order: number;\n}\n\n/**\n * When connecting relations, the order you connect them matters.\n *\n * Example, if you connect the following relations:\n * { id: 5, position: { before: 1 } }\n * { id: 1, position: { before: 2 } }\n * { id: 2, position: { end: true } }\n *\n * Going through the connect array, id 5 has to be connected before id 1,\n * so the order of id5 = id1 - 1. But the order value of id 1 is unknown.\n * The only way to know the order of id 1 is to connect it first.\n *\n * This function makes sure the relations are connected in the right order:\n * { id: 2, position: { end: true } }\n * { id: 1, position: { before: 2 } }\n * { id: 5, position: { before: 1 } }\n *\n */\nconst sortConnectArray = (connectArr: Link[], initialArr: Link[] = [], strictSort = true) => {\n const sortedConnect: Link[] = [];\n // Boolean to know if we have to recalculate the order of the relations\n let needsSorting = false;\n // Map to validate if relation is already in sortedConnect or DB.\n const relationInInitialArray = initialArr.reduce(\n (acc, rel) => ({ ...acc, [rel.id]: true }),\n {} as Record<ID, boolean>\n );\n // Map to store the first index where a relation id is connected\n const mappedRelations = connectArr.reduce(\n (mapper, relation: Link) => {\n const adjacentRelId = relation.position?.before || relation.position?.after;\n\n if (!adjacentRelId || (!relationInInitialArray[adjacentRelId] && !mapper[adjacentRelId])) {\n needsSorting = true;\n }\n\n /**\n * We do not allow duplicate relations to be connected, so we need to check for uniqueness with components\n * Note that the id here includes the uid for polymorphic relations\n *\n * So for normal relations, the same id means the same relation\n * For component relations, it means the unique combo of (id, component name)\n */\n\n // Check if there's an existing relation with this id\n const existingRelation = mapper[relation.id];\n\n // Check if existing relation has a component or not\n const hasNoComponent = existingRelation && !('__component' in existingRelation);\n\n // Check if the existing relation has the same component as the new relation\n const hasSameComponent =\n existingRelation && existingRelation.__component === relation.__component;\n\n // If we have an existing relation that is not unique (no component or same component) we won't accept it\n if (existingRelation && (hasNoComponent || hasSameComponent)) {\n throw new InvalidRelationError(\n `The relation with id ${relation.id} is already connected. ` +\n 'You cannot connect the same relation twice.'\n );\n }\n\n return {\n [relation.id]: { ...relation, computed: false },\n ...mapper,\n };\n },\n {} as Record<ID, Link & { computed: boolean }>\n );\n\n // If we don't need to sort the connect array, we can return it as is\n if (!needsSorting) return connectArr;\n\n // Recursively compute in which order the relation should be connected\n const computeRelation = (relation: Link, relationsSeenInBranch: Record<ID, boolean>) => {\n const adjacentRelId = relation.position?.before || relation.position?.after;\n const adjacentRelation = mappedRelations[adjacentRelId as ID];\n\n // If the relation has already been seen in the current branch,\n // it means there is a circular reference\n if (adjacentRelId && relationsSeenInBranch[adjacentRelId]) {\n throw new InvalidRelationError(\n 'A circular reference was found in the connect array. ' +\n 'One relation is trying to connect before/after another one that is trying to connect before/after it'\n );\n }\n\n // This relation has already been computed\n if (mappedRelations[relation.id]?.computed) {\n return;\n }\n\n mappedRelations[relation.id].computed = true;\n\n // Relation does not have a before or after attribute or is in the initial array\n if (!adjacentRelId || relationInInitialArray[adjacentRelId]) {\n sortedConnect.push(relation);\n return;\n }\n\n // Look if id is referenced elsewhere in the array\n if (mappedRelations[adjacentRelId]) {\n computeRelation(adjacentRelation, { ...relationsSeenInBranch, [relation.id]: true });\n sortedConnect.push(relation);\n } else if (strictSort) {\n // If we reach this point, it means that the adjacent relation is not in the connect array\n // and it is not in the database.\n throw new InvalidRelationError(\n `There was a problem connecting relation with id ${\n relation.id\n } at position ${JSON.stringify(\n relation.position\n )}. The relation with id ${adjacentRelId} needs to be connected first.`\n );\n } else {\n // We are in non-strict mode so we can push the relation.\n sortedConnect.push({ id: relation.id, position: { end: true } });\n }\n };\n\n // Iterate over connectArr and populate sortedConnect\n connectArr.forEach((relation) => computeRelation(relation, {}));\n\n return sortedConnect;\n};\n\n/**\n * Responsible for calculating the relations order when connecting them.\n *\n * The connect method takes an array of relations with positional attributes:\n * - before: the id of the relation to connect before\n * - after: the id of the relation to connect after\n * - end: it should be at the end\n * - start: it should be at the start\n *\n * Example:\n * - Having a connect array like:\n * [ { id: 4, before: 2 }, { id: 4, before: 3}, {id: 5, before: 4} ]\n * - With the initial relations:\n * [ { id: 2, order: 4 }, { id: 3, order: 10 } ]\n * - Step by step, going through the connect array, the array of relations would be:\n * [ { id: 4, order: 3.5 }, { id: 2, order: 4 }, { id: 3, order: 10 } ]\n * [ { id: 2, order: 4 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]\n * [ { id: 2, order: 4 }, { id: 5, order: 3.5 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]\n * - The final step would be to recalculate fractional order values.\n * [ { id: 2, order: 4 }, { id: 5, order: 3.33 }, { id: 4, order: 3.66 }, { id: 3, order: 10 } ]\n *\n * @param {Array<*>} initArr - array of relations to initialize the class with\n * @param {string} idColumn - the column name of the id\n * @param {string} orderColumn - the column name of the order\n * @param {boolean} strict - if true, will throw an error if a relation is connected adjacent to\n * another one that does not exist\n * @return {*}\n */\nconst relationsOrderer = <TRelation extends Record<string, ID | number | null>>(\n initArr: TRelation[],\n idColumn: keyof TRelation,\n orderColumn: keyof TRelation,\n strict?: boolean\n) => {\n const computedRelations: OrderedLink[] = castArray(initArr ?? []).map((r) => ({\n init: true,\n id: r[idColumn] as ID,\n order: Number(r[orderColumn]) || 1,\n }));\n\n const maxOrder = maxBy('order', computedRelations)?.order || 0;\n\n const findRelation = (id: ID) => {\n const idx = computedRelations.findIndex((r) => r.id === id);\n return { idx, relation: computedRelations[idx] };\n };\n\n const removeRelation = (r: Link) => {\n const { idx } = findRelation(r.id);\n if (idx >= 0) {\n computedRelations.splice(idx, 1);\n }\n };\n\n const insertRelation = (r: Link) => {\n let idx;\n\n if (r.position?.before) {\n const { idx: _idx, relation } = findRelation(r.position.before);\n if (relation.init) {\n r.order = relation.order - 0.5;\n } else {\n r.order = relation.order;\n }\n idx = _idx;\n } else if (r.position?.after) {\n const { idx: _idx, relation } = findRelation(r.position.after);\n if (relation.init) {\n r.order = relation.order + 0.5;\n } else {\n r.order = relation.order;\n }\n\n idx = _idx + 1;\n } else if (r.position?.start) {\n r.order = 0.5;\n idx = 0;\n } else {\n r.order = maxOrder + 0.5;\n idx = computedRelations.length;\n }\n\n // Insert the relation in the array\n computedRelations.splice(idx, 0, r as OrderedLink);\n };\n\n return {\n disconnect(relations: Link | Link[]) {\n castArray(relations).forEach((relation) => {\n removeRelation(relation);\n });\n return this;\n },\n connect(relations: Link | Link[]) {\n sortConnectArray(castArray(relations), computedRelations, strict).forEach((relation) => {\n this.disconnect(relation);\n\n try {\n insertRelation(relation);\n } catch (err) {\n throw new Error(\n `There was a problem connecting relation with id ${\n relation.id\n } at position ${JSON.stringify(\n relation.position\n )}. The list of connect relations is not valid`\n );\n }\n });\n return this;\n },\n get() {\n return computedRelations;\n },\n /**\n * Get a map between the relation id and its order\n */\n getOrderMap() {\n return _(computedRelations)\n .groupBy('order')\n .reduce(\n (acc, relations) => {\n if (relations[0]?.init) return acc;\n relations.forEach((relation, idx) => {\n acc[relation.id] = Math.floor(relation.order) + (idx + 1) / (relations.length + 1);\n });\n return acc;\n },\n {} as Record<ID, number>\n );\n },\n };\n};\n\nexport { relationsOrderer, sortConnectArray };\n"],"names":["sortConnectArray","connectArr","initialArr","strictSort","sortedConnect","needsSorting","relationInInitialArray","reduce","acc","rel","id","mappedRelations","mapper","relation","adjacentRelId","position","before","after","existingRelation","hasNoComponent","hasSameComponent","__component","InvalidRelationError","computed","computeRelation","relationsSeenInBranch","adjacentRelation","push","JSON","stringify","end","forEach","relationsOrderer","initArr","idColumn","orderColumn","strict","computedRelations","castArray","map","r","init","order","Number","maxOrder","maxBy","findRelation","idx","findIndex","removeRelation","splice","insertRelation","_idx","start","length","disconnect","relations","connect","err","Error","get","getOrderMap","_","groupBy","Math","floor"],"mappings":";;;;;;AAkBA;;;;;;;;;;;;;;;;;IAkBA,MAAMA,mBAAmB,CAACC,UAAAA,EAAoBC,aAAqB,EAAE,EAAEC,aAAa,IAAI,GAAA;AACtF,IAAA,MAAMC,gBAAwB,EAAE;;AAEhC,IAAA,IAAIC,YAAe,GAAA,KAAA;;AAEnB,IAAA,MAAMC,yBAAyBJ,UAAWK,CAAAA,MAAM,CAC9C,CAACC,GAAAA,EAAKC,OAAS;AAAE,YAAA,GAAGD,GAAG;YAAE,CAACC,GAAAA,CAAIC,EAAE,GAAG;AAAK,SAAA,GACxC,EAAC,CAAA;;AAGH,IAAA,MAAMC,eAAkBV,GAAAA,UAAAA,CAAWM,MAAM,CACvC,CAACK,MAAQC,EAAAA,QAAAA,GAAAA;AACP,QAAA,MAAMC,gBAAgBD,QAASE,CAAAA,QAAQ,EAAEC,MAAUH,IAAAA,QAAAA,CAASE,QAAQ,EAAEE,KAAAA;QAEtE,IAAI,CAACH,aAAkB,IAAA,CAACR,sBAAsB,CAACQ,aAAc,CAAA,IAAI,CAACF,MA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etC,CAAAA,QAAAA,CAAAA;AACjB,iBAAA,CAAE,OAAO6C,GAAK,EAAA;AACZ,oBAAA,MAAM,IAAIC,KACR,CAAA,CAAC,gDAAgD,EAC/C9C,SAASH,EAAE,CACZ,aAAa,EAAEkB,KAAKC,SAAS,CAC5BhB,SAASE,QAAQ,CAAA,CACjB,4CAA4C,CAAC,CAAA;AAEnD;AACF,aAAA,CAAA;AACA,YAAA,OAAO,IAAI;AACb,SAAA;AACA6C,QAAAA,GAAAA,CAAAA,GAAAA;YACE,OAAOvB,iBAAAA;AACT,SAAA;AACA;;QAGAwB,WAAAA,CAAAA,GAAAA;YACE,OAAOC,GAAAA,CAAEzB,mBACN0B,OAAO,CAAC,SACRxD,MAAM,CACL,CAACC,GAAKgD,EAAAA,SAAAA,GAAAA;AACJ,gBAAA,IAAIA,SAAS,CAAC,CAAE,CAAA,EAAEf,MAAM,OAAOjC,GAAAA;gBAC/BgD,SAAUzB,CAAAA,OAAO,CAAC,CAAClB,QAAUkC,EAAAA,GAAAA,GAAAA;oBAC3BvC,GAAG,CAACK,SAASH,EAAE,CAAC,GAAGsD,IAAKC,CAAAA,KAAK,CAACpD,QAAS6B,CAAAA,KAAK,IAAI,CAACK,MAAM,CAAA,KAAMS,SAAUF,CAAAA,MAAM,GAAG,CAAA,CAAA;AAClF,iBAAA,CAAA;gBACA,OAAO9C,GAAAA;AACT,aAAA,EACA,EAAC,CAAA;AAEP;AACF,KAAA;AACF;;;;;"}
@@ -0,0 +1,218 @@
1
+ import { castArray, maxBy } from 'lodash/fp';
2
+ import _ from 'lodash';
3
+ import InvalidRelationError from '../errors/invalid-relation.mjs';
4
+
5
+ /**
6
+ * When connecting relations, the order you connect them matters.
7
+ *
8
+ * Example, if you connect the following relations:
9
+ * { id: 5, position: { before: 1 } }
10
+ * { id: 1, position: { before: 2 } }
11
+ * { id: 2, position: { end: true } }
12
+ *
13
+ * Going through the connect array, id 5 has to be connected before id 1,
14
+ * so the order of id5 = id1 - 1. But the order value of id 1 is unknown.
15
+ * The only way to know the order of id 1 is to connect it first.
16
+ *
17
+ * This function makes sure the relations are connected in the right order:
18
+ * { id: 2, position: { end: true } }
19
+ * { id: 1, position: { before: 2 } }
20
+ * { id: 5, position: { before: 1 } }
21
+ *
22
+ */ const sortConnectArray = (connectArr, initialArr = [], strictSort = true)=>{
23
+ const sortedConnect = [];
24
+ // Boolean to know if we have to recalculate the order of the relations
25
+ let needsSorting = false;
26
+ // Map to validate if relation is already in sortedConnect or DB.
27
+ const relationInInitialArray = initialArr.reduce((acc, rel)=>({
28
+ ...acc,
29
+ [rel.id]: true
30
+ }), {});
31
+ // Map to store the first index where a relation id is connected
32
+ const mappedRelations = connectArr.reduce((mapper, relation)=>{
33
+ const adjacentRelId = relation.position?.before || relation.position?.after;
34
+ if (!adjacentRelId || !relationInInitialArray[adjacentRelId] && !mapper[adjacentRelId]) {
35
+ needsSorting = true;
36
+ }
37
+ /**
38
+ * We do not allow duplicate relations to be connected, so we need to check for uniqueness with components
39
+ * Note that the id here includes the uid for polymorphic relations
40
+ *
41
+ * So for normal relations, the same id means the same relation
42
+ * For component relations, it means the unique combo of (id, component name)
43
+ */ // Check if there's an existing relation with this id
44
+ const existingRelation = mapper[relation.id];
45
+ // Check if existing relation has a component or not
46
+ const hasNoComponent = existingRelation && !('__component' in existingRelation);
47
+ // Check if the existing relation has the same component as the new relation
48
+ const hasSameComponent = existingRelation && existingRelation.__component === relation.__component;
49
+ // If we have an existing relation that is not unique (no component or same component) we won't accept it
50
+ if (existingRelation && (hasNoComponent || hasSameComponent)) {
51
+ throw new InvalidRelationError(`The relation with id ${relation.id} is already connected. ` + 'You cannot connect the same relation twice.');
52
+ }
53
+ return {
54
+ [relation.id]: {
55
+ ...relation,
56
+ computed: false
57
+ },
58
+ ...mapper
59
+ };
60
+ }, {});
61
+ // If we don't need to sort the connect array, we can return it as is
62
+ if (!needsSorting) return connectArr;
63
+ // Recursively compute in which order the relation should be connected
64
+ const computeRelation = (relation, relationsSeenInBranch)=>{
65
+ const adjacentRelId = relation.position?.before || relation.position?.after;
66
+ const adjacentRelation = mappedRelations[adjacentRelId];
67
+ // If the relation has already been seen in the current branch,
68
+ // it means there is a circular reference
69
+ if (adjacentRelId && relationsSeenInBranch[adjacentRelId]) {
70
+ throw new InvalidRelationError('A circular reference was found in the connect array. ' + 'One relation is trying to connect before/after another one that is trying to connect before/after it');
71
+ }
72
+ // This relation has already been computed
73
+ if (mappedRelations[relation.id]?.computed) {
74
+ return;
75
+ }
76
+ mappedRelations[relation.id].computed = true;
77
+ // Relation does not have a before or after attribute or is in the initial array
78
+ if (!adjacentRelId || relationInInitialArray[adjacentRelId]) {
79
+ sortedConnect.push(relation);
80
+ return;
81
+ }
82
+ // Look if id is referenced elsewhere in the array
83
+ if (mappedRelations[adjacentRelId]) {
84
+ computeRelation(adjacentRelation, {
85
+ ...relationsSeenInBranch,
86
+ [relation.id]: true
87
+ });
88
+ sortedConnect.push(relation);
89
+ } else if (strictSort) {
90
+ // If we reach this point, it means that the adjacent relation is not in the connect array
91
+ // and it is not in the database.
92
+ throw new InvalidRelationError(`There was a problem connecting relation with id ${relation.id} at position ${JSON.stringify(relation.position)}. The relation with id ${adjacentRelId} needs to be connected first.`);
93
+ } else {
94
+ // We are in non-strict mode so we can push the relation.
95
+ sortedConnect.push({
96
+ id: relation.id,
97
+ position: {
98
+ end: true
99
+ }
100
+ });
101
+ }
102
+ };
103
+ // Iterate over connectArr and populate sortedConnect
104
+ connectArr.forEach((relation)=>computeRelation(relation, {}));
105
+ return sortedConnect;
106
+ };
107
+ /**
108
+ * Responsible for calculating the relations order when connecting them.
109
+ *
110
+ * The connect method takes an array of relations with positional attributes:
111
+ * - before: the id of the relation to connect before
112
+ * - after: the id of the relation to connect after
113
+ * - end: it should be at the end
114
+ * - start: it should be at the start
115
+ *
116
+ * Example:
117
+ * - Having a connect array like:
118
+ * [ { id: 4, before: 2 }, { id: 4, before: 3}, {id: 5, before: 4} ]
119
+ * - With the initial relations:
120
+ * [ { id: 2, order: 4 }, { id: 3, order: 10 } ]
121
+ * - Step by step, going through the connect array, the array of relations would be:
122
+ * [ { id: 4, order: 3.5 }, { id: 2, order: 4 }, { id: 3, order: 10 } ]
123
+ * [ { id: 2, order: 4 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]
124
+ * [ { id: 2, order: 4 }, { id: 5, order: 3.5 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]
125
+ * - The final step would be to recalculate fractional order values.
126
+ * [ { id: 2, order: 4 }, { id: 5, order: 3.33 }, { id: 4, order: 3.66 }, { id: 3, order: 10 } ]
127
+ *
128
+ * @param {Array<*>} initArr - array of relations to initialize the class with
129
+ * @param {string} idColumn - the column name of the id
130
+ * @param {string} orderColumn - the column name of the order
131
+ * @param {boolean} strict - if true, will throw an error if a relation is connected adjacent to
132
+ * another one that does not exist
133
+ * @return {*}
134
+ */ const relationsOrderer = (initArr, idColumn, orderColumn, strict)=>{
135
+ const computedRelations = castArray(initArr ?? []).map((r)=>({
136
+ init: true,
137
+ id: r[idColumn],
138
+ order: Number(r[orderColumn]) || 1
139
+ }));
140
+ const maxOrder = maxBy('order', computedRelations)?.order || 0;
141
+ const findRelation = (id)=>{
142
+ const idx = computedRelations.findIndex((r)=>r.id === id);
143
+ return {
144
+ idx,
145
+ relation: computedRelations[idx]
146
+ };
147
+ };
148
+ const removeRelation = (r)=>{
149
+ const { idx } = findRelation(r.id);
150
+ if (idx >= 0) {
151
+ computedRelations.splice(idx, 1);
152
+ }
153
+ };
154
+ const insertRelation = (r)=>{
155
+ let idx;
156
+ if (r.position?.before) {
157
+ const { idx: _idx, relation } = findRelation(r.position.before);
158
+ if (relation.init) {
159
+ r.order = relation.order - 0.5;
160
+ } else {
161
+ r.order = relation.order;
162
+ }
163
+ idx = _idx;
164
+ } else if (r.position?.after) {
165
+ const { idx: _idx, relation } = findRelation(r.position.after);
166
+ if (relation.init) {
167
+ r.order = relation.order + 0.5;
168
+ } else {
169
+ r.order = relation.order;
170
+ }
171
+ idx = _idx + 1;
172
+ } else if (r.position?.start) {
173
+ r.order = 0.5;
174
+ idx = 0;
175
+ } else {
176
+ r.order = maxOrder + 0.5;
177
+ idx = computedRelations.length;
178
+ }
179
+ // Insert the relation in the array
180
+ computedRelations.splice(idx, 0, r);
181
+ };
182
+ return {
183
+ disconnect (relations) {
184
+ castArray(relations).forEach((relation)=>{
185
+ removeRelation(relation);
186
+ });
187
+ return this;
188
+ },
189
+ connect (relations) {
190
+ sortConnectArray(castArray(relations), computedRelations, strict).forEach((relation)=>{
191
+ this.disconnect(relation);
192
+ try {
193
+ insertRelation(relation);
194
+ } catch (err) {
195
+ throw new Error(`There was a problem connecting relation with id ${relation.id} at position ${JSON.stringify(relation.position)}. The list of connect relations is not valid`);
196
+ }
197
+ });
198
+ return this;
199
+ },
200
+ get () {
201
+ return computedRelations;
202
+ },
203
+ /**
204
+ * Get a map between the relation id and its order
205
+ */ getOrderMap () {
206
+ return _(computedRelations).groupBy('order').reduce((acc, relations)=>{
207
+ if (relations[0]?.init) return acc;
208
+ relations.forEach((relation, idx)=>{
209
+ acc[relation.id] = Math.floor(relation.order) + (idx + 1) / (relations.length + 1);
210
+ });
211
+ return acc;
212
+ }, {});
213
+ }
214
+ };
215
+ };
216
+
217
+ export { relationsOrderer, sortConnectArray };
218
+ //# sourceMappingURL=relations-orderer.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relations-orderer.mjs","sources":["../../src/entity-manager/relations-orderer.ts"],"sourcesContent":["import { castArray, maxBy } from 'lodash/fp';\nimport _ from 'lodash';\n\nimport { InvalidRelationError } from '../errors';\nimport type { ID } from '../types';\n\ninterface Link {\n id: ID;\n position?: { before?: ID; after?: ID; start?: true; end?: true };\n order?: number;\n __component?: string;\n}\n\ninterface OrderedLink extends Link {\n init?: boolean;\n order: number;\n}\n\n/**\n * When connecting relations, the order you connect them matters.\n *\n * Example, if you connect the following relations:\n * { id: 5, position: { before: 1 } }\n * { id: 1, position: { before: 2 } }\n * { id: 2, position: { end: true } }\n *\n * Going through the connect array, id 5 has to be connected before id 1,\n * so the order of id5 = id1 - 1. But the order value of id 1 is unknown.\n * The only way to know the order of id 1 is to connect it first.\n *\n * This function makes sure the relations are connected in the right order:\n * { id: 2, position: { end: true } }\n * { id: 1, position: { before: 2 } }\n * { id: 5, position: { before: 1 } }\n *\n */\nconst sortConnectArray = (connectArr: Link[], initialArr: Link[] = [], strictSort = true) => {\n const sortedConnect: Link[] = [];\n // Boolean to know if we have to recalculate the order of the relations\n let needsSorting = false;\n // Map to validate if relation is already in sortedConnect or DB.\n const relationInInitialArray = initialArr.reduce(\n (acc, rel) => ({ ...acc, [rel.id]: true }),\n {} as Record<ID, boolean>\n );\n // Map to store the first index where a relation id is connected\n const mappedRelations = connectArr.reduce(\n (mapper, relation: Link) => {\n const adjacentRelId = relation.position?.before || relation.position?.after;\n\n if (!adjacentRelId || (!relationInInitialArray[adjacentRelId] && !mapper[adjacentRelId])) {\n needsSorting = true;\n }\n\n /**\n * We do not allow duplicate relations to be connected, so we need to check for uniqueness with components\n * Note that the id here includes the uid for polymorphic relations\n *\n * So for normal relations, the same id means the same relation\n * For component relations, it means the unique combo of (id, component name)\n */\n\n // Check if there's an existing relation with this id\n const existingRelation = mapper[relation.id];\n\n // Check if existing relation has a component or not\n const hasNoComponent = existingRelation && !('__component' in existingRelation);\n\n // Check if the existing relation has the same component as the new relation\n const hasSameComponent =\n existingRelation && existingRelation.__component === relation.__component;\n\n // If we have an existing relation that is not unique (no component or same component) we won't accept it\n if (existingRelation && (hasNoComponent || hasSameComponent)) {\n throw new InvalidRelationError(\n `The relation with id ${relation.id} is already connected. ` +\n 'You cannot connect the same relation twice.'\n );\n }\n\n return {\n [relation.id]: { ...relation, computed: false },\n ...mapper,\n };\n },\n {} as Record<ID, Link & { computed: boolean }>\n );\n\n // If we don't need to sort the connect array, we can return it as is\n if (!needsSorting) return connectArr;\n\n // Recursively compute in which order the relation should be connected\n const computeRelation = (relation: Link, relationsSeenInBranch: Record<ID, boolean>) => {\n const adjacentRelId = relation.position?.before || relation.position?.after;\n const adjacentRelation = mappedRelations[adjacentRelId as ID];\n\n // If the relation has already been seen in the current branch,\n // it means there is a circular reference\n if (adjacentRelId && relationsSeenInBranch[adjacentRelId]) {\n throw new InvalidRelationError(\n 'A circular reference was found in the connect array. ' +\n 'One relation is trying to connect before/after another one that is trying to connect before/after it'\n );\n }\n\n // This relation has already been computed\n if (mappedRelations[relation.id]?.computed) {\n return;\n }\n\n mappedRelations[relation.id].computed = true;\n\n // Relation does not have a before or after attribute or is in the initial array\n if (!adjacentRelId || relationInInitialArray[adjacentRelId]) {\n sortedConnect.push(relation);\n return;\n }\n\n // Look if id is referenced elsewhere in the array\n if (mappedRelations[adjacentRelId]) {\n computeRelation(adjacentRelation, { ...relationsSeenInBranch, [relation.id]: true });\n sortedConnect.push(relation);\n } else if (strictSort) {\n // If we reach this point, it means that the adjacent relation is not in the connect array\n // and it is not in the database.\n throw new InvalidRelationError(\n `There was a problem connecting relation with id ${\n relation.id\n } at position ${JSON.stringify(\n relation.position\n )}. The relation with id ${adjacentRelId} needs to be connected first.`\n );\n } else {\n // We are in non-strict mode so we can push the relation.\n sortedConnect.push({ id: relation.id, position: { end: true } });\n }\n };\n\n // Iterate over connectArr and populate sortedConnect\n connectArr.forEach((relation) => computeRelation(relation, {}));\n\n return sortedConnect;\n};\n\n/**\n * Responsible for calculating the relations order when connecting them.\n *\n * The connect method takes an array of relations with positional attributes:\n * - before: the id of the relation to connect before\n * - after: the id of the relation to connect after\n * - end: it should be at the end\n * - start: it should be at the start\n *\n * Example:\n * - Having a connect array like:\n * [ { id: 4, before: 2 }, { id: 4, before: 3}, {id: 5, before: 4} ]\n * - With the initial relations:\n * [ { id: 2, order: 4 }, { id: 3, order: 10 } ]\n * - Step by step, going through the connect array, the array of relations would be:\n * [ { id: 4, order: 3.5 }, { id: 2, order: 4 }, { id: 3, order: 10 } ]\n * [ { id: 2, order: 4 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]\n * [ { id: 2, order: 4 }, { id: 5, order: 3.5 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]\n * - The final step would be to recalculate fractional order values.\n * [ { id: 2, order: 4 }, { id: 5, order: 3.33 }, { id: 4, order: 3.66 }, { id: 3, order: 10 } ]\n *\n * @param {Array<*>} initArr - array of relations to initialize the class with\n * @param {string} idColumn - the column name of the id\n * @param {string} orderColumn - the column name of the order\n * @param {boolean} strict - if true, will throw an error if a relation is connected adjacent to\n * another one that does not exist\n * @return {*}\n */\nconst relationsOrderer = <TRelation extends Record<string, ID | number | null>>(\n initArr: TRelation[],\n idColumn: keyof TRelation,\n orderColumn: keyof TRelation,\n strict?: boolean\n) => {\n const computedRelations: OrderedLink[] = castArray(initArr ?? []).map((r) => ({\n init: true,\n id: r[idColumn] as ID,\n order: Number(r[orderColumn]) || 1,\n }));\n\n const maxOrder = maxBy('order', computedRelations)?.order || 0;\n\n const findRelation = (id: ID) => {\n const idx = computedRelations.findIndex((r) => r.id === id);\n return { idx, relation: computedRelations[idx] };\n };\n\n const removeRelation = (r: Link) => {\n const { idx } = findRelation(r.id);\n if (idx >= 0) {\n computedRelations.splice(idx, 1);\n }\n };\n\n const insertRelation = (r: Link) => {\n let idx;\n\n if (r.position?.before) {\n const { idx: _idx, relation } = findRelation(r.position.before);\n if (relation.init) {\n r.order = relation.order - 0.5;\n } else {\n r.order = relation.order;\n }\n idx = _idx;\n } else if (r.position?.after) {\n const { idx: _idx, relation } = findRelation(r.position.after);\n if (relation.init) {\n r.order = relation.order + 0.5;\n } else {\n r.order = relation.order;\n }\n\n idx = _idx + 1;\n } else if (r.position?.start) {\n r.order = 0.5;\n idx = 0;\n } else {\n r.order = maxOrder + 0.5;\n idx = computedRelations.length;\n }\n\n // Insert the relation in the array\n computedRelations.splice(idx, 0, r as OrderedLink);\n };\n\n return {\n disconnect(relations: Link | Link[]) {\n castArray(relations).forEach((relation) => {\n removeRelation(relation);\n });\n return this;\n },\n connect(relations: Link | Link[]) {\n sortConnectArray(castArray(relations), computedRelations, strict).forEach((relation) => {\n this.disconnect(relation);\n\n try {\n insertRelation(relation);\n } catch (err) {\n throw new Error(\n `There was a problem connecting relation with id ${\n relation.id\n } at position ${JSON.stringify(\n relation.position\n )}. The list of connect relations is not valid`\n );\n }\n });\n return this;\n },\n get() {\n return computedRelations;\n },\n /**\n * Get a map between the relation id and its order\n */\n getOrderMap() {\n return _(computedRelations)\n .groupBy('order')\n .reduce(\n (acc, relations) => {\n if (relations[0]?.init) return acc;\n relations.forEach((relation, idx) => {\n acc[relation.id] = Math.floor(relation.order) + (idx + 1) / (relations.length + 1);\n });\n return acc;\n },\n {} as Record<ID, number>\n );\n },\n };\n};\n\nexport { relationsOrderer, sortConnectArray };\n"],"names":["sortConnectArray","connectArr","initialArr","strictSort","sortedConnect","needsSorting","relationInInitialArray","reduce","acc","rel","id","mappedRelations","mapper","relation","adjacentRelId","position","before","after","existingRelation","hasNoComponent","hasSameComponent","__component","InvalidRelationError","computed","computeRelation","relationsSeenInBranch","adjacentRelation","push","JSON","stringify","end","forEach","relationsOrderer","initArr","idColumn","orderColumn","strict","computedRelations","castArray","map","r","init","order","Number","maxOrder","maxBy","findRelation","idx","findIndex","removeRelation","splice","insertRelation","_idx","start","length","disconnect","relations","connect","err","Error","get","getOrderMap","_","groupBy","Math","floor"],"mappings":";;;;AAkBA;;;;;;;;;;;;;;;;;IAkBA,MAAMA,mBAAmB,CAACC,UAAAA,EAAoBC,aAAqB,EAAE,EAAEC,aAAa,IAAI,GAAA;AACtF,IAAA,MAAMC,gBAAwB,EAAE;;AAEhC,IAAA,IAAIC,YAAe,GAAA,KAAA;;AAEnB,IAAA,MAAMC,yBAAyBJ,UAAWK,CAAAA,MAAM,CAC9C,CAACC,GAAAA,EAAKC,OAAS;AAAE,YAAA,GAAGD,GAAG;YAAE,CAACC,GAAAA,CAAIC,EAAE,GAAG;AAAK,SAAA,GACxC,EAAC,CAAA;;AAGH,IAAA,M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{
4
+ constructor(message = 'A database error occured', details = {}){
5
+ super();
6
+ this.name = 'DatabaseError';
7
+ this.message = message;
8
+ this.details = details;
9
+ }
10
+ }
11
+
12
+ module.exports = DatabaseError;
13
+ //# sourceMappingURL=database.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.js","sources":["../../src/errors/database.ts"],"sourcesContent":["export default class DatabaseError extends Error {\n details: unknown;\n\n constructor(message = 'A database error occured', details = {}) {\n super();\n this.name = 'DatabaseError';\n this.message = message;\n this.details = details;\n }\n}\n"],"names":["DatabaseError","Error","constructor","message","details","name"],"mappings":";;AAAe,MAAMA,aAAsBC,SAAAA,KAAAA,CAAAA;AAGzCC,IAAAA,WAAAA,CAAYC,UAAU,0BAA0B,EAAEC,OAAU,GAAA,EAAE,CAAE;QAC9D,KAAK,EAAA;QACL,IAAI,CAACC,IAAI,GAAG,eAAA;QACZ,IAAI,CAACF,OAAO,GAAGA,OAAAA;QACf,IAAI,CAACC,OAAO,GAAGA,OAAAA;AACjB;AACF;;;;"}
@@ -0,0 +1,11 @@
1
+ class DatabaseError extends Error {
2
+ constructor(message = 'A database error occured', details = {}){
3
+ super();
4
+ this.name = 'DatabaseError';
5
+ this.message = message;
6
+ this.details = details;
7
+ }
8
+ }
9
+
10
+ export { DatabaseError as default };
11
+ //# sourceMappingURL=database.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.mjs","sources":["../../src/errors/database.ts"],"sourcesContent":["export default class DatabaseError extends Error {\n details: unknown;\n\n constructor(message = 'A database error occured', details = {}) {\n super();\n this.name = 'DatabaseError';\n this.message = message;\n this.details = details;\n }\n}\n"],"names":["DatabaseError","Error","constructor","message","details","name"],"mappings":"AAAe,MAAMA,aAAsBC,SAAAA,KAAAA,CAAAA;AAGzCC,IAAAA,WAAAA,CAAYC,UAAU,0BAA0B,EAAEC,OAAU,GAAA,EAAE,CAAE;QAC9D,KAAK,EAAA;QACL,IAAI,CAACC,IAAI,GAAG,eAAA;QACZ,IAAI,CAACF,OAAO,GAAGA,OAAAA;QACf,IAAI,CAACC,OAAO,GAAGA,OAAAA;AACjB;AACF;;;;"}
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ var database = require('./database.js');
4
+ var notNull = require('./not-null.js');
5
+ var invalidTime = require('./invalid-time.js');
6
+ var invalidDate = require('./invalid-date.js');
7
+ var invalidDatetime = require('./invalid-datetime.js');
8
+ var invalidRelation = require('./invalid-relation.js');
9
+
10
+
11
+
12
+ exports.DatabaseError = database;
13
+ exports.NotNullError = notNull;
14
+ exports.InvalidTimeError = invalidTime;
15
+ exports.InvalidDateError = invalidDate;
16
+ exports.InvalidDateTimeError = invalidDatetime;
17
+ exports.InvalidRelationError = invalidRelation;
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;"}
@@ -0,0 +1,7 @@
1
+ export { default as DatabaseError } from './database.mjs';
2
+ export { default as NotNullError } from './not-null.mjs';
3
+ export { default as InvalidTimeError } from './invalid-time.mjs';
4
+ export { default as InvalidDateError } from './invalid-date.mjs';
5
+ export { default as InvalidDateTimeError } from './invalid-datetime.mjs';
6
+ export { default as InvalidRelationError } from './invalid-relation.mjs';
7
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ var database = require('./database.js');
4
+
5
+ class InvalidDateError extends database {
6
+ constructor(message = 'Invalid date format, expected YYYY-MM-DD'){
7
+ super(message);
8
+ this.name = 'InvalidDateFormat';
9
+ }
10
+ }
11
+
12
+ module.exports = InvalidDateError;
13
+ //# sourceMappingURL=invalid-date.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invalid-date.js","sources":["../../src/errors/invalid-date.ts"],"sourcesContent":["import DatabaseError from './database';\n\nexport default class InvalidDateError extends DatabaseError {\n constructor(message = 'Invalid date format, expected YYYY-MM-DD') {\n super(message);\n this.name = 'InvalidDateFormat';\n }\n}\n"],"names":["InvalidDateError","DatabaseError","constructor","message","name"],"mappings":";;;;AAEe,MAAMA,gBAAyBC,SAAAA,QAAAA,CAAAA;IAC5CC,WAAYC,CAAAA,OAAAA,GAAU,0CAA0C,CAAE;AAChE,QAAA,KAAK,CAACA,OAAAA,CAAAA;QACN,IAAI,CAACC,IAAI,GAAG,mBAAA;AACd;AACF;;;;"}
@@ -0,0 +1,11 @@
1
+ import DatabaseError from './database.mjs';
2
+
3
+ class InvalidDateError extends DatabaseError {
4
+ constructor(message = 'Invalid date format, expected YYYY-MM-DD'){
5
+ super(message);
6
+ this.name = 'InvalidDateFormat';
7
+ }
8
+ }
9
+
10
+ export { InvalidDateError as default };
11
+ //# sourceMappingURL=invalid-date.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invalid-date.mjs","sources":["../../src/errors/invalid-date.ts"],"sourcesContent":["import DatabaseError from './database';\n\nexport default class InvalidDateError extends DatabaseError {\n constructor(message = 'Invalid date format, expected YYYY-MM-DD') {\n super(message);\n this.name = 'InvalidDateFormat';\n }\n}\n"],"names":["InvalidDateError","DatabaseError","constructor","message","name"],"mappings":";;AAEe,MAAMA,gBAAyBC,SAAAA,aAAAA,CAAAA;IAC5CC,WAAYC,CAAAA,OAAAA,GAAU,0CAA0C,CAAE;AAChE,QAAA,KAAK,CAACA,OAAAA,CAAAA;QACN,IAAI,CAACC,IAAI,GAAG,mBAAA;AACd;AACF;;;;"}
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ var database = require('./database.js');
4
+
5
+ class InvalidDateTimeError extends database {
6
+ constructor(message = 'Invalid relation format'){
7
+ super(message);
8
+ this.name = 'InvalidDatetimeFormat';
9
+ }
10
+ }
11
+
12
+ module.exports = InvalidDateTimeError;
13
+ //# sourceMappingURL=invalid-datetime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invalid-datetime.js","sources":["../../src/errors/invalid-datetime.ts"],"sourcesContent":["import DatabaseError from './database';\n\nexport default class InvalidDateTimeError extends DatabaseError {\n constructor(message = 'Invalid relation format') {\n super(message);\n this.name = 'InvalidDatetimeFormat';\n }\n}\n"],"names":["InvalidDateTimeError","DatabaseError","constructor","message","name"],"mappings":";;;;AAEe,MAAMA,oBAA6BC,SAAAA,QAAAA,CAAAA;IAChDC,WAAYC,CAAAA,OAAAA,GAAU,yBAAyB,CAAE;AAC/C,QAAA,KAAK,CAACA,OAAAA,CAAAA;QACN,IAAI,CAACC,IAAI,GAAG,uBAAA;AACd;AACF;;;;"}