This JavaScript API class allows you to interact with the local RHOM database and programatically add models or get references to models.
This API is part of the coreapi
extension that is included automatically.
extensions: ["coreapi"]
Be sure to review the JavaScript API Usage guide for important information about using this API in JavaScript.
If you are going to be using the JS ORM API, you’ll also need to include these lines in any files that will be using it:
<script type="text/javascript" charset="utf-8" src="/public/api/rhoapi-modules-ORM.js"></script> <script type="text/javascript" charset="utf-8" src="/public/api/rhoapi-modules-ORMHelper.js"></script> <script type="text/javascript" charset="utf-8" src="/public/api/rhoapi-modules-Ruby-RunTime.js"></script>
For Ruby access to the RHOM database please see the Rhom Ruby API guide.
Creates an ORM model reference.
Parameters
The name of your model.
Enables Model Properties.
Possible Values :
If set then model is set for RhoConnect Sync.
(Default) Model will be of type propertyBag.
Model will be of type fixedSchema.
Used to create properties on your model.
The name of the property.
The type of property value: String, Float, integer, Varchar, Blob – String is default.
Add an index to your fixedSchema model.
The name of the index.
Array of model column names to use in index, Ex: [‘name’,‘qty’]
Add an index to your fixedSchema model.
The name of the index.
Array of model column names to use in index, Ex: [‘name’,‘qty’]
Add belongs_to association to another defined model, for sync models. Comma-separated String for more than one model.
Set Behavior of this model. Possible property
options and corresponding values.
The partition to use for this model. Partitions can be used to segment the data and keep non-synched data separate from synched models. Possible values
.
Possible Values :
This partition is used automatically if the model is not a sync model.
This partition is used automatically if the model is a sync model.
Sets the type of sync it will be.
Possible Values :
If data model is not a sync model.
Used when the data model is a sync model. Just syncs changes.
Determines how frequently to check for changes. In seconds. Default is 1000.
Synchronous Return:
Method Access:
Rho.ORM.addModel( Anoynomous Function methods)
Deletes all records from the property bag and model tables.
Parameters
True to clean the client_info table, false otherwise.
False on default.
True to clean local (non-synced) models, false otherwise.
Reset all models.
Synchronous Return:
Method Access:
Rho.ORM.databaseFullReset(BOOLEAN resetClientInfo, BOOLEAN resetLocalModels)
Deletes all records from the property bag and model tables. Logout the user from RhoConnectClient. For examples, see Resetting the Database in Using the Local Database with ORM.
Synchronous Return:
Method Access:
Rho.ORM.databaseFullResetAndLogout()
Deletes all records from the property bag and model tables for the model names passed in parameters.
Parameters
Properties map.
Array of models names to reset.
If set to true, client_info table will be cleaned.
False on default.
If set to true, local(non-synced models) will be cleaned.
Reset all models.
Synchronous Return:
Method Access:
Rho.ORM.databaseFullResetEx(HASH propertyMap)
Deletes all records from the property bag and model tables. Logout the user from RhoConnectClient and erase all RhoConnectClient device information. Equivalent to ORM::ORM.databaseFullReset(true) followed by RhoConnectClient.logout.
Synchronous Return:
Method Access:
Rho.ORM.databaseFullclientResetAndLogout()
Reset only local (non-sync-enabled) models.
Synchronous Return:
Method Access:
Rho.ORM.databaseLocalReset()
Export db.
Synchronous Return:
Method Access:
Rho.ORM.export()
Returns the current sync client id.
Synchronous Return:
Method Access:
Rho.ORM.getClientId()
Returns a model.
Parameters
Name of the model you want returned.
Synchronous Return:
Method Access:
Rho.ORM.getModel(String modelName)
Returns true if any of the Rhodes model objects have local database changes that need to be synchronized, false otherwise.
Synchronous Return:
Method Access:
Rho.ORM.haveLocalChanges()
Import db.
Parameters
The name of the zip file.
Synchronous Return:
Method Access:
Rho.ORM.import(STRING zipName)
This example describes creating model and adding model object.
var productModel = Rho.ORM.addModel(function(model){ model.modelName("Product"); model.enable("sync"); model.property("name","string"); model.property("brand","string"); model.property("price","float"); model.set("partition","user"); }); // create model object and save it to database var product = productModel.create({ brand: 'Apple', name: 'iPhone5', price: 199.99}); // read product from database var product = productModel.find('first'); product.get('brand'); // Apple product.get('name'); // iPhone5
Get a model.
// The model 'Product' must have been defined in JavaScript execution earlier // in order for 'getModel' to work properly. // The models generated in RhoStudio, are not accessible in JavaScript unless // the classes are defined via the Rho.ORM.addModel api // Get a model by its name after it has been added var productModel = Rho.ORM.getModel("Product");
Delete all Rhodes model objects for a source, filtering by conditions.
Rho.ORM.databaseFullResetEx({'models' : ['Product', 'Customer'], 'reset_client_info' : true, 'reset_local_models' : true}
In our testing, we have found that, overall, the Fixed Schema database model has much better performance that the same tests run using Property Bag, concerning complex data sets. We strongly recommend that enterprise-grade apps use the Fixed Schema model for their app databases as we have seen test results upwards of 10 times faster using the Fixed Schema model. The only current drawback to Fixed Schema is that it is more complex to initially setup. If your dataset is fairly simple in construction, Property Bag should prove sufficient.