All guides Backups and undo: getting an earlier version of your app back Files and images your app's users upload: taking, storing, showing and sharing them Plans and limits: how many projects you can have, the Exotool notice, and what happens at the limit Resources: connecting an Exotool app to outside services Sign-in and permissions: users, logging in and who may do what in your app Storing data: objects, sets and where your app keeps them Taking payments: cards, charges and holds with Stripe or PayPal

Storing data: objects, sets and where your app keeps them

This guide is for the person whose app has to remember things: tasks, customers, orders, bookings. It says what a cloud object and a cloud set are, how to create, read, change, delete and find objects, how to show them in a list on a screen, where the code that touches data runs, and which store the data is kept in. It ends with what commonly goes wrong.

Contents

  1. Cloud objects and cloud sets
  2. Types and fields
  3. Creating, reading, changing and deleting an object
  4. Finding objects: sets
  5. Going through a set: lists, loops and totals
  6. Showing a set in a list on a screen
  7. Data code runs on the server
  8. Where the data is kept: the three stores
  9. Data in a deployed app
  10. When something does not work

1. Cloud objects and cloud sets

A cloud object is a record that your app keeps: a thing with named fields, such as a task with a title and a due date. It is stored on the server, not on the phone or in the browser, so it is still there the next time the app is opened, on any device.

A cloud set is a collection of cloud objects: all the tasks, or the tasks that are not done yet. You get a set by asking for every object of a type, and you narrow it down, sort it or count it with the set functions (section 4).

A cloud object belongs to the app, not to a user or a device. Every user of the app can read the objects of a type through cloudobjects_of_type, whether or not anyone is signed in. To keep data per user, store the owner on the object and filter by it (sections 3 and 7).

2. Types and fields

Every cloud object has a type: a name you choose, such as "task" or "customer". The type is the name you give when you create the object, and the name you ask for when you want all objects of that kind. Write it as a constant string ("task", not a variable): the compiler then produces faster and more secure code.

A field is a named value of an object: task.title, task.due. An object gets its fields when you create it with values, or when you assign to a field (section 3). The language infers the type of a variable from what is assigned to it, so most activities need nothing more.

Where the compiler cannot work the fields out by itself, typically for a variable that is filled by a PHP< ... >PHP or JS< ... >JS block, you describe them with a type declaration:

declare_type task cloudobject with { title : string  due : number  done : number }

cloudobject with { ... } says that the variable holds a stored object; the member list is name : type pairs separated by spaces. A declaration advises the compiler; it does not necessarily override a type the compiler has inferred.

To keep a list or a record inside one field, store it as text with json_encode(value) and read it back with json_decode(text). Each object also has an id of its own, in its id field.

3. Creating, reading, changing and deleting an object

Create. create_cloudobject(type [, ...]) makes a new object and returns it. There are four forms:

// an empty object of the type
t = create_cloudobject("task")
// with fields, as a record written in place (the keys must be plain names)
t = create_cloudobject("task", {title: "Call Ann", done: 0})
// the same, as pairs of field name and value
t = create_cloudobject("task", "title", "Call Ann", "done", 0)
// a second argument that is not a record is the object's identifier, not a field
cfg = create_cloudobject("config", "the_config")

To keep data per user, store the owner in a field and filter by it with subset. Read the session into a variable first, then take the name from it: s = get_current_session() and then t.owner = s.user.name. (Written as one chain, get_current_session().user.name, it does not compile.)

Read. A field is read with the dot: t.title. To get an object back later, either pull it from a set (section 4), or find it by type and identifier:

Function What it gives
locate_cloudobject(type [, identifier]) The object, or false if there is none. If several match, one of them
expect_cloudobject(type [, identifier]) The object, creating one if none is found. If several match, one of them

Change. Assign to a field: t.done = 1. The assignment runs on the server, where the object is kept.

Delete. delete(object_or_set) deletes one cloud object, as in delete(t), or every object in a cloud set, as in delete(subset(cloudobjects_of_type("task"), "done", 1)).

4. Finding objects: sets

cloudobjects_of_type(type) gives the set of all objects of a type. The other set functions take a set and give a narrower or sorted set, so you chain them:

s = get_current_session()
me = s.user.name
tasks = cloudobjects_of_type("task")
mine = subset(tasks, "owner", me)
open_tasks = ordered(subset(mine, "done", 0), "due")
Function What it gives
subset(set_or_list, key, value [, ...]) The members whose field has the value; more key and value pairs narrow it further
subset_neq(set_or_list, key, value) The members whose field is not equal to the value
subset_gt, subset_ge, subset_lt, subset_le (set_or_list, key, value) Field greater than, greater than or equal to, less than, less than or equal to the value
subset_like(set_or_list, key, pattern) The members whose field matches a pattern (see below)
ordered(set_or_list, key [, direction]) Sorted by a field, ascending unless the direction is "descending"
limited(set_or_list, start [, count]) Some of the members, from a position for a count, as for a page. start is 0 for the first member
First(set_or_list) The first member in the set's order (most useful after ordered); null when there is none
One(set_or_list) One member of a set; null when there is none
possible_values(set_or_list, key) The different values of a field across the members, as a list
  • First and One are capitalized. first(...) and one(...) are syntax errors.
  • subset_like matches the whole value, without regard to case, with * as the only wildcard. "*pan*" finds "Fluffy Pancakes"; "pan" does not, and neither does %. A pattern that is still null matches nothing.
  • These functions also take a list. A cloud set is filtered on the server and stays a set; a list is filtered where it is and gives a list.

To find one object again, a common pattern is a key field of your own: set tid: random_key() when you create the object, and later get it with One(subset(cloudobjects_of_type("task"), "tid", cur_id)).

5. Going through a set: lists, loops and totals

Function What it gives
count(set_or_array) The number of members
lookup(set) The objects of a cloud set as a list, in order
sum(set_or_list [, field]) The total of one field, for example sum(invoices, "amount"); values that are not numbers are left out; 0 for an empty list
avg(set_or_list [, field]) The average; null for an empty list
max_of(set_or_list [, field]) / min_of(set_or_list [, field]) The largest / smallest; null for an empty list

A cloud set itself stays on the server. What reaches the device is its members, as a list in order (an empty list when the set is empty); lookup gives that same list.

foreach runs a block once for each member of a set or a list. The body must be a block in braces; there is no break or continue, and no return inside a loop. Give each loop a variable of its own (section 10).

n = 0
foreach (t in subset(cloudobjects_of_type("task"), "owner", me)) {
    t.done = 1
    n = n + 1
}

6. Showing a set in a list on a screen

A data-driven list shows one row for each member of a set. There are two list elements:

Element The rows
Data List 2 Fixed parts for each row: an item title, item content, and swipe operations
Custom Data List A layout of your own: ordinary elements that you arrange for each row

Two properties bind the list to the data:

  • Data Reference (Set): the set or list to show, written as an expression, for example ordered(cloudobjects_of_type("task"), "due"), or the name of a variable that holds a set or a list.
  • Refresh When: Page Refreshes (the default) fills the list each time its page opens; Application Starts fills it when the app starts.

Inside a row, the template names the fields of that row's object in double braces, for example {{title}} (due {{dateString due}}). A field shown with {{field}} is HTML-escaped: what a user typed shows as text. Read an object's id from its id field; do not use {{@key}} as an id.

A tapped row selects its object. The list sets two variables, selected_object (the object of the row) and selected_id (its id), then runs the list's Do On Action code and follows the list's connectors. So the activity after the list can work with the object:

if (confirm()) {
    delete(selected_object)
}

Keeping the list up to date. The device holds only the objects it has read through a cloud set, and the server keeps those up to date, so a list bound to a set changes as the data changes. sync() refreshes the app's data lists with the most current data from the server; call it in an activity when you want that at a given moment.

7. Data code runs on the server

The compiler decides where each statement runs; the language reference has the full rules under "Where code runs, where values live". For data this means:

  • Every cloud-object fetch and delete, and every set function, runs on the server. A statement that uses one runs on the server, whatever else it contains. Assigning to a field of a cloud object runs on the server too.
  • Objects reach the device only through a cloud set, and later changes are sent to the device only for objects a set once delivered.
  • So data access is decided by your sets. Define one cloud set of what the current user may see, and derive every other set from it with further subset calls. Then only objects the user may see are ever copied to the device. Keep the chain in cloud sets: a subset over a list that is already on the device filters on the device and protects nothing.
  • A fetch by identifier is outside that fence. locate_cloudobject and expect_cloudobject run on the server but do not go through your set, so guard the activity that calls them (for an admin-only step, with is_admin_user()).

8. Where the data is kept: the three stores

A project always has a store for its data. You choose which in the project's Database role:

Store What it is Needs
Single File (the default) All the app's data in one built-in data file. Good for getting started and for smaller apps Nothing
Multiple Files (File-Per-Object) Many small files, one for each record, plus index files, in a folder. Also built in Nothing
MySQL A MySQL database of your own A Database resource

Nothing in the app's design depends on the choice: objects and sets are read and written the same way with each store. With MySQL the data is in a database that you back up as you back up any database, and that you can load data into directly. The Resources guide says how to make a Database resource and give it to a project.

Two rules when you change the store:

  • The change takes effect at the next build. Until you build again, the app runs with the store it was built with.
  • Existing data does not move over by itself. The app starts in the new store without the data that is in the old one. Choose the store before the app holds data you care about.

9. Data in a deployed app

  • A deployed app starts with empty data. It has none of the preview's data; what you entered in the preview stays in the preview. To seed a deployed app, enter the data through the app itself, or through MySQL when the app uses a MySQL resource.
  • Deploying again keeps the data. The app's files are replaced; its data is not.
  • Backups. With a built-in store the data is the app's backend/data folder (and img for uploaded images): back it up as you back up files. With MySQL, back up the database.
  • Several apps on one MySQL database. Give each app a table prefix in the resource, or a database of its own. The deployment guide has the rest, including moving to another host.

10. When something does not work

What you see Likely cause What to do
first(...) or one(...) is a syntax error The two functions are capitalized Write First(...) and One(...)
subset_like finds nothing The pattern has no wildcard, uses %, or the variable is still null Use *, as in "*pan*"; set the variable before the list is filled
Compile error: the loop variable "x" runs over a set of cloud objects in an earlier foreach and over a list here One variable used for two kinds of loop Give each foreach its own variable
Compile warning that a loop could run slowly The loop body mixes a device-only step with server work Move the marked statement out of the loop
Every user sees everybody's records Objects belong to the app, not to a user Store the owner in a field and bind lists to subset(..., "owner", me)
A list does not show a new or changed record The list was filled before the change Call sync(), or check the list's Refresh When setting
The data is gone after you changed the store Data does not move between stores by itself Choose the store before the app holds data you care about; this guide does not cover moving data between stores
The app still uses the old store after a change The project was not built again Build again: the Database role takes effect at the next build
The deployed app has no data A deployment starts empty Enter the data through the deployed app, or load it into MySQL
A deployed app saves nothing, or reports "failure on fopen of .../backend/data/heap.data" The backend/data folder is not writable by the web server Make the folder writable: see the deployment guide