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

How to update 'hasManyBy'? #101

Closed
longuid opened this issue Feb 27, 2018 · 4 comments
Closed

How to update 'hasManyBy'? #101

longuid opened this issue Feb 27, 2018 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@longuid
Copy link

longuid commented Feb 27, 2018

i have a very complex project, vuex-orm saved me. but how to update array?

demo

when update hasManyBy field, array be merged (in demo case, parameter cannot by delete);

i found the code:

Query.prototype.update = function (data, condition) {
        if (typeof condition !== 'function') {
            if (this.entity.data[condition]) {
                this.entity.data[condition] = merge(this.entity.data[condition], data);
            }
            return;
        }
        this.entity.data = mapValues(this.entity.data, function (record) {
            return condition(record) ? merge(record, data) : record;
        });
    };

how to solve the problem? thx!

@kiaking kiaking added the bug Something isn't working label Feb 27, 2018
@kiaking
Copy link
Member

kiaking commented Feb 27, 2018

Oh OK, this is yes a problem. Sorry I haven't thought enough about updating Array... This issue must have some kind of solution, but for now as a workaround, please use insert method and replace the whole record. It's a pain so, sorry for that.

This problem is not specific to hasManyBy but the update itself. As you mentioned, update method recursively merges the passed value so you can't delete something inside Array.

I'm thinking 3 steps here for the update method improvement.

  1. Change the update behavior to not merge Array or Object but replace the field value with a new one.
// State.
{
  schemas: {
    data: {
      '1': {
        id: 1,
        name: 'Schema Name',
        parameters: [1, 2]
      }
    }
  }
}

// Update array.
store.dispatch('entities/schemas/update', {
  where: 1,
  data: { parameters: [3] }
})

// Then in the state.
{
  schemas: {
    data: {
      '1': {
        id: 1,
        name: 'Schema Name',
        parameters: [3] // <- Replaced!
      }
    }
  }
}
  1. Add new feature to use closure base updating for easier modification.
// State.
{
  schemas: {
    data: {
      '1': {
        id: 1,
        name: 'Schema Name',
        parameters: [1, 2]
      }
    }
  }
}

// Update array with closure.
store.dispatch('entities/schemas/update', {
  where: 1,
  data (record) {
    record.parameters.push(3)
  }
})

// Then in the state.
{
  schemas: {
    data: {
      '1': {
        id: 1,
        name: 'Schema Name',
        parameters: [1, 2, 3] // <- Updated!
      }
    }
  }
}
  1. Add nice feature to update Array or Object field?
// State.
{
  schemas: {
    data: {
      '1': {
        id: 1,
        name: 'Schema Name',
        parameters: [1, 2]
      }
    }
  }
}

// Update array with special action.
store.dispatch('entities/schemas/push', {
  where: 1,
  field: 'parameters',
  data: 3
})

// Then in the state.
{
  schemas: {
    data: {
      '1': {
        id: 1,
        name: 'Schema Name',
        parameters: [1, 2, 3] // <- Updated!
      }
    }
  }
}

Maybe 3 is too much. But I think 1 is a must. What do you think?

/Cc @iNaD May I ask you for your idea on this topic (since it is about update 😃 )?

@longuid
Copy link
Author

longuid commented Feb 27, 2018

thanks for your reply, i played vuex-orm a whole day,
now, i add a middle table to solve this problem, (i know is really stupid ...)

demo again

the first way is my hope, 😃 ,will make data structure simple

@kiaking
Copy link
Member

kiaking commented Feb 27, 2018

Thanks for confirming! yea the 1st should is a must I think! We should work on to it.

@kiaking kiaking self-assigned this Mar 3, 2018
kiaking added a commit that referenced this issue Mar 4, 2018
@kiaking
Copy link
Member

kiaking commented Mar 4, 2018

This issue has been fixed with Vuex ORM 0.22.0 🎉
https://github.com/vuex-orm/vuex-orm/releases/tag/v0.22.0

Thank you for raising this issue! Hope this will will make your coding easier 👍

@kiaking kiaking closed this as completed Mar 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants