Add support for nested saves by hjdivad · Pull Request #173 · emberjs/rfcs · GitHub
Skip to content

Add support for nested saves#173

Closed
hjdivad wants to merge 6 commits into
emberjs:masterfrom
hjdivad:nested-save
Closed

Add support for nested saves#173
hjdivad wants to merge 6 commits into
emberjs:masterfrom
hjdivad:nested-save

Conversation

@hjdivad

@hjdivad hjdivad commented Oct 18, 2016

Copy link
Copy Markdown
Member

@sandstrom

Copy link
Copy Markdown
Contributor

@hjdivad

hjdivad commented Oct 21, 2016

Copy link
Copy Markdown
Member Author

How about using a key other than clientId, something like tempId or ephemeralId to signify that it's a temporary value? Nothing major though, clientId totally works — this comment is in the bike-shed category.

Although both tempId and ephemeralId signify that the id is short-lived, neither signify that the id originates from the client. If I had to pick between the two, I'd rather signify the id is client-originated rather than ephemeral. I don't feel strongly about this though.

Is this supposed to work together with EmbeddedRecordsMixin or as an alternative? If it's supposed to work along-side, it would be neat if the logic provided by savedWith could be hidden. I.e. embedded records as defined by the EmbeddedRecordsMixin would always get this behaviour. Or am I missing some obvious drawback with that?

After some discussion, this RFC will be reworked slightly to move savedWith so that it can be entirely an adapter concern. That this will help make EmbeddedRecordsMixin integration smoother is a motivation for this.

With out-of-band, presumably the serializer can be customized such that store.pushPayload will handle the clientId in the payload transparently, right?

Yes. Your serializer will need to ensure that clientId is in the right place and then the store will prioritize updating an existing unpersisted record with that clientId over pushing a new record into the identity map.

@sandstrom

Copy link
Copy Markdown
Contributor

@hjdivad I was mainly throwing the idea out there (id-naming). If you prefer clientID lets go with that, I'm very happy whichever name it gets 😃

Moving savedWith into an adapter-concern would be awesome! 🚀

@mspisars

Copy link
Copy Markdown

Is there a timeline for this? Can this be brought up for discussion on being added to the timeline?

@robneville73

Copy link
Copy Markdown

is anyone working on this? I might be interested in tackling some of this.

@hjdivad

hjdivad commented Oct 30, 2017

Copy link
Copy Markdown
Member Author

@robneville73 nobody is working on it at the moment, but everyone's pretty positive on the idea.

The first step is to rework the rfc (slightly) to make the mechanics entirely an adapter concern.

  • move savedWith to snapshot (off DS.Model)
  • entangle in snapshot via adapter
  • move clientId to the meta portion of a resource to be consistent with jsonapi

@robneville73

Copy link
Copy Markdown

I can fork your rfc and try to do that if you'd like though I'm a little fuzzy on what you mean by entangling in snaphot via adapter.

@hjdivad

hjdivad commented Oct 31, 2017

Copy link
Copy Markdown
Member Author

@robneville73

I can fork your rfc and try to do that if you'd like though I'm a little fuzzy on what you mean by entangling in snaphot via adapter.

Basically think through how to change the example when you don't have DS.Model#savedWith.

I imagine the way this would work is that the parent object will be saved and then in its adapter the relevant hook (createRecord, updateRecord) would call belongsTo / hasMany to get the related snapshots to call savedWith.

Something like

// models/order.js
DS.Model.extend({
  items: DS.hasMany('items'),
});

// routes/order.js
Ember.Route.extend({
  actions: {
    saveOrder(order) {
      order.save();
    }
  }
});

// adapters/order.js
DS.Adapter.extend({
  createRecord(store, modelClass, snapshot) {
    snapshot.belongsTo('items').forEach(itemSnapshot => {
      // this would transition `itemSnaphot.record` and entangle `itemSnapshot` with 
      //`snapshot` such that when the order's record transitions away from in flight, 
      // so does the item's record
      itemSnapshot.savedWith(snapshot);
    });
  }
});

Does that make sense?

@hjdivad

hjdivad commented Jun 19, 2019

Copy link
Copy Markdown
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-ember-data RFCs that impact the ember-data library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants