Clientside DB API

joris hermans edited this page May 8, 2015 · 11 revisions

With the clientside DB API you can manage collections of data from the client.

You can already take a look at this project as a angular force example.

clientside

On the client you need to subscribe yourself to a collection.

ForceClient forceClient;
ViewCollection hunts;
  
HuntController() {
    forceClient = new ForceClient();
    forceClient.connect();
        
    forceClient.onConnected.listen((ConnectEvent ce) {
        hunts = forceClient.register("hunters", new Cargo(MODE: CargoMode.LOCAL));
    });
}

ViewCollection is an iterable list and can be very useful in combination with angular or polymer.

See below an example of an angular template.

<div class="pure-g" ng-repeat="hunt in hunts">
     <a class="title" target="_blank" href="{{hunt.value.url}}">
          {{hunt.value.name}}
     </a> 
</div>

You can add data to the ViewCollection object. In the example below 'hunts' is an instance of ViewCollection.

var hunt = new Hunt.fromJson(value);
hunt.point += 1;
      
hunts.update(key, hunt);

You can also remove an item from the ViewCollection object.

hunts.remove(key);

serverside

On the server you can control what data is coming in and what you are able to see. First of all you need to publish your collections on the server.

Cargo cargo = new Cargo(MODE: CargoMode.MONGODB, conf: {"address": "mongodb://127.0.0.1/test" });
  
forceServer.publish("hunters", cargo);

You can easily control and validate the data that is coming in as follow.

forceServer.publish("hunters", cargo, validate: (CargoPackage fcp, Sender sender) {
        if (fcp.json!=null) {
          // Check if the url is a url ...
          Hunt hunt = new Hunt.fromJson(fcp.json);
          if (!hunt.url.startsWith("http")) {
            fcp.cancel();
            
            sender.reply("notify", "url not correct!");
          }
        }
      });

More info about validation!

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.