2018-01-29 11:37:54 +00:00
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
/**
|
|
|
|
* Computes the final instance state after hook is executed.
|
|
|
|
*
|
|
|
|
* @param {Object} ctx The hook context
|
|
|
|
* @return {Object} The final instance state
|
|
|
|
*/
|
2018-01-29 11:37:54 +00:00
|
|
|
exports.getFinalState = function(ctx) {
|
|
|
|
if (ctx.isNewInstance)
|
|
|
|
return ctx.instance;
|
2018-11-20 09:32:54 +00:00
|
|
|
if (ctx.currentInstance) {
|
2018-01-29 11:37:54 +00:00
|
|
|
return Object.assign({},
|
|
|
|
ctx.currentInstance.__data,
|
|
|
|
ctx.data || ctx.instance
|
|
|
|
);
|
2018-11-20 09:32:54 +00:00
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
/**
|
|
|
|
* Determines if hook is affecting multiple instances.
|
|
|
|
*
|
|
|
|
* @param {Object} ctx The hook context
|
|
|
|
* @return {Boolean} %true for multiple instances, %false otherwhise
|
|
|
|
*/
|
2018-01-29 11:37:54 +00:00
|
|
|
exports.isMultiple = function(ctx) {
|
|
|
|
return !ctx.isNewInstance && !ctx.currentInstance;
|
|
|
|
};
|
2018-11-20 09:32:54 +00:00
|
|
|
|
|
|
|
/* exports.fkToValue = async function(instance, ctx, model) {
|
|
|
|
let transaction = ctx.options && ctx.options.transaction;
|
|
|
|
let cleanInstance = JSON.parse(JSON.stringify(instance));
|
|
|
|
let result = {};
|
|
|
|
for (let key in cleanInstance) {
|
|
|
|
let val = cleanInstance[key];
|
|
|
|
if (val === undefined || val === null) continue;
|
|
|
|
for (let key1 in model.relations) {
|
|
|
|
let val1 = model.relations[key1];
|
|
|
|
if (val1.keyFrom == key && key != 'id') {
|
|
|
|
let recordSet = await val1.modelTo.findById(val, {}, {transaction});
|
|
|
|
val = recordSet.name || recordSet.id; // FIXME preparar todos los modelos con campo name
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result[key] = val;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
*/
|