@akemona-org/strapi-plugin-content-type-builder 3.7.0

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 (223) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +19 -0
  3. package/admin/src/InjectedComponents/ContentManager/EditSettingViewButton.js +76 -0
  4. package/admin/src/InjectedComponents/ContentManager/EditViewLink.js +44 -0
  5. package/admin/src/assets/images/logo.svg +1 -0
  6. package/admin/src/components/AllowedTypesSelect/MenuList.js +115 -0
  7. package/admin/src/components/AllowedTypesSelect/Text.js +26 -0
  8. package/admin/src/components/AllowedTypesSelect/index.js +55 -0
  9. package/admin/src/components/AttributeOption/Button.js +50 -0
  10. package/admin/src/components/AttributeOption/Card.js +26 -0
  11. package/admin/src/components/AttributeOption/index.js +127 -0
  12. package/admin/src/components/BooleanBox/CT.js +21 -0
  13. package/admin/src/components/BooleanBox/Enumeration.js +10 -0
  14. package/admin/src/components/BooleanBox/EnumerationWrapper.js +14 -0
  15. package/admin/src/components/BooleanBox/Label.js +9 -0
  16. package/admin/src/components/BooleanBox/ST.js +21 -0
  17. package/admin/src/components/BooleanBox/Wrapper.js +75 -0
  18. package/admin/src/components/BooleanBox/icons/CTSelected.js +136 -0
  19. package/admin/src/components/BooleanBox/icons/CTUnselected.js +136 -0
  20. package/admin/src/components/BooleanBox/icons/STSelected.js +80 -0
  21. package/admin/src/components/BooleanBox/icons/STUnselected.js +76 -0
  22. package/admin/src/components/BooleanBox/index.js +103 -0
  23. package/admin/src/components/CheckboxWithDescription/Wrapper.js +12 -0
  24. package/admin/src/components/CheckboxWithDescription/index.js +40 -0
  25. package/admin/src/components/ComponentCard/Close.js +12 -0
  26. package/admin/src/components/ComponentCard/Wrapper.js +84 -0
  27. package/admin/src/components/ComponentCard/index.js +62 -0
  28. package/admin/src/components/ComponentIconPicker/Cell.js +40 -0
  29. package/admin/src/components/ComponentIconPicker/CellRenderer.js +36 -0
  30. package/admin/src/components/ComponentIconPicker/Search.js +12 -0
  31. package/admin/src/components/ComponentIconPicker/SearchWrapper.js +34 -0
  32. package/admin/src/components/ComponentIconPicker/Wrapper.js +60 -0
  33. package/admin/src/components/ComponentIconPicker/index.js +143 -0
  34. package/admin/src/components/ComponentList/index.js +78 -0
  35. package/admin/src/components/ComponentSelect/Category.js +20 -0
  36. package/admin/src/components/ComponentSelect/CategoryName.js +11 -0
  37. package/admin/src/components/ComponentSelect/MenuList.js +110 -0
  38. package/admin/src/components/ComponentSelect/MultipleMenuList.js +271 -0
  39. package/admin/src/components/ComponentSelect/Value.js +83 -0
  40. package/admin/src/components/ComponentSelect/index.js +83 -0
  41. package/admin/src/components/ComponentSelect/utils/HasSomeSubArray.js +5 -0
  42. package/admin/src/components/ComponentSelect/utils/hasSubArray.js +5 -0
  43. package/admin/src/components/CreatableSelect/index.js +58 -0
  44. package/admin/src/components/CustomCheckbox/StyledCustomCheckbox.js +25 -0
  45. package/admin/src/components/CustomCheckbox/index.js +71 -0
  46. package/admin/src/components/CustomLink/P.js +17 -0
  47. package/admin/src/components/CustomLink/StyledCustomLink.js +20 -0
  48. package/admin/src/components/CustomLink/index.js +31 -0
  49. package/admin/src/components/DynamicZoneList/ComponentButton.js +39 -0
  50. package/admin/src/components/DynamicZoneList/index.js +124 -0
  51. package/admin/src/components/HeaderModalNavContainer/index.js +25 -0
  52. package/admin/src/components/HeaderNavLink/Wrapper.js +21 -0
  53. package/admin/src/components/HeaderNavLink/index.js +50 -0
  54. package/admin/src/components/List/List.js +131 -0
  55. package/admin/src/components/List/index.js +295 -0
  56. package/admin/src/components/ListButton/index.js +9 -0
  57. package/admin/src/components/ListHeader/Title.js +19 -0
  58. package/admin/src/components/ListHeader/Wrapper.js +24 -0
  59. package/admin/src/components/ListHeader/index.js +37 -0
  60. package/admin/src/components/ListRow/Wrapper.js +76 -0
  61. package/admin/src/components/ListRow/index.js +334 -0
  62. package/admin/src/components/ModalHeader/ComponentIcon.js +21 -0
  63. package/admin/src/components/ModalHeader/ComponentInfos.js +22 -0
  64. package/admin/src/components/ModalHeader/ComponentInfosWrapper.js +9 -0
  65. package/admin/src/components/ModalHeader/DropdownInfos.js +50 -0
  66. package/admin/src/components/ModalHeader/Icon.js +26 -0
  67. package/admin/src/components/ModalHeader/IconWrapper.js +11 -0
  68. package/admin/src/components/ModalHeader/Item.js +19 -0
  69. package/admin/src/components/ModalHeader/Menu.js +8 -0
  70. package/admin/src/components/ModalHeader/Toggle.js +14 -0
  71. package/admin/src/components/ModalHeader/Wrapper.js +12 -0
  72. package/admin/src/components/ModalHeader/index.js +105 -0
  73. package/admin/src/components/RelationForm/Wrapper.js +9 -0
  74. package/admin/src/components/RelationForm/index.js +66 -0
  75. package/admin/src/components/RelationFormBox/Wrapper.js +34 -0
  76. package/admin/src/components/RelationFormBox/index.js +85 -0
  77. package/admin/src/components/RelationFormNaturePicker/Wrapper.js +53 -0
  78. package/admin/src/components/RelationFormNaturePicker/index.js +112 -0
  79. package/admin/src/components/RelationTargetPicker/Item.js +62 -0
  80. package/admin/src/components/RelationTargetPicker/Wrapper.js +89 -0
  81. package/admin/src/components/RelationTargetPicker/index.js +88 -0
  82. package/admin/src/components/SelectCheckbox/index.js +8 -0
  83. package/admin/src/components/SelectMenuSubUl/ToggleUl.js +23 -0
  84. package/admin/src/components/SelectMenuSubUl/index.js +23 -0
  85. package/admin/src/components/SelectMenuUl/index.js +66 -0
  86. package/admin/src/components/Td/index.js +37 -0
  87. package/admin/src/components/UpperFirst/index.js +14 -0
  88. package/admin/src/components/WrapperSelect/index.js +89 -0
  89. package/admin/src/containers/App/Wrapper.js +14 -0
  90. package/admin/src/containers/App/index.js +39 -0
  91. package/admin/src/containers/App/utils/icons.json +962 -0
  92. package/admin/src/containers/DataManagerProvider/constants.js +15 -0
  93. package/admin/src/containers/DataManagerProvider/index.js +610 -0
  94. package/admin/src/containers/DataManagerProvider/reducer.js +461 -0
  95. package/admin/src/containers/DataManagerProvider/selectors.js +25 -0
  96. package/admin/src/containers/DataManagerProvider/utils/cleanData.js +167 -0
  97. package/admin/src/containers/DataManagerProvider/utils/createDataObject.js +8 -0
  98. package/admin/src/containers/DataManagerProvider/utils/createModifiedDataSchema.js +91 -0
  99. package/admin/src/containers/DataManagerProvider/utils/retrieveComponentsFromSchema.js +55 -0
  100. package/admin/src/containers/DataManagerProvider/utils/retrieveComponentsThatHaveComponents.js +35 -0
  101. package/admin/src/containers/DataManagerProvider/utils/retrieveNestedComponents.js +34 -0
  102. package/admin/src/containers/DataManagerProvider/utils/retrieveSpecificInfoFromComponents.js +12 -0
  103. package/admin/src/containers/FormModal/CustomButton.js +12 -0
  104. package/admin/src/containers/FormModal/attributes/advancedForm.js +269 -0
  105. package/admin/src/containers/FormModal/attributes/attributeOptions.js +99 -0
  106. package/admin/src/containers/FormModal/attributes/baseForm.js +322 -0
  107. package/admin/src/containers/FormModal/attributes/commonBaseForm.js +7 -0
  108. package/admin/src/containers/FormModal/attributes/form.js +9 -0
  109. package/admin/src/containers/FormModal/attributes/index.js +4 -0
  110. package/admin/src/containers/FormModal/attributes/nameField.js +19 -0
  111. package/admin/src/containers/FormModal/attributes/types.js +309 -0
  112. package/admin/src/containers/FormModal/attributes/uiHelpers.js +11 -0
  113. package/admin/src/containers/FormModal/attributes/validation/common.js +151 -0
  114. package/admin/src/containers/FormModal/category/createCategorySchema.js +28 -0
  115. package/admin/src/containers/FormModal/category/form.js +27 -0
  116. package/admin/src/containers/FormModal/category/index.js +3 -0
  117. package/admin/src/containers/FormModal/category/regex.js +3 -0
  118. package/admin/src/containers/FormModal/component/componentField.js +25 -0
  119. package/admin/src/containers/FormModal/component/createComponentSchema.js +49 -0
  120. package/admin/src/containers/FormModal/component/form.js +67 -0
  121. package/admin/src/containers/FormModal/component/index.js +4 -0
  122. package/admin/src/containers/FormModal/constants.js +11 -0
  123. package/admin/src/containers/FormModal/contentType/createContentTypeSchema.js +44 -0
  124. package/admin/src/containers/FormModal/contentType/form.js +116 -0
  125. package/admin/src/containers/FormModal/contentType/index.js +2 -0
  126. package/admin/src/containers/FormModal/dynamicZone/form.js +45 -0
  127. package/admin/src/containers/FormModal/dynamicZone/index.js +2 -0
  128. package/admin/src/containers/FormModal/forms/index.js +164 -0
  129. package/admin/src/containers/FormModal/index.js +1552 -0
  130. package/admin/src/containers/FormModal/reducer.js +293 -0
  131. package/admin/src/containers/FormModal/selectors.js +24 -0
  132. package/admin/src/containers/FormModal/utils/canEditContentType.js +19 -0
  133. package/admin/src/containers/FormModal/utils/createHeadersArray.js +37 -0
  134. package/admin/src/containers/FormModal/utils/createHeadersObjectFromArray.js +27 -0
  135. package/admin/src/containers/FormModal/utils/createUid.js +17 -0
  136. package/admin/src/containers/FormModal/utils/getAttributesToDisplay.js +35 -0
  137. package/admin/src/containers/FormModal/utils/getModalTitleSubHeader.js +25 -0
  138. package/admin/src/containers/FormModal/utils/getNextSearch.js +17 -0
  139. package/admin/src/containers/FormModal/utils/index.js +6 -0
  140. package/admin/src/containers/FormModal/utils/relations.js +9 -0
  141. package/admin/src/containers/FormModal/utils/staticData.js +43 -0
  142. package/admin/src/containers/Initializer/index.js +26 -0
  143. package/admin/src/containers/LeftMenu/Wrapper.js +14 -0
  144. package/admin/src/containers/LeftMenu/index.js +220 -0
  145. package/admin/src/containers/ListView/Wrapper.js +16 -0
  146. package/admin/src/containers/ListView/index.js +322 -0
  147. package/admin/src/containers/NotFoundPage/index.js +20 -0
  148. package/admin/src/containers/RecursivePath/index.js +22 -0
  149. package/admin/src/contexts/DataManagerContext.js +5 -0
  150. package/admin/src/contexts/ListViewContext.js +5 -0
  151. package/admin/src/hooks/useDataManager.js +6 -0
  152. package/admin/src/hooks/useListView.js +6 -0
  153. package/admin/src/icons/Curve.js +26 -0
  154. package/admin/src/icons/ManyToMany.js +98 -0
  155. package/admin/src/icons/ManyToOne.js +64 -0
  156. package/admin/src/icons/ManyWay.js +36 -0
  157. package/admin/src/icons/OneToMany.js +65 -0
  158. package/admin/src/icons/OneToOne.js +47 -0
  159. package/admin/src/icons/OneWay.js +38 -0
  160. package/admin/src/index.js +83 -0
  161. package/admin/src/lifecycles.js +13 -0
  162. package/admin/src/permissions.js +9 -0
  163. package/admin/src/pluginId.js +5 -0
  164. package/admin/src/reducers.js +10 -0
  165. package/admin/src/translations/ar.json +48 -0
  166. package/admin/src/translations/cs.json +151 -0
  167. package/admin/src/translations/de.json +187 -0
  168. package/admin/src/translations/dk.json +181 -0
  169. package/admin/src/translations/en.json +188 -0
  170. package/admin/src/translations/es.json +188 -0
  171. package/admin/src/translations/fr.json +87 -0
  172. package/admin/src/translations/id.json +187 -0
  173. package/admin/src/translations/index.js +51 -0
  174. package/admin/src/translations/it.json +187 -0
  175. package/admin/src/translations/ja.json +50 -0
  176. package/admin/src/translations/ko.json +57 -0
  177. package/admin/src/translations/ms.json +179 -0
  178. package/admin/src/translations/nl.json +171 -0
  179. package/admin/src/translations/pl.json +152 -0
  180. package/admin/src/translations/pt-BR.json +51 -0
  181. package/admin/src/translations/pt.json +51 -0
  182. package/admin/src/translations/ru.json +188 -0
  183. package/admin/src/translations/sk.json +186 -0
  184. package/admin/src/translations/th.json +181 -0
  185. package/admin/src/translations/tr.json +51 -0
  186. package/admin/src/translations/uk.json +180 -0
  187. package/admin/src/translations/zh-Hans.json +188 -0
  188. package/admin/src/translations/zh.json +178 -0
  189. package/admin/src/utils/convertAttrObjToArray.js +7 -0
  190. package/admin/src/utils/formAPI.js +112 -0
  191. package/admin/src/utils/getAttributeDisplayedType.js +31 -0
  192. package/admin/src/utils/getComponents.js +31 -0
  193. package/admin/src/utils/getTrad.js +5 -0
  194. package/admin/src/utils/makeSearch.js +15 -0
  195. package/admin/src/utils/makeUnique.js +3 -0
  196. package/config/functions/bootstrap.js +14 -0
  197. package/config/routes.json +144 -0
  198. package/controllers/Builder.js +7 -0
  199. package/controllers/ComponentCategories.js +43 -0
  200. package/controllers/Components.js +152 -0
  201. package/controllers/Connections.js +9 -0
  202. package/controllers/ContentTypes.js +143 -0
  203. package/controllers/validation/common.js +118 -0
  204. package/controllers/validation/component-category.js +21 -0
  205. package/controllers/validation/component.js +89 -0
  206. package/controllers/validation/content-type.js +161 -0
  207. package/controllers/validation/data-transform.js +32 -0
  208. package/controllers/validation/model-schema.js +91 -0
  209. package/controllers/validation/relations.js +56 -0
  210. package/controllers/validation/types.js +277 -0
  211. package/package.json +63 -0
  212. package/services/Builder.js +7 -0
  213. package/services/ComponentCategories.js +88 -0
  214. package/services/Components.js +103 -0
  215. package/services/ContentTypes.js +263 -0
  216. package/services/api-handler.js +158 -0
  217. package/services/constants.js +59 -0
  218. package/services/schema-builder/component-builder.js +137 -0
  219. package/services/schema-builder/content-type-builder.js +256 -0
  220. package/services/schema-builder/index.js +196 -0
  221. package/services/schema-builder/schema-handler.js +297 -0
  222. package/utils/attributes.js +154 -0
  223. package/utils/helpers.js +31 -0
@@ -0,0 +1,256 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const _ = require('lodash');
5
+ const pluralize = require('pluralize');
6
+
7
+ const { nameToSlug, nameToCollectionName } = require('@akemona-org/strapi-utils');
8
+ const { isRelation, toUID, isConfigurable } = require('../../utils/attributes');
9
+ const { typeKinds } = require('../constants');
10
+ const createSchemaHandler = require('./schema-handler');
11
+
12
+ module.exports = function createComponentBuilder() {
13
+ return {
14
+ setRelation({ key, modelName, plugin, attribute }) {
15
+ const targetCT = this.contentTypes.get(attribute.target);
16
+ const targetAttribute = targetCT.getAttribute(attribute.targetAttribute);
17
+ targetCT.setAttribute(
18
+ attribute.targetAttribute,
19
+ generateRelation({
20
+ key,
21
+ attribute,
22
+ plugin,
23
+ modelName,
24
+ targetAttribute,
25
+ })
26
+ );
27
+ },
28
+
29
+ unsetRelation(attribute) {
30
+ const target = attribute.model || attribute.collection;
31
+ const plugin = attribute.plugin;
32
+
33
+ const uid = toUID(target, plugin);
34
+
35
+ const targetCT = this.contentTypes.get(uid);
36
+ const targetAttribute = targetCT.getAttribute(attribute.via);
37
+
38
+ if (!targetAttribute) return;
39
+
40
+ // do not delete polymorphic relations
41
+ if (targetAttribute.collection === '*' || targetAttribute.model === '*') {
42
+ return;
43
+ }
44
+
45
+ return targetCT.deleteAttribute(attribute.via);
46
+ },
47
+
48
+ /**
49
+ * create a component in the tmpComponent map
50
+ */
51
+ createContentType(infos) {
52
+ const uid = createContentTypeUID(infos);
53
+
54
+ if (this.contentTypes.has(uid)) {
55
+ throw new Error('contentType.alreadyExists');
56
+ }
57
+
58
+ const contentType = createSchemaHandler({
59
+ modelName: nameToSlug(infos.name),
60
+ dir: path.join(strapi.dir, 'api', nameToSlug(infos.name), 'models'),
61
+ filename: `${nameToSlug(infos.name)}.settings.json`,
62
+ });
63
+
64
+ this.contentTypes.set(uid, contentType);
65
+
66
+ const defaultCollectionName = `${nameToCollectionName(pluralize(infos.name))}`;
67
+
68
+ // support self referencing content type relation
69
+ Object.keys(infos.attributes).forEach((key) => {
70
+ const { target } = infos.attributes[key];
71
+ if (target === '__self__') {
72
+ infos.attributes[key].target = uid;
73
+ }
74
+ });
75
+
76
+ contentType
77
+ .setUID(uid)
78
+ .set('kind', infos.kind || typeKinds.COLLECTION_TYPE)
79
+ .set('collectionName', infos.collectionName || defaultCollectionName)
80
+ .set('info', {
81
+ name: infos.name,
82
+ description: infos.description,
83
+ })
84
+ .set('options', {
85
+ increments: true,
86
+ timestamps: true,
87
+ draftAndPublish: infos.draftAndPublish || false,
88
+ })
89
+ .set('pluginOptions', infos.pluginOptions)
90
+ .setAttributes(this.convertAttributes(infos.attributes));
91
+
92
+ Object.keys(infos.attributes).forEach((key) => {
93
+ const attribute = infos.attributes[key];
94
+
95
+ if (isRelation(attribute)) {
96
+ this.setRelation({
97
+ key,
98
+ modelName: contentType.modelName,
99
+ plugin: contentType.plugin,
100
+ attribute,
101
+ });
102
+ }
103
+ });
104
+
105
+ return contentType;
106
+ },
107
+
108
+ editContentType(infos) {
109
+ const { uid } = infos;
110
+
111
+ if (!this.contentTypes.has(uid)) {
112
+ throw new Error('contentType.notFound');
113
+ }
114
+
115
+ const contentType = this.contentTypes.get(uid);
116
+
117
+ const oldAttributes = contentType.schema.attributes;
118
+
119
+ const newAttributes = _.omitBy(infos.attributes, (attr, key) => {
120
+ return _.has(oldAttributes, key) && !isConfigurable(oldAttributes[key]);
121
+ });
122
+
123
+ const newKeys = _.difference(Object.keys(newAttributes), Object.keys(oldAttributes));
124
+
125
+ const deletedKeys = _.difference(Object.keys(oldAttributes), Object.keys(newAttributes));
126
+
127
+ const remainingKeys = _.intersection(Object.keys(oldAttributes), Object.keys(newAttributes));
128
+
129
+ // remove old relations
130
+ deletedKeys.forEach((key) => {
131
+ const attribute = oldAttributes[key];
132
+
133
+ // if the old relation has a target attribute. we need to remove it
134
+ if (isConfigurable(attribute) && isRelation(attribute) && _.has(attribute, 'via')) {
135
+ this.unsetRelation(attribute);
136
+ }
137
+ });
138
+
139
+ remainingKeys.forEach((key) => {
140
+ const oldAttribute = oldAttributes[key];
141
+ const newAttribute = newAttributes[key];
142
+
143
+ if (!isRelation(oldAttribute) && isRelation(newAttribute)) {
144
+ return this.setRelation({
145
+ key,
146
+ modelName: contentType.modelName,
147
+ plugin: contentType.plugin,
148
+ attribute: newAttributes[key],
149
+ });
150
+ }
151
+
152
+ if (isRelation(oldAttribute) && !isRelation(newAttribute)) {
153
+ return this.unsetRelation(oldAttribute);
154
+ }
155
+
156
+ if (isRelation(oldAttribute) && isRelation(newAttribute)) {
157
+ if (_.has(oldAttribute, 'via') && oldAttribute.via !== newAttribute.targetAttribute) {
158
+ this.unsetRelation(oldAttribute);
159
+ }
160
+
161
+ // keep extra options that were set manually on oldAttribute
162
+ _.defaults(newAttribute, oldAttribute);
163
+
164
+ return this.setRelation({
165
+ key,
166
+ modelName: contentType.modelName,
167
+ plugin: contentType.plugin,
168
+ attribute: newAttribute,
169
+ });
170
+ }
171
+ });
172
+
173
+ // add new relations
174
+ newKeys.forEach((key) => {
175
+ const attribute = newAttributes[key];
176
+
177
+ if (isRelation(attribute)) {
178
+ this.setRelation({
179
+ key,
180
+ modelName: contentType.modelName,
181
+ plugin: contentType.plugin,
182
+ attribute,
183
+ });
184
+ }
185
+ });
186
+
187
+ contentType
188
+ .set('collectionName', infos.collectionName)
189
+ .set('kind', infos.kind || contentType.schema.kind)
190
+ .set(['info', 'name'], infos.name)
191
+ .set(['info', 'description'], infos.description)
192
+ .set(['options', 'draftAndPublish'], infos.draftAndPublish || false)
193
+ .set('pluginOptions', infos.pluginOptions)
194
+ .setAttributes(this.convertAttributes(newAttributes));
195
+
196
+ return contentType;
197
+ },
198
+
199
+ deleteContentType(uid) {
200
+ if (!this.contentTypes.has(uid)) {
201
+ throw new Error('contentType.notFound');
202
+ }
203
+
204
+ this.components.forEach((compo) => {
205
+ compo.removeContentType(uid);
206
+ });
207
+
208
+ this.contentTypes.forEach((ct) => {
209
+ ct.removeContentType(uid);
210
+ });
211
+
212
+ return this.contentTypes.get(uid).delete();
213
+ },
214
+ };
215
+ };
216
+
217
+ /**
218
+ * Returns a uid from a content type infos
219
+ * @param {Object} options options
220
+ * @param {string} options.name component name
221
+ */
222
+ const createContentTypeUID = ({ name }) => `application::${nameToSlug(name)}.${nameToSlug(name)}`;
223
+
224
+ const generateRelation = ({ key, attribute, plugin, modelName, targetAttribute = {} }) => {
225
+ const opts = {
226
+ via: key,
227
+ plugin,
228
+ columnName: attribute.targetColumnName || undefined,
229
+ autoPopulate: targetAttribute.autoPopulate,
230
+ private: targetAttribute.private || undefined,
231
+ };
232
+
233
+ switch (attribute.nature) {
234
+ case 'manyWay':
235
+ case 'oneWay':
236
+ return;
237
+ case 'oneToOne':
238
+ case 'oneToMany':
239
+ opts.model = modelName;
240
+ break;
241
+ case 'manyToOne':
242
+ opts.collection = modelName;
243
+ break;
244
+ case 'manyToMany': {
245
+ opts.collection = modelName;
246
+
247
+ if (!attribute.dominant) {
248
+ opts.dominant = true;
249
+ }
250
+ break;
251
+ }
252
+ default:
253
+ }
254
+
255
+ return opts;
256
+ };
@@ -0,0 +1,196 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const _ = require('lodash');
5
+
6
+ const createSchemaHandler = require('./schema-handler');
7
+ const createComponentBuilder = require('./component-builder');
8
+ const createContentTypeBuilder = require('./content-type-builder');
9
+
10
+ const MODEL_RELATIONS = ['oneWay', 'oneToOne', 'manyToOne'];
11
+ const COLLECTION_RELATIONS = ['manyWay', 'manyToMany', 'oneToMany'];
12
+
13
+ module.exports = function createBuilder() {
14
+ const components = Object.keys(strapi.components).map(key => {
15
+ const compo = strapi.components[key];
16
+
17
+ return {
18
+ category: compo.category,
19
+ modelName: compo.modelName,
20
+ plugin: compo.modelName,
21
+ uid: compo.uid,
22
+ filename: compo.__filename__,
23
+ dir: path.join(strapi.dir, 'components', compo.category),
24
+ schema: compo.__schema__,
25
+ };
26
+ });
27
+
28
+ const contentTypes = Object.keys(strapi.contentTypes).map(key => {
29
+ const contentType = strapi.contentTypes[key];
30
+
31
+ let dir;
32
+ if (contentType.plugin) {
33
+ dir = `./extensions/${contentType.plugin}/models`;
34
+ } else {
35
+ dir = `./api/${contentType.apiName}/models`;
36
+ }
37
+
38
+ return {
39
+ modelName: contentType.modelName,
40
+ plugin: contentType.plugin,
41
+ uid: contentType.uid,
42
+ filename: contentType.__filename__,
43
+ dir: path.join(strapi.dir, dir),
44
+ schema: contentType.__schema__,
45
+ };
46
+ });
47
+
48
+ return createSchemaBuilder({
49
+ components,
50
+ contentTypes,
51
+ });
52
+ };
53
+
54
+ /**
55
+ * Schema builder
56
+ */
57
+ function createSchemaBuilder({ components, contentTypes }) {
58
+ const tmpComponents = new Map();
59
+ const tmpContentTypes = new Map();
60
+
61
+ // init temporary ContentTypes
62
+ Object.keys(contentTypes).forEach(key => {
63
+ tmpContentTypes.set(contentTypes[key].uid, createSchemaHandler(contentTypes[key]));
64
+ });
65
+
66
+ // init temporary components
67
+ Object.keys(components).forEach(key => {
68
+ tmpComponents.set(components[key].uid, createSchemaHandler(components[key]));
69
+ });
70
+
71
+ return {
72
+ get components() {
73
+ return tmpComponents;
74
+ },
75
+ get contentTypes() {
76
+ return tmpContentTypes;
77
+ },
78
+
79
+ convertAttributes(attributes) {
80
+ return Object.keys(attributes).reduce((acc, key) => {
81
+ const attribute = attributes[key];
82
+
83
+ const { configurable } = attribute;
84
+
85
+ if (_.has(attribute, 'type')) {
86
+ if (attribute.type === 'media') {
87
+ const fileModel = strapi.getModel('file', 'upload');
88
+ if (!fileModel) return acc;
89
+
90
+ const via = _.findKey(fileModel.attributes, { collection: '*' });
91
+ acc[key] = {
92
+ [attribute.multiple ? 'collection' : 'model']: 'file',
93
+ via,
94
+ allowedTypes: attribute.allowedTypes,
95
+ plugin: 'upload',
96
+ required: !!attribute.required,
97
+ configurable: configurable === false ? false : undefined,
98
+ pluginOptions: attribute.pluginOptions || {},
99
+ };
100
+ } else {
101
+ acc[key] = {
102
+ ...attribute,
103
+ configurable: configurable === false ? false : undefined,
104
+ };
105
+ }
106
+
107
+ return acc;
108
+ }
109
+
110
+ if (_.has(attribute, 'target')) {
111
+ const {
112
+ target,
113
+ nature,
114
+ unique,
115
+ targetAttribute,
116
+ columnName,
117
+ dominant,
118
+ private: isPrivate,
119
+ ...restOfOptions
120
+ } = attribute;
121
+
122
+ const attr = {
123
+ unique: unique === true ? true : undefined,
124
+ columnName: columnName || undefined,
125
+ configurable: configurable === false ? false : undefined,
126
+ private: isPrivate === true ? true : undefined,
127
+ ...restOfOptions,
128
+ };
129
+
130
+ if (!this.contentTypes.has(target)) {
131
+ throw new Error(`target: ${target} does not exist`);
132
+ }
133
+
134
+ const { modelName, plugin } = this.contentTypes.get(target);
135
+
136
+ attr.plugin = plugin;
137
+
138
+ if (MODEL_RELATIONS.includes(nature)) {
139
+ attr.model = modelName;
140
+ } else if (COLLECTION_RELATIONS.includes(nature)) {
141
+ attr.collection = modelName;
142
+ }
143
+
144
+ if (!['manyWay', 'oneWay'].includes(nature)) {
145
+ attr.via = targetAttribute;
146
+ attr.dominant = dominant || undefined;
147
+ }
148
+
149
+ acc[key] = attr;
150
+ }
151
+
152
+ return acc;
153
+ }, {});
154
+ },
155
+
156
+ ...createComponentBuilder({ tmpComponents, tmpContentTypes }),
157
+ ...createContentTypeBuilder({ tmpComponents, tmpContentTypes }),
158
+
159
+ /**
160
+ * Write all type to files
161
+ */
162
+ writeFiles() {
163
+ return Promise.all(
164
+ [
165
+ ...Array.from(tmpComponents.values()),
166
+ ...Array.from(tmpContentTypes.values()),
167
+ ].map(schema => schema.flush())
168
+ )
169
+ .catch(error => {
170
+ strapi.log.error('Error writing schema files');
171
+ strapi.log.error(error);
172
+ return this.rollback();
173
+ })
174
+ .catch(error => {
175
+ strapi.log.error(
176
+ 'Error rolling back schema files. You might need to fix your files manually'
177
+ );
178
+ strapi.log.error(error);
179
+
180
+ throw new Error('Invalid schema edition');
181
+ });
182
+ },
183
+
184
+ /**
185
+ * rollback all files
186
+ */
187
+ rollback() {
188
+ return Promise.all(
189
+ [
190
+ ...Array.from(tmpComponents.values()),
191
+ ...Array.from(tmpContentTypes.values()),
192
+ ].map(schema => schema.rollback())
193
+ );
194
+ },
195
+ };
196
+ }