Access the Database (DB)

Three types of databases are available to access data in JourneyApps: DB, OnlineDB and LocalDB. Each of these is described below.

DB

The standard database (designated by DB in JavaScript/TypeScript) in JourneyApps is accessible whether the app has access to the internet or not. All data is locally stored on the device, and changes to the data are synced to the JourneyApps Backend using our Store and Forward functionality.

All documentation regarding queries or the manipulation of database objects will assume that DB will be used.

OnlineDB

In some cases it may be desired to run a query directly against the JourneyApps Backend, instead of the local database. In most cases it is better to use the local database, so that the application can function offline, but in some cases it may require too much data to be synchronized.

OnlineDB is an alternative for the normal DB, that performs all queries online, against the JourneyApps Backend.

Usage

Wherever DB would normally be used, OnlineDB can be used instead. The objects can be used in plain JavaScript/TypeScript, as well as view variables.

Keep in mind that queries and saves will have a delay, and may fail if there are network connectivity issues.

Some examples:

main.js
// Query for a single asset
var asset = OnlineDB.asset.first('serial_number = ?', view.serial_number);

// Find a related object, also online
var building = asset.building();

// Find a related object in the local database instead
var localBuilding = DB.building.first(asset.building_id);

// Query for multiple objects
var assets = OnlineDB.asset.where('manufacturer = ?', view.manufacturer).toArray();
// Query by relationship with local object
var buildingAssets = OnlineDB.asset.where('building_id = ?', building.id);
// Note that if building is a local object, building.assets() will also return
// local objects, so we query directly by the id instead.

// Update an object
asset.condition += 1;
asset.save();

// Create a new object
var newAsset = OnlineDB.asset.create();
newAsset.make = 'Test';
newAsset.save();

Displaying lists

When displaying results from an OnlineDB query in an object-list/table/dropdown, it is advised to use an array view variable instead of query view variable. This ensures that the query is only run once, and error handling can be added for situations when there is no reliable network connection.

Example:

main.xml
<view>
    <var name="assets" type="array:asset" />

    <object-list query="assets" label="Assets" />

    <button label="Refresh" on-press="refresh()" />
</view>
main.js
function init() {
    refresh();
}

function refresh() {
    try {
        view.assets = OnlineDB.asset.all().toArray();
    } catch(err) {
        dialog("Error", "Failed to load assets. Check your network connection.");
    }
}

LocalDB

In some cases an app may want to save data locally, without synchronizing the data with the server. LocalDB is an alternative database that does not upload any changes, and does not download any data from the server.

A typical use cases is to create data to display in a temporary list.

Usage

Wherever DB would normally be used, LocalDB can be used instead. The objects can be used in plain JavaScript/TypeScript, as well as view variables.

Some examples:

main.js
// Create local objects

for(var i = 0; i < 10; i++) {
  var message = LocalDB.message.create();
  message.text = "Message " + i;
  message.save();
}

// Assign to view variable to display in a list
view.messages = LocalDB.message.all();

// When leaving the view, perform cleanup
LocalDB.message.all().destroyAll();

Last updated