@@ -3,7 +3,7 @@ const { logMessage, sanitizeConfig, dynamicSort, noLimit, getCombinedUid, getCom
33const { difference, same } = require ( '../utils/getArrayDiff' ) ;
44
55const ConfigType = class ConfigType {
6- constructor ( { queryString, configName, uid, jsonFields, relations } ) {
6+ constructor ( { queryString, configName, uid, jsonFields, relations, components } ) {
77 if ( ! configName ) {
88 strapi . log . error ( logMessage ( 'A config type was registered without a config name.' ) ) ;
99 process . exit ( 0 ) ;
@@ -25,6 +25,7 @@ const ConfigType = class ConfigType {
2525 this . configPrefix = configName ;
2626 this . jsonFields = jsonFields || [ ] ;
2727 this . relations = relations || [ ] ;
28+ this . components = components || null ;
2829 }
2930
3031 /**
@@ -68,15 +69,11 @@ const ConfigType = class ConfigType {
6869 } ) ;
6970
7071 await Promise . all ( relations . map ( async ( relation ) => {
71- await strapi . query ( queryString ) . delete ( {
72- where : { id : relation . id } ,
73- } ) ;
72+ await strapi . entityService . delete ( queryString , relation . id ) ;
7473 } ) ) ;
7574 } ) ) ;
7675
77- await queryAPI . delete ( {
78- where : { id : existingConfig . id } ,
79- } ) ;
76+ await strapi . entityService . delete ( this . queryString , existingConfig . id ) ;
8077
8178 return ;
8279 }
@@ -89,15 +86,17 @@ const ConfigType = class ConfigType {
8986
9087 // Create entity.
9188 this . relations . map ( ( { relationName } ) => delete query [ relationName ] ) ;
92- const newEntity = await queryAPI . create ( { data : query } ) ;
89+ const newEntity = await strapi . entityService . create ( this . queryString , {
90+ data : query ,
91+ } ) ;
9392
9493 // Create relation entities.
9594 await Promise . all ( this . relations . map ( async ( { queryString, relationName, parentName } ) => {
96- const relationQueryApi = strapi . query ( queryString ) ;
97-
9895 await Promise . all ( configContent [ relationName ] . map ( async ( relationEntity ) => {
9996 const relationQuery = { ...relationEntity , [ parentName ] : newEntity } ;
100- await relationQueryApi . create ( { data : relationQuery } ) ;
97+ await strapi . entityService . create ( queryString , {
98+ data : relationQuery ,
99+ } ) ;
101100 } ) ) ;
102101 } ) ) ;
103102 } else { // Config does exist in DB --> update config in DB
@@ -111,7 +110,16 @@ const ConfigType = class ConfigType {
111110
112111 // Update entity.
113112 this . relations . map ( ( { relationName } ) => delete query [ relationName ] ) ;
114- const entity = await queryAPI . update ( { where : combinedUidWhereFilter , data : query } ) ;
113+
114+ const entity = await queryAPI . findOne ( { where : combinedUidWhereFilter } ) ;
115+ try {
116+ await strapi . entityService . update ( this . queryString , entity . id , {
117+ data : query ,
118+ } ) ;
119+ } catch ( error ) {
120+ console . warn ( logMessage ( `Use Query Engine API instead of Entity Service API for type ${ this . configPrefix } ` ) ) ;
121+ await queryAPI . update ( { where : combinedUidWhereFilter , data : query } ) ;
122+ }
115123
116124 // Delete/create relations.
117125 await Promise . all ( this . relations . map ( async ( { queryString, relationName, parentName, relationSortFields } ) => {
@@ -137,7 +145,7 @@ const ConfigType = class ConfigType {
137145 } ) ) ;
138146
139147 await Promise . all ( configToAdd . map ( async ( config ) => {
140- await relationQueryApi . create ( {
148+ await strapi . entityService . create ( queryString , {
141149 data : { ...config , [ parentName ] : entity . id } ,
142150 } ) ;
143151 } ) ) ;
@@ -192,7 +200,9 @@ const ConfigType = class ConfigType {
192200 * @returns {object } Object with key value pairs of configs.
193201 */
194202 getAllFromDatabase = async ( ) => {
195- const AllConfig = await noLimit ( strapi . query ( this . queryString ) , { } ) ;
203+ const AllConfig = await noLimit ( strapi . query ( this . queryString ) , {
204+ populate : this . components ,
205+ } ) ;
196206 const configs = { } ;
197207
198208 await Promise . all ( Object . values ( AllConfig ) . map ( async ( config ) => {
0 commit comments