2021-03-24 16:22:03 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const coreStoreQueryString = 'core_store';
|
|
|
|
const configPrefix = 'core-store'; // Should be the same as the filename.
|
2021-03-27 18:20:51 +01:00
|
|
|
const difference = require('../utils/getObjectDiff');
|
2021-03-24 16:22:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Import/Export for core-store configs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
/**
|
|
|
|
* Export all core-store config to files.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
exportAll: async () => {
|
2021-03-27 18:20:51 +01:00
|
|
|
const formattedDiff = {
|
|
|
|
fileConfig: {},
|
|
|
|
databaseConfig: {},
|
|
|
|
diff: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles(configPrefix);
|
|
|
|
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase(configPrefix);
|
|
|
|
const diff = difference(databaseConfig, fileConfig);
|
2021-03-24 16:22:03 +01:00
|
|
|
|
2021-03-27 18:20:51 +01:00
|
|
|
formattedDiff.diff = diff;
|
|
|
|
|
|
|
|
Object.keys(diff).map((changedConfigName) => {
|
|
|
|
formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
|
|
|
|
formattedDiff.databaseConfig[changedConfigName] = databaseConfig[changedConfigName];
|
|
|
|
})
|
|
|
|
|
|
|
|
await Promise.all(Object.entries(diff).map(async ([configName, config]) => {
|
2021-03-27 00:29:21 +01:00
|
|
|
// Check if the config should be excluded.
|
2021-03-27 18:20:51 +01:00
|
|
|
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configName}`);
|
2021-03-27 00:29:21 +01:00
|
|
|
if (shouldExclude) return;
|
|
|
|
|
2021-03-27 18:20:51 +01:00
|
|
|
const currentConfig = formattedDiff.databaseConfig[configName];
|
|
|
|
|
|
|
|
if (
|
|
|
|
!currentConfig &&
|
|
|
|
formattedDiff.fileConfig[configName]
|
|
|
|
) {
|
|
|
|
await strapi.plugins['config-sync'].services.main.deleteConfigFile(configName);
|
|
|
|
} else {
|
2021-05-12 17:24:42 +02:00
|
|
|
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, currentConfig.key.replace('::', '##'), currentConfig);
|
2021-03-27 18:20:51 +01:00
|
|
|
}
|
2021-03-24 16:22:03 +01:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import a single core-store config file into the db.
|
|
|
|
*
|
|
|
|
* @param {string} configName - The name of the config file.
|
|
|
|
* @param {string} configContent - The JSON content of the config file.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
importSingle: async (configName, configContent) => {
|
2021-03-27 00:29:21 +01:00
|
|
|
// Check if the config should be excluded.
|
|
|
|
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${configName}`);
|
|
|
|
if (shouldExclude) return;
|
|
|
|
|
2021-03-24 16:22:03 +01:00
|
|
|
const coreStoreAPI = strapi.query(coreStoreQueryString);
|
|
|
|
|
|
|
|
const configExists = await coreStoreAPI
|
2021-03-27 17:12:40 +01:00
|
|
|
.findOne({ key: configName });
|
|
|
|
|
|
|
|
if (configExists && configContent === null) {
|
|
|
|
await coreStoreAPI.delete({ key: configName });
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { value, ...fileContent } = configContent;
|
2021-03-24 16:22:03 +01:00
|
|
|
|
|
|
|
if (!configExists) {
|
|
|
|
await coreStoreAPI.create({ value: JSON.stringify(value), ...fileContent });
|
|
|
|
} else {
|
|
|
|
await coreStoreAPI.update({ key: configName }, { value: JSON.stringify(value), ...fileContent });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-03-24 18:36:16 +01:00
|
|
|
/**
|
|
|
|
* Get all core-store config from the db.
|
|
|
|
*
|
|
|
|
* @returns {object} Object with key value pairs of configs.
|
|
|
|
*/
|
|
|
|
getAllFromDatabase: async () => {
|
|
|
|
const coreStore = await strapi.query(coreStoreQueryString).find({ _limit: -1 });
|
|
|
|
let configs = {};
|
|
|
|
|
2021-03-25 21:28:59 +01:00
|
|
|
Object.values(coreStore).map( ({ id, value, key, ...config }) => {
|
2021-03-27 00:29:21 +01:00
|
|
|
// Check if the config should be excluded.
|
|
|
|
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${key}`);
|
|
|
|
if (shouldExclude) return;
|
|
|
|
|
2021-05-05 17:07:35 +02:00
|
|
|
// Do not export the _id field, as it is immutable
|
|
|
|
delete config._id;
|
|
|
|
|
2021-03-25 21:28:59 +01:00
|
|
|
configs[`${configPrefix}.${key}`] = { key, value: JSON.parse(value), ...config };
|
2021-03-24 18:36:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return configs;
|
|
|
|
},
|
|
|
|
|
2021-03-24 16:22:03 +01:00
|
|
|
/**
|
|
|
|
* Import all core-store config files into the db.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
importAll: async () => {
|
|
|
|
// The main.importAllConfig service will loop the core-store.importSingle service.
|
|
|
|
await strapi.plugins['config-sync'].services.main.importAllConfig(configPrefix);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a single core-store config to a file.
|
|
|
|
*
|
|
|
|
* @param {string} configName - The name of the config file.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
exportSingle: async (configName) => {
|
|
|
|
// @TODO: write export for a single core-store config.
|
|
|
|
},
|
|
|
|
};
|