Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Deep Aware for JSON Objects on Patch #371

Open
william-seaton opened this issue Jul 30, 2021 · 1 comment
Open

Support Deep Aware for JSON Objects on Patch #371

william-seaton opened this issue Jul 30, 2021 · 1 comment

Comments

@william-seaton
Copy link

Steps to reproduce

Build a feathers instance using Postgres with a JSONB Column. The resulting object lets say for the sake of example looks like this:

var user = {
    name: 'John Doe',
    ...details,  //More columns
   settings: { //This is a JSONB Column
        region: 'en-ca',
        colour: '#FFFFFFF',
        ...moreSettings
   }
}

Lets say I want to update only the user.settings.colour field and patch that into the database. If I set for instance user.settings.colour = '#BADEED' its going to act like an update not a patch and delete all other things. The object will look like this instead:

var user = {
    name: 'John Doe',
    ...details,  //More columns
   settings: { //This is a JSONB Column
        colour: '#FFFFFFF'
   }
}

Expected behavior

Only the settings.colour should be modified.

Actual behavior

settings only contains colour and deletes all other entries. I think sequelize is treating it like a string.

System configuration

Tell us about the applicable parts of your setup.

Module versions (especially the part that's not working):

{
    "@feathersjs/authentication": "^4.5.11",
    "@feathersjs/authentication-local": "^4.5.11",
    "@feathersjs/authentication-oauth": "^4.5.11",
    "@feathersjs/configuration": "^4.5.11",
    "@feathersjs/errors": "^4.5.11",
    "@feathersjs/express": "^4.5.11",
    "@feathersjs/feathers": "^4.5.11",
    "@feathersjs/socketio": "^4.5.11",
    "@feathersjs/transport-commons": "^4.5.11",
    "feathers-sequelize": "^6.2.0"
}

NodeJS version:
v16.3.0

Operating System:
macOS Big Sur 11.4 (20F71) & Docker node:alpine

I think it should be possible based on this: Sequelize 6 Supports Deep Aware for JSON

@DaddyWarbucks
Copy link
Contributor

Sequelize does not support this as you have described.

From the link you posted

const instance = await MyModel.findOne();

instance.myJsonField.someProperty = 12345; // Changed from something else to 12345
console.log(instance.changed()); // false

await instance.save(); // this will not save anything

instance.changed("myJsonField", true);
console.log(instance.changed()); // ['myJsonField']

await instance.save(); // will save

Notice that this is working with Sequelize instances and that you have to manually mark a nested column as having changed. So this is actually saying that Sequelize doesn't really support what you have described and that you have to manually manage it.

I don't think feathers-sequelize should try to support this as it is not natively supported by Sequelize. But, you should be able to accomplish this in a hook quite easily. Something like this should get you goin.

async function deepPatch(context) {
  const stashed = await context.service.get(context.id);
  const merged = deepMerge(stashed, context.data);
  context.data = merged;
  return context;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants