Data, Data, Everywhere

Kelly AndrewsKelly Andrews

Data is the lifeblood of any application.

Not being able to access your application’s data quickly can be equated to having clogged arteries - it’s going to be painful, and if it continues long enough, may lead to your app’s death. The good news is that with Syncano, we have a clear pathway to our data - using classes and data objects!

Classes without the homework

Classes to a developer mean about the same thing across all languages - an object that has properties inherited from its constructor. In Syncano, Classes are basically the same thing - essentially a template for your data objects. To create a class you need to provide the following information:

Defining a schema

The schema is where you add your required properties. Let’s take a look at an example schema:

[  
   {  
      "name":"title",
      "type":"string",
      "order_index":true,
      "filter_index":true
   },
   {  
      "name":"release_year",
      "type":"integer",
      "order_index":true,
      "filter_index":true
   },
   {  
      "name":"price",
      "type":"float",
      "order_index":true,
      "filter_index":true
   },
   {  
      "name":"author",
      "type":"reference",
      "order_index":true,
      "filter_index":true,
      "target":"Author"
   }
]

Each item listed in the example schema includes title and type. Syncano gives you several type options including array, object string, text, integer, float, boolean, datetime, and file. Did you also notice the last property in the schema uses reference and target? This is your foreign key to link your data objects to other classes as you would in any SQL-like table structure.

One more important item to note here is that by setting order_index and filter_index to true, you can order and filter data objects on that specific property when retrieving data. Setting these properties to true would give you these properties, in addition to the following default filter/order properties:

Permissions

When you create your class you will want to ensure that the permissions have been set according to your app’s needs. Classes have two permission types:group and other. There are three settings each of these permission types:none, read, create_objects.

Other

Setting other gives anyone who has access to the data objects in a class the permission above. If a class has set the permission to read for the other type, then any time the classes’ data objects are listed, anyone would be able to view the data objects.

Group

The group type allows anyone in a specific group to have a set of permissions. This creates a bit of flexibility so that other's can be set to a tighter restriction, but perhaps a group has additional permissions. This would allow you to easily create segmented data for a user’s type. For example, let’s say you have a group of “admins” who have access to more items within the application.

Classes will typically be created in your Syncano dashboard - there really isn’t a need to define these in your application. The routes are available, but really - just use the dashboard, that’s what it’s there for.

The name’s Object - Data Object

Once you have your classes available, it’s time to fill them up with objects for your application to use. At this point, you’ve already done the hard part, and creating data objects are exactly like you would think - pass in all required data properties.

Data objects will be created with your application - and will typically be created by a user. One important thing to understand when creating your data objects is to be explicit with your permissions.

{
    "id": 1,
    "owner": 123,
    "owner_permissions": "full",
    "group": 321,
    "group_permissions": "full",
    "other_permissions": "full"
}

Syncano does not make assumptions regarding who receives access to your data. In fact, we default to none for everything. The above example would allow anyone to read, update, and delete this particular object (create permissions are on the class level). The other permission levels would be none, read, write. Using these permissions in parallel with the class permission options will give you a fairly complex permissions system.

Like classes, objects have group and other permissions. Unlike classes, they also includeowner permissions. Just like a UNIX system, Syncano gives you full control over who has access and who doesn’t.

Filter and Order your data

The last thing I want to go over is how to filter and order your data. Syncano uses a fairly standard syntax to create queries, using familiar filters like _gt and _lt.

If you want to filter on the created date, you might pass something like this:

query={"created_at":{"_gt":"2015-02-23T12:00:00Z"}}  

You can also filter on multiple fields as well:

query={"release_year":{"_gt":1900,"_lte":2000},"pages":{"_gt":199}}  

Another nice feature is the ability to reduce the fields you get back. This way you get back the exact object you want to show:

fields=release_year,id,pages  

Finally - you can order your results like so:

order_by=release_year  

All of these can be combined to provide you flexibility when retrieving data.

Wrap Up

These are the basics of classes and data objects, but our developer manual goes into more detail if you want to learn more:

We are constantly improving the platform, and would love to incorporate your feedback. If you have any specific suggestions or ideas, feel free to share with us at [email protected].
Be sure to follow @syncano and @kellyjandrews for the latest announcements and information as well.

Build powerful apps in half the time

Use our serverless platform to set up your backend in minutes.

Learn more
Kelly Andrews
Author

Kelly Andrews

Principal Developer Evangelist, Notre Dame Fan, Hacker, Geek. Did I mention I sing too?