balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

Model.update() converts object keys passed in set() from attributes name to SQL columnName #7165

Open himankpathak opened 3 years ago

himankpathak commented 3 years ago

Node version: 16.6.1 Sails version (sails): 1.4.4 ORM hook version (sails-hook-orm): 2.1.1 Sockets hook version (sails-hook-sockets): 1.5.5 Organics hook version (sails-hook-organics): NA Grunt hook version (sails-hook-grunt): 1.0.8 Uploads hook version (sails-hook-uploads): NA DB adapter & version : sails-postgresql@2.0.0 Skipper adapter & version : skipper@0.9.0


Issue

When calling Model.update().set(), sails waterline is modifying the object which is passed to the set function. Camel case keys are attribute names in the sails model and in SQL the column names are in snake case. Camel case is converted to snake case after the update call.

const newMessage = {
id: 123
sessionId: 1234,
messageBody: 'This is a test message'
...
}

await Promise.all([
...
Model.update({ id: newMessage.id }).set(newMessage)
])

console.log(newMessage)
/*
{
id: 123
session_id: 1234,
message_body: 'This is a test message'
...
}
*/
sailsbot commented 3 years ago

@himankpathak Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

eashaw commented 3 years ago

Hey @himankpathak, I wasn't able to reproduce this behavior you're seeing. Would you be willing to create a github repo with a minimal sails app that reproduces this issue?

himankpathak commented 2 years ago

Hey @eashaw , I have created a repo on which this bug is very easily reproducible. https://github.com/himankpathak/sails-obj-bug

Migrations for the app are mentioned in Readme.

Output:

Current value: {
  createdAt: 2021-11-11T07:22:31.297Z,
  updatedAt: 2021-11-11T07:22:31.297Z,
  id: 1,
  user: 2,
  action: 'This is test data'
}
After passing the object to update function
Modified value: {
  id: 1,
  created_at: 2021-11-11T07:22:31.297Z,
  updated_at: 2021-11-11T07:22:31.297Z,
  user_id: 2,
  action_name: 'This is test data'
}
Current value: {
  createdAt: 2021-11-11T07:22:53.407Z,
  updatedAt: 2021-11-11T07:22:53.407Z,
  user: 4,
  action: 'This_is_test_data'
}
After passing the object to create function
Modified value: {
  created_at: 2021-11-11T07:22:53.407Z,
  updated_at: 2021-11-11T07:22:53.407Z,
  user_id: 4,
  action_name: 'This_is_test_data'
}
eashaw commented 2 years ago

Hi @himankpathak, I took a look at your repo and I just noticed that you are logging the same object passed into Model.update(). Mutation on objects passed into Model.update() is expected behavior.

To get the updated value, you can set a variable using model.update() e.g., let foo = await User.update({id: 1}).fetch() (Note that .fetch() is required to return the updated record when using the .update() method.)

You can read more about .update() in our docs.

himankpathak commented 2 years ago

Hey @eashaw, I found this to be an unexpected behavior as this issue does not occur in v0.12. There is no mention of this behavior either in method docs or in migration to v1 docs.

It does make sense to use .fetch() method to get the updated record. But the valuesToSet passed to an ORM in some of my use cases are used to drive additional logic. Also to note that I have never seen such behavior from other ORMs that I have worked with before.

eashaw commented 2 years ago

Hi @himankpathak, I was looking at this with @mikermcneil. We realized that there was a gap in the upgrade guide here.

Here's some background on why this is happening:

Are you seeing this behavior on sails-postgresql@4.0.0?

himankpathak commented 2 years ago

Thanks @eashaw for providing the background on this issue. and yes, this behaviour is occurring with sails-postgresql@4.0.0.