strapi-plugin-config-sync/server/services/type.js

138 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-10-15 14:54:58 +02:00
const { logMessage, sanitizeConfig } = require('../utils');
2021-10-14 16:37:32 +02:00
const difference = require('../utils/getObjectDiff');
const ConfigType = class ConfigType {
2021-10-27 13:45:55 +02:00
constructor(queryString, configPrefix, uid, jsonFields, relations) {
2021-10-14 16:37:32 +02:00
if (!queryString) {
strapi.log.error(logMessage('Query string is missing for ConfigType'));
}
this.queryString = queryString;
this.configPrefix = configPrefix;
this.uid = uid;
2021-10-15 14:54:58 +02:00
this.jsonFields = jsonFields || [];
2021-10-27 13:45:55 +02:00
this.relations = relations || {};
2021-10-14 16:37:32 +02:00
}
/**
* Export all core-store config to files.
*
* @returns {void}
*/
exportAll = async () => {
const formattedDiff = {
fileConfig: {},
databaseConfig: {},
diff: {},
};
const fileConfig = await strapi.plugin('config-sync').service('main').getAllConfigFromFiles(this.configPrefix);
const databaseConfig = await strapi.plugin('config-sync').service('main').getAllConfigFromDatabase(this.configPrefix);
const diff = difference(databaseConfig, fileConfig);
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]) => {
// Check if the config should be excluded.
const shouldExclude = strapi.config.get('plugin.config-sync.exclude').includes(`${configName}`);
if (shouldExclude) return;
const currentConfig = formattedDiff.databaseConfig[configName];
if (
!currentConfig
&& formattedDiff.fileConfig[configName]
) {
await strapi.plugin('config-sync').service('main').deleteConfigFile(configName);
} else {
2021-10-15 14:54:58 +02:00
await strapi.plugin('config-sync').service('main').writeConfigFile(this.configPrefix, currentConfig[this.uid], currentConfig);
2021-10-14 16:37:32 +02: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) => {
// Check if the config should be excluded.
const shouldExclude = strapi.config.get('plugin.config-sync.exclude').includes(`${this.configPrefix}.${configName}`);
if (shouldExclude) return;
const queryAPI = strapi.query(this.queryString);
const configExists = await queryAPI
2021-10-15 14:54:58 +02:00
.findOne({ where: { [this.uid]: configName } });
2021-10-14 16:37:32 +02:00
if (configExists && configContent === null) {
await queryAPI.delete({ where: { [this.uid]: configName } });
return;
}
if (!configExists) {
const query = { ...configContent };
2021-10-15 14:54:58 +02:00
this.jsonFields.map((field) => query[field] = JSON.stringify(configContent[field]));
await queryAPI.create({ data: query });
2021-10-14 16:37:32 +02:00
} else {
const query = { ...configContent };
2021-10-15 14:54:58 +02:00
this.jsonFields.map((field) => query[field] = JSON.stringify(configContent[field]));
2021-10-14 16:37:32 +02:00
await queryAPI.update({ where: { [this.uid]: configName }, data: { ...query } });
}
}
/**
* Get all core-store config from the db.
*
* @returns {object} Object with key value pairs of configs.
*/
getAllFromDatabase = async () => {
const AllConfig = await strapi.query(this.queryString).findMany({ _limit: -1 });
const configs = {};
Object.values(AllConfig).map((config) => {
// Check if the config should be excluded.
const shouldExclude = strapi.config.get('plugin.config-sync.exclude').includes(`${this.configPrefix}.${config[this.uid]}`);
if (shouldExclude) return;
2021-10-15 14:54:58 +02:00
config = sanitizeConfig(config);
2021-10-14 16:37:32 +02:00
const formattedObject = { ...config };
2021-10-15 14:54:58 +02:00
this.jsonFields.map((field) => formattedObject[field] = JSON.parse(config[field]));
2021-10-14 16:37:32 +02:00
configs[`${this.configPrefix}.${config[this.uid]}`] = formattedObject;
});
return configs;
}
/**
* 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.plugin('config-sync').service('main').importAllConfig(this.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.
}
};
module.exports = ConfigType;