11const { isEmpty } = require ( 'lodash' ) ;
2- const { logMessage, sanitizeConfig, dynamicSort, noLimit } = require ( '../utils' ) ;
2+ const { logMessage, sanitizeConfig, dynamicSort, noLimit, getCombinedUid , getCombinedUidWhereFilter , getUidParamsFromName } = require ( '../utils' ) ;
33const difference = require ( '../utils/getArrayDiff' ) ;
44
55const ConfigType = class ConfigType {
@@ -12,13 +12,17 @@ const ConfigType = class ConfigType {
1212 strapi . log . error ( logMessage ( `No query string found for the '${ configName } ' config type.` ) ) ;
1313 process . exit ( 0 ) ;
1414 }
15- if ( ! uid ) {
16- strapi . log . error ( logMessage ( `No uid found for the '${ configName } ' config type.` ) ) ;
15+ // uid could be a single key or an array for a combined uid. So the type of uid is either string or string[]
16+ if ( typeof uid === "string" ) {
17+ this . uidKeys = [ uid ] ;
18+ } else if ( Array . isArray ( uid ) ) {
19+ this . uidKeys = uid . sort ( ) ;
20+ } else {
21+ strapi . log . error ( logMessage ( `Wrong uid config for the '${ configName } ' config type.` ) ) ;
1722 process . exit ( 0 ) ;
1823 }
1924 this . queryString = queryString ;
2025 this . configPrefix = configName ;
21- this . uid = uid ;
2226 this . jsonFields = jsonFields || [ ] ;
2327 this . relations = relations || [ ] ;
2428 }
@@ -30,29 +34,25 @@ const ConfigType = class ConfigType {
3034 * @param {string } configContent - The JSON content of the config file.
3135 * @returns {void }
3236 */
33- importSingle = async ( configName , configContent ) => {
37+ importSingle = async ( configName , configContent ) => {
3438 // Check if the config should be excluded.
3539 const shouldExclude = ! isEmpty ( strapi . config . get ( 'plugin.config-sync.excludedConfig' ) . filter ( ( option ) => `${ this . configPrefix } .${ configName } ` . startsWith ( option ) ) ) ;
3640 if ( shouldExclude ) return ;
3741
3842 const queryAPI = strapi . query ( this . queryString ) ;
39-
43+ const uidParams = getUidParamsFromName ( this . uidKeys , configName ) ;
44+ const combinedUidWhereFilter = getCombinedUidWhereFilter ( this . uidKeys , uidParams ) ;
4045 let existingConfig = await queryAPI
4146 . findOne ( {
42- where : { [ this . uid ] : configName } ,
43- populate : this . relations . map ( ( { relationName } ) => relationName ) ,
44- } ) ;
45-
46- if ( existingConfig && configContent === null ) {
47- const entity = await queryAPI . findOne ( {
48- where : { [ this . uid ] : configName } ,
47+ where : combinedUidWhereFilter ,
4948 populate : this . relations . map ( ( { relationName } ) => relationName ) ,
5049 } ) ;
5150
51+ if ( existingConfig && configContent === null ) { // Config exists in DB but no configfile content --> delete config from DB
5252 await Promise . all ( this . relations . map ( async ( { queryString, parentName } ) => {
5353 const relations = await noLimit ( strapi . query ( queryString ) , {
5454 where : {
55- [ parentName ] : entity . id ,
55+ [ parentName ] : existingConfig . id ,
5656 } ,
5757 } ) ;
5858
@@ -64,13 +64,13 @@ const ConfigType = class ConfigType {
6464 } ) ) ;
6565
6666 await queryAPI . delete ( {
67- where : { id : entity . id } ,
67+ where : { id : existingConfig . id } ,
6868 } ) ;
6969
7070 return ;
7171 }
7272
73- if ( ! existingConfig ) {
73+ if ( ! existingConfig ) { // Config does not exist in DB --> create config in DB
7474 // Format JSON fields.
7575 const query = { ...configContent } ;
7676 this . jsonFields . map ( ( field ) => query [ field ] = JSON . stringify ( configContent [ field ] ) ) ;
@@ -88,15 +88,15 @@ const ConfigType = class ConfigType {
8888 await relationQueryApi . create ( { data : relationQuery } ) ;
8989 } ) ) ;
9090 } ) ) ;
91- } else {
91+ } else { // Config does exist in DB --> update config in DB
9292 // Format JSON fields.
9393 configContent = sanitizeConfig ( configContent ) ;
9494 const query = { ...configContent } ;
9595 this . jsonFields . map ( ( field ) => query [ field ] = JSON . stringify ( configContent [ field ] ) ) ;
9696
9797 // Update entity.
9898 this . relations . map ( ( { relationName } ) => delete query [ relationName ] ) ;
99- const entity = await queryAPI . update ( { where : { [ this . uid ] : configName } , data : query } ) ;
99+ const entity = await queryAPI . update ( { where : combinedUidWhereFilter , data : query } ) ;
100100
101101 // Delete/create relations.
102102 await Promise . all ( this . relations . map ( async ( { queryString, relationName, parentName, relationSortField } ) => {
@@ -146,7 +146,8 @@ const ConfigType = class ConfigType {
146146 ) {
147147 await strapi . plugin ( 'config-sync' ) . service ( 'main' ) . deleteConfigFile ( configName ) ;
148148 } else {
149- await strapi . plugin ( 'config-sync' ) . service ( 'main' ) . writeConfigFile ( this . configPrefix , currentConfig [ this . uid ] , currentConfig ) ;
149+ const combinedUid = getCombinedUid ( this . uidKeys , currentConfig ) ;
150+ await strapi . plugin ( 'config-sync' ) . service ( 'main' ) . writeConfigFile ( this . configPrefix , combinedUid , currentConfig ) ;
150151 }
151152 }
152153
@@ -160,14 +161,16 @@ const ConfigType = class ConfigType {
160161 const configs = { } ;
161162
162163 await Promise . all ( Object . values ( AllConfig ) . map ( async ( config ) => {
164+ const combinedUid = getCombinedUid ( this . uidKeys , config ) ;
165+ const combinedUidWhereFilter = getCombinedUidWhereFilter ( this . uidKeys , config ) ;
163166 // Check if the config should be excluded.
164- const shouldExclude = ! isEmpty ( strapi . config . get ( 'plugin.config-sync.excludedConfig' ) . filter ( ( option ) => `${ this . configPrefix } .${ config [ this . uid ] } ` . startsWith ( option ) ) ) ;
167+ const shouldExclude = ! isEmpty ( strapi . config . get ( 'plugin.config-sync.excludedConfig' ) . filter ( ( option ) => `${ this . configPrefix } .${ combinedUid } ` . startsWith ( option ) ) ) ;
165168 if ( shouldExclude ) return ;
166169
167170 const formattedConfig = { ...sanitizeConfig ( config ) } ;
168171 await Promise . all ( this . relations . map ( async ( { queryString, relationName, relationSortField, parentName } ) => {
169172 const relations = await noLimit ( strapi . query ( queryString ) , {
170- where : { [ parentName ] : { [ this . uid ] : config [ this . uid ] } } ,
173+ where : { [ parentName ] : combinedUidWhereFilter } ,
171174 } ) ;
172175
173176 relations . map ( ( relation ) => sanitizeConfig ( relation ) ) ;
@@ -176,7 +179,7 @@ const ConfigType = class ConfigType {
176179 } ) ) ;
177180
178181 this . jsonFields . map ( ( field ) => formattedConfig [ field ] = JSON . parse ( config [ field ] ) ) ;
179- configs [ `${ this . configPrefix } .${ config [ this . uid ] } ` ] = formattedConfig ;
182+ configs [ `${ this . configPrefix } .${ combinedUid } ` ] = formattedConfig ;
180183 } ) ) ;
181184
182185
0 commit comments