2020-09-07 15:04:20 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
2021-03-25 14:59:44 +01:00
|
|
|
const getKeyForDefinition = definition => `model_def_${definition.uid}`;
|
|
|
|
|
2020-09-07 15:04:20 +02:00
|
|
|
const formatDefinitionToStore = definition =>
|
|
|
|
JSON.stringify(
|
2021-03-24 12:14:49 +01:00
|
|
|
_.pick(definition, [
|
|
|
|
'uid',
|
|
|
|
'collectionName',
|
|
|
|
'kind',
|
|
|
|
'info',
|
|
|
|
'options',
|
|
|
|
'pluginOptions',
|
|
|
|
'attributes',
|
|
|
|
])
|
2020-09-07 15:04:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Using MongoDB instead of Mongoose since this function
|
|
|
|
// may be called before the model 'core_store' is instanciated
|
2021-03-25 14:59:44 +01:00
|
|
|
const getDefinitionFromStore = async (definition, ORM) => {
|
|
|
|
const rawDefinition = await ORM.connection.db
|
|
|
|
.collection('core_store')
|
|
|
|
.findOne({ key: getKeyForDefinition(definition) });
|
|
|
|
|
|
|
|
return JSON.parse(_.get(rawDefinition, 'value', null));
|
|
|
|
};
|
2020-09-07 15:04:20 +02:00
|
|
|
|
|
|
|
// Using MongoDB instead of Mongoose since this function
|
|
|
|
// may be called before the model 'core_store' is instanciated
|
|
|
|
const storeDefinition = async (definition, ORM) => {
|
|
|
|
const defToStore = formatDefinitionToStore(definition);
|
|
|
|
|
|
|
|
await ORM.connection.db.collection('core_store').updateOne(
|
|
|
|
{
|
2021-03-25 14:59:44 +01:00
|
|
|
key: getKeyForDefinition(definition),
|
2020-09-07 15:04:20 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
2021-03-25 14:59:44 +01:00
|
|
|
key: getKeyForDefinition(definition),
|
2020-09-07 15:04:20 +02:00
|
|
|
type: 'object',
|
|
|
|
value: defToStore,
|
|
|
|
environment: '',
|
|
|
|
tag: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
upsert: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const didDefinitionChange = async (definition, ORM) => {
|
2021-03-25 14:59:44 +01:00
|
|
|
const previousDefJSON = await getDefinitionFromStore(definition, ORM);
|
|
|
|
|
|
|
|
const previousDef = formatDefinitionToStore(previousDefJSON);
|
|
|
|
const actualDef = formatDefinitionToStore(definition);
|
2020-09-07 15:04:20 +02:00
|
|
|
|
2021-03-25 14:59:44 +01:00
|
|
|
return previousDef !== actualDef;
|
2020-09-07 15:04:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
didDefinitionChange,
|
|
|
|
storeDefinition,
|
|
|
|
getDefinitionFromStore,
|
|
|
|
};
|