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 2 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
52 changes: 38 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, populate }) {
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.populate = populate || 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(this.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,17 @@ 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(error);
console.log("Use Query Engine API instead of Entity Service API");
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 +146,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 +201,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.populate,
});
const configs = {};

await Promise.all(Object.values(AllConfig).map(async (config) => {
Expand Down Expand Up @@ -221,6 +232,19 @@ const ConfigType = class ConfigType {
formattedConfig[relationName] = relations;
}));

if (Array.isArray(this.populate)) {
this.populate
.filter((populatedFields) => !populatedFields.includes("."))
.map((populatedFields) => {
formattedConfig[populatedFields] = formattedConfig[
populatedFields
].map((fields) => {
sanitizeConfig(fields);
return fields;
});
});
}

this.jsonFields.map((field) => formattedConfig[field] = JSON.parse(config[field]));
configs[`${this.configPrefix}.${combinedUid}`] = formattedConfig;
}));
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 sanitizeRecursive = (config) => {
delete config._id;
delete config.id;
delete config.updatedAt;
delete config.createdAt;

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

return config;
};

Expand Down