question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Relationships and Changesets

See original GitHub issue

When working with changesets, this pattern is quite common:

let home = new Address();
let work = new Address();

let user = new User();
user.addresses = [home, work];

let validator = function() {};
let model = Changeset(user, validator);
<form>
  <h3>Personal details</h3>
  {{input type='text' value=model.firstName}}
  <!-- etc… -->
  
  <h3>Addresses</h3>
  {{#each model.addresses as |address|}}
    {{input type='text' value=address.street}}
    {{input type='text' value=address.city}}
    <!-- etc… -->
  {{/each}}

</form>

What I’d like to do here is to make changes to the address instances and buffer them inside a changeset. So that I can do rollback on the model and automatically have rollbacks applied to all child changesets too.

Maybe by specifying which relationships should create proxy changesets and buffer, e.g. let model = Changeset(user, validator, { nested: ['addresses'] }); or similar (opt-in).

Right now changesets seems to support access via keys, e.g. changeset.set('profile.url', '…') but not changeset.get('profile').set('url', '…')

  1. Has there been any discussion on supporting relationships, such that the handlebars example above would work with rollback/save on the user, with changes applied to nested addresses?

  2. Would also be good to support rollback/apply for changes to the collection itself, i.e. adding/removing addresses. Maybe via an API such as model.get('addresses').add(vacationHome); where model.get('addresses') would return an instance of e.g. ChangeSetList.

Related issues:

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

5reactions
richbrowncommented, Nov 16, 2020

@sandstrom and @snewcomer, just to confirm your instincts on this; I would also find this feature very useful. I work around it by creating my own Changeset and define a property on that that overrides the original array and returns an array of changesets for each item in the original array. Equally, I override the save(), rollback(), isPristine and isDirty on the parent changeset to take into account the new array of child changesets. Having the feature @sandstrom described would save me quite a lot of boilerplate code.

1reaction
sandstromcommented, Oct 14, 2020

@snewcomer Thanks for taking time answering!

One idea could be to extend the basic principle of the changeset to relationships.

It’s already a proxy at the key/value surface of an object, basically get/set is proxied and buffered. But this “leaks” is if myChangeset.children return an array (or an array-like relationship object from Ember Data).

One approach would be to “tell” the changeset, when initiated, about the keys that return relationships, for example new Changeset(object, { listKeys: ['children'] }). For these keys, instead of proxying down to the underlying model the changeset could create a ChangeSetList (on-the-fly), associate this with the changeset and wrap it around the relationship [array or similar] and return it as the response.

myChangeset.children would then return a ChangeSetList with methods such as add and remove, plus support iteration, e.g. via toArray or Symbol.iterator. Adding something to the changeset-list, via add, would then buffer/proxy just like get and set already does on a changeset. This would allow rollback/save of added/removed relationship items.

Happy to clarify my thinking about this and explain in more detail!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changesets for many-to-many relationships - Stack Overflow
I keep getting validation errors when creating a changeset for jobs... tech = JobJawn.Listing.Technology |> limit(1) |> JobJawn.Repo.one JobJawn ...
Read more >
Change Sets and Relationships | Download Scientific Diagram
The entire collection of change sets and relationships is shown on the right-hand-side of Figure 2 in a “variability spreadsheet”.
Read more >
Deployment Connections for Change Sets - Salesforce Help
A deployment connection is required between two Salesforce orgs to send change sets from one org to another. You can't create deployment connections...
Read more >
Modeling Product Line Architectures through Change Sets ...
Modeling Product Line Architectures through Change Sets and Relationships. Abstract: The essence of any modeling approach for product line architectures ...
Read more >
Conflicts between changeset commits
Changed parent/child relationship: New items were added in another changeset while you made changes to the parent data item.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found