Lucid model : Get only updated attributes

I want to get only updated model attributes.

My User mode:
id:1
name: Adonis user
location: India

Now I am updating model:
await User.update({ name: 'Sanjay', location: 'India' })

Here I have just change the name of User model, So I just want to know only name field is changed.

1 Like

Hi @sanjay :wave:

I try something like this :

// { username: "CrBast", email: "temp@temp.com", password: "789" }
let user = await User.find(1); 
var arr1 = { username: "CrBast", email: "temp@temp.com", password: "123" };

/**
 * Object.entries() :
 * convert JSON entity to array
 * Example : {1: "1", 2: "2"}  => [["1", "1"], ["2", "2"]]
 */
var OUTPUT = Object.entries(arr1)
  .filter(obj => obj[1] !== user[obj[0]]) /** selection */
  .map(obj => {
    // INPUT example : [ "password", "123" ]
    return obj[0]; // Get only first element
  });

OUTPUT :

["password"]

arr1 : values inside your update() method

Inseresting links :

1 Like