Modified Strapi Api calls for components
Change Strapi Query Engine Api to Strapi entityService Api. Add parameter "populate" to plugin config. Use import and export with components logic.pull/93/head
parent
f6677886c3
commit
69597c7464
|
@ -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);
|
||||
|
@ -25,6 +25,7 @@ const ConfigType = class ConfigType {
|
|||
this.configPrefix = configName;
|
||||
this.jsonFields = jsonFields || [];
|
||||
this.relations = relations || [];
|
||||
this.populate = populate || [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -111,7 +110,21 @@ const ConfigType = class ConfigType {
|
|||
|
||||
// Update entity.
|
||||
this.relations.map(({ relationName }) => delete query[relationName]);
|
||||
const entity = await queryAPI.update({ where: combinedUidWhereFilter, data: query });
|
||||
|
||||
let entity;
|
||||
switch (this.queryString) {
|
||||
case "strapi::core-store":
|
||||
entity = await queryAPI.update({
|
||||
where: combinedUidWhereFilter,
|
||||
data: query,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
entity = await queryAPI.findOne({ where: combinedUidWhereFilter });
|
||||
await strapi.entityService.update(this.queryString, entity.id, {
|
||||
data: query,
|
||||
});
|
||||
}
|
||||
|
||||
// Delete/create relations.
|
||||
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName, relationSortFields }) => {
|
||||
|
@ -137,7 +150,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 },
|
||||
});
|
||||
}));
|
||||
|
@ -192,7 +205,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 || null,
|
||||
});
|
||||
const configs = {};
|
||||
|
||||
await Promise.all(Object.values(AllConfig).map(async (config) => {
|
||||
|
@ -221,6 +236,25 @@ const ConfigType = class ConfigType {
|
|||
formattedConfig[relationName] = relations;
|
||||
}));
|
||||
|
||||
this.populate
|
||||
.filter((populatedFields) => !populatedFields.includes("."))
|
||||
.map((populatedFields) => {
|
||||
formattedConfig[populatedFields] = formattedConfig[
|
||||
populatedFields
|
||||
].map((fields) => {
|
||||
const sanitizeObjects = (fields) => {
|
||||
sanitizeConfig(fields);
|
||||
Object.keys(fields).map((key, index) => {
|
||||
if (fields[key] && typeof fields[key] === "object") {
|
||||
sanitizeObjects(fields[key]);
|
||||
}
|
||||
});
|
||||
};
|
||||
sanitizeObjects(fields);
|
||||
return fields;
|
||||
});
|
||||
});
|
||||
|
||||
this.jsonFields.map((field) => formattedConfig[field] = JSON.parse(config[field]));
|
||||
configs[`${this.configPrefix}.${combinedUid}`] = formattedConfig;
|
||||
}));
|
||||
|
|
Loading…
Reference in New Issue