Object Level APIs
The Object Level API helps the developer to perform ORM operations at the object (or) entity (or) table level.
\<VMXObj>.startSync
Syntax
<VMXObj>.startSync (syncOptions, successCallback, failureCallback)
Parameters
Parameter | Type | Description | Required |
syncOptions | JSON | The user can provide options to customize sync behavior. | Yes |
successCallback | Function | The function is invoked on a successful sync. | Yes |
failureCallback | Function | The function is invoked on an error in sync with the cause of failure as an argument. | Yes |
Sync Options
Options | Type | Descriptions | Required |
downloadBatchSize | Number (or) String | The downloadBatchSize option defines record based batching in downloads. Make sure to provide appropriate batch size (number of records) based on backend’s ability (to support the same). downloadBatchSize is 500(records) by default. Default BatchSize is assumed if the supplied value is less than or equal to 0. | No |
uploadBatchSize | Number (or) String | uploadBatchSize option defines record based batching in uploads. uploadBatchSize is 50 (records) by default. Default BatchSize is assumed if the supplied value is less than or equal to 0. | No |
Example
function successCallback(object){
//Handle sync success response.
}
function failureCallback (error){
//Handle sync failure response.
}
try{
var syncObject = new voltmx.sdk.VMXObj("Employee");
var syncOptions = {};
//Set upload and download BatchSize.
syncOptions.downloadBatchSize = 100;
syncOptions.uploadBatchSize = 200;
syncObject.startSync(syncOptions, successCallback,failureCallback);
} catch (err) {
//Handles Exception.
}
\<VMXObj>.get
Syntax
<VMXObj>.get(options, successCallback, failureCallback)
Parameters
Parameter | Type | Description | Required |
options | JSON | If the options parameter is omitted, all the records are returned. | Yes |
successCallback | Function | The function is invoked when get operation is successful. | Yes |
failureCallback | Function | The function is invoked on an error in get operation with the cause of failure as an argument. | Yes |
Options Keys
Keys | Type | Description | Required |
primaryKeys | JSON (or) Table | Specify the primary keys to use, for the select query. Use record's primary key column names as key and respective values to populate primaryKeys JSON. | No |
whereConditionAsAString | String | Specify where condition for the select query if primary keys are not provided. Key Name: whereConditionAsAString and the value is string. | No |
orderByMap | JSON elements | Specify the way the data must be arranged in ascending (or) descending order of the column name. Key Name: orderByMap and value is an array of the dictionary. | No |
whereCondition | JSON | Specify the where condition for the select query if primary keys are not provided. Key Name: whereCondition and values are column names and their respective values. | No |
likeCondition | JSON (or) Table | List of column names and their respective values according to which the records are to be fetched. | No |
Example
//----- Example : Get all --------
function successCallback(records){
//records is an array of records. each record is a dictionary.
}
function failureCallback(error){
voltmx.print(“unable to retrieve records from db”+ error.code);
}
var categories = new VMXObj("CATEGORY");
//all records of object CATEGORY are returned as an argument to success callback.
categories.get( null, successCallback, failureCallback);
//------- Example : get with orderBy clause --------
function successCallback(records){
//records is an array of records. each record is a dictionary.
}
function failureCallback(error){
voltmx.print(“unable to retrieve records from db”+ error.code);
}
var categories = new VMXObj("CATEGORY");
var orderByMap = [];
orderByMap.push({"CategoryID":"DESC"});
orderByMap.push({"CategoryName":"ASC"});
var options = {};
options["orderByMap"] = orderByMap;
//all records of object CATEGORY are returned as an argument to success callback ordered by CategoryID and CategoryName.
categories.get(options, successCallback, failureCallback);
//------ Example : get by PK --------
function successCallback(records){
//records is an array of records. each record is a dictionary.
}
function failureCallback(error){
voltmx.print(“unable to retrieve records from db”+ error.code);
}
var categories = new VMXObj("CATEGORY");
var options = {};
//Primary keys or a where clause can be used
var primaryKeys = {};
primaryKeys["CategoryID"] = "2233";
options["primaryKeys"]= primaryKeys;
// record with PK CategoryID with value "2233" of object CATEGORY are returned as an argument to success callback.
categories.get( options, successCallback, failureCallback);
//---- Example : get by where clause --------
function successCallback(records){
//records is an array of records. each record is a dictionary.
}
function failureCallback(error){
voltmx.print(“unable to retrieve records from db”+ error.code);
}
var categories = new VMXObj("CATEGORY");
var whereClause = "Category_PN = '7'";
var options = {};
options["whereConditionAsAString"] = whereClause;
//Invoking get returns record with Category_PN = 7 .
categories.get(options, successCallback, failureCallback);
\<VMXObj>.create
Syntax
<VMXObj>.create(record, options, successCallback, failureCallback)
Parameters
Parameter | Type | Description | Required |
record | JSON | Specify the record to be created in the database under a given object. | Yes |
options | JSON | Reserved for future use. Supply null or empty JSON as options. | Yes |
successCallback | Function | The function is invoked when records are created successfully. Newly created record is returned through successCallback. | Yes |
failureCallback | Function | The function is invoked on an error with the cause of failure as an argument. | Yes |
Example
function successCallback(response){
voltmx.print("Record created successfully with primary keys: " + response["CATEGORY_ID"]);
}
function failureCallback(error)
voltmx.print("Create is failed" + JSON.stringify(error));
}
var category = new voltmx.sdk.VMXObj("CATEGORY");
var record = {};
// CATEGORY_ID is the autogenerated primary key
record["CATEGORY_PN"] = Number("7");
record["CATEGORY_DES"] = "Creating a new record";
category.create(record, {}, successCallback, failureCallback);
\<VMXObj>.updateByPK
Syntax
<VMXObj>.updateByPK(record, options, successCallback, failureCallback)
Parameters
Parameter | Type | Description | Required |
record | JSON | A dictionary containing column name and value pairs that are to be updated. | Yes |
options | JSON | A dictionary containing primaryKeys. | Yes |
sucessCallback | Function | The function is invoked when records are updated successfully. | Yes |
failureCallback | Function | The function is invoked on an error with the cause of failure as an argument. | Yes |
Example
function successCallback(response){
voltmx.print("Record Updated successfully");
}
function failureCallback(error)
voltmx.print("Update failed with error: " + JSON.stringify(error));
}
var category = new voltmx.sdk.VMXObj("CATEGORY");
var options = {};
var record = {};
var primaryKeys = {};
record["CATEGORY_DES"] = "Update existing record";
primaryKeys["CATEGORY_ID"] = "1234"
options["primaryKeys"] = primaryKeys;
category.updateByPK(record, options, successCallback, failureCallback);
\<VMXObj>.deleteByPK
Syntax
<VMXObj>.deleteByPK(options, successCallback, failureCallback)
Parameters
Parameter | Type | Description | Required |
options | JSON | A dictionary containing mandatory key primaryKey which contains the PK (column name and value). In case of a composite PK, the dictionary will contain multiple key-value pairs. | Yes |
successCallback | Function | The function is invoked when records are deleted successfully. | Yes |
failureCallback | Function | The function is invoked on an error with the cause of failure as an argument. | Yes |
Example
//Success callback
function successCallback(){
voltmx.print("record updated successfully");
}
//Failure callback
function failureCallback(error){
voltmx.print(error.code);
}
//Sample to delete a record using PK
var categories = new voltmx.sdk.VMXObj(“CATEGORY”);
var primaryKeys = {};
primaryKeys[“CATEGORY_ID”] = ”1234”;
var options = {};
options["primaryKeys"]= primaryKeys;
//Invoke the delete API
categories.deleteByPK(options, successCallback, failureCallback);
//Sample to delete a record using composite primary key
var categories2 = new voltmx.sdk.VMXObj(“CATEGORY2”);
var primaryKeys = {};
primaryKeys[“CATEGORY_KEY1”] = ”1234”;
primaryKeys[“CATEGORY_KEY2”] = ”23”;
var options = {};
options["primaryKeys"]= primaryKeys;
//Invoke the delete API to delete record having composite primary key(CATEGORY_KEY1 and CATEGORY_KEY2)
categories2.deleteByPK(options, successCallback, failureCallback);