Storage Service

App42 back-end platform provides easy interface to save your app data in form of JSON objects using Storage API. Your JSON app data is saved inside a given collection and database. Here collection is like a table and your JSON object represent row inside that collection. Your App can have only a single db and as many collections inside it.

Create Object

To save your JSON app data, collection name and JSON object that you want to save. If you are saving JSON object first time, a new database (named as App42DB) and collection is created automatically. If you want to create database by your own (AppHQ Console > Technical Service > Storage Service > Add DB), make sure you name it as App42DB. You are allowed to create only one DB per app however you can have multiple collections inside your app database. To save your JSON app data, you have to pass your LClassName and JSON object that you want to save. Once your JSON object is saved, you will get an unique identifier objectId of stored json object.

All stored JSON object can be viewed inside AppHQ console under Technical Service > Storage Service option.

LClassName : string; 
LJSON : TJSONObject; 
LCreatedObject : TBackendEntityValue; 
BackendStorage.Storage.CreateObject(LClassName, LJSON, LCreatedObject);

Query Objects

Once your app data is stored in App42 cloud in JSON objects also referred as JSON documents, you can query your data using finder methods available in Storage API. There are several ways of fetching stored objects, like if you want to fetch objects based on multiple conditions (like where clause in SQL), you can use query interface to write those conditions and fetching target objects as shown in below code snippet. If you want to fetch objects which has age value less than or equal to 25 and company value as ShepHertz, you can pass following snippet as shown below.

LBackendClassName: string;
LJSONArray : TJSONArray;
LQuery : TArray<string>;
LMetaArray : TArray<TBackendEntityValue>;
LQuery := TArray<string>.Create('age<=25','company==shephertz','orderByAscending=age')
BackendStorage.Storage.QueryObjects(LBackendClassName, LQuery, LJSONArray, LMetaArray);

Query Operators Below are the query operators that can be used while query the object

Operator

Giving Permission on User data

While saving the user data, one can define the ACL on saved data as shown below. You have to first create TJSONObject with defined permission as explained and pass this while saving the data.

{"PUBLIC":"R", "PUBLIC":"W"}                   // for giving the Read and Write Permission to Public (PUBLIC must be in caps).
{"PUBLIC":"R", "elmo":"W", "elmo":"R"}  // for giving the Read Permission to Public and Read/Write for user elmo.
{"elmo":"W", "elmo":"R"}                     // Private Document for user elmo.
{"elmo":"W", "elmo":"R", "bert":"R"}  // user elmo can Read/Write and user bert can only read.

LApp42ACL : string;
LACL: TJSONObject;
LClassName: string;
LJSONObject: TJSONObject;
LObject: TBackendEntityValue;
LApp42ACL := '{' +'"elmo": "R",' +'"elmo": "W"' +'}'; // This is for Read Write permission to user elmo
LACL := TJSONObject.ParseJSONValue(LApp42ACL) as TJSONObject;
AStorage.CreateObject(LClassName, LACL, LJSONObject, LObject);

Find Object

If you want to fetch object by passing meta object and associated value of it. Find object is the best way to find your document on App42 Cloud database.

AMetaObject : TBackendEntityValue; 
AProc : TFindObjectProc; 
BackendStorage.Storage.FindObject(AMetaObject, AProc); 

Update Object

Update your target JSON document. The TJSONObject will be searched in the JSON doc stored in the cloud and matching doc will be updated with the new value passed.

AObject : TBackendEntityValue; 
AJSONObject : TJSONObject; 
AUpdatedObject : TBackendEntityValue; 
BackendStorage.Storage.UpdateObject(AObject, AJSONObject, AUpdatedObject);

Delete Object

Remove your JSON data which has been stored on App42 Cloud database.

AObject : TBackendEntityValue; 
BackendStorage.Storage.DeleteObject(AObject);