Skip to content

Commit

Permalink
sanity-check: this should throw an error in the pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
boazpoolman committed Oct 19, 2023
1 parent 19f5ba1 commit 4442322
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions server/config/type.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -70,11 +69,11 @@ const ConfigType = class ConfigType {
});

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

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

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

// Create entity.
this.relations.map(({ relationName }) => delete query[relationName]);
const newEntity = await queryFallBack.create(this.queryString, {
const newEntity = await strapi.entityService.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 queryFallBack.create(queryString, {
await strapi.entityService.create(queryString, {
data: relationQuery,
});
}));
Expand All @@ -111,7 +110,16 @@ const ConfigType = class ConfigType {

// Update entity.
this.relations.map(({ relationName }) => delete query[relationName]);
const entity = queryFallBack.update(this.queryString, { 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 queryFallBack.create(queryString, {
await strapi.entityService.create(queryString, {
data: { ...config, [parentName]: entity.id },
});
}));
Expand Down

0 comments on commit 4442322

Please sign in to comment.