Accessing data from device

The supersonic.data namespace includes APIs that work with any cloud data providers configured for the Supersonic application. They provide efficient ways for manipulating data – adding, updating and deleting records.

supersonic.data

In the following we demonstrate how supersonic data makes it joyfully easy to manipulate resources in the defined backend database (such as AppGyver sandbox DB) in a clean, object-oriented way.

Code snippets in below assume that the application has a resource Beer with two string fields name and brewery, and an integer field brewed_since configured.

Once a resource has been defined, app can get hold of it with method supersonic.data.model which takes the resource name as parameter:

var Beer = supersonic.data.model('Beer');
Beer = supersonic.data.model('Beer')

Getting data from the server

The call supersonic.data.model('Beer') returns a supersonic.data.Model class, that provides methods to accessing the data of the resource.

The entire collection can be achieved by a promise returning function findAll of the Model class:

// all the below examples assume that variable Beer has been created
var Beer = supersonic.data.model('Beer')

Beer.findAll().then( function(allBeers) {
  // access here the collection allBeers
});
# all the below examples assume that variable Beer has been created
Beer = supersonic.data.model('Beer')

Beer.findAll().then (allBeers) ->
  # access here the collection allBeers

The returned object allBeers is of type supersonic.data.Collection that acts like an array:

Beer.findAll().then( function(allBeers) {
  console.log("the first was " + allBeers[0].name);

  for (i = 0; i < allBeers.length; i++) {
    console.log(allBeers[i].name + " by " + allBeers[i].brewery + " brewed since " + allBeers[i].brewed_since);
  }
});
Beer.findAll().then (allBeers) ->
  console.log "the first was #{allBeers[0].name}"

  for beer in allBeers
    console.log "#{beer.name} by #{beer.brewery} brewed since #{beer.brewed_since}"

Objects in the collection are instances of the class supersonic.data.Model.

Three-way Data Binding

The most awesome feature of Supersonic data is it's ability to listen if the data has changed at the backend server. Listening the changes is done with method all:

Beer.all().whenChanged( function(beers) {
  console.log("the state of the beers has changed, current ");
  for (i = 0; i < beers.length; i++) {
    console.log(beers[i].name + " by " + beers[i].brewery + " brewed since " + beers[i].brewed_since);
  }
});
Beer.all().whenChanged (beers) ->
  console.log "the state of the beers has changed, current "
  for beer in beers
    console.log "#{beer.name} by #{beer.brewery} brewed since #{beer.brewed_since}"

Any time there are changes in data, the registered callback function is called. So with Angular's databinding and magical touch of Supersonic data one can easily build applications where the data of all the users is kept in sync all the time!

See more from the supersonic.data API.

Model instances

Method find in the Model class can be used to get a single object based on the identifier field:

Beer.find("123").then( function(beer) {
  console.log("beer with id 123 is " + beer.name + " by " + beer.brewery);
});
Beer.find("123").then (beer) ->
  console.log "beer with id 123 is #{beer.name} by #{beer.brewery}"

The returned objects are instances of supersonic.data.Model.

A model instance can be edited and the edited version saved back to the cloud database:

Beer.find("123").then( function(beer) {
  beer.brewery = "Sierra Nevada Brewing";
  beer.save().then( function() {
    console.log("beer saved");
  });
});
Beer.find("123").then (beer) ->
  beer.brewery = "Sierra Nevada Brewing"
  beer.save().then () ->
    console.log "beer saved"

It is also possible to delete model instances:

Beer.find("555").then( function(beer) {
  beer.delete().then( function() {
    console.log("a crappy beer was deleted");
  });
});
Beer.find("555").then (beer) ->
  beer.delete().then () ->
    console.log "a crappy beer was deleted"

Creating new model instances

Create and save new objects to database is easy:

var object = {
  name: "Arctic Cirle Ale",
  brewery: "Malmgard",
  brewed_since: 2009,
};

var beer = new Beer(object);
beer.save().then( function() {
  console.log("beer created!");
});
object =
  name: "Arctic Cirle Ale"
  brewery: "Malmgard"
  brewed_since: 2009

beer = new Beer(object)
beer.save().then () ->
  console.log "beer created!"

Once the method save has been called to the Model instance, the new object is saved to the backing database. And thanks to three way data binding, all the apps who have registered for the changes in collection Beers will get updated with the new data!

Further reading

Learn more about Supersonic.data from the API docs.