Skip to content

Instantly share code, notes, and snippets.

@gr2m
Last active February 23, 2020 00:03
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gr2m/5463475 to your computer and use it in GitHub Desktop.
Save gr2m/5463475 to your computer and use it in GitHub Desktop.
Imagine saving and finding user data would work right in the browser, persistent and synchronised. How would the code look like? This is what I came up with. Forks & comments much appreciated! #nobackend #dreamcode
// add a new object
var type = 'note';
var attributes = {color: 'red'};
store.add(type, attributes)
.done(function (newObject) {});
.fail(function (error) {});
// update an existing object
var type = 'note';
var id = 'abc4567';
var update = {size: 2};
store.update(type, id, update)
.done(function (updatedObject) {});
// find one object
var type = 'note';
var id = 'abc4567';
store.find(type, id)
.done(function (object) {});
// Load all objects
store.findAll()
.done(function (objects) {});
// Load all objects from one type
var type = 'note';
store.findAll(type)
.done(function (objects) {});
// remove an existing object
var type = 'note';
var id = 'abc4567';
store.remove(type, id)
.done(function (removedObject) {});
// EVENTS
// listen to store events
store.on('add', function (newObject) {});
// new doc created
store.on('add', function (newObject) {});
// existing doc updated
store.on('update', function (updatedObject) {});
// doc removed
store.on('remove', function (removedObject) {});
// any of the events above
store.on('change', function (event, changedObject) {});
// all listeners can be filtered by type
store.on('add:note', function (newObject) {});
store.on('update:note', function (updatedObject) {});
store.on('remove:note', function (removedObject) {});
store.on('change:note', function (event, changedObject) {});
// ... and by type and id
store.on('update:note:uuid123', function (updatedObject) {});
store.on('remove:note:uuid123', function (removedObject) {});
store.on('change:note:uuid123', function (event, changedObject) {});
// ... react on changes coming from remote
store.on('add:note', function (newObject, options) {
if (options.remote) {
// do something wit
}
});
@danculley
Copy link

I would suggest replacing the objects returned from most of your methods (which have "done" and "fail" methods) with ES6-compliant promises, so as to avoid recreating APIs and to make it easier to interoperate with other APIs. That should be as simple as replacing "done" with "then" and "fail" with "catch", as well as supporting a two-argument version of then(onsuccess, onfailure).

@gr2m
Copy link
Author

gr2m commented Jan 19, 2015

@dancully The difference between .done & .fail to .then and .catch is: they have no side effects. Especially beginners can get really hard to understand weird behavior in their apps. The methods should still return standard promises, so they can be chained using .then and .catch, but that's out of scope here

@mk-pmb
Copy link

mk-pmb commented Jun 10, 2017

Whenever you think about users or moderators editing or deleting posts, also think of version history. People will contact your support about how someone stole their password and modified or removed their precious blog entry. Your lawyer will want to know whether for those five minutes yesterday evening, your blog might really have had the discount offer shown in that customer's screenshot, and how it may have come to appear there.

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