@strapi/core 5.0.0-beta.3 → 5.0.0-beta.5

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 (78) hide show
  1. package/dist/core-api/controller/collection-type.d.ts.map +1 -1
  2. package/dist/core-api/controller/collection-type.js +1 -0
  3. package/dist/core-api/controller/collection-type.js.map +1 -1
  4. package/dist/core-api/controller/collection-type.mjs +1 -0
  5. package/dist/core-api/controller/collection-type.mjs.map +1 -1
  6. package/dist/core-api/routes/index.d.ts.map +1 -1
  7. package/dist/ee/license.d.ts.map +1 -1
  8. package/dist/ee/license.js +2 -1
  9. package/dist/ee/license.js.map +1 -1
  10. package/dist/ee/license.mjs +2 -1
  11. package/dist/ee/license.mjs.map +1 -1
  12. package/dist/factories.d.ts +2 -2
  13. package/dist/factories.d.ts.map +1 -1
  14. package/dist/migrations/database/5.0.0-discard-drafts.d.ts +12 -0
  15. package/dist/migrations/database/5.0.0-discard-drafts.d.ts.map +1 -0
  16. package/dist/migrations/database/5.0.0-discard-drafts.js +49 -0
  17. package/dist/migrations/database/5.0.0-discard-drafts.js.map +1 -0
  18. package/dist/migrations/database/5.0.0-discard-drafts.mjs +49 -0
  19. package/dist/migrations/database/5.0.0-discard-drafts.mjs.map +1 -0
  20. package/dist/providers/registries.d.ts.map +1 -1
  21. package/dist/providers/registries.js +2 -0
  22. package/dist/providers/registries.js.map +1 -1
  23. package/dist/providers/registries.mjs +2 -0
  24. package/dist/providers/registries.mjs.map +1 -1
  25. package/dist/services/core-store.d.ts.map +1 -1
  26. package/dist/services/document-service/attributes/index.d.ts +4 -4
  27. package/dist/services/document-service/attributes/index.d.ts.map +1 -1
  28. package/dist/services/document-service/attributes/index.js +5 -5
  29. package/dist/services/document-service/attributes/index.js.map +1 -1
  30. package/dist/services/document-service/attributes/index.mjs +5 -5
  31. package/dist/services/document-service/attributes/index.mjs.map +1 -1
  32. package/dist/services/document-service/common.d.ts.map +1 -1
  33. package/dist/services/document-service/components.d.ts +8 -80
  34. package/dist/services/document-service/components.d.ts.map +1 -1
  35. package/dist/services/document-service/components.js +18 -19
  36. package/dist/services/document-service/components.js.map +1 -1
  37. package/dist/services/document-service/components.mjs +19 -20
  38. package/dist/services/document-service/components.mjs.map +1 -1
  39. package/dist/services/document-service/entries.d.ts +2 -0
  40. package/dist/services/document-service/entries.d.ts.map +1 -1
  41. package/dist/services/document-service/entries.js +47 -14
  42. package/dist/services/document-service/entries.js.map +1 -1
  43. package/dist/services/document-service/entries.mjs +42 -9
  44. package/dist/services/document-service/entries.mjs.map +1 -1
  45. package/dist/services/document-service/index.d.ts.map +1 -1
  46. package/dist/services/document-service/index.js +8 -10
  47. package/dist/services/document-service/index.js.map +1 -1
  48. package/dist/services/document-service/index.mjs +8 -10
  49. package/dist/services/document-service/index.mjs.map +1 -1
  50. package/dist/services/document-service/internationalization.d.ts.map +1 -1
  51. package/dist/services/document-service/internationalization.js.map +1 -1
  52. package/dist/services/document-service/internationalization.mjs.map +1 -1
  53. package/dist/services/document-service/middlewares/middleware-manager.d.ts +1 -1
  54. package/dist/services/document-service/middlewares/middleware-manager.d.ts.map +1 -1
  55. package/dist/services/document-service/middlewares/middleware-manager.js +1 -1
  56. package/dist/services/document-service/middlewares/middleware-manager.js.map +1 -1
  57. package/dist/services/document-service/middlewares/middleware-manager.mjs +1 -1
  58. package/dist/services/document-service/middlewares/middleware-manager.mjs.map +1 -1
  59. package/dist/services/document-service/params.d.ts +1 -5
  60. package/dist/services/document-service/params.d.ts.map +1 -1
  61. package/dist/services/document-service/repository.d.ts.map +1 -1
  62. package/dist/services/document-service/repository.js +13 -39
  63. package/dist/services/document-service/repository.js.map +1 -1
  64. package/dist/services/document-service/repository.mjs +6 -32
  65. package/dist/services/document-service/repository.mjs.map +1 -1
  66. package/dist/services/document-service/transform/id-map.d.ts.map +1 -1
  67. package/dist/services/entity-service/index.d.ts.map +1 -1
  68. package/dist/services/entity-validator/index.js.map +1 -1
  69. package/dist/services/entity-validator/index.mjs.map +1 -1
  70. package/dist/services/metrics/middleware.d.ts.map +1 -1
  71. package/dist/services/server/compose-endpoint.d.ts.map +1 -1
  72. package/dist/services/server/koa.d.ts.map +1 -1
  73. package/dist/services/webhook-store.d.ts.map +1 -1
  74. package/dist/utils/load-files.d.ts.map +1 -1
  75. package/dist/utils/startup-logger.d.ts.map +1 -1
  76. package/package.json +15 -15
  77. package/dist/services/document-service/document-engine.d.ts +0 -2
  78. package/dist/services/document-service/document-engine.d.ts.map +0 -1
@@ -1,4 +1,6 @@
1
- import type { Struct, Utils, UID, Schema, Data, Modules } from '@strapi/types';
1
+ import _ from 'lodash';
2
+ import type { UID, Schema, Data, Modules } from '@strapi/types';
3
+ type Input<T extends UID.Schema> = Modules.Documents.Params.Data.Input<T>;
2
4
  type LoadedComponents<TUID extends UID.Schema> = Data.Entity<TUID, Schema.AttributeNamesByType<TUID, 'component' | 'dynamiczone'>>;
3
5
  type SingleComponentValue = Schema.Attribute.ComponentValue<UID.Component, false>;
4
6
  type RepeatableComponentValue = Schema.Attribute.ComponentValue<UID.Component, true>;
@@ -7,92 +9,18 @@ type DynamicZoneValue = Schema.Attribute.DynamicZoneValue<UID.Component[]>;
7
9
  type ComponentBody = {
8
10
  [key: string]: ComponentValue | DynamicZoneValue;
9
11
  };
10
- declare function omitComponentData(contentType: Struct.ContentTypeSchema, data: Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>): Partial<Modules.EntityService.Params.Data.Input<Struct.ContentTypeSchema['uid']>>;
11
- declare function omitComponentData(contentType: Struct.ComponentSchema, data: Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>): Partial<Modules.EntityService.Params.Data.Input<Struct.ComponentSchema['uid']>>;
12
- declare const createComponents: <TUID extends UID.Schema, TData extends Modules.EntityService.Params.Data.Input<TUID>>(uid: TUID, data: TData) => Promise<ComponentBody>;
12
+ declare const omitComponentData: _.CurriedFunction2<import("@strapi/types/dist/struct").ComponentSchema | import("@strapi/types/dist/struct").ContentTypeSchema, Input<UID.Schema>, Partial<Input<UID.Schema>>>;
13
+ declare const createComponents: <TUID extends UID.Schema, TData extends Input<TUID>>(uid: TUID, data: TData) => Promise<ComponentBody>;
13
14
  declare const getComponents: <TUID extends UID.Schema>(uid: TUID, entity: {
14
15
  id: Modules.EntityService.Params.Attribute.ID;
15
16
  }) => Promise<LoadedComponents<TUID>>;
16
- declare const updateComponents: <TUID extends UID.Schema, TData extends Partial<Modules.EntityService.Params.Data.Input<TUID>>>(uid: TUID, entityToUpdate: {
17
+ declare const updateComponents: <TUID extends UID.Schema, TData extends Partial<Input<TUID>>>(uid: TUID, entityToUpdate: {
17
18
  id: Modules.EntityService.Params.Attribute.ID;
18
19
  }, data: TData) => Promise<ComponentBody>;
19
20
  declare const deleteComponents: <TUID extends UID.Schema, TEntity extends Data.Entity<TUID, Extract<keyof Schema.Attributes<TUID>, string>>>(uid: TUID, entityToDelete: TEntity, { loadComponents }?: {
20
21
  loadComponents?: boolean | undefined;
21
22
  }) => Promise<void>;
22
- declare const deleteComponent: <TUID extends `${string}.${string}`>(uid: TUID, componentToDelete: {
23
- id: Data.ID;
24
- } & Pick<{ [TAttributeName in Extract<keyof Schema.Attributes<TUID>, string>]?: Utils.If<Utils.IsTrue<Utils.IsTrue<Utils.Extends<{
25
- biginteger: Schema.Attribute.GetBigIntegerValue<Schema.AttributeByName<TUID, TAttributeName>>;
26
- boolean: Schema.Attribute.GetBooleanValue<Schema.AttributeByName<TUID, TAttributeName>>;
27
- blocks: Schema.Attribute.GetBlocksValue<Schema.AttributeByName<TUID, TAttributeName>>;
28
- decimal: Schema.Attribute.GetDecimalValue<Schema.AttributeByName<TUID, TAttributeName>>;
29
- enumeration: Schema.Attribute.GetEnumerationValue<Schema.AttributeByName<TUID, TAttributeName>>;
30
- email: Schema.Attribute.GetEmailValue<Schema.AttributeByName<TUID, TAttributeName>>;
31
- float: Schema.Attribute.GetFloatValue<Schema.AttributeByName<TUID, TAttributeName>>;
32
- integer: Schema.Attribute.GetIntegerValue<Schema.AttributeByName<TUID, TAttributeName>>;
33
- json: Schema.Attribute.GetJsonValue<Schema.AttributeByName<TUID, TAttributeName>>;
34
- password: Schema.Attribute.GetPasswordValue<Schema.AttributeByName<TUID, TAttributeName>>;
35
- richtext: Schema.Attribute.GetRichTextValue<Schema.AttributeByName<TUID, TAttributeName>>;
36
- string: Schema.Attribute.GetStringValue<Schema.AttributeByName<TUID, TAttributeName>>;
37
- text: Schema.Attribute.GetTextValue<Schema.AttributeByName<TUID, TAttributeName>>;
38
- uid: Schema.Attribute.GetUIDValue<Schema.AttributeByName<TUID, TAttributeName>>;
39
- date: Schema.Attribute.GetDateValue<Schema.AttributeByName<TUID, TAttributeName>>;
40
- datetime: Schema.Attribute.GetDateTimeValue<Schema.AttributeByName<TUID, TAttributeName>>;
41
- time: Schema.Attribute.GetTimeValue<Schema.AttributeByName<TUID, TAttributeName>>;
42
- timestamp: Schema.Attribute.GetTimestampValue<Schema.AttributeByName<TUID, TAttributeName>>;
43
- component: Schema.Attribute.GetComponentValue<Schema.AttributeByName<TUID, TAttributeName>>;
44
- dynamiczone: Schema.Attribute.GetDynamicZoneValue<Schema.AttributeByName<TUID, TAttributeName>>;
45
- media: Schema.Attribute.GetMediaValue<Schema.AttributeByName<TUID, TAttributeName>>;
46
- relation: Schema.Attribute.GetRelationValue<Schema.AttributeByName<TUID, TAttributeName>>;
47
- }[Schema.Attribute.TypeOf<Schema.AttributeByName<TUID, TAttributeName>>], never>> | Utils.IsTrue<Utils.Extends<never, {
48
- biginteger: Schema.Attribute.GetBigIntegerValue<Schema.AttributeByName<TUID, TAttributeName>>;
49
- boolean: Schema.Attribute.GetBooleanValue<Schema.AttributeByName<TUID, TAttributeName>>;
50
- blocks: Schema.Attribute.GetBlocksValue<Schema.AttributeByName<TUID, TAttributeName>>;
51
- decimal: Schema.Attribute.GetDecimalValue<Schema.AttributeByName<TUID, TAttributeName>>;
52
- enumeration: Schema.Attribute.GetEnumerationValue<Schema.AttributeByName<TUID, TAttributeName>>;
53
- email: Schema.Attribute.GetEmailValue<Schema.AttributeByName<TUID, TAttributeName>>;
54
- float: Schema.Attribute.GetFloatValue<Schema.AttributeByName<TUID, TAttributeName>>;
55
- integer: Schema.Attribute.GetIntegerValue<Schema.AttributeByName<TUID, TAttributeName>>;
56
- json: Schema.Attribute.GetJsonValue<Schema.AttributeByName<TUID, TAttributeName>>;
57
- password: Schema.Attribute.GetPasswordValue<Schema.AttributeByName<TUID, TAttributeName>>;
58
- richtext: Schema.Attribute.GetRichTextValue<Schema.AttributeByName<TUID, TAttributeName>>;
59
- string: Schema.Attribute.GetStringValue<Schema.AttributeByName<TUID, TAttributeName>>;
60
- text: Schema.Attribute.GetTextValue<Schema.AttributeByName<TUID, TAttributeName>>;
61
- uid: Schema.Attribute.GetUIDValue<Schema.AttributeByName<TUID, TAttributeName>>;
62
- date: Schema.Attribute.GetDateValue<Schema.AttributeByName<TUID, TAttributeName>>;
63
- datetime: Schema.Attribute.GetDateTimeValue<Schema.AttributeByName<TUID, TAttributeName>>;
64
- time: Schema.Attribute.GetTimeValue<Schema.AttributeByName<TUID, TAttributeName>>;
65
- timestamp: Schema.Attribute.GetTimestampValue<Schema.AttributeByName<TUID, TAttributeName>>;
66
- component: Schema.Attribute.GetComponentValue<Schema.AttributeByName<TUID, TAttributeName>>;
67
- dynamiczone: Schema.Attribute.GetDynamicZoneValue<Schema.AttributeByName<TUID, TAttributeName>>;
68
- media: Schema.Attribute.GetMediaValue<Schema.AttributeByName<TUID, TAttributeName>>;
69
- relation: Schema.Attribute.GetRelationValue<Schema.AttributeByName<TUID, TAttributeName>>;
70
- }[Schema.Attribute.TypeOf<Schema.AttributeByName<TUID, TAttributeName>>]>>>, unknown, {
71
- biginteger: Schema.Attribute.GetBigIntegerValue<Schema.AttributeByName<TUID, TAttributeName>>;
72
- boolean: Schema.Attribute.GetBooleanValue<Schema.AttributeByName<TUID, TAttributeName>>;
73
- blocks: Schema.Attribute.GetBlocksValue<Schema.AttributeByName<TUID, TAttributeName>>;
74
- decimal: Schema.Attribute.GetDecimalValue<Schema.AttributeByName<TUID, TAttributeName>>;
75
- enumeration: Schema.Attribute.GetEnumerationValue<Schema.AttributeByName<TUID, TAttributeName>>;
76
- email: Schema.Attribute.GetEmailValue<Schema.AttributeByName<TUID, TAttributeName>>;
77
- float: Schema.Attribute.GetFloatValue<Schema.AttributeByName<TUID, TAttributeName>>;
78
- integer: Schema.Attribute.GetIntegerValue<Schema.AttributeByName<TUID, TAttributeName>>;
79
- json: Schema.Attribute.GetJsonValue<Schema.AttributeByName<TUID, TAttributeName>>;
80
- password: Schema.Attribute.GetPasswordValue<Schema.AttributeByName<TUID, TAttributeName>>;
81
- richtext: Schema.Attribute.GetRichTextValue<Schema.AttributeByName<TUID, TAttributeName>>;
82
- string: Schema.Attribute.GetStringValue<Schema.AttributeByName<TUID, TAttributeName>>;
83
- text: Schema.Attribute.GetTextValue<Schema.AttributeByName<TUID, TAttributeName>>;
84
- uid: Schema.Attribute.GetUIDValue<Schema.AttributeByName<TUID, TAttributeName>>;
85
- date: Schema.Attribute.GetDateValue<Schema.AttributeByName<TUID, TAttributeName>>;
86
- datetime: Schema.Attribute.GetDateTimeValue<Schema.AttributeByName<TUID, TAttributeName>>;
87
- time: Schema.Attribute.GetTimeValue<Schema.AttributeByName<TUID, TAttributeName>>;
88
- timestamp: Schema.Attribute.GetTimestampValue<Schema.AttributeByName<TUID, TAttributeName>>;
89
- component: Schema.Attribute.GetComponentValue<Schema.AttributeByName<TUID, TAttributeName>>;
90
- dynamiczone: Schema.Attribute.GetDynamicZoneValue<Schema.AttributeByName<TUID, TAttributeName>>;
91
- media: Schema.Attribute.GetMediaValue<Schema.AttributeByName<TUID, TAttributeName>>;
92
- relation: Schema.Attribute.GetRelationValue<Schema.AttributeByName<TUID, TAttributeName>>;
93
- }[Schema.Attribute.TypeOf<Schema.AttributeByName<TUID, TAttributeName>>]> | null | undefined; }, Extract<keyof Schema.Attributes<TUID>, string>>) => Promise<void>;
94
- declare const assignComponentData: <TUID extends UID.ContentType>(data: Modules.EntityService.Params.Data.Input<TUID>, componentData: ComponentBody, { contentType, }: {
95
- contentType: Schema.ContentType<TUID>;
96
- }) => Partial<Modules.EntityService.Params.Data.Input<UID.ContentType>> & ComponentBody;
23
+ declare const deleteComponent: <TUID extends `${string}.${string}`>(uid: TUID, componentToDelete: Data.Component<TUID>) => Promise<void>;
24
+ declare const assignComponentData: _.CurriedFunction3<import("@strapi/types/dist/struct").ComponentSchema | import("@strapi/types/dist/struct").ContentTypeSchema, ComponentBody, Input<UID.Schema>, ComponentBody & Partial<Input<UID.Schema>>>;
97
25
  export { omitComponentData, assignComponentData, getComponents, createComponents, updateComponents, deleteComponents, deleteComponent, };
98
26
  //# sourceMappingURL=components.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../../../src/services/document-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,EAAE,WAAW,GAAG,aAAa,CAAC,CAC/D,CAAC;AAEF,KAAK,oBAAoB,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,GAAG,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAClF,KAAK,wBAAwB,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AAErF,KAAK,cAAc,GAAG,oBAAoB,GAAG,wBAAwB,CAAC;AAEtE,KAAK,gBAAgB,GAAG,MAAM,CAAC,SAAS,CAAC,gBAAgB,CAAC,GAAG,CAAC,SAAS,EAAE,CA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,CAAC;AAEF,QAAA,MAAM,aAAa,iCACZ,IAAI,UACD;IAAE,EAAE,EAAE,QAAQ,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAA;CAAE,KACxD,QAAQ,iBAAiB,IAAI,CAAC,CAQhC,CAAC;AAMF,QAAA,MAAM,gBAAgB,qEACf,IAAI,kBACO;IAAE,EAAE,EAAE,QAAQ,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAA;CAAE,QAC3D,KAAK,2BA0EZ,CAAC;AAkGF,QAAA,MAAM,gBAAgB,oHACf,IAAI,kBACO,OAAO;;mBAqCxB,CAAC;AAyDF,QAAA,MAAM,eAAe,4CACd,IAAI,qBACU,KAAK,SAAS,CAAC,IAAI,CAAC,kBAIxC,CAAC;AAEF,QAAA,MAAM,mBAAmB,+MAIxB,CAAC;AAEF,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,GAChB,CAAC"}
@@ -5,13 +5,15 @@ const _ = require("lodash/fp");
5
5
  const strapiUtils = require("@strapi/utils");
6
6
  const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
7
7
  const ___default = /* @__PURE__ */ _interopDefault(_$1);
8
- function omitComponentData(contentType, data) {
9
- const { attributes } = contentType;
10
- const componentAttributes = Object.keys(attributes).filter(
11
- (attributeName) => strapiUtils.contentTypes.isComponentAttribute(attributes[attributeName])
12
- );
13
- return _.omit(componentAttributes, data);
14
- }
8
+ const omitComponentData = _.curry(
9
+ (schema, data) => {
10
+ const { attributes } = schema;
11
+ const componentAttributes = Object.keys(attributes).filter(
12
+ (attributeName) => strapiUtils.contentTypes.isComponentAttribute(attributes[attributeName])
13
+ );
14
+ return _.omit(componentAttributes, data);
15
+ }
16
+ );
15
17
  const createComponents = async (uid, data) => {
16
18
  const { attributes = {} } = strapi.getModel(uid);
17
19
  const componentBody = {};
@@ -239,26 +241,23 @@ const deleteComponents = async (uid, entityToDelete, { loadComponents = true } =
239
241
  }
240
242
  };
241
243
  const createComponent = async (uid, data) => {
242
- const model = strapi.getModel(uid);
244
+ const schema = strapi.getModel(uid);
243
245
  const componentData = await createComponents(uid, data);
244
246
  const transform = _.pipe(
245
247
  // Make sure we don't save the component with a pre-defined ID
246
248
  _.omit("id"),
247
- // Remove the component data from the original data object ...
248
- (payload) => omitComponentData(model, payload),
249
- // ... and assign the newly created component instead
250
- _.assign(componentData)
249
+ assignComponentData(schema, componentData)
251
250
  );
252
251
  return strapi.db.query(uid).create({ data: transform(data) });
253
252
  };
254
253
  const updateComponent = async (uid, componentToUpdate, data) => {
255
- const model = strapi.getModel(uid);
254
+ const schema = strapi.getModel(uid);
256
255
  const componentData = await updateComponents(uid, componentToUpdate, data);
257
256
  return strapi.db.query(uid).update({
258
257
  where: {
259
258
  id: componentToUpdate.id
260
259
  },
261
- data: Object.assign(omitComponentData(model, data), componentData)
260
+ data: assignComponentData(schema, componentData, data)
262
261
  });
263
262
  };
264
263
  const updateOrCreateComponent = (componentUID, value) => {
@@ -274,11 +273,11 @@ const deleteComponent = async (uid, componentToDelete) => {
274
273
  await deleteComponents(uid, componentToDelete);
275
274
  await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });
276
275
  };
277
- const assignComponentData = (data, componentData, {
278
- contentType
279
- }) => {
280
- return Object.assign(omitComponentData(contentType, data), componentData);
281
- };
276
+ const assignComponentData = _.curry(
277
+ (schema, componentData, data) => {
278
+ return _.pipe(omitComponentData(schema), _.assign(componentData))(data);
279
+ }
280
+ );
282
281
  exports.assignComponentData = assignComponentData;
283
282
  exports.createComponents = createComponents;
284
283
  exports.deleteComponent = deleteComponent;
@@ -1 +1 @@
1
- {"version":3,"file":"components.js","sources":["../../../src/services/document-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\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: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n createComponent(componentUID, value)\n );\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\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 );\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: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n updateOrCreateComponent(componentUID, value)\n );\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(dynamiczoneValues, 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 }\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(_.castArray(value), (subValue: any) =>\n deleteComponent(componentUID, subValue)\n );\n } else {\n // delete dynamic zone components\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(subValue.__component, subValue)\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\nconst assignComponentData = <TUID extends UID.ContentType>(\n data: Modules.EntityService.Params.Data.Input<TUID>,\n componentData: ComponentBody,\n {\n contentType,\n }: {\n contentType: Schema.ContentType<TUID>;\n }\n) => {\n return Object.assign(omitComponentData(contentType, data), componentData);\n};\n\nexport {\n omitComponentData,\n assignComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n};\n"],"names":["contentTypesUtils","omit","has","async","_","errors","pipe","assign"],"mappings":";;;;;;;AA6BA,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,g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/components.ts"],"sourcesContent":["import _ from 'lodash';\nimport { has, omit, pipe, assign, curry } from 'lodash/fp';\nimport type { Utils, UID, Schema, Data, Modules } from '@strapi/types';\nimport { contentTypes as contentTypesUtils, async, errors } from '@strapi/utils';\n\n// type aliases for readability\ntype Input<T extends UID.Schema> = Modules.Documents.Params.Data.Input<T>;\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 omitComponentData = curry(\n (schema: Schema.Schema, data: Input<UID.Schema>): Partial<Input<UID.Schema>> => {\n const { attributes } = schema;\n const componentAttributes = Object.keys(attributes).filter((attributeName) =>\n contentTypesUtils.isComponentAttribute(attributes[attributeName])\n );\n\n return omit(componentAttributes, data);\n }\n);\n\n// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer\nconst createComponents = async <TUID extends UID.Schema, TData extends Input<TUID>>(\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 const components: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n createComponent(componentUID, value)\n );\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 Input<UID.Component>\n );\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 );\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 <TUID extends UID.Schema, TData extends Partial<Input<TUID>>>(\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: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n updateOrCreateComponent(componentUID, value)\n );\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(dynamiczoneValues, 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 }\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\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 await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(componentUID, subValue)\n );\n } else {\n await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(subValue.__component, subValue)\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>(uid: TUID, data: Input<TUID>) => {\n const schema = strapi.getModel(uid);\n\n const componentData = await createComponents(uid, data);\n\n const transform = pipe(\n // Make sure we don't save the component with a pre-defined ID\n omit('id'),\n assignComponentData(schema, 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: Input<TUID>\n) => {\n const schema = 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: assignComponentData(schema, componentData, data),\n });\n};\n\nconst updateOrCreateComponent = <TUID extends UID.Component>(\n componentUID: TUID,\n value: 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\nconst assignComponentData = curry(\n (schema: Schema.Schema, componentData: ComponentBody, data: Input<UID.Schema>) => {\n return pipe(omitComponentData(schema), assign(componentData))(data);\n }\n);\n\nexport {\n omitComponentData,\n assignComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n};\n"],"names":["curry","contentTypesUtils","omit","has","async","_","errors","pipe","assign"],"mappings":";;;;;;;AAwBA,MAAM,oBAAoBA,EAAA;AAAA,EACxB,CAAC,QAAuB,SAAwD;AACxE,UAAA,EAAE,WAAe,IAAA;AACvB,UAAM,sBAAsB,OAAO,KAAK,UAAU,EAAE;AAAA,MAAO,CAAC,kBAC1DC,YAAA,aAAkB,qBAAqB,WAAW,aAAa,CAAC;AAAA,IAAA;AAG3D,WAAAC,EAAA,KAAK,qBAAqB,IAAI;AAAA,EACvC;AACF;AAGM,MAAA,mBAAmB,OACvB,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,MAAM;AACvB,YAAI,CAAC,MAAM,QAAQ,cAAc,GAAG;AAC5B,gBAAA,IAAI,MAAM,kDAAkD;AAAA,QACpE;AAEM,cAAA,aAAuC,MAAMG,YAAAA,MAAM;AAAA,UAAI;AAAA,UAAgB,CAAC,UAC5E,gBAAgB,cAAc,KAAK;AAAA,QAAA;AAGrC,sBAAc,a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e,IAAI;AAAA,EAAA,CACtD;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;AAEA,MAAM,sBAAsBF,EAAA;AAAA,EAC1B,CAAC,QAAuB,eAA8B,SAA4B;AACzE,WAAAO,EAAA,KAAK,kBAAkB,MAAM,GAAGC,SAAO,aAAa,CAAC,EAAE,IAAI;AAAA,EACpE;AACF;;;;;;;;"}
@@ -1,13 +1,15 @@
1
1
  import _ from "lodash";
2
- import { omit, has, pipe, assign } from "lodash/fp";
2
+ import { curry, omit, pipe, assign, has } from "lodash/fp";
3
3
  import { contentTypes, async, errors } from "@strapi/utils";
4
- function omitComponentData(contentType, data) {
5
- const { attributes } = contentType;
6
- const componentAttributes = Object.keys(attributes).filter(
7
- (attributeName) => contentTypes.isComponentAttribute(attributes[attributeName])
8
- );
9
- return omit(componentAttributes, data);
10
- }
4
+ const omitComponentData = curry(
5
+ (schema, data) => {
6
+ const { attributes } = schema;
7
+ const componentAttributes = Object.keys(attributes).filter(
8
+ (attributeName) => contentTypes.isComponentAttribute(attributes[attributeName])
9
+ );
10
+ return omit(componentAttributes, data);
11
+ }
12
+ );
11
13
  const createComponents = async (uid, data) => {
12
14
  const { attributes = {} } = strapi.getModel(uid);
13
15
  const componentBody = {};
@@ -235,26 +237,23 @@ const deleteComponents = async (uid, entityToDelete, { loadComponents = true } =
235
237
  }
236
238
  };
237
239
  const createComponent = async (uid, data) => {
238
- const model = strapi.getModel(uid);
240
+ const schema = strapi.getModel(uid);
239
241
  const componentData = await createComponents(uid, data);
240
242
  const transform = pipe(
241
243
  // Make sure we don't save the component with a pre-defined ID
242
244
  omit("id"),
243
- // Remove the component data from the original data object ...
244
- (payload) => omitComponentData(model, payload),
245
- // ... and assign the newly created component instead
246
- assign(componentData)
245
+ assignComponentData(schema, componentData)
247
246
  );
248
247
  return strapi.db.query(uid).create({ data: transform(data) });
249
248
  };
250
249
  const updateComponent = async (uid, componentToUpdate, data) => {
251
- const model = strapi.getModel(uid);
250
+ const schema = strapi.getModel(uid);
252
251
  const componentData = await updateComponents(uid, componentToUpdate, data);
253
252
  return strapi.db.query(uid).update({
254
253
  where: {
255
254
  id: componentToUpdate.id
256
255
  },
257
- data: Object.assign(omitComponentData(model, data), componentData)
256
+ data: assignComponentData(schema, componentData, data)
258
257
  });
259
258
  };
260
259
  const updateOrCreateComponent = (componentUID, value) => {
@@ -270,11 +269,11 @@ const deleteComponent = async (uid, componentToDelete) => {
270
269
  await deleteComponents(uid, componentToDelete);
271
270
  await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });
272
271
  };
273
- const assignComponentData = (data, componentData, {
274
- contentType
275
- }) => {
276
- return Object.assign(omitComponentData(contentType, data), componentData);
277
- };
272
+ const assignComponentData = curry(
273
+ (schema, componentData, data) => {
274
+ return pipe(omitComponentData(schema), assign(componentData))(data);
275
+ }
276
+ );
278
277
  export {
279
278
  assignComponentData,
280
279
  createComponents,
@@ -1 +1 @@
1
- {"version":3,"file":"components.mjs","sources":["../../../src/services/document-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\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: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n createComponent(componentUID, value)\n );\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\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 );\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: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n updateOrCreateComponent(componentUID, value)\n );\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(dynamiczoneValues, 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 }\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(_.castArray(value), (subValue: any) =>\n deleteComponent(componentUID, subValue)\n );\n } else {\n // delete dynamic zone components\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(subValue.__component, subValue)\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\nconst assignComponentData = <TUID extends UID.ContentType>(\n data: Modules.EntityService.Params.Data.Input<TUID>,\n componentData: ComponentBody,\n {\n contentType,\n }: {\n contentType: Schema.ContentType<TUID>;\n }\n) => {\n return Object.assign(omitComponentData(contentType, data), componentData);\n};\n\nexport {\n omitComponentData,\n assignComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n};\n"],"names":["contentTypesUtils"],"mappings":";;;AA6BA,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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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;AAEM,MAAA,sBAAsB,CAC1B,MACA,eACA;AAAA,EACE;AACF,MAGG;AACH,SAAO,OAAO,OAAO,kBAAkB,aAAa,IAAI,GAAG,aAAa;AAC1E;"}
1
+ {"version":3,"file":"components.mjs","sources":["../../../src/services/document-service/components.ts"],"sourcesContent":["import _ from 'lodash';\nimport { has, omit, pipe, assign, curry } from 'lodash/fp';\nimport type { Utils, UID, Schema, Data, Modules } from '@strapi/types';\nimport { contentTypes as contentTypesUtils, async, errors } from '@strapi/utils';\n\n// type aliases for readability\ntype Input<T extends UID.Schema> = Modules.Documents.Params.Data.Input<T>;\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 omitComponentData = curry(\n (schema: Schema.Schema, data: Input<UID.Schema>): Partial<Input<UID.Schema>> => {\n const { attributes } = schema;\n const componentAttributes = Object.keys(attributes).filter((attributeName) =>\n contentTypesUtils.isComponentAttribute(attributes[attributeName])\n );\n\n return omit(componentAttributes, data);\n }\n);\n\n// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer\nconst createComponents = async <TUID extends UID.Schema, TData extends Input<TUID>>(\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 const components: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n createComponent(componentUID, value)\n );\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 Input<UID.Component>\n );\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 );\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 <TUID extends UID.Schema, TData extends Partial<Input<TUID>>>(\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: RepeatableComponentValue = await async.map(componentValue, (value: any) =>\n updateOrCreateComponent(componentUID, value)\n );\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(dynamiczoneValues, 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 }\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\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 await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(componentUID, subValue)\n );\n } else {\n await async.map(_.castArray(value), (subValue: any) =>\n deleteComponent(subValue.__component, subValue)\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>(uid: TUID, data: Input<TUID>) => {\n const schema = strapi.getModel(uid);\n\n const componentData = await createComponents(uid, data);\n\n const transform = pipe(\n // Make sure we don't save the component with a pre-defined ID\n omit('id'),\n assignComponentData(schema, 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: Input<TUID>\n) => {\n const schema = 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: assignComponentData(schema, componentData, data),\n });\n};\n\nconst updateOrCreateComponent = <TUID extends UID.Component>(\n componentUID: TUID,\n value: 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\nconst assignComponentData = curry(\n (schema: Schema.Schema, componentData: ComponentBody, data: Input<UID.Schema>) => {\n return pipe(omitComponentData(schema), assign(componentData))(data);\n }\n);\n\nexport {\n omitComponentData,\n assignComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n};\n"],"names":["contentTypesUtils"],"mappings":";;;AAwBA,MAAM,oBAAoB;AAAA,EACxB,CAAC,QAAuB,SAAwD;AACxE,UAAA,EAAE,WAAe,IAAA;AACvB,UAAM,sBAAsB,OAAO,KAAK,UAAU,E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E;AACF;"}
@@ -3,6 +3,8 @@ declare const createEntriesService: (uid: UID.ContentType) => {
3
3
  create: (params?: any) => Promise<any>;
4
4
  delete: (id: number) => Promise<void>;
5
5
  update: (entryToUpdate: any, params?: any) => Promise<any>;
6
+ publish: (entry: any, params?: any) => Promise<object>;
7
+ discardDraft: (entry: any, params?: any) => Promise<object>;
6
8
  };
7
9
  export { createEntriesService };
8
10
  //# sourceMappingURL=entries.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"entries.d.ts","sourceRoot":"","sources":["../../../src/services/document-service/entries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAUzC,QAAA,MAAM,oBAAoB,QAAS,IAAI,WAAW;;iBA+BjB,MAAM;4BAQK,GAAG;CA8B9C,CAAC;AAEF,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
1
+ {"version":3,"file":"entries.d.ts","sourceRoot":"","sources":["../../../src/services/document-service/entries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAazC,QAAA,MAAM,oBAAoB,QAAS,IAAI,WAAW;;iBAkCjB,MAAM;4BAQK,GAAG;qBA4BV,GAAG;0BAaE,GAAG;CAoB5C,CAAC;AAEF,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
@@ -1,29 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const strapiUtils = require("@strapi/utils");
4
+ const _ = require("lodash/fp");
3
5
  const components = require("./components.js");
4
6
  const idTransform = require("./transform/id-transform.js");
5
7
  const query = require("./transform/query.js");
6
8
  const params = require("./params.js");
7
9
  const index$1 = require("./attributes/index.js");
10
+ const data = require("./transform/data.js");
8
11
  const index = require("../entity-validator/index.js");
9
12
  const createEntriesService = (uid) => {
10
13
  const contentType = strapi.contentType(uid);
11
14
  async function createEntry(params$1 = {}) {
12
- const { data, ...restParams } = await idTransform.transformParamsDocumentId(uid, params$1);
15
+ const { data: data2, ...restParams } = await idTransform.transformParamsDocumentId(uid, params$1);
13
16
  const query$1 = query.transformParamsToQuery(uid, params.pickSelectionParams(restParams));
14
- if (!data) {
17
+ if (!data2) {
15
18
  throw new Error("Create requires data attribute");
16
19
  }
17
- const validData = await index.validateEntityCreation(contentType, data, {
20
+ const validData = await index.validateEntityCreation(contentType, data2, {
18
21
  // Note: publishedAt value will always be set when DP is disabled
19
22
  isDraft: !params$1?.data?.publishedAt,
20
23
  locale: params$1?.locale
21
24
  });
22
25
  const componentData = await components.createComponents(uid, validData);
23
- const dataWithComponents = components.assignComponentData(validData, componentData, {
24
- contentType
25
- });
26
- const entryData = index$1.applyTransforms(dataWithComponents, { contentType });
26
+ const dataWithComponents = components.assignComponentData(
27
+ contentType,
28
+ componentData,
29
+ validData
30
+ );
31
+ const entryData = index$1.applyTransforms(contentType, dataWithComponents);
27
32
  const doc = await strapi.db.query(uid).create({ ...query$1, data: entryData });
28
33
  return doc;
29
34
  }
@@ -33,11 +38,11 @@ const createEntriesService = (uid) => {
33
38
  await components.deleteComponents(uid, componentsToDelete, { loadComponents: false });
34
39
  }
35
40
  async function updateEntry(entryToUpdate, params$1 = {}) {
36
- const { data, ...restParams } = await idTransform.transformParamsDocumentId(uid, params$1);
41
+ const { data: data2, ...restParams } = await idTransform.transformParamsDocumentId(uid, params$1);
37
42
  const query$1 = query.transformParamsToQuery(uid, params.pickSelectionParams(restParams));
38
43
  const validData = await index.validateEntityUpdate(
39
44
  contentType,
40
- data,
45
+ data2,
41
46
  {
42
47
  isDraft: !params$1?.data?.publishedAt,
43
48
  // Always update the draft version
@@ -46,16 +51,44 @@ const createEntriesService = (uid) => {
46
51
  entryToUpdate
47
52
  );
48
53
  const componentData = await components.updateComponents(uid, entryToUpdate, validData);
49
- const dataWithComponents = components.assignComponentData(validData, componentData, {
50
- contentType
51
- });
52
- const entryData = index$1.applyTransforms(dataWithComponents, { contentType });
54
+ const dataWithComponents = components.assignComponentData(
55
+ contentType,
56
+ componentData,
57
+ validData
58
+ );
59
+ const entryData = index$1.applyTransforms(contentType, dataWithComponents);
53
60
  return strapi.db.query(uid).update({ ...query$1, where: { id: entryToUpdate.id }, data: entryData });
54
61
  }
62
+ async function publishEntry(entry, params2 = {}) {
63
+ return strapiUtils.async.pipe(
64
+ _.omit("id"),
65
+ _.assoc("publishedAt", /* @__PURE__ */ new Date()),
66
+ (draft) => {
67
+ const opts = { uid, locale: draft.locale, status: "published", allowMissingId: true };
68
+ return data.transformData(draft, opts);
69
+ },
70
+ // Create the published entry
71
+ (draft) => createEntry({ ...params2, data: draft, locale: draft.locale, status: "published" })
72
+ )(entry);
73
+ }
74
+ async function discardDraftEntry(entry, params2 = {}) {
75
+ return strapiUtils.async.pipe(
76
+ _.omit("id"),
77
+ _.assoc("publishedAt", null),
78
+ (entry2) => {
79
+ const opts = { uid, locale: entry2.locale, status: "draft", allowMissingId: true };
80
+ return data.transformData(entry2, opts);
81
+ },
82
+ // Create the draft entry
83
+ (data2) => createEntry({ ...params2, locale: data2.locale, data: data2, status: "draft" })
84
+ )(entry);
85
+ }
55
86
  return {
56
87
  create: createEntry,
57
88
  delete: deleteEntry,
58
- update: updateEntry
89
+ update: updateEntry,
90
+ publish: publishEntry,
91
+ discardDraft: discardDraftEntry
59
92
  };
60
93
  };
61
94
  exports.createEntriesService = createEntriesService;