Skip to content

Commit

Permalink
Merge pull request #93 from PhilippPaoli/master
Browse files Browse the repository at this point in the history
Feat: Import/export for types with populated components
  • Loading branch information
boazpoolman authored Oct 14, 2023
2 parents a63cfa2 + d3bcb1c commit 4581bf1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,21 @@ This property can accept an array of field names from the type. It is meant to s

> `required:` NO | `type:` array
#### Components

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.
```
customTypes: [{
configName: 'webhook',
queryString: 'webhook',
uid: 'name',
components: ['ParentComponentA', 'ParentComponentA.ChildComponent', 'ParentComponentB']
}],
###### Key: `components`
> `required:` NO | `type:` array
## 🔍 Naming convention
All the config files written in the sync directory have the same naming convention. It goes as follows:
Expand Down
1 change: 1 addition & 0 deletions playground/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ PORT=1337
APP_KEYS=SIwLyqu+IpSHIuUBDQfPZg==,Nzqbq2C3ATsR19u5XEAJQA==,/Agk5Sn8M4EzfoSiIHcDlQ==,gSxT2T0k2zbQatKXUV0zCA==
API_TOKEN_SALT=reQcUBbGXD2KWG2QpRn7DA==
ADMIN_JWT_SECRET= 69mzgwRGfEBUhPEaas8EBA==
TRANSFER_TOKEN_SALT=/LTsSGpC5afHICjZu0oEuQ==
JWT_SECRET=E0TTVdsr+M/FXAjfrNIgXA==
8 changes: 8 additions & 0 deletions playground/config/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ module.exports = ({ env }) => ({
watchIgnoreFiles: [
'**/config/sync/**',
],
apiToken: {
salt: env('API_TOKEN_SALT'),
},
transfer: {
token: {
salt: env('TRANSFER_TOKEN_SALT'),
},
},
});
38 changes: 24 additions & 14 deletions server/config/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { logMessage, sanitizeConfig, dynamicSort, noLimit, getCombinedUid, getCom
const { difference, same } = require('../utils/getArrayDiff');

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

/**
Expand Down Expand Up @@ -68,15 +69,11 @@ const ConfigType = class ConfigType {
});

await Promise.all(relations.map(async (relation) => {
await strapi.query(queryString).delete({
where: { id: relation.id },
});
await strapi.entityService.delete(queryString, relation.id);
}));
}));

await queryAPI.delete({
where: { id: existingConfig.id },
});
await strapi.entityService.delete(this.queryString, existingConfig.id);

return;
}
Expand All @@ -89,15 +86,17 @@ const ConfigType = class ConfigType {

// Create entity.
this.relations.map(({ relationName }) => delete query[relationName]);
const newEntity = await queryAPI.create({ data: query });
const newEntity = await strapi.entityService.create(this.queryString, {
data: query,
});

// Create relation entities.
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName }) => {
const relationQueryApi = strapi.query(queryString);

await Promise.all(configContent[relationName].map(async (relationEntity) => {
const relationQuery = { ...relationEntity, [parentName]: newEntity };
await relationQueryApi.create({ data: relationQuery });
await strapi.entityService.create(queryString, {
data: relationQuery,
});
}));
}));
} else { // Config does exist in DB --> update config in DB
Expand All @@ -111,7 +110,16 @@ const ConfigType = class ConfigType {

// Update entity.
this.relations.map(({ relationName }) => delete query[relationName]);
const entity = await queryAPI.update({ where: combinedUidWhereFilter, data: query });

const entity = await queryAPI.findOne({ where: combinedUidWhereFilter });
try {
await strapi.entityService.update(this.queryString, entity.id, {
data: query,
});
} catch (error) {
console.warn(logMessage(`Use Query Engine API instead of Entity Service API for type ${this.configPrefix}`));
await queryAPI.update({ where: combinedUidWhereFilter, data: query });
}

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

await Promise.all(configToAdd.map(async (config) => {
await relationQueryApi.create({
await strapi.entityService.create(queryString, {
data: { ...config, [parentName]: entity.id },
});
}));
Expand Down Expand Up @@ -192,7 +200,9 @@ const ConfigType = class ConfigType {
* @returns {object} Object with key value pairs of configs.
*/
getAllFromDatabase = async () => {
const AllConfig = await noLimit(strapi.query(this.queryString), {});
const AllConfig = await noLimit(strapi.query(this.queryString), {
populate: this.components,
});
const configs = {};

await Promise.all(Object.values(AllConfig).map(async (config) => {
Expand Down
19 changes: 14 additions & 5 deletions server/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ const dynamicSort = (property) => {
};

const sanitizeConfig = (config, relation, relationSortFields) => {
delete config._id;
delete config.id;
delete config.updatedAt;
delete config.createdAt;

if (relation) {
const formattedRelations = [];

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

const recursiveSanitizeConfig = (recursivedSanitizedConfig) => {
delete recursivedSanitizedConfig._id;
delete recursivedSanitizedConfig.id;
delete recursivedSanitizedConfig.updatedAt;
delete recursivedSanitizedConfig.createdAt;

Object.keys(recursivedSanitizedConfig).map((key, index) => {
if (recursivedSanitizedConfig[key] && typeof recursivedSanitizedConfig[key] === "object") {
recursiveSanitizeConfig(recursivedSanitizedConfig[key]);
}
});
};
recursiveSanitizeConfig(config);

return config;
};

Expand Down

0 comments on commit 4581bf1

Please sign in to comment.