Skip to content

Commit

Permalink
Merge pull request #13 from ks-labs/feat-save-only-different-values
Browse files Browse the repository at this point in the history
✨ feat: saving only the values ​​that have changed
  • Loading branch information
vinicioslc authored Oct 17, 2023
2 parents c6eec56 + 7aa6c4a commit 8971d2c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
19 changes: 13 additions & 6 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
module.exports = {
env: {
'env': {
node: true,
},
globals: {
'globals': {
use: true,
},
extends: ['semistandard', 'prettier'],
plugins: ['prettier'],
parserOptions: {
'extends': ['semistandard', 'prettier'],
'plugins': ['prettier'],
'parserOptions': {
ecmaVersion: 11,
},
'prettier/prettier': [
'error',
{
singleQuote: true,
parser: 'flow',
},
],
// "off" or 0 - turn the rule off
// "warn" or 1 - turn the rule on as a warning (doesn't affect exit code)
// "error" or 2 - turn the rule on as an error (exit code is 1 when triggered)
rules: {
'rules': {
'no-unused-vars': 0,
'prettier/prettier': ['error'],
},
Expand Down
27 changes: 27 additions & 0 deletions src/Traits/Auditable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ const { AuditableInvalidRelationshipError } = require('../Exceptions')
/** @type {typeof import('../../templates/Audit')} */
const Audit = use('App/Models/Audit')

/**
* Return only the values ​​that have changed
* @param {*} objectA
* @param {*} objectB
* @returns {Object, Object} Object
*/

function differenceObj(objectA, objectB) {
let result = { oldValue: {}, newValue: {} }
for (let prop in objectA) {
if (objectB.hasOwnProperty(prop)) {
const value1 = objectA[prop]
const value2 = objectB[prop]
if (value1 !== value2) {
result.oldValue[prop] = value1
result.newValue[prop] = value2
}
}
}
return result
}

/**
* Validates all relations and raise appropiate exceptions when missing
* this indeed to be used as validation function
Expand Down Expand Up @@ -99,6 +121,7 @@ function createWithAudit(ctx, opts, relationFn) {
{ event, auditable, auditableId, newData },
Object.assign(ctx || {}, opts, { trx })
)

return resetRelatedState(newModel, syncRelated)
}
}
Expand Down Expand Up @@ -160,6 +183,10 @@ function updateWithAudit(ctx, opts, relationsFn) {
return persisted
}

const { oldValue, newValue } = differenceObj(oldData, newData)
oldData = oldValue
newData = newValue

// update / patch are shared
const event = Audit.events.UPDATE

Expand Down
22 changes: 20 additions & 2 deletions test/auditable-models.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ test.group('Integration Tests', (group) => {

test('Audit correctly on UPDATE Model', async (assert) => {
await User.audit().create(dummyData.djikstra)
const email = 'foo@gmail.com'
const username = 'Anton'
const user = await User.first()
await user.audit().update({ username: 'Anton' })
await user.audit().update({ username, email })
const auditData = await ioc
.use('Adonis/Src/Database')
.table('audits')
Expand All @@ -91,10 +93,26 @@ test.group('Integration Tests', (group) => {
assert.isNotEmpty(auditData)
assert.equal(auditData.event, 'update')
assert.equal(auditData.auditable_id, user.id)

const oldData = JSON.parse(auditData.old_data)
const newData = JSON.parse(auditData.new_data)
assert.equal(oldData.username, dummyData.djikstra.username)
assert.equal(newData.username, 'Anton')
assert.equal(newData.username, username)
assert.equal(oldData.email, dummyData.djikstra.email)
assert.equal(newData.email, email)
})

test('Audit correctly on update without change values', async (assert) => {
await User.audit().create(dummyData.djikstra)
const user = await User.first()
await user.audit().update({ username: dummyData.djikstra.username })
const auditData = await ioc
.use('Adonis/Src/Database')
.table('audits')
.orderBy('id', 'desc')
.first()

assert.isFalse(auditData.event == 'update')
})

// test('Should audit RELATION between User and Industry', async (assert) => {
Expand Down

0 comments on commit 8971d2c

Please sign in to comment.