@strapi/core 5.0.0-beta.1 → 5.0.0-beta.2

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 (109) hide show
  1. package/dist/Strapi.d.ts +4 -11
  2. package/dist/Strapi.d.ts.map +1 -1
  3. package/dist/Strapi.js +9 -16
  4. package/dist/Strapi.js.map +1 -1
  5. package/dist/Strapi.mjs +9 -17
  6. package/dist/Strapi.mjs.map +1 -1
  7. package/dist/configuration/config-loader.d.ts.map +1 -1
  8. package/dist/configuration/config-loader.js +8 -5
  9. package/dist/configuration/config-loader.js.map +1 -1
  10. package/dist/configuration/config-loader.mjs +8 -5
  11. package/dist/configuration/config-loader.mjs.map +1 -1
  12. package/dist/factories.js.map +1 -1
  13. package/dist/factories.mjs.map +1 -1
  14. package/dist/index.d.ts +4 -2
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +8 -1
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.mjs +10 -3
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/loaders/plugins/get-enabled-plugins.d.ts.map +1 -1
  21. package/dist/loaders/plugins/get-enabled-plugins.js +2 -1
  22. package/dist/loaders/plugins/get-enabled-plugins.js.map +1 -1
  23. package/dist/loaders/plugins/get-enabled-plugins.mjs +2 -1
  24. package/dist/loaders/plugins/get-enabled-plugins.mjs.map +1 -1
  25. package/dist/services/content-api/index.js +1 -1
  26. package/dist/services/content-api/index.js.map +1 -1
  27. package/dist/services/content-api/index.mjs +1 -1
  28. package/dist/services/content-api/index.mjs.map +1 -1
  29. package/dist/services/content-api/permissions/index.js +1 -1
  30. package/dist/services/content-api/permissions/index.js.map +1 -1
  31. package/dist/services/content-api/permissions/index.mjs +1 -1
  32. package/dist/services/content-api/permissions/index.mjs.map +1 -1
  33. package/dist/services/document-service/index.d.ts.map +1 -1
  34. package/dist/services/document-service/index.js +4 -0
  35. package/dist/services/document-service/index.js.map +1 -1
  36. package/dist/services/document-service/index.mjs +4 -0
  37. package/dist/services/document-service/index.mjs.map +1 -1
  38. package/dist/services/document-service/repository.d.ts.map +1 -1
  39. package/dist/services/document-service/repository.js +11 -1
  40. package/dist/services/document-service/repository.js.map +1 -1
  41. package/dist/services/document-service/repository.mjs +11 -1
  42. package/dist/services/document-service/repository.mjs.map +1 -1
  43. package/dist/services/document-service/transform/data.d.ts.map +1 -1
  44. package/dist/services/document-service/transform/data.js +4 -2
  45. package/dist/services/document-service/transform/data.js.map +1 -1
  46. package/dist/services/document-service/transform/data.mjs +4 -2
  47. package/dist/services/document-service/transform/data.mjs.map +1 -1
  48. package/dist/services/document-service/transform/relations/extract/data-ids.d.ts +8 -7
  49. package/dist/services/document-service/transform/relations/extract/data-ids.d.ts.map +1 -1
  50. package/dist/services/document-service/transform/relations/extract/data-ids.js +42 -78
  51. package/dist/services/document-service/transform/relations/extract/data-ids.js.map +1 -1
  52. package/dist/services/document-service/transform/relations/extract/data-ids.mjs +42 -78
  53. package/dist/services/document-service/transform/relations/extract/data-ids.mjs.map +1 -1
  54. package/dist/services/document-service/transform/relations/transform/data-ids.d.ts +7 -3
  55. package/dist/services/document-service/transform/relations/transform/data-ids.d.ts.map +1 -1
  56. package/dist/services/document-service/transform/relations/transform/data-ids.js +57 -125
  57. package/dist/services/document-service/transform/relations/transform/data-ids.js.map +1 -1
  58. package/dist/services/document-service/transform/relations/transform/data-ids.mjs +58 -126
  59. package/dist/services/document-service/transform/relations/transform/data-ids.mjs.map +1 -1
  60. package/dist/services/document-service/transform/relations/transform/default-locale.d.ts +9 -0
  61. package/dist/services/document-service/transform/relations/transform/default-locale.d.ts.map +1 -0
  62. package/dist/services/document-service/transform/relations/transform/default-locale.js +32 -0
  63. package/dist/services/document-service/transform/relations/transform/default-locale.js.map +1 -0
  64. package/dist/services/document-service/transform/relations/transform/default-locale.mjs +32 -0
  65. package/dist/services/document-service/transform/relations/transform/default-locale.mjs.map +1 -0
  66. package/dist/services/document-service/transform/relations/utils/i18n.d.ts +2 -2
  67. package/dist/services/document-service/transform/relations/utils/i18n.d.ts.map +1 -1
  68. package/dist/services/document-service/transform/relations/utils/i18n.js +3 -9
  69. package/dist/services/document-service/transform/relations/utils/i18n.js.map +1 -1
  70. package/dist/services/document-service/transform/relations/utils/i18n.mjs +3 -9
  71. package/dist/services/document-service/transform/relations/utils/i18n.mjs.map +1 -1
  72. package/dist/services/document-service/transform/relations/utils/map-relation.d.ts +6 -0
  73. package/dist/services/document-service/transform/relations/utils/map-relation.d.ts.map +1 -0
  74. package/dist/services/document-service/transform/relations/utils/map-relation.js +87 -0
  75. package/dist/services/document-service/transform/relations/utils/map-relation.js.map +1 -0
  76. package/dist/services/document-service/transform/relations/utils/map-relation.mjs +87 -0
  77. package/dist/services/document-service/transform/relations/utils/map-relation.mjs.map +1 -0
  78. package/dist/services/document-service/transform/relations/utils/types.d.ts +6 -0
  79. package/dist/services/document-service/transform/relations/utils/types.d.ts.map +1 -1
  80. package/dist/services/entity-service/components.d.ts.map +1 -1
  81. package/dist/services/entity-service/components.js +1 -4
  82. package/dist/services/entity-service/components.js.map +1 -1
  83. package/dist/services/entity-service/components.mjs +1 -4
  84. package/dist/services/entity-service/components.mjs.map +1 -1
  85. package/dist/services/entity-validator/index.d.ts.map +1 -1
  86. package/dist/services/entity-validator/index.js +93 -87
  87. package/dist/services/entity-validator/index.js.map +1 -1
  88. package/dist/services/entity-validator/index.mjs +93 -87
  89. package/dist/services/entity-validator/index.mjs.map +1 -1
  90. package/dist/services/metrics/rate-limiter.d.ts.map +1 -1
  91. package/dist/services/metrics/rate-limiter.js.map +1 -1
  92. package/dist/services/metrics/rate-limiter.mjs.map +1 -1
  93. package/dist/services/server/register-routes.js +2 -2
  94. package/dist/services/server/register-routes.js.map +1 -1
  95. package/dist/services/server/register-routes.mjs +2 -2
  96. package/dist/services/server/register-routes.mjs.map +1 -1
  97. package/dist/utils/transform-content-types-to-models.d.ts +21 -16
  98. package/dist/utils/transform-content-types-to-models.d.ts.map +1 -1
  99. package/dist/utils/transform-content-types-to-models.js +95 -57
  100. package/dist/utils/transform-content-types-to-models.js.map +1 -1
  101. package/dist/utils/transform-content-types-to-models.mjs +95 -57
  102. package/dist/utils/transform-content-types-to-models.mjs.map +1 -1
  103. package/package.json +15 -15
  104. package/dist/services/document-service/transform/relations/transform/output-ids.d.ts +0 -2
  105. package/dist/services/document-service/transform/relations/transform/output-ids.d.ts.map +0 -1
  106. package/dist/services/document-service/transform/relations/utils/data.js +0 -12
  107. package/dist/services/document-service/transform/relations/utils/data.js.map +0 -1
  108. package/dist/services/document-service/transform/relations/utils/data.mjs +0 -12
  109. package/dist/services/document-service/transform/relations/utils/data.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"i18n.js","sources":["../../../../../../src/services/document-service/transform/relations/utils/i18n.ts"],"sourcesContent":["import { UID } from '@strapi/types';\nimport { errors } from '@strapi/utils';\nimport { LongHandDocument } from './types';\n\nexport const isLocalizedContentType = (uid: UID.Schema) => {\n const model = strapi.getModel(uid);\n return strapi.plugin('i18n').service('content-types').isLocalizedContentType(model);\n};\n\nexport const getDefaultLocale = () => {\n // TODO: V5 make this more performant\n // return strapi.plugin('i18n').service('locales').getDefaultLocale();\n return 'en';\n};\n\nexport const getRelationTargetLocale = (\n relation: LongHandDocument,\n opts: {\n targetUid: UID.Schema;\n sourceUid: UID.Schema;\n sourceLocale?: string | null;\n }\n) => {\n const defaultLocale = getDefaultLocale();\n const targetLocale = relation.locale || opts.sourceLocale || defaultLocale;\n\n const isTargetLocalized = isLocalizedContentType(opts.targetUid);\n const isSourceLocalized = isLocalizedContentType(opts.sourceUid);\n\n // Locale validations\n if (isSourceLocalized && isTargetLocalized) {\n // Check the targetLocale matches\n if (targetLocale !== opts.sourceLocale) {\n throw new errors.ValidationError(\n `Relation locale does not match the source locale ${JSON.stringify(relation)}`\n );\n }\n }\n\n if (isTargetLocalized) {\n return targetLocale;\n }\n\n return null;\n};\n"],"names":["errors"],"mappings":";;;AAIa,MAAA,yBAAyB,CAAC,QAAoB;AACnD,QAAA,QAAQ,OAAO,SAAS,GAAG;AAC1B,SAAA,OAAO,OAAO,MAAM,EAAE,QAAQ,eAAe,EAAE,uBAAuB,KAAK;AACpF;AAEO,MAAM,mBAAmB,MAAM;AAG7B,SAAA;AACT;AAEa,MAAA,0BAA0B,CACrC,UACA,SAKG;AACH,QAAM,gBAAgB;AACtB,QAAM,eAAe,SAAS,UAAU,KAAK,gBAAgB;AAEvD,QAAA,oBAAoB,uBAAuB,KAAK,SAAS;AACzD,QAAA,oBAAoB,uBAAuB,KAAK,SAAS;AAG/D,MAAI,qBAAqB,mBAAmB;AAEtC,QAAA,iBAAiB,KAAK,cAAc;AACtC,YAAM,IAAIA,YAAO,OAAA;AAAA,QACf,oDAAoD,KAAK,UAAU,QAAQ,CAAC;AAAA,MAAA;AAAA,IAEhF;AAAA,EACF;AAEA,MAAI,mBAAmB;AACd,WAAA;AAAA,EACT;AAEO,SAAA;AACT;;;;"}
1
+ {"version":3,"file":"i18n.js","sources":["../../../../../../src/services/document-service/transform/relations/utils/i18n.ts"],"sourcesContent":["import { UID } from '@strapi/types';\nimport { LongHandDocument } from './types';\n\nexport const isLocalizedContentType = (uid: UID.Schema) => {\n const model = strapi.getModel(uid);\n return strapi.plugin('i18n').service('content-types').isLocalizedContentType(model);\n};\n\nexport const getDefaultLocale = () => {\n return strapi.plugin('i18n').service('locales').getDefaultLocale();\n};\n\nexport const getRelationTargetLocale = (\n relation: LongHandDocument,\n opts: {\n targetUid: UID.Schema;\n sourceUid: UID.Schema;\n sourceLocale?: string | null;\n }\n) => {\n const targetLocale = relation.locale || opts.sourceLocale;\n\n const isTargetLocalized = isLocalizedContentType(opts.targetUid);\n const isSourceLocalized = isLocalizedContentType(opts.sourceUid);\n\n // Both source and target locales should match\n if (isSourceLocalized && isTargetLocalized) {\n return opts.sourceLocale;\n }\n\n if (isTargetLocalized) {\n return targetLocale;\n }\n\n return null;\n};\n"],"names":[],"mappings":";;AAGa,MAAA,yBAAyB,CAAC,QAAoB;AACnD,QAAA,QAAQ,OAAO,SAAS,GAAG;AAC1B,SAAA,OAAO,OAAO,MAAM,EAAE,QAAQ,eAAe,EAAE,uBAAuB,KAAK;AACpF;AAEO,MAAM,mBAAmB,MAAM;AACpC,SAAO,OAAO,OAAO,MAAM,EAAE,QAAQ,SAAS,EAAE;AAClD;AAEa,MAAA,0BAA0B,CACrC,UACA,SAKG;AACG,QAAA,eAAe,SAAS,UAAU,KAAK;AAEvC,QAAA,oBAAoB,uBAAuB,KAAK,SAAS;AACzD,QAAA,oBAAoB,uBAAuB,KAAK,SAAS;AAG/D,MAAI,qBAAqB,mBAAmB;AAC1C,WAAO,KAAK;AAAA,EACd;AAEA,MAAI,mBAAmB;AACd,WAAA;AAAA,EACT;AAEO,SAAA;AACT;;;;"}
@@ -1,22 +1,16 @@
1
- import { errors } from "@strapi/utils";
2
1
  const isLocalizedContentType = (uid) => {
3
2
  const model = strapi.getModel(uid);
4
3
  return strapi.plugin("i18n").service("content-types").isLocalizedContentType(model);
5
4
  };
6
5
  const getDefaultLocale = () => {
7
- return "en";
6
+ return strapi.plugin("i18n").service("locales").getDefaultLocale();
8
7
  };
9
8
  const getRelationTargetLocale = (relation, opts) => {
10
- const defaultLocale = getDefaultLocale();
11
- const targetLocale = relation.locale || opts.sourceLocale || defaultLocale;
9
+ const targetLocale = relation.locale || opts.sourceLocale;
12
10
  const isTargetLocalized = isLocalizedContentType(opts.targetUid);
13
11
  const isSourceLocalized = isLocalizedContentType(opts.sourceUid);
14
12
  if (isSourceLocalized && isTargetLocalized) {
15
- if (targetLocale !== opts.sourceLocale) {
16
- throw new errors.ValidationError(
17
- `Relation locale does not match the source locale ${JSON.stringify(relation)}`
18
- );
19
- }
13
+ return opts.sourceLocale;
20
14
  }
21
15
  if (isTargetLocalized) {
22
16
  return targetLocale;
@@ -1 +1 @@
1
- {"version":3,"file":"i18n.mjs","sources":["../../../../../../src/services/document-service/transform/relations/utils/i18n.ts"],"sourcesContent":["import { UID } from '@strapi/types';\nimport { errors } from '@strapi/utils';\nimport { LongHandDocument } from './types';\n\nexport const isLocalizedContentType = (uid: UID.Schema) => {\n const model = strapi.getModel(uid);\n return strapi.plugin('i18n').service('content-types').isLocalizedContentType(model);\n};\n\nexport const getDefaultLocale = () => {\n // TODO: V5 make this more performant\n // return strapi.plugin('i18n').service('locales').getDefaultLocale();\n return 'en';\n};\n\nexport const getRelationTargetLocale = (\n relation: LongHandDocument,\n opts: {\n targetUid: UID.Schema;\n sourceUid: UID.Schema;\n sourceLocale?: string | null;\n }\n) => {\n const defaultLocale = getDefaultLocale();\n const targetLocale = relation.locale || opts.sourceLocale || defaultLocale;\n\n const isTargetLocalized = isLocalizedContentType(opts.targetUid);\n const isSourceLocalized = isLocalizedContentType(opts.sourceUid);\n\n // Locale validations\n if (isSourceLocalized && isTargetLocalized) {\n // Check the targetLocale matches\n if (targetLocale !== opts.sourceLocale) {\n throw new errors.ValidationError(\n `Relation locale does not match the source locale ${JSON.stringify(relation)}`\n );\n }\n }\n\n if (isTargetLocalized) {\n return targetLocale;\n }\n\n return null;\n};\n"],"names":[],"mappings":";AAIa,MAAA,yBAAyB,CAAC,QAAoB;AACnD,QAAA,QAAQ,OAAO,SAAS,GAAG;AAC1B,SAAA,OAAO,OAAO,MAAM,EAAE,QAAQ,eAAe,EAAE,uBAAuB,KAAK;AACpF;AAEO,MAAM,mBAAmB,MAAM;AAG7B,SAAA;AACT;AAEa,MAAA,0BAA0B,CACrC,UACA,SAKG;AACH,QAAM,gBAAgB;AACtB,QAAM,eAAe,SAAS,UAAU,KAAK,gBAAgB;AAEvD,QAAA,oBAAoB,uBAAuB,KAAK,SAAS;AACzD,QAAA,oBAAoB,uBAAuB,KAAK,SAAS;AAG/D,MAAI,qBAAqB,mBAAmB;AAEtC,QAAA,iBAAiB,KAAK,cAAc;AACtC,YAAM,IAAI,OAAO;AAAA,QACf,oDAAoD,KAAK,UAAU,QAAQ,CAAC;AAAA,MAAA;AAAA,IAEhF;AAAA,EACF;AAEA,MAAI,mBAAmB;AACd,WAAA;AAAA,EACT;AAEO,SAAA;AACT;"}
1
+ {"version":3,"file":"i18n.mjs","sources":["../../../../../../src/services/document-service/transform/relations/utils/i18n.ts"],"sourcesContent":["import { UID } from '@strapi/types';\nimport { LongHandDocument } from './types';\n\nexport const isLocalizedContentType = (uid: UID.Schema) => {\n const model = strapi.getModel(uid);\n return strapi.plugin('i18n').service('content-types').isLocalizedContentType(model);\n};\n\nexport const getDefaultLocale = () => {\n return strapi.plugin('i18n').service('locales').getDefaultLocale();\n};\n\nexport const getRelationTargetLocale = (\n relation: LongHandDocument,\n opts: {\n targetUid: UID.Schema;\n sourceUid: UID.Schema;\n sourceLocale?: string | null;\n }\n) => {\n const targetLocale = relation.locale || opts.sourceLocale;\n\n const isTargetLocalized = isLocalizedContentType(opts.targetUid);\n const isSourceLocalized = isLocalizedContentType(opts.sourceUid);\n\n // Both source and target locales should match\n if (isSourceLocalized && isTargetLocalized) {\n return opts.sourceLocale;\n }\n\n if (isTargetLocalized) {\n return targetLocale;\n }\n\n return null;\n};\n"],"names":[],"mappings":"AAGa,MAAA,yBAAyB,CAAC,QAAoB;AACnD,QAAA,QAAQ,OAAO,SAAS,GAAG;AAC1B,SAAA,OAAO,OAAO,MAAM,EAAE,QAAQ,eAAe,EAAE,uBAAuB,KAAK;AACpF;AAEO,MAAM,mBAAmB,MAAM;AACpC,SAAO,OAAO,OAAO,MAAM,EAAE,QAAQ,SAAS,EAAE;AAClD;AAEa,MAAA,0BAA0B,CACrC,UACA,SAKG;AACG,QAAA,eAAe,SAAS,UAAU,KAAK;AAEvC,QAAA,oBAAoB,uBAAuB,KAAK,SAAS;AACzD,QAAA,oBAAoB,uBAAuB,KAAK,SAAS;AAG/D,MAAI,qBAAqB,mBAAmB;AAC1C,WAAO,KAAK;AAAA,EACd;AAEA,MAAI,mBAAmB;AACd,WAAA;AAAA,EACT;AAEO,SAAA;AACT;"}
@@ -0,0 +1,6 @@
1
+ /// <reference types="lodash" />
2
+ import { Relation } from './types';
3
+ declare const mapRelationCurried: import("lodash").CurriedFunction2<(relation: any) => any, Relation, Promise<Relation>>;
4
+ declare const traverseEntityRelationsCurried: import("lodash").CurriedFunction3<import("@strapi/utils/dist/traverse-entity").Visitor, import("@strapi/utils/dist/traverse-entity").TraverseOptions, import("@strapi/utils/dist/types").Data, Promise<import("@strapi/utils/dist/types").Data>>;
5
+ export { mapRelationCurried as mapRelation, traverseEntityRelationsCurried as traverseEntityRelations, };
6
+ //# sourceMappingURL=map-relation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-relation.d.ts","sourceRoot":"","sources":["../../../../../../src/services/document-service/transform/relations/utils/map-relation.ts"],"names":[],"mappings":";AAMA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAgJnC,QAAA,MAAM,kBAAkB,+CA/GD,GAAG,KAAK,GAAG,8BA+GW,CAAC;AAC9C,QAAA,MAAM,8BAA8B,kPAAiC,CAAC;AAEtE,OAAO,EACL,kBAAkB,IAAI,WAAW,EACjC,8BAA8B,IAAI,uBAAuB,GAC1D,CAAC"}
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const _ = require("lodash/fp");
4
+ const strapiUtils = require("@strapi/utils");
5
+ const isNumeric = (value) => {
6
+ if (Array.isArray(value))
7
+ return false;
8
+ const parsed = parseInt(value, 10);
9
+ return !Number.isNaN(parsed);
10
+ };
11
+ const toArray = (value) => {
12
+ if (_.isNil(value))
13
+ return value;
14
+ if (Array.isArray(value))
15
+ return value;
16
+ return [value];
17
+ };
18
+ const mapRelation = async (callback, rel, isRecursive = false) => {
19
+ let relation = rel;
20
+ const wrapInSet = (value) => {
21
+ if (isRecursive) {
22
+ return value;
23
+ }
24
+ return { set: toArray(value) };
25
+ };
26
+ if (_.isNil(relation)) {
27
+ return callback(relation);
28
+ }
29
+ if (Array.isArray(relation)) {
30
+ return strapiUtils.async.map(relation, (r) => mapRelation(callback, r, true)).then((result) => result.flat().filter(Boolean)).then(wrapInSet);
31
+ }
32
+ if (_.isObject(relation)) {
33
+ if ("id" in relation || "documentId" in relation) {
34
+ const result = await callback(relation);
35
+ return wrapInSet(result);
36
+ }
37
+ if (!relation.set && !relation.disconnect && !relation.connect) {
38
+ return callback(relation);
39
+ }
40
+ if (relation.set) {
41
+ const set = await mapRelation(callback, relation.set, true);
42
+ relation = { ...relation, set: toArray(set) };
43
+ }
44
+ if (relation.disconnect) {
45
+ const disconnect = await mapRelation(callback, relation.disconnect, true);
46
+ relation = { ...relation, disconnect: toArray(disconnect) };
47
+ }
48
+ if (relation.connect) {
49
+ const connect = await mapRelation(callback, relation.connect, true);
50
+ relation = { ...relation, connect: toArray(connect) };
51
+ }
52
+ return relation;
53
+ }
54
+ if (isNumeric(relation)) {
55
+ const result = await callback({ id: relation });
56
+ return wrapInSet(result);
57
+ }
58
+ if (typeof relation === "string") {
59
+ const result = await callback({ documentId: relation });
60
+ return wrapInSet(result);
61
+ }
62
+ return callback(relation);
63
+ };
64
+ const traverseEntityRelations = async (visitor, options, data) => {
65
+ return strapiUtils.traverseEntity(
66
+ async (options2, utils) => {
67
+ if (options2.attribute.type !== "relation") {
68
+ return;
69
+ }
70
+ if (options2.attribute.useJoinTable === false) {
71
+ return;
72
+ }
73
+ const target = options2.attribute.target;
74
+ if (!target) {
75
+ return;
76
+ }
77
+ return visitor(options2, utils);
78
+ },
79
+ options,
80
+ data
81
+ );
82
+ };
83
+ const mapRelationCurried = _.curry(mapRelation);
84
+ const traverseEntityRelationsCurried = _.curry(traverseEntityRelations);
85
+ exports.mapRelation = mapRelationCurried;
86
+ exports.traverseEntityRelations = traverseEntityRelationsCurried;
87
+ //# sourceMappingURL=map-relation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-relation.js","sources":["../../../../../../src/services/document-service/transform/relations/utils/map-relation.ts"],"sourcesContent":["/* eslint-disable node/no-callback-literal */\nimport { isObject, curry, isNil } from 'lodash/fp';\n\nimport type { UID } from '@strapi/types';\nimport { traverseEntity, async } from '@strapi/utils';\n\nimport { Relation } from './types';\n\nconst isNumeric = (value: any): value is number => {\n if (Array.isArray(value)) return false; // Handle [1, 'docId'] case\n const parsed = parseInt(value, 10);\n return !Number.isNaN(parsed);\n};\n\nconst toArray = (value: any) => {\n // Keep value as it is if it's a nullish value\n if (isNil(value)) return value;\n if (Array.isArray(value)) return value;\n\n return [value];\n};\n\n/**\n * There are multiple ways to create Strapi relations.\n * This is a utility to traverse and transform relation data\n *\n *\n * For consistency and ease of use, the response will always be an object with the following shape:\n * { set: [{...}], connect: [{...}], disconnect: [{...}] }\n *\n * @example\n * transformRelationData({\n * onLongHand: (relation) => {\n * // Change the id of the relation\n * return { id: 'other' };\n * },\n * }, relation)\n */\nconst mapRelation = async (\n callback: (relation: any) => any,\n rel: Relation,\n isRecursive = false\n): Promise<Relation> => {\n let relation: Relation = rel;\n\n const wrapInSet = (value: any) => {\n // Ignore wrapping if it's a recursive call\n if (isRecursive) {\n return value;\n }\n return { set: toArray(value) };\n };\n\n // undefined | null\n if (isNil(relation)) {\n return callback(relation);\n }\n\n // LongHand[] | ShortHand[]\n if (Array.isArray(relation)) {\n return async\n .map(relation, (r: Relation) => mapRelation(callback, r, true))\n .then((result: any) => result.flat().filter(Boolean))\n .then(wrapInSet);\n }\n\n // LongHand\n if (isObject(relation)) {\n // { id: 1 } || { documentId: 1 }\n if ('id' in relation || 'documentId' in relation) {\n const result = await callback(relation);\n return wrapInSet(result);\n }\n\n // If not connecting anything, return default visitor\n if (!relation.set && !relation.disconnect && !relation.connect) {\n return callback(relation);\n }\n\n // { set }\n if (relation.set) {\n const set: any = await mapRelation(callback, relation.set, true);\n relation = { ...relation, set: toArray(set) };\n }\n\n // { disconnect}\n if (relation.disconnect) {\n const disconnect: any = await mapRelation(callback, relation.disconnect, true);\n relation = { ...relation, disconnect: toArray(disconnect) };\n }\n\n // { connect }\n if (relation.connect) {\n // Transform the relation to connect\n const connect: any = await mapRelation(callback, relation.connect, true);\n relation = { ...relation, connect: toArray(connect) };\n }\n\n return relation;\n }\n\n // ShortHand\n if (isNumeric(relation)) {\n const result = await callback({ id: relation });\n return wrapInSet(result);\n }\n\n if (typeof relation === 'string') {\n const result = await callback({ documentId: relation });\n return wrapInSet(result);\n }\n\n // Anything else\n return callback(relation);\n};\n\ntype TraverseEntity = Parameters<typeof traverseEntity>;\n\n/**\n * Utility function, same as `traverseEntity` but only for relations.\n */\nconst traverseEntityRelations = async (\n visitor: TraverseEntity[0],\n options: TraverseEntity[1],\n data: TraverseEntity[2]\n) => {\n return traverseEntity(\n async (options, utils) => {\n if (options.attribute.type !== 'relation') {\n return;\n }\n\n // TODO: Handle join columns\n if (options.attribute.useJoinTable === false) {\n return;\n }\n\n // TODO: Handle morph relations (they have multiple targets)\n const target = options.attribute.target as UID.Schema | undefined;\n if (!target) {\n return;\n }\n\n return visitor(options, utils);\n },\n options,\n data\n );\n};\n\nconst mapRelationCurried = curry(mapRelation);\nconst traverseEntityRelationsCurried = curry(traverseEntityRelations);\n\nexport {\n mapRelationCurried as mapRelation,\n traverseEntityRelationsCurried as traverseEntityRelations,\n};\n"],"names":["isNil","async","isObject","traverseEntity","options","curry"],"mappings":";;;;AAQA,MAAM,YAAY,CAAC,UAAgC;AAC7C,MAAA,MAAM,QAAQ,KAAK;AAAU,WAAA;AAC3B,QAAA,SAAS,SAAS,OAAO,EAAE;AAC1B,SAAA,CAAC,OAAO,MAAM,MAAM;AAC7B;AAEA,MAAM,UAAU,CAAC,UAAe;AAE9B,MAAIA,EAAAA,MAAM,KAAK;AAAU,WAAA;AACrB,MAAA,MAAM,QAAQ,KAAK;AAAU,WAAA;AAEjC,SAAO,CAAC,KAAK;AACf;AAkBA,MAAM,cAAc,OAClB,UACA,KACA,cAAc,UACQ;AACtB,MAAI,WAAqB;AAEnB,QAAA,YAAY,CAAC,UAAe;AAEhC,QAAI,aAAa;AACR,aAAA;AA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AAY,SAAU,CAAA;AACtD,WAAO,UAAU,MAAM;AAAA,EACzB;AAGA,SAAO,SAAS,QAAQ;AAC1B;AAOA,MAAM,0BAA0B,OAC9B,SACA,SACA,SACG;AACI,SAAAC,YAAA;AAAA,IACL,OAAOC,UAAS,UAAU;AACpBA,UAAAA,SAAQ,UAAU,SAAS,YAAY;AACzC;AAAA,MACF;AAGIA,UAAAA,SAAQ,UAAU,iBAAiB,OAAO;AAC5C;AAAA,MACF;AAGM,YAAA,SAASA,SAAQ,UAAU;AACjC,UAAI,CAAC,QAAQ;AACX;AAAA,MACF;AAEO,aAAA,QAAQA,UAAS,KAAK;AAAA,IAC/B;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;AAEM,MAAA,qBAAqBC,QAAM,WAAW;AACtC,MAAA,iCAAiCA,QAAM,uBAAuB;;;"}
@@ -0,0 +1,87 @@
1
+ import { curry, isNil, isObject } from "lodash/fp";
2
+ import { async, traverseEntity } from "@strapi/utils";
3
+ const isNumeric = (value) => {
4
+ if (Array.isArray(value))
5
+ return false;
6
+ const parsed = parseInt(value, 10);
7
+ return !Number.isNaN(parsed);
8
+ };
9
+ const toArray = (value) => {
10
+ if (isNil(value))
11
+ return value;
12
+ if (Array.isArray(value))
13
+ return value;
14
+ return [value];
15
+ };
16
+ const mapRelation = async (callback, rel, isRecursive = false) => {
17
+ let relation = rel;
18
+ const wrapInSet = (value) => {
19
+ if (isRecursive) {
20
+ return value;
21
+ }
22
+ return { set: toArray(value) };
23
+ };
24
+ if (isNil(relation)) {
25
+ return callback(relation);
26
+ }
27
+ if (Array.isArray(relation)) {
28
+ return async.map(relation, (r) => mapRelation(callback, r, true)).then((result) => result.flat().filter(Boolean)).then(wrapInSet);
29
+ }
30
+ if (isObject(relation)) {
31
+ if ("id" in relation || "documentId" in relation) {
32
+ const result = await callback(relation);
33
+ return wrapInSet(result);
34
+ }
35
+ if (!relation.set && !relation.disconnect && !relation.connect) {
36
+ return callback(relation);
37
+ }
38
+ if (relation.set) {
39
+ const set = await mapRelation(callback, relation.set, true);
40
+ relation = { ...relation, set: toArray(set) };
41
+ }
42
+ if (relation.disconnect) {
43
+ const disconnect = await mapRelation(callback, relation.disconnect, true);
44
+ relation = { ...relation, disconnect: toArray(disconnect) };
45
+ }
46
+ if (relation.connect) {
47
+ const connect = await mapRelation(callback, relation.connect, true);
48
+ relation = { ...relation, connect: toArray(connect) };
49
+ }
50
+ return relation;
51
+ }
52
+ if (isNumeric(relation)) {
53
+ const result = await callback({ id: relation });
54
+ return wrapInSet(result);
55
+ }
56
+ if (typeof relation === "string") {
57
+ const result = await callback({ documentId: relation });
58
+ return wrapInSet(result);
59
+ }
60
+ return callback(relation);
61
+ };
62
+ const traverseEntityRelations = async (visitor, options, data) => {
63
+ return traverseEntity(
64
+ async (options2, utils) => {
65
+ if (options2.attribute.type !== "relation") {
66
+ return;
67
+ }
68
+ if (options2.attribute.useJoinTable === false) {
69
+ return;
70
+ }
71
+ const target = options2.attribute.target;
72
+ if (!target) {
73
+ return;
74
+ }
75
+ return visitor(options2, utils);
76
+ },
77
+ options,
78
+ data
79
+ );
80
+ };
81
+ const mapRelationCurried = curry(mapRelation);
82
+ const traverseEntityRelationsCurried = curry(traverseEntityRelations);
83
+ export {
84
+ mapRelationCurried as mapRelation,
85
+ traverseEntityRelationsCurried as traverseEntityRelations
86
+ };
87
+ //# sourceMappingURL=map-relation.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-relation.mjs","sources":["../../../../../../src/services/document-service/transform/relations/utils/map-relation.ts"],"sourcesContent":["/* eslint-disable node/no-callback-literal */\nimport { isObject, curry, isNil } from 'lodash/fp';\n\nimport type { UID } from '@strapi/types';\nimport { traverseEntity, async } from '@strapi/utils';\n\nimport { Relation } from './types';\n\nconst isNumeric = (value: any): value is number => {\n if (Array.isArray(value)) return false; // Handle [1, 'docId'] case\n const parsed = parseInt(value, 10);\n return !Number.isNaN(parsed);\n};\n\nconst toArray = (value: any) => {\n // Keep value as it is if it's a nullish value\n if (isNil(value)) return value;\n if (Array.isArray(value)) return value;\n\n return [value];\n};\n\n/**\n * There are multiple ways to create Strapi relations.\n * This is a utility to traverse and transform relation data\n *\n *\n * For consistency and ease of use, the response will always be an object with the following shape:\n * { set: [{...}], connect: [{...}], disconnect: [{...}] }\n *\n * @example\n * transformRelationData({\n * onLongHand: (relation) => {\n * // Change the id of the relation\n * return { id: 'other' };\n * },\n * }, relation)\n */\nconst mapRelation = async (\n callback: (relation: any) => any,\n rel: Relation,\n isRecursive = false\n): Promise<Relation> => {\n let relation: Relation = rel;\n\n const wrapInSet = (value: any) => {\n // Ignore wrapping if it's a recursive call\n if (isRecursive) {\n return value;\n }\n return { set: toArray(value) };\n };\n\n // undefined | null\n if (isNil(relation)) {\n return callback(relation);\n }\n\n // LongHand[] | ShortHand[]\n if (Array.isArray(relation)) {\n return async\n .map(relation, (r: Relation) => mapRelation(callback, r, true))\n .then((result: any) => result.flat().filter(Boolean))\n .then(wrapInSet);\n }\n\n // LongHand\n if (isObject(relation)) {\n // { id: 1 } || { documentId: 1 }\n if ('id' in relation || 'documentId' in relation) {\n const result = await callback(relation);\n return wrapInSet(result);\n }\n\n // If not connecting anything, return default visitor\n if (!relation.set && !relation.disconnect && !relation.connect) {\n return callback(relation);\n }\n\n // { set }\n if (relation.set) {\n const set: any = await mapRelation(callback, relation.set, true);\n relation = { ...relation, set: toArray(set) };\n }\n\n // { disconnect}\n if (relation.disconnect) {\n const disconnect: any = await mapRelation(callback, relation.disconnect, true);\n relation = { ...relation, disconnect: toArray(disconnect) };\n }\n\n // { connect }\n if (relation.connect) {\n // Transform the relation to connect\n const connect: any = await mapRelation(callback, relation.connect, true);\n relation = { ...relation, connect: toArray(connect) };\n }\n\n return relation;\n }\n\n // ShortHand\n if (isNumeric(relation)) {\n const result = await callback({ id: relation });\n return wrapInSet(result);\n }\n\n if (typeof relation === 'string') {\n const result = await callback({ documentId: relation });\n return wrapInSet(result);\n }\n\n // Anything else\n return callback(relation);\n};\n\ntype TraverseEntity = Parameters<typeof traverseEntity>;\n\n/**\n * Utility function, same as `traverseEntity` but only for relations.\n */\nconst traverseEntityRelations = async (\n visitor: TraverseEntity[0],\n options: TraverseEntity[1],\n data: TraverseEntity[2]\n) => {\n return traverseEntity(\n async (options, utils) => {\n if (options.attribute.type !== 'relation') {\n return;\n }\n\n // TODO: Handle join columns\n if (options.attribute.useJoinTable === false) {\n return;\n }\n\n // TODO: Handle morph relations (they have multiple targets)\n const target = options.attribute.target as UID.Schema | undefined;\n if (!target) {\n return;\n }\n\n return visitor(options, utils);\n },\n options,\n data\n );\n};\n\nconst mapRelationCurried = curry(mapRelation);\nconst traverseEntityRelationsCurried = curry(traverseEntityRelations);\n\nexport {\n mapRelationCurried as mapRelation,\n traverseEntityRelationsCurried as traverseEntityRelations,\n};\n"],"names":["options"],"mappings":";;AAQA,MAAM,YAAY,CAAC,UAAgC;AAC7C,MAAA,MAAM,QAAQ,KAAK;AAAU,WAAA;AAC3B,QAAA,SAAS,SAAS,OAAO,EAAE;AAC1B,SAAA,CAAC,OAAO,MAAM,MAAM;AAC7B;AAEA,MAAM,UAAU,CAAC,UAAe;AAE9B,MAAI,MAAM,KAAK;AAAU,WAAA;AACrB,MAAA,MAAM,QAAQ,KAAK;AAAU,WAAA;AAEjC,SAAO,CAAC,KAAK;AACf;AAkBA,MAAM,cAAc,OAClB,UACA,KACA,cAAc,UACQ;AACtB,MAAI,WAAqB;AAEnB,QAAA,YAAY,CAAC,UAAe;AAEhC,QAAI,aAAa;AACR,aAAA;AAAA,IACT;AACA,WAAO,EAAE,KAAK,QAAQ,KAAK,EAAE;AAAA,EAAA;AAI3B,MAAA,MAAM,QAAQ,GAAG;AACnB,WAAO,SAAS,QAAQ;AAAA,EAC1B;AAGI,MAAA,MAAM,QAAQ,QAAQ,GAAG;AACpB,WAAA,MACJ,IAAI,UAAU,CAAC,MAAgB,YAAY,UAAU,GAAG,IAAI,CAAC,EAC7D,KAAK,CAAC,WAAgB,OA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AASA,SAAQ,UAAU;AACjC,UAAI,CAAC,QAAQ;AACX;AAAA,MACF;AAEO,aAAA,QAAQA,UAAS,KAAK;AAAA,IAC/B;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;AAEM,MAAA,qBAAqB,MAAM,WAAW;AACtC,MAAA,iCAAiC,MAAM,uBAAuB;"}
@@ -11,5 +11,11 @@ export type LongHandDocument = {
11
11
  position?: object;
12
12
  };
13
13
  export type LongHand = LongHandEntity | LongHandDocument;
14
+ export type RelationPrimitives = ShortHand | LongHand | ShortHand[] | LongHand[] | null | undefined;
15
+ export type Relation = RelationPrimitives | {
16
+ connect?: RelationPrimitives;
17
+ disconnect?: RelationPrimitives;
18
+ set?: RelationPrimitives;
19
+ };
14
20
  export type GetIds = (relation: ID) => ID[] | null;
15
21
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../../src/services/document-service/transform/relations/utils/types.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,CAAC;AACjC,MAAM,MAAM,SAAS,GAAG,EAAE,CAAC;AAE3B,MAAM,MAAM,cAAc,GAAG;IAAE,EAAE,EAAE,EAAE,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAC3D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,UAAU,EAAE,EAAE,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,GAAG,WAAW,CAAC;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AACF,MAAM,MAAM,QAAQ,GAAG,cAAc,GAAG,gBAAgB,CAAC;AAEzD,MAAM,MAAM,MAAM,GAAG,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,IAAI,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../../src/services/document-service/transform/relations/utils/types.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,CAAC;AACjC,MAAM,MAAM,SAAS,GAAG,EAAE,CAAC;AAE3B,MAAM,MAAM,cAAc,GAAG;IAAE,EAAE,EAAE,EAAE,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAC3D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,UAAU,EAAE,EAAE,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,GAAG,WAAW,CAAC;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG,cAAc,GAAG,gBAAgB,CAAC;AACzD,MAAM,MAAM,kBAAkB,GAAG,SAAS,GAAG,QAAQ,GAAG,SAAS,EAAE,GAAG,QAAQ,EAAE,GAAG,IAAI,GAAG,SAAS,CAAC;AACpG,MAAM,MAAM,QAAQ,GAChB,kBAAkB,GAClB;IAAE,OAAO,CAAC,EAAE,kBAAkB,CAAC;IAAC,UAAU,CAAC,EAAE,kBAAkB,CAAC;IAAC,GAAG,CAAC,EAAE,kBAAkB,CAAA;CAAE,CAAC;AAEhG,MAAM,MAAM,MAAM,GAAG,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,IAAI,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../../../src/services/entity-service/components.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAG/E,KAAK,gBAAgB,CAAC,IAAI,SAAS,GAAG,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAC1D,IAAI,EACJ,MAAM,CAAC,oBAAoB,CAAC,IAAI,EA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AAgB,EAChB,gBAAgB,EAChB,eAAe,GAChB,CAAC"}
@@ -133,9 +133,7 @@ const updateComponents = async (uid, entityToUpdate, data) => {
133
133
  }
134
134
  };
135
135
  }
136
- continue;
137
- }
138
- if (attribute.type === "dynamiczone") {
136
+ } else if (attribute.type === "dynamiczone") {
139
137
  const dynamiczoneValues = data[attributeName];
140
138
  await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);
141
139
  if (!Array.isArray(dynamiczoneValues)) {
@@ -155,7 +153,6 @@ const updateComponents = async (uid, entityToUpdate, data) => {
155
153
  },
156
154
  { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }
157
155
  );
158
- continue;
159
156
  }
160
157
  }
161
158
  return componentBody;
@@ -1 +1 @@
1
- {"version":3,"file":"components.js","sources":["../../../src/services/entity-service/components.ts"],"sourcesContent":["import _ from 'lodash';\nimport { has, omit, pipe, assign } from 'lodash/fp';\nimport type { Struct, Utils, UID, Schema, Data, Modules } from '@strapi/types';\nimport { contentTypes as contentTypesUtils, async, errors } from '@strapi/utils';\n\ntype LoadedComponents<TUID extends UID.Schema> = Data.Entity<\n TUID,\n Schema.AttributeNamesByType<TUID, 'component' | 'dynamiczone'>\n>;\n\ntype SingleComponentValue = Schema.Attribute.ComponentValue<UID.Component, false>;\ntype RepeatableComponentValue = Schema.Attribute.ComponentValue<UID.Component, true>;\n\ntype ComponentValue = SingleComponentValue | RepeatableComponentValue;\n\ntype DynamicZoneValue = Schema.Attribute.DynamicZoneValue<UID.Component[]>;\n\ntype ComponentBody = {\n [key: string]: ComponentValue | DynamicZoneValue;\n};\n\nconst isDialectMySQL = () => strapi.db?.dialect.client === 'mysql';\n\nfunction omitComponentData(\n contentType: Struct.ContentTypeSchema,\n data: Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>>;\nfunction omitComponentData(\n contentType: Struct.ComponentSchema,\n data: Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>>;\nfunction omitComponentData(\n contentType: Struct.Schema,\n data: Modules.EntityService.Params.Data.Input<\n Struct.ContentTypeSchema['uid'] | Struct.ComponentSchema['uid']\n >\n): Partial<\n Modules.EntityService.Params.Data.Input<\n Struct.ContentTypeSchema['uid'] | Struct.ComponentSchema['uid']\n >\n> {\n const { attributes } = contentType;\n const componentAttributes = Object.keys(attributes).filter((attributeName) =>\n contentTypesUtils.isComponentAttribute(attributes[attributeName])\n );\n\n return omit(componentAttributes, data);\n}\n\n// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer\nconst createComponents = async <\n TUID extends UID.Schema,\n TData extends Modules.EntityService.Params.Data.Input<TUID>\n>(\n uid: TUID,\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data) || !contentTypesUtils.isComponentAttribute(attribute)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData];\n\n if (componentValue === null) {\n continue;\n }\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => createComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as RepeatableComponentValue;\n\n componentBody[attributeName] = components.map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await createComponent(\n componentUID,\n componentValue as Modules.EntityService.Params.Data.Input<UID.Component>\n );\n componentBody[attributeName] = {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[\n attributeName as keyof TData\n ] as Modules.EntityService.Params.Attribute.GetValue<Schema.Attribute.DynamicZone>;\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n const createDynamicZoneComponents = async (\n value: Utils.Array.Values<typeof dynamiczoneValues>\n ) => {\n const { id } = await createComponent(value.__component, value);\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n };\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n createDynamicZoneComponents,\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst getComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entity: { id: Modules.EntityService.Params.Attribute.ID }\n): Promise<LoadedComponents<TUID>> => {\n const componentAttributes = contentTypesUtils.getComponentAttributes(strapi.getModel(uid));\n\n if (_.isEmpty(componentAttributes)) {\n return {} as LoadedComponents<TUID>;\n }\n\n return strapi.db.query(uid).load(entity, componentAttributes) as Promise<LoadedComponents<TUID>>;\n};\n\n/*\n delete old components\n create or update\n*/\nconst updateComponents = async <\n TUID extends UID.Schema,\n TData extends Partial<Modules.EntityService.Params.Data.Input<TUID>>\n>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n for (const attributeName of Object.keys(attributes)) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData] as ComponentValue;\n\n await deleteOldComponents(uid, componentUID, entityToUpdate, attributeName, componentValue);\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => updateOrCreateComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as RepeatableComponentValue;\n\n componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await updateOrCreateComponent(componentUID, componentValue);\n componentBody[attributeName] = component && {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[attributeName as keyof TData] as DynamicZoneValue;\n\n await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n async (value: any) => {\n const { id } = await updateOrCreateComponent(value.__component, value);\n\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n },\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst pickStringifiedId = ({\n id,\n}: {\n id: Modules.EntityService.Params.Attribute.ID;\n}): Modules.EntityService.Params.Attribute.ID & string => {\n if (typeof id === 'string') {\n return id;\n }\n\n return `${id}`;\n};\n\nconst deleteOldComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n componentUID: UID.Component,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n componentValue: ComponentValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as ComponentValue;\n\n const idsToKeep = _.castArray(componentValue).filter(has('id')).map(pickStringifiedId);\n const allIds = _.castArray(previousValue).filter(has('id')).map(pickStringifiedId);\n\n idsToKeep.forEach((id) => {\n if (!allIds.includes(id)) {\n throw new errors.ApplicationError(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n }\n });\n\n const idsToDelete = _.difference(allIds, idsToKeep);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n await deleteComponent(componentUID, { id: idToDelete });\n }\n }\n};\n\nconst deleteOldDZComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n dynamiczoneValues: DynamicZoneValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as DynamicZoneValue;\n\n const idsToKeep = _.castArray(dynamiczoneValues)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n const allIds = _.castArray(previousValue)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n idsToKeep.forEach(({ id, __component }) => {\n if (!allIds.find((el) => el.id === id && el.__component === __component)) {\n const err = new Error(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n\n Object.assign(err, { status: 400 });\n throw err;\n }\n });\n\n type IdsToDelete = DynamicZoneValue;\n\n const idsToDelete = allIds.reduce((acc, { id, __component }) => {\n if (!idsToKeep.find((el) => el.id === id && el.__component === __component)) {\n acc.push({ id, __component });\n }\n\n return acc;\n }, [] as IdsToDelete);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n const { id, __component } = idToDelete;\n await deleteComponent(__component, { id });\n }\n }\n};\n\nconst deleteComponents = async <TUID extends UID.Schema, TEntity extends Data.Entity<TUID>>(\n uid: TUID,\n entityToDelete: TEntity,\n { loadComponents = true } = {}\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (attribute.type === 'component' || attribute.type === 'dynamiczone') {\n let value;\n if (loadComponents) {\n value = await strapi.db.query(uid).load(entityToDelete, attributeName);\n } else {\n value = entityToDelete[attributeName as keyof TEntity];\n }\n\n if (!value) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID } = attribute;\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(componentUID, subValue),\n {\n concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity,\n }\n );\n } else {\n // delete dynamic zone components\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(subValue.__component, subValue),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n }\n\n continue;\n }\n }\n};\n\n/** *************************\n Component queries\n************************** */\n\n// components can have nested compos so this must be recursive\nconst createComponent = async <TUID extends UID.Component>(\n uid: TUID,\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid);\n\n const componentData = await createComponents(uid, data);\n const transform = pipe(\n // Make sure we don't save the component with a pre-defined ID\n omit('id'),\n // Remove the component data from the original data object ...\n (payload) => omitComponentData(model, payload),\n // ... and assign the newly created component instead\n assign(componentData)\n );\n\n return strapi.db.query(uid).create({ data: transform(data) });\n};\n\n// components can have nested compos so this must be recursive\nconst updateComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid);\n\n const componentData = await updateComponents(uid, componentToUpdate, data);\n\n return strapi.db.query(uid).update({\n where: {\n id: componentToUpdate.id,\n },\n data: Object.assign(omitComponentData(model, data), componentData),\n });\n};\n\nconst updateOrCreateComponent = <TUID extends UID.Component>(\n componentUID: TUID,\n value: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n if (value === null) {\n return null;\n }\n\n // update\n if ('id' in value && typeof value.id !== 'undefined') {\n // TODO: verify the compo is associated with the entity\n return updateComponent(componentUID, { id: value.id }, value);\n }\n\n // create\n return createComponent(componentUID, value);\n};\n\nconst deleteComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToDelete: Data.Component<TUID>\n) => {\n await deleteComponents(uid, componentToDelete);\n await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });\n};\n\nexport {\n omitComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n};\n"],"names":["contentTypesUtils","omit","has","async","_","errors","pipe","assign"],"mappings":";;;;;;;AAqBA,MAAM,iBAAiB,MAAM,OAAO,IAAI,QAAQ,WAAW;AAU3D,SAAS,kBACP,aACA,MAOA;AACM,QAAA,EAAE,WAAe,IAAA;AACvB,QAAM,sBAAsB,OAAO,KAAK,UAAU,EAAE;AAAA,IAAO,CAAC,kBAC1DA,YAAA,aAAkB,qBAAqB,WAAW,aAAa,CAAC;AAAA,EAAA;AAG3D,SAAAC,EAAA,KAAK,qBAAqB,IAAI;AACvC;AAGM,MAAA,mBAAmB,OAIvB,KACA,SACG;AACH,QAAM,EAAE,aAAa,OAAO,OAAO,SAAS,GAAG;AAE/C,QAAM,gBAA+B,CAAA;AAE/B,QAAA,iBAAiB,OAAO,KAAK,UAAU;AAE7C,aAAW,iBAAiB,gBAAgB;AACpC,UAAA,YAAY,WAAW,aAAa;AAEtC,QAAA,CAACC,EAAAA,IAAI,eAAe,IAAI,KAAK,CAACF,yBAAkB,qBAAqB,SAAS,GAAG;AACnF;AAAA,IACF;AAEI,QAAA,UAAU,SAAS,aAAa;AAClC,YAAM,EAAE,WAAW,cAAc,aAAa,UAAU;AAElD,YAAA,iBAAiB,KAAK,aAA4B;AAExD,UAAI,mBAAmB,MAAM;AAC3B;AAAA,MACF;AAEA,UAAI,eAAe,MA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IAAI,kBAAkB,GAAA,EAAM,CAAA;AAC3E;;;;;;;"}
1
+ {"version":3,"file":"components.js","sources":["../../../src/services/entity-service/components.ts"],"sourcesContent":["import _ from 'lodash';\nimport { has, omit, pipe, assign } from 'lodash/fp';\nimport type { Struct, Utils, UID, Schema, Data, Modules } from '@strapi/types';\nimport { contentTypes as contentTypesUtils, async, errors } from '@strapi/utils';\n\ntype LoadedComponents<TUID extends UID.Schema> = Data.Entity<\n TUID,\n Schema.AttributeNamesByType<TUID, 'component' | 'dynamiczone'>\n>;\n\ntype SingleComponentValue = Schema.Attribute.ComponentValue<UID.Component, false>;\ntype RepeatableComponentValue = Schema.Attribute.ComponentValue<UID.Component, true>;\n\ntype ComponentValue = SingleComponentValue | RepeatableComponentValue;\n\ntype DynamicZoneValue = Schema.Attribute.DynamicZoneValue<UID.Component[]>;\n\ntype ComponentBody = {\n [key: string]: ComponentValue | DynamicZoneValue;\n};\n\nconst isDialectMySQL = () => strapi.db?.dialect.client === 'mysql';\n\nfunction omitComponentData(\n contentType: Struct.ContentTypeSchema,\n data: Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>>;\nfunction omitComponentData(\n contentType: Struct.ComponentSchema,\n data: Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>>;\nfunction omitComponentData(\n contentType: Struct.Schema,\n data: Modules.EntityService.Params.Data.Input<\n Struct.ContentTypeSchema['uid'] | Struct.ComponentSchema['uid']\n >\n): Partial<\n Modules.EntityService.Params.Data.Input<\n Struct.ContentTypeSchema['uid'] | Struct.ComponentSchema['uid']\n >\n> {\n const { attributes } = contentType;\n const componentAttributes = Object.keys(attributes).filter((attributeName) =>\n contentTypesUtils.isComponentAttribute(attributes[attributeName])\n );\n\n return omit(componentAttributes, data);\n}\n\n// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer\nconst createComponents = async <\n TUID extends UID.Schema,\n TData extends Modules.EntityService.Params.Data.Input<TUID>,\n>(\n uid: TUID,\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data) || !contentTypesUtils.isComponentAttribute(attribute)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData];\n\n if (componentValue === null) {\n continue;\n }\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => createComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as RepeatableComponentValue;\n\n componentBody[attributeName] = components.map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await createComponent(\n componentUID,\n componentValue as Modules.EntityService.Params.Data.Input<UID.Component>\n );\n componentBody[attributeName] = {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[\n attributeName as keyof TData\n ] as Modules.EntityService.Params.Attribute.GetValue<Schema.Attribute.DynamicZone>;\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n const createDynamicZoneComponents = async (\n value: Utils.Array.Values<typeof dynamiczoneValues>\n ) => {\n const { id } = await createComponent(value.__component, value);\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n };\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n createDynamicZoneComponents,\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst getComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entity: { id: Modules.EntityService.Params.Attribute.ID }\n): Promise<LoadedComponents<TUID>> => {\n const componentAttributes = contentTypesUtils.getComponentAttributes(strapi.getModel(uid));\n\n if (_.isEmpty(componentAttributes)) {\n return {} as LoadedComponents<TUID>;\n }\n\n return strapi.db.query(uid).load(entity, componentAttributes) as Promise<LoadedComponents<TUID>>;\n};\n\n/*\n delete old components\n create or update\n*/\nconst updateComponents = async <\n TUID extends UID.Schema,\n TData extends Partial<Modules.EntityService.Params.Data.Input<TUID>>,\n>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n for (const attributeName of Object.keys(attributes)) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData] as ComponentValue;\n\n await deleteOldComponents(uid, componentUID, entityToUpdate, attributeName, componentValue);\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => updateOrCreateComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as RepeatableComponentValue;\n\n componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await updateOrCreateComponent(componentUID, componentValue);\n componentBody[attributeName] = component && {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n } else if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[attributeName as keyof TData] as DynamicZoneValue;\n\n await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n async (value: any) => {\n const { id } = await updateOrCreateComponent(value.__component, value);\n\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n },\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n }\n }\n\n return componentBody;\n};\n\nconst pickStringifiedId = ({\n id,\n}: {\n id: Modules.EntityService.Params.Attribute.ID;\n}): Modules.EntityService.Params.Attribute.ID & string => {\n if (typeof id === 'string') {\n return id;\n }\n\n return `${id}`;\n};\n\nconst deleteOldComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n componentUID: UID.Component,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n componentValue: ComponentValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as ComponentValue;\n\n const idsToKeep = _.castArray(componentValue).filter(has('id')).map(pickStringifiedId);\n const allIds = _.castArray(previousValue).filter(has('id')).map(pickStringifiedId);\n\n idsToKeep.forEach((id) => {\n if (!allIds.includes(id)) {\n throw new errors.ApplicationError(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n }\n });\n\n const idsToDelete = _.difference(allIds, idsToKeep);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n await deleteComponent(componentUID, { id: idToDelete });\n }\n }\n};\n\nconst deleteOldDZComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n dynamiczoneValues: DynamicZoneValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as DynamicZoneValue;\n\n const idsToKeep = _.castArray(dynamiczoneValues)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n const allIds = _.castArray(previousValue)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n idsToKeep.forEach(({ id, __component }) => {\n if (!allIds.find((el) => el.id === id && el.__component === __component)) {\n const err = new Error(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n\n Object.assign(err, { status: 400 });\n throw err;\n }\n });\n\n type IdsToDelete = DynamicZoneValue;\n\n const idsToDelete = allIds.reduce((acc, { id, __component }) => {\n if (!idsToKeep.find((el) => el.id === id && el.__component === __component)) {\n acc.push({ id, __component });\n }\n\n return acc;\n }, [] as IdsToDelete);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n const { id, __component } = idToDelete;\n await deleteComponent(__component, { id });\n }\n }\n};\n\nconst deleteComponents = async <TUID extends UID.Schema, TEntity extends Data.Entity<TUID>>(\n uid: TUID,\n entityToDelete: TEntity,\n { loadComponents = true } = {}\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (attribute.type === 'component' || attribute.type === 'dynamiczone') {\n let value;\n if (loadComponents) {\n value = await strapi.db.query(uid).load(entityToDelete, attributeName);\n } else {\n value = entityToDelete[attributeName as keyof TEntity];\n }\n\n if (!value) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID } = attribute;\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(componentUID, subValue),\n {\n concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity,\n }\n );\n } else {\n // delete dynamic zone components\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(subValue.__component, subValue),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n }\n\n continue;\n }\n }\n};\n\n/** *************************\n Component queries\n************************** */\n\n// components can have nested compos so this must be recursive\nconst createComponent = async <TUID extends UID.Component>(\n uid: TUID,\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid);\n\n const componentData = await createComponents(uid, data);\n const transform = pipe(\n // Make sure we don't save the component with a pre-defined ID\n omit('id'),\n // Remove the component data from the original data object ...\n (payload) => omitComponentData(model, payload),\n // ... and assign the newly created component instead\n assign(componentData)\n );\n\n return strapi.db.query(uid).create({ data: transform(data) });\n};\n\n// components can have nested compos so this must be recursive\nconst updateComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid);\n\n const componentData = await updateComponents(uid, componentToUpdate, data);\n\n return strapi.db.query(uid).update({\n where: {\n id: componentToUpdate.id,\n },\n data: Object.assign(omitComponentData(model, data), componentData),\n });\n};\n\nconst updateOrCreateComponent = <TUID extends UID.Component>(\n componentUID: TUID,\n value: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n if (value === null) {\n return null;\n }\n\n // update\n if ('id' in value && typeof value.id !== 'undefined') {\n // TODO: verify the compo is associated with the entity\n return updateComponent(componentUID, { id: value.id }, value);\n }\n\n // create\n return createComponent(componentUID, value);\n};\n\nconst deleteComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToDelete: Data.Component<TUID>\n) => {\n await deleteComponents(uid, componentToDelete);\n await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });\n};\n\nexport {\n omitComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n};\n"],"names":["contentTypesUtils","omit","has","async","_","errors","pipe","assign"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AAE,OAAO,EAAE,MAAM,UAAU,IAAI,EAAG,CAAA;AAC9D;AAGA,MAAM,kBAAkB,OACtB,KACA,mBACA,SACG;AACG,QAAA,QAAQ,OAAO,SAAS,GAAG;AAEjC,QAAM,gBAAgB,MAAM,iBAAiB,KAAK,mBAAmB,IAAI;AAEzE,SAAO,OAAO,GAAG,MAAM,GAAG,EAAE,OAAO;AAAA,IACjC,OAAO;AAAA,MACL,IAAI,kBAAkB;AAAA,IACxB;AAAA,IACA,MAAM,OAAO,OAAO,kBAAkB,OAAO,IAAI,GAAG,aAAa;AAAA,EAAA,CAClE;AACH;AAEA,MAAM,0BAA0B,CAC9B,cACA,UACG;AACH,MAAI,UAAU,MAAM;AACX,WAAA;AAAA,EACT;AAGA,MAAI,QAAQ,SAAS,OAAO,MAAM,OAAO,aAAa;AAEpD,WAAO,gBAAgB,cAAc,EAAE,IAAI,MAAM,GAAA,GAAM,KAAK;AAAA,EAC9D;AAGO,SAAA,gBAAgB,cAAc,KAAK;AAC5C;AAEM,MAAA,kBAAkB,OACtB,KACA,sBACG;AACG,QAAA,iBAAiB,KAAK,iBAAiB;AAC7C,QAAM,OAAO,GAAG,MAAM,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,kBAAkB,GAAA,EAAM,CAAA;AAC3E;;;;;;;"}
@@ -129,9 +129,7 @@ const updateComponents = async (uid, entityToUpdate, data) => {
129
129
  }
130
130
  };
131
131
  }
132
- continue;
133
- }
134
- if (attribute.type === "dynamiczone") {
132
+ } else if (attribute.type === "dynamiczone") {
135
133
  const dynamiczoneValues = data[attributeName];
136
134
  await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);
137
135
  if (!Array.isArray(dynamiczoneValues)) {
@@ -151,7 +149,6 @@ const updateComponents = async (uid, entityToUpdate, data) => {
151
149
  },
152
150
  { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }
153
151
  );
154
- continue;
155
152
  }
156
153
  }
157
154
  return componentBody;
@@ -1 +1 @@
1
- {"version":3,"file":"components.mjs","sources":["../../../src/services/entity-service/components.ts"],"sourcesContent":["import _ from 'lodash';\nimport { has, omit, pipe, assign } from 'lodash/fp';\nimport type { Struct, Utils, UID, Schema, Data, Modules } from '@strapi/types';\nimport { contentTypes as contentTypesUtils, async, errors } from '@strapi/utils';\n\ntype LoadedComponents<TUID extends UID.Schema> = Data.Entity<\n TUID,\n Schema.AttributeNamesByType<TUID, 'component' | 'dynamiczone'>\n>;\n\ntype SingleComponentValue = Schema.Attribute.ComponentValue<UID.Component, false>;\ntype RepeatableComponentValue = Schema.Attribute.ComponentValue<UID.Component, true>;\n\ntype ComponentValue = SingleComponentValue | RepeatableComponentValue;\n\ntype DynamicZoneValue = Schema.Attribute.DynamicZoneValue<UID.Component[]>;\n\ntype ComponentBody = {\n [key: string]: ComponentValue | DynamicZoneValue;\n};\n\nconst isDialectMySQL = () => strapi.db?.dialect.client === 'mysql';\n\nfunction omitComponentData(\n contentType: Struct.ContentTypeSchema,\n data: Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>>;\nfunction omitComponentData(\n contentType: Struct.ComponentSchema,\n data: Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>>;\nfunction omitComponentData(\n contentType: Struct.Schema,\n data: Modules.EntityService.Params.Data.Input<\n Struct.ContentTypeSchema['uid'] | Struct.ComponentSchema['uid']\n >\n): Partial<\n Modules.EntityService.Params.Data.Input<\n Struct.ContentTypeSchema['uid'] | Struct.ComponentSchema['uid']\n >\n> {\n const { attributes } = contentType;\n const componentAttributes = Object.keys(attributes).filter((attributeName) =>\n contentTypesUtils.isComponentAttribute(attributes[attributeName])\n );\n\n return omit(componentAttributes, data);\n}\n\n// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer\nconst createComponents = async <\n TUID extends UID.Schema,\n TData extends Modules.EntityService.Params.Data.Input<TUID>\n>(\n uid: TUID,\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data) || !contentTypesUtils.isComponentAttribute(attribute)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData];\n\n if (componentValue === null) {\n continue;\n }\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => createComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as RepeatableComponentValue;\n\n componentBody[attributeName] = components.map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await createComponent(\n componentUID,\n componentValue as Modules.EntityService.Params.Data.Input<UID.Component>\n );\n componentBody[attributeName] = {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[\n attributeName as keyof TData\n ] as Modules.EntityService.Params.Attribute.GetValue<Schema.Attribute.DynamicZone>;\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n const createDynamicZoneComponents = async (\n value: Utils.Array.Values<typeof dynamiczoneValues>\n ) => {\n const { id } = await createComponent(value.__component, value);\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n };\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n createDynamicZoneComponents,\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst getComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entity: { id: Modules.EntityService.Params.Attribute.ID }\n): Promise<LoadedComponents<TUID>> => {\n const componentAttributes = contentTypesUtils.getComponentAttributes(strapi.getModel(uid));\n\n if (_.isEmpty(componentAttributes)) {\n return {} as LoadedComponents<TUID>;\n }\n\n return strapi.db.query(uid).load(entity, componentAttributes) as Promise<LoadedComponents<TUID>>;\n};\n\n/*\n delete old components\n create or update\n*/\nconst updateComponents = async <\n TUID extends UID.Schema,\n TData extends Partial<Modules.EntityService.Params.Data.Input<TUID>>\n>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n for (const attributeName of Object.keys(attributes)) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData] as ComponentValue;\n\n await deleteOldComponents(uid, componentUID, entityToUpdate, attributeName, componentValue);\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => updateOrCreateComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as RepeatableComponentValue;\n\n componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await updateOrCreateComponent(componentUID, componentValue);\n componentBody[attributeName] = component && {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[attributeName as keyof TData] as DynamicZoneValue;\n\n await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n async (value: any) => {\n const { id } = await updateOrCreateComponent(value.__component, value);\n\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n },\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst pickStringifiedId = ({\n id,\n}: {\n id: Modules.EntityService.Params.Attribute.ID;\n}): Modules.EntityService.Params.Attribute.ID & string => {\n if (typeof id === 'string') {\n return id;\n }\n\n return `${id}`;\n};\n\nconst deleteOldComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n componentUID: UID.Component,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n componentValue: ComponentValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as ComponentValue;\n\n const idsToKeep = _.castArray(componentValue).filter(has('id')).map(pickStringifiedId);\n const allIds = _.castArray(previousValue).filter(has('id')).map(pickStringifiedId);\n\n idsToKeep.forEach((id) => {\n if (!allIds.includes(id)) {\n throw new errors.ApplicationError(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n }\n });\n\n const idsToDelete = _.difference(allIds, idsToKeep);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n await deleteComponent(componentUID, { id: idToDelete });\n }\n }\n};\n\nconst deleteOldDZComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n dynamiczoneValues: DynamicZoneValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as DynamicZoneValue;\n\n const idsToKeep = _.castArray(dynamiczoneValues)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n const allIds = _.castArray(previousValue)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n idsToKeep.forEach(({ id, __component }) => {\n if (!allIds.find((el) => el.id === id && el.__component === __component)) {\n const err = new Error(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n\n Object.assign(err, { status: 400 });\n throw err;\n }\n });\n\n type IdsToDelete = DynamicZoneValue;\n\n const idsToDelete = allIds.reduce((acc, { id, __component }) => {\n if (!idsToKeep.find((el) => el.id === id && el.__component === __component)) {\n acc.push({ id, __component });\n }\n\n return acc;\n }, [] as IdsToDelete);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n const { id, __component } = idToDelete;\n await deleteComponent(__component, { id });\n }\n }\n};\n\nconst deleteComponents = async <TUID extends UID.Schema, TEntity extends Data.Entity<TUID>>(\n uid: TUID,\n entityToDelete: TEntity,\n { loadComponents = true } = {}\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (attribute.type === 'component' || attribute.type === 'dynamiczone') {\n let value;\n if (loadComponents) {\n value = await strapi.db.query(uid).load(entityToDelete, attributeName);\n } else {\n value = entityToDelete[attributeName as keyof TEntity];\n }\n\n if (!value) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID } = attribute;\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(componentUID, subValue),\n {\n concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity,\n }\n );\n } else {\n // delete dynamic zone components\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(subValue.__component, subValue),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n }\n\n continue;\n }\n }\n};\n\n/** *************************\n Component queries\n************************** */\n\n// components can have nested compos so this must be recursive\nconst createComponent = async <TUID extends UID.Component>(\n uid: TUID,\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid);\n\n const componentData = await createComponents(uid, data);\n const transform = pipe(\n // Make sure we don't save the component with a pre-defined ID\n omit('id'),\n // Remove the component data from the original data object ...\n (payload) => omitComponentData(model, payload),\n // ... and assign the newly created component instead\n assign(componentData)\n );\n\n return strapi.db.query(uid).create({ data: transform(data) });\n};\n\n// components can have nested compos so this must be recursive\nconst updateComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid);\n\n const componentData = await updateComponents(uid, componentToUpdate, data);\n\n return strapi.db.query(uid).update({\n where: {\n id: componentToUpdate.id,\n },\n data: Object.assign(omitComponentData(model, data), componentData),\n });\n};\n\nconst updateOrCreateComponent = <TUID extends UID.Component>(\n componentUID: TUID,\n value: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n if (value === null) {\n return null;\n }\n\n // update\n if ('id' in value && typeof value.id !== 'undefined') {\n // TODO: verify the compo is associated with the entity\n return updateComponent(componentUID, { id: value.id }, value);\n }\n\n // create\n return createComponent(componentUID, value);\n};\n\nconst deleteComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToDelete: Data.Component<TUID>\n) => {\n await deleteComponents(uid, componentToDelete);\n await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });\n};\n\nexport {\n omitComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n};\n"],"names":["contentTypesUtils"],"mappings":";;;AAqBA,MAAM,iBAAiB,MAA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AA,IACjC,OAAO;AAAA,MACL,IAAI,kBAAkB;AAAA,IACxB;AAAA,IACA,MAAM,OAAO,OAAO,kBAAkB,OAAO,IAAI,GAAG,aAAa;AAAA,EAAA,CAClE;AACH;AAEA,MAAM,0BAA0B,CAC9B,cACA,UACG;AACH,MAAI,UAAU,MAAM;AACX,WAAA;AAAA,EACT;AAGA,MAAI,QAAQ,SAAS,OAAO,MAAM,OAAO,aAAa;AAEpD,WAAO,gBAAgB,cAAc,EAAE,IAAI,MAAM,GAAA,GAAM,KAAK;AAAA,EAC9D;AAGO,SAAA,gBAAgB,cAAc,KAAK;AAC5C;AAEM,MAAA,kBAAkB,OACtB,KACA,sBACG;AACG,QAAA,iBAAiB,KAAK,iBAAiB;AAC7C,QAAM,OAAO,GAAG,MAAM,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,kBAAkB,GAAA,EAAM,CAAA;AAC3E;"}
1
+ {"version":3,"file":"components.mjs","sources":["../../../src/services/entity-service/components.ts"],"sourcesContent":["import _ from 'lodash';\nimport { has, omit, pipe, assign } from 'lodash/fp';\nimport type { Struct, Utils, UID, Schema, Data, Modules } from '@strapi/types';\nimport { contentTypes as contentTypesUtils, async, errors } from '@strapi/utils';\n\ntype LoadedComponents<TUID extends UID.Schema> = Data.Entity<\n TUID,\n Schema.AttributeNamesByType<TUID, 'component' | 'dynamiczone'>\n>;\n\ntype SingleComponentValue = Schema.Attribute.ComponentValue<UID.Component, false>;\ntype RepeatableComponentValue = Schema.Attribute.ComponentValue<UID.Component, true>;\n\ntype ComponentValue = SingleComponentValue | RepeatableComponentValue;\n\ntype DynamicZoneValue = Schema.Attribute.DynamicZoneValue<UID.Component[]>;\n\ntype ComponentBody = {\n [key: string]: ComponentValue | DynamicZoneValue;\n};\n\nconst isDialectMySQL = () => strapi.db?.dialect.client === 'mysql';\n\nfunction omitComponentData(\n contentType: Struct.ContentTypeSchema,\n data: Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>>;\nfunction omitComponentData(\n contentType: Struct.ComponentSchema,\n data: Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>>;\nfunction omitComponentData(\n contentType: Struct.Schema,\n data: Modules.EntityService.Params.Data.Input<\n Struct.ContentTypeSchema['uid'] | Struct.ComponentSchema['uid']\n >\n): Partial<\n Modules.EntityService.Params.Data.Input<\n Struct.ContentTypeSchema['uid'] | Struct.ComponentSchema['uid']\n >\n> {\n const { attributes } = contentType;\n const componentAttributes = Object.keys(attributes).filter((attributeName) =>\n contentTypesUtils.isComponentAttribute(attributes[attributeName])\n );\n\n return omit(componentAttributes, data);\n}\n\n// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer\nconst createComponents = async <\n TUID extends UID.Schema,\n TData extends Modules.EntityService.Params.Data.Input<TUID>,\n>(\n uid: TUID,\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data) || !contentTypesUtils.isComponentAttribute(attribute)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData];\n\n if (componentValue === null) {\n continue;\n }\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => createComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as RepeatableComponentValue;\n\n componentBody[attributeName] = components.map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await createComponent(\n componentUID,\n componentValue as Modules.EntityService.Params.Data.Input<UID.Component>\n );\n componentBody[attributeName] = {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[\n attributeName as keyof TData\n ] as Modules.EntityService.Params.Attribute.GetValue<Schema.Attribute.DynamicZone>;\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n const createDynamicZoneComponents = async (\n value: Utils.Array.Values<typeof dynamiczoneValues>\n ) => {\n const { id } = await createComponent(value.__component, value);\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n };\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n createDynamicZoneComponents,\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst getComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entity: { id: Modules.EntityService.Params.Attribute.ID }\n): Promise<LoadedComponents<TUID>> => {\n const componentAttributes = contentTypesUtils.getComponentAttributes(strapi.getModel(uid));\n\n if (_.isEmpty(componentAttributes)) {\n return {} as LoadedComponents<TUID>;\n }\n\n return strapi.db.query(uid).load(entity, componentAttributes) as Promise<LoadedComponents<TUID>>;\n};\n\n/*\n delete old components\n create or update\n*/\nconst updateComponents = async <\n TUID extends UID.Schema,\n TData extends Partial<Modules.EntityService.Params.Data.Input<TUID>>,\n>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n for (const attributeName of Object.keys(attributes)) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData] as ComponentValue;\n\n await deleteOldComponents(uid, componentUID, entityToUpdate, attributeName, componentValue);\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => updateOrCreateComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as RepeatableComponentValue;\n\n componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await updateOrCreateComponent(componentUID, componentValue);\n componentBody[attributeName] = component && {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n } else if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[attributeName as keyof TData] as DynamicZoneValue;\n\n await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n async (value: any) => {\n const { id } = await updateOrCreateComponent(value.__component, value);\n\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n },\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n }\n }\n\n return componentBody;\n};\n\nconst pickStringifiedId = ({\n id,\n}: {\n id: Modules.EntityService.Params.Attribute.ID;\n}): Modules.EntityService.Params.Attribute.ID & string => {\n if (typeof id === 'string') {\n return id;\n }\n\n return `${id}`;\n};\n\nconst deleteOldComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n componentUID: UID.Component,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n componentValue: ComponentValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as ComponentValue;\n\n const idsToKeep = _.castArray(componentValue).filter(has('id')).map(pickStringifiedId);\n const allIds = _.castArray(previousValue).filter(has('id')).map(pickStringifiedId);\n\n idsToKeep.forEach((id) => {\n if (!allIds.includes(id)) {\n throw new errors.ApplicationError(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n }\n });\n\n const idsToDelete = _.difference(allIds, idsToKeep);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n await deleteComponent(componentUID, { id: idToDelete });\n }\n }\n};\n\nconst deleteOldDZComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n dynamiczoneValues: DynamicZoneValue\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as DynamicZoneValue;\n\n const idsToKeep = _.castArray(dynamiczoneValues)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n const allIds = _.castArray(previousValue)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n idsToKeep.forEach(({ id, __component }) => {\n if (!allIds.find((el) => el.id === id && el.__component === __component)) {\n const err = new Error(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n\n Object.assign(err, { status: 400 });\n throw err;\n }\n });\n\n type IdsToDelete = DynamicZoneValue;\n\n const idsToDelete = allIds.reduce((acc, { id, __component }) => {\n if (!idsToKeep.find((el) => el.id === id && el.__component === __component)) {\n acc.push({ id, __component });\n }\n\n return acc;\n }, [] as IdsToDelete);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n const { id, __component } = idToDelete;\n await deleteComponent(__component, { id });\n }\n }\n};\n\nconst deleteComponents = async <TUID extends UID.Schema, TEntity extends Data.Entity<TUID>>(\n uid: TUID,\n entityToDelete: TEntity,\n { loadComponents = true } = {}\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (attribute.type === 'component' || attribute.type === 'dynamiczone') {\n let value;\n if (loadComponents) {\n value = await strapi.db.query(uid).load(entityToDelete, attributeName);\n } else {\n value = entityToDelete[attributeName as keyof TEntity];\n }\n\n if (!value) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID } = attribute;\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(componentUID, subValue),\n {\n concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity,\n }\n );\n } else {\n // delete dynamic zone components\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(subValue.__component, subValue),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n }\n\n continue;\n }\n }\n};\n\n/** *************************\n Component queries\n************************** */\n\n// components can have nested compos so this must be recursive\nconst createComponent = async <TUID extends UID.Component>(\n uid: TUID,\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid);\n\n const componentData = await createComponents(uid, data);\n const transform = pipe(\n // Make sure we don't save the component with a pre-defined ID\n omit('id'),\n // Remove the component data from the original data object ...\n (payload) => omitComponentData(model, payload),\n // ... and assign the newly created component instead\n assign(componentData)\n );\n\n return strapi.db.query(uid).create({ data: transform(data) });\n};\n\n// components can have nested compos so this must be recursive\nconst updateComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid);\n\n const componentData = await updateComponents(uid, componentToUpdate, data);\n\n return strapi.db.query(uid).update({\n where: {\n id: componentToUpdate.id,\n },\n data: Object.assign(omitComponentData(model, data), componentData),\n });\n};\n\nconst updateOrCreateComponent = <TUID extends UID.Component>(\n componentUID: TUID,\n value: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n if (value === null) {\n return null;\n }\n\n // update\n if ('id' in value && typeof value.id !== 'undefined') {\n // TODO: verify the compo is associated with the entity\n return updateComponent(componentUID, { id: value.id }, value);\n }\n\n // create\n return createComponent(componentUID, value);\n};\n\nconst deleteComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToDelete: Data.Component<TUID>\n) => {\n await deleteComponents(uid, componentToDelete);\n await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });\n};\n\nexport {\n omitComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n};\n"],"names":["contentTypesUtils"],"mappings":";;;AAqBA,MAAM,iBAAiB,MAAM,OAAO,IAAI,QAAQ,WAAW;AAU3D,SAAS,kBACP,aACA,MAOA;AACM,QAAA,EAAE,WAAe,IAAA;AACvB,QAAM,sBAAsB,OAAO,KAAK,UAAU,EAAE;AAAA,IAAO,CAAC,kBAC1DA,aAAkB,qBAAqB,WAAW,aAAa,CAAC;AAAA,EAAA;AAG3D,SAAA,KAAK,qBAAqB,IAAI;AACvC;AAGM,MAAA,mBAAmB,OAIvB,KACA,SACG;AACH,QAAM,EAAE,aAAa,OAAO,OAAO,SAAS,GAAG;AAE/C,QAAM,gBAA+B,CAAA;AAE/B,QAAA,iBAAiB,OAAO,KAAK,UAAU;AAE7C,aAAW,iBAAiB,gBAAgB;AACpC,UAAA,YAAY,WAAW,aAAa;AAEtC,QAAA,CAAC,IAAI,eAAe,IAAI,KAAK,CAACA,aAAkB,qBAAqB,SAAS,GAAG;AACnF;AAAA,IACF;AAEI,QAAA,UAAU,SAAS,aAAa;AAClC,YAAM,EAAE,WAAW,cAAc,aAAa,UAAU;AAElD,YAAA,iBAAiB,KAAK,aAA4B;AAExD,UAAI,mBAAmB,MAAM;AAC3B;AAAA,MACF;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;AA6e7D,QAAA,MAAM,eAAe,EAAE,OAAO,CAAC,eAAe,CAAC,eAG9C,CAAC;AAEF,eAAe,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/entity-validator/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAE,OAAO,EAAuB,MAAM,eAAe,CAAC;AAmf7D,QAAA,MAAM,eAAe,EAAE,OAAO,CAAC,eAAe,CAAC,eAG9C,CAAC;AAEF,eAAe,eAAe,CAAC"}