psr7-demo icon indicating copy to clipboard operation
psr7-demo copied to clipboard

Support atomic multi-location updates

Open rlivsey opened this issue 9 years ago • 0 comments

The latest Firebase client supports atomic multi-location updates

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
// Generate a new push ID for the new post
var newPostRef = ref.child("posts").push();
var newPostKey = newPostRef.key();
// Create the data we want to update
var updatedUserData = {};
updatedUserData["user/posts/" + newPostKey] = true;
updatedUserData["posts/" + newPostKey] = {
  title: "New Post",
  content: "Here is my new post!"
};
// Do a deep-path update
ref.update(updatedUserData, function(error) {
  if (error) {
    console.log("Error updating data:", error);
  }
});

So the ref needs to be a parent to all child paths and simply receives an object of paths & data to update.

In Fireplace, finding a common root to a bunch of models should be relatively simple, the question is what the API should be.

API

I'm thinking some method on store would make the most sense which simply takes a list of models to update:

const company = this.store.createRecord("company", { name: "Acme" });
const person = this.store.createRecord("person", { name: "bob", company });

this.store.atomicSave(company, person).then(models => {
  // resolves with an array of the same models?
}).catch(e => {
  // rejects with the Firebase error
})

That wouldn't allow updating of specific keys within a model which is currently possible with person.save("name") for example.

It's possible that an extension of the above API could support that by passing an object with model and key properties but I'm not sure if that's needed:

const company = this.store.createRecord("company", { name: "Acme" });
const person = this.store.createRecord("person", { name: "bob", company });

this.store.atomicSave({ model: company, key: "name" }, person)

We could/should support passing an array of models vs multiple parameters:

const models = [company, person];
this.store.atomicSave(models)

That's also easily supported in ES6 (Babel), so may not be necessary:

const models = [company, person];
this.store.atomicSave(...models)

Bikeshedding

What should the method be called, off the top of my head:

  • store.atomicUpdate / store.atomicSave
  • store.updateAtomic / store.saveAtomic
  • store.batchUpdate / store.batchSave
  • store.batch
  • store.updateMultiple / store.saveMultiple
  • store.updateMany / store.saveMany
  • something else?

I'm partial to store.atomicSave as a companion to simply store.save

rlivsey avatar Sep 25 '15 11:09 rlivsey