Skip to content

Commit 4581bf1

Browse files
authored
Merge pull request #93 from PhilippPaoli/master
Feat: Import/export for types with populated components
2 parents a63cfa2 + d3bcb1c commit 4581bf1

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,21 @@ This property can accept an array of field names from the type. It is meant to s
354354

355355
> `required:` NO | `type:` array
356356
357+
#### Components
358+
359+
This property can accept an array of component names from the type. Strapi Components can be included in the export/import process. With "." nested components can also be included in the process.
360+
```
361+
customTypes: [{
362+
configName: 'webhook',
363+
queryString: 'webhook',
364+
uid: 'name',
365+
components: ['ParentComponentA', 'ParentComponentA.ChildComponent', 'ParentComponentB']
366+
}],
367+
368+
###### Key: `components`
369+
370+
> `required:` NO | `type:` array
371+
357372
358373
## 🔍 Naming convention
359374
All the config files written in the sync directory have the same naming convention. It goes as follows:

playground/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ PORT=1337
33
APP_KEYS=SIwLyqu+IpSHIuUBDQfPZg==,Nzqbq2C3ATsR19u5XEAJQA==,/Agk5Sn8M4EzfoSiIHcDlQ==,gSxT2T0k2zbQatKXUV0zCA==
44
API_TOKEN_SALT=reQcUBbGXD2KWG2QpRn7DA==
55
ADMIN_JWT_SECRET= 69mzgwRGfEBUhPEaas8EBA==
6+
TRANSFER_TOKEN_SALT=/LTsSGpC5afHICjZu0oEuQ==
67
JWT_SECRET=E0TTVdsr+M/FXAjfrNIgXA==

playground/config/admin.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,12 @@ module.exports = ({ env }) => ({
55
watchIgnoreFiles: [
66
'**/config/sync/**',
77
],
8+
apiToken: {
9+
salt: env('API_TOKEN_SALT'),
10+
},
11+
transfer: {
12+
token: {
13+
salt: env('TRANSFER_TOKEN_SALT'),
14+
},
15+
},
816
});

server/config/type.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { logMessage, sanitizeConfig, dynamicSort, noLimit, getCombinedUid, getCom
33
const { difference, same } = require('../utils/getArrayDiff');
44

55
const ConfigType = class ConfigType {
6-
constructor({ queryString, configName, uid, jsonFields, relations }) {
6+
constructor({ queryString, configName, uid, jsonFields, relations, components }) {
77
if (!configName) {
88
strapi.log.error(logMessage('A config type was registered without a config name.'));
99
process.exit(0);
@@ -25,6 +25,7 @@ const ConfigType = class ConfigType {
2525
this.configPrefix = configName;
2626
this.jsonFields = jsonFields || [];
2727
this.relations = relations || [];
28+
this.components = components || null;
2829
}
2930

3031
/**
@@ -68,15 +69,11 @@ const ConfigType = class ConfigType {
6869
});
6970

7071
await Promise.all(relations.map(async (relation) => {
71-
await strapi.query(queryString).delete({
72-
where: { id: relation.id },
73-
});
72+
await strapi.entityService.delete(queryString, relation.id);
7473
}));
7574
}));
7675

77-
await queryAPI.delete({
78-
where: { id: existingConfig.id },
79-
});
76+
await strapi.entityService.delete(this.queryString, existingConfig.id);
8077

8178
return;
8279
}
@@ -89,15 +86,17 @@ const ConfigType = class ConfigType {
8986

9087
// Create entity.
9188
this.relations.map(({ relationName }) => delete query[relationName]);
92-
const newEntity = await queryAPI.create({ data: query });
89+
const newEntity = await strapi.entityService.create(this.queryString, {
90+
data: query,
91+
});
9392

9493
// Create relation entities.
9594
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName }) => {
96-
const relationQueryApi = strapi.query(queryString);
97-
9895
await Promise.all(configContent[relationName].map(async (relationEntity) => {
9996
const relationQuery = { ...relationEntity, [parentName]: newEntity };
100-
await relationQueryApi.create({ data: relationQuery });
97+
await strapi.entityService.create(queryString, {
98+
data: relationQuery,
99+
});
101100
}));
102101
}));
103102
} else { // Config does exist in DB --> update config in DB
@@ -111,7 +110,16 @@ const ConfigType = class ConfigType {
111110

112111
// Update entity.
113112
this.relations.map(({ relationName }) => delete query[relationName]);
114-
const entity = await queryAPI.update({ where: combinedUidWhereFilter, data: query });
113+
114+
const entity = await queryAPI.findOne({ where: combinedUidWhereFilter });
115+
try {
116+
await strapi.entityService.update(this.queryString, entity.id, {
117+
data: query,
118+
});
119+
} catch (error) {
120+
console.warn(logMessage(`Use Query Engine API instead of Entity Service API for type ${this.configPrefix}`));
121+
await queryAPI.update({ where: combinedUidWhereFilter, data: query });
122+
}
115123

116124
// Delete/create relations.
117125
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName, relationSortFields }) => {
@@ -137,7 +145,7 @@ const ConfigType = class ConfigType {
137145
}));
138146

139147
await Promise.all(configToAdd.map(async (config) => {
140-
await relationQueryApi.create({
148+
await strapi.entityService.create(queryString, {
141149
data: { ...config, [parentName]: entity.id },
142150
});
143151
}));
@@ -192,7 +200,9 @@ const ConfigType = class ConfigType {
192200
* @returns {object} Object with key value pairs of configs.
193201
*/
194202
getAllFromDatabase = async () => {
195-
const AllConfig = await noLimit(strapi.query(this.queryString), {});
203+
const AllConfig = await noLimit(strapi.query(this.queryString), {
204+
populate: this.components,
205+
});
196206
const configs = {};
197207

198208
await Promise.all(Object.values(AllConfig).map(async (config) => {

server/utils/index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ const dynamicSort = (property) => {
4747
};
4848

4949
const sanitizeConfig = (config, relation, relationSortFields) => {
50-
delete config._id;
51-
delete config.id;
52-
delete config.updatedAt;
53-
delete config.createdAt;
54-
5550
if (relation) {
5651
const formattedRelations = [];
5752

@@ -74,6 +69,20 @@ const sanitizeConfig = (config, relation, relationSortFields) => {
7469
config[relation] = formattedRelations;
7570
}
7671

72+
const recursiveSanitizeConfig = (recursivedSanitizedConfig) => {
73+
delete recursivedSanitizedConfig._id;
74+
delete recursivedSanitizedConfig.id;
75+
delete recursivedSanitizedConfig.updatedAt;
76+
delete recursivedSanitizedConfig.createdAt;
77+
78+
Object.keys(recursivedSanitizedConfig).map((key, index) => {
79+
if (recursivedSanitizedConfig[key] && typeof recursivedSanitizedConfig[key] === "object") {
80+
recursiveSanitizeConfig(recursivedSanitizedConfig[key]);
81+
}
82+
});
83+
};
84+
recursiveSanitizeConfig(config);
85+
7786
return config;
7887
};
7988

0 commit comments

Comments
 (0)