Cloud Database Basics
Skygear allows users to store records to Skygear's PostgreSQL cloud database.
You can even define your own type of
Record and add customized fields to suit
Before you proceed, please make sure:
You have your Skygear container configured. Simply follows the guide at Setup Skygear if you haven't.
You have signed up as a
Userand logged in, as you have to be authenticated to perform any database operations. For guide on sign up and log in, go to User Authentication Basics.
To visualize your database with graphical presentation, you can go to the Database section in Skygear Portal and open our web data browser.
The Record Class
Record is the data storage unit in Skygear.
Recordmust be given a
typeis simply a
Recorddescribing the type of data this record holds.
Recordobject contains a
java.util.Maprepresenting its attributes; Keys will be mapped to database column names, and values will be stored accordingly based on the data type. Please refer to Data Type for more information.
- There exist some reserved keys that cannot be used as a key for an attribute. Examples are
recordType. Please refer to Reserved Columns section for more information.
Recordcreated will be owned by the currently logged in user.
Recordobject has a unique
id(a string combination of record type and uuid is used).
Before doing any record operations, you have to understand how databases in Skygear work. You are provided with a private and a public database:
Every record in the private database is truly private, regardless of the Access Control List (ACL) set.
Records saved at the public database is set to public by default. ACL can be set per record.
The public and private database can be retrieved using:
Creating a Record
In Skygear, you can specify a type and an id for a
Record. A record type must be given when a record is instantiated, while its id will be generated afterwards if not given. We will talk more
Record id with examples in a later section.
You can assign attributes to a
Record. A key of an attribute must be a
String while its value can be of various types. All primitive types, array of primitive types and Java
Date type are currently supported.
Below are some examples on how attributes can be set and retrieved.
To save just a single record or multiple records, a
RecordSaveResponseHandler has to be instantiated first.
If a record has been given an ID that already exists on the cloud
database, saving this record will update the existing record.
A record can be saved to either the public or private database. Here is an example saving the record
note1 we have created above to a public database.
To save multiple records at one time, put them in an array:
You can also save multiple records atomically, where result of the save operation can either be Success or Fail. Note that the callback
onPartiallySaveSuccess will not be triggered in such case.
Reading a record
Record(s) can be queried with conditions. You will have to
specify the conditions in a
The following code snippet shows how to perform record query with
For more information, please check out the Query Section.
Metadata of Record
After a record has been saved, some metadata will be generated automatically. You can get the metadata as follows:
Deleting a record
You can delete a record on either the public or private database.
Here, we are going to use the record
note1 that was created and saved
above to demonstrate the process of deleting a record.
Similar to the above handlers, you can delete multiple records at a time.
For each new record type stored in the database, a table with the same name as the record type is created. For example, if your record type is called
Note which no record of this type has been saved before, a new table called
will be created. Each row in the table corresponds to one record of type
For each record table there exists two types of columns (fields of a record), those reserved by Skygear and those user-defined. Reserved columns contain metadata of a record, such as record ID, record owner and creation time. These reserved columns are prefixed with underscore, like
It is possible to manipulate data in record tables directly. However, one should exercise with caution when modifying data directly in record tables.
Each record table contains the following reserved columns:
|Column Name||Object Attribute||Description|