loggable now works with transactions
gitea/salix/dev This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-02-27 10:56:31 +01:00
parent 7aada9e0aa
commit 331b69963f
1 changed files with 32 additions and 16 deletions

View File

@ -12,6 +12,10 @@ module.exports = function(Self) {
});
Self.observe('before save', async function(ctx) {
let options = {};
if (ctx.options && ctx.options.transaction)
options.transaction = ctx.options.transaction;
let oldInstance;
let oldInstanceFk;
let newInstance;
@ -22,7 +26,7 @@ module.exports = function(Self) {
oldInstance = await fkToValue(oldInstanceFk, ctx);
if (ctx.where && !ctx.currentInstance) {
let fields = Object.keys(ctx.data);
ctx.oldInstances = await ctx.Model.app.models[ctx.Model.definition.name].find({where: ctx.where, fields: fields});
ctx.oldInstances = await ctx.Model.app.models[ctx.Model.definition.name].find({where: ctx.where, fields: fields}, options);
}
}
if (ctx.isNewInstance)
@ -33,10 +37,14 @@ module.exports = function(Self) {
});
Self.observe('before delete', async function(ctx) {
let options = {};
if (ctx.options && ctx.options.transaction)
options.transaction = ctx.options.transaction;
if (ctx.where) {
let affectedModel = ctx.Model.definition.name;
let definition = ctx.Model.definition;
let deletedInstances = await ctx.Model.app.models[affectedModel].find({where: ctx.where});
let deletedInstances = await ctx.Model.app.models[affectedModel].find({where: ctx.where}, options);
let relation = definition.settings.log.relation;
if (relation) {
@ -61,6 +69,10 @@ module.exports = function(Self) {
});
async function logDeletedInstances(ctx, loopBackContext) {
let options = {};
if (ctx.options && ctx.options.transaction)
options.transaction = ctx.options.transaction;
ctx.hookState.oldInstance.forEach(async instance => {
let userFk;
if (loopBackContext)
@ -80,16 +92,16 @@ module.exports = function(Self) {
newInstance: {}
};
let transaction = {};
if (ctx.options && ctx.options.transaction)
transaction = ctx.options.transaction;
let logModel = definition.settings.log.model;
await ctx.Model.app.models[logModel].create(logRecord, transaction);
await ctx.Model.app.models[logModel].create(logRecord, options);
});
}
async function fkToValue(instance, ctx) {
let options = {};
if (ctx.options && ctx.options.transaction)
options.transaction = ctx.options.transaction;
let cleanInstance = JSON.parse(JSON.stringify(instance));
let result = {};
for (let key in cleanInstance) {
@ -98,8 +110,11 @@ module.exports = function(Self) {
for (let key1 in ctx.Model.relations) {
let val1 = ctx.Model.relations[key1];
if (val1.keyFrom == key && key != 'id') {
let recordSet = await val1.modelTo.findById(val);
val = recordSet.name; // FIXME preparar todos los modelos con campo name
let recordSet = await ctx.Model.app.models[val1.modelTo.modelName].findById(val, options);
let definition = val1.modelTo.definition;
let changedModelValue = definition.settings.log && definition.settings.log.changedModelValue;
val = (changedModelValue && recordSet && recordSet[changedModelValue]) || (recordSet && recordSet.id) || val; // FIXME preparar todos los modelos con campo name
break;
}
}
@ -109,6 +124,10 @@ module.exports = function(Self) {
}
async function logInModel(ctx, loopBackContext) {
let options = {};
if (ctx.options && ctx.options.transaction)
options.transaction = ctx.options.transaction;
let definition = ctx.Model.definition;
let primaryKey;
for (let property in definition.properties) {
@ -143,10 +162,11 @@ module.exports = function(Self) {
// Sets the changedModelValue to save and the instances changed in case its an updateAll
let changedModelValue = definition.settings.log.changedModelValue;
let where;
if (changedModelValue && (!ctx.instance || !ctx.instance[changedModelValue])) {
var where = [];
changedModelId = [];
let changedInstances = await ctx.Model.app.models[definition.name].find({where: ctx.where, fields: ['id', changedModelValue]});
where = [];
let changedInstances = await ctx.Model.app.models[definition.name].find({where: ctx.where, fields: ['id', changedModelValue]}, options);
changedInstances.forEach(element => {
where.push(element[changedModelValue]);
changedModelId.push(element.id);
@ -185,11 +205,7 @@ module.exports = function(Self) {
let logModel = definition.settings.log.model;
let transaction = {};
if (ctx.options && ctx.options.transaction)
transaction = ctx.options.transaction;
await ctx.Model.app.models[logModel].create(logsToSave, transaction);
await ctx.Model.app.models[logModel].create(logsToSave, options);
}
// this function retuns all the instances changed in case this is an updateAll