Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Import/export for types with populated components #93

Merged
merged 17 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,30 @@ If you do not have a single unique value, you can also pass in a array of keys f

> `required:` YES | `type:` string | string[]

#### Relations

The relations array specifies the relations you want to include in the sync process.
This feature is used to sync the relations between `roles` and `permissions`. See https://github.com/boazpoolman/strapi-plugin-config-sync/blob/master/server/config/types.js#L16.

Example:
```
{
configName: 'admin-role',
queryString: 'admin::role',
uid: 'code',
relations: [{
queryString: 'admin::permission',
relationName: 'permissions',
parentName: 'role',
relationSortFields: ['action', 'subject'],
}],
},
```

###### Key: `relations`

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

#### JSON fields

This property can accept an array of field names from the type. It is meant to specify the JSON fields on the type so the plugin can better format the field values when calculating the config difference.
Expand All @@ -330,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'),
},
},
});
34 changes: 23 additions & 11 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 @@ -74,9 +75,7 @@ const ConfigType = class ConfigType {
}));
}));

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

return;
}
Expand All @@ -89,15 +88,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 +112,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 +147,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 +202,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
Loading