Skip to content

Commit

Permalink
fix: Add fallback for entitySerivce
Browse files Browse the repository at this point in the history
  • Loading branch information
boazpoolman committed Oct 19, 2023
1 parent 1acf0ee commit 81f22c6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
22 changes: 7 additions & 15 deletions server/config/type.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { isEmpty } = require('lodash');
const { logMessage, sanitizeConfig, dynamicSort, noLimit, getCombinedUid, getCombinedUidWhereFilter, getUidParamsFromName } = require('../utils');
const { difference, same } = require('../utils/getArrayDiff');
const queryFallBack = require('../utils/queryFallBack');

const ConfigType = class ConfigType {
constructor({ queryString, configName, uid, jsonFields, relations, components }) {
Expand Down Expand Up @@ -69,11 +70,11 @@ const ConfigType = class ConfigType {
});

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

await strapi.entityService.delete(this.queryString, existingConfig.id);
await queryFallBack.delete(this.queryString, existingConfig.id);

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

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

// Create relation entities.
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName }) => {
await Promise.all(configContent[relationName].map(async (relationEntity) => {
const relationQuery = { ...relationEntity, [parentName]: newEntity };
await strapi.entityService.create(queryString, {
await queryFallBack.create(queryString, {
data: relationQuery,
});
}));
Expand All @@ -110,16 +111,7 @@ const ConfigType = class ConfigType {

// Update entity.
this.relations.map(({ relationName }) => delete query[relationName]);

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 });
}
const entity = queryFallBack.update(this.queryString, { where: combinedUidWhereFilter, data: query });

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

await Promise.all(configToAdd.map(async (config) => {
await strapi.entityService.create(queryString, {
await queryFallBack.create(queryString, {
data: { ...config, [parentName]: entity.id },
});
}));
Expand Down
28 changes: 28 additions & 0 deletions server/utils/queryFallBack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const queryFallBack = {
create: async (queryString, options) => {
try {
return strapi.entityService.create(queryString, options);
} catch (e) {
return strapi.query(queryString).create(options);
}
},
update: async (queryString, options) => {
try {
const entity = await strapi.query(queryString).findOne(options.where);
return strapi.entityService.update(queryString, entity.id);
} catch (e) {
return strapi.query(queryString).update(options);
}
},
delete: async (queryString, id) => {
try {
await strapi.entityService.delete(queryString, id);
} catch (e) {
await strapi.query(queryString).delete({
where: { id },
});
}
},
};

module.exports = queryFallBack;

0 comments on commit 81f22c6

Please sign in to comment.