Permalink
Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
866 lines (650 sloc) 30.1 KB
# Using the Local Database with Rhom
Rhom is a mini database object mapper for Rhodes. It provides a high level interface to make it very powerful and simple to use a local database. That database is SQLite on all platforms except BlackBerry where it is HSQLDB.
Rhom currently supports two model types: ***Property Bag (default)*** and ***Fixed Schema***
## Property Bag
With a property bag model, all data is stored in a single table using the object-attribute-value pattern also referred to as the [Entity-attribute-value model](http://en.wikipedia.org/wiki/Entity-attribute-value_model).
### Property Bag Advantages
* Simple to use, it doesn't require specifying attributes.
* Data migrations are not necessary.
* Attributes can be added or removed without modifying the database schema.
### Property Bag Disadvantages
* For some applications, the database size may be significantly larger than fixed schema. This is because each attribute is indexed for fast lookup.
* Sync process may be slightly slower because inserts are performed at attribute level.
In a property bag model, Rhom groups objects by their source id and object id. The following example illustrates this idea:
<pre>
Source ID: 1, Model Name: Account
+-----------+----------+--------------+----------------------+
| source_id | attrib | object | value |
+-----------+----------+--------------+------- --------------+
| 1 | name | 48f39f63741b | A.G. Parr PLC 37862 |
| 1 | industry | 48f39f63741b | Entertainment |
| 1 | name | 48f39f230529 | Jones Group |
| 1 | industry | 48f39f230529 | Sales |
+-----------+----------+--------------+----------------------+
</pre>
Here, Rhom will expose a class `Account` with two attributes: `name` and `industry`
:::ruby
account = Account.find('48f39f63741b')
account.name
#=> "A.G. Parr PLC 37862"
account.industry
#=> "Entertainment"
### Using Property Bag Models
To use a property bag model, simply generate a new model with some attributes:
:::term
$ rhodes model product name,brand,price,quantity,sku
This will generate a file called `product.rb` which looks like:
:::ruby
class Product
include Rhom::PropertyBag
# Uncomment the following line to enable sync with Product.
# enable :sync
#add model specific code here
end
There are several features you can enable or disable in the model, below is a complete list:
:::ruby
class SomeModel
include Rhom::PropertyBag
# rhoconnect settings
# Enable sync for this model.
# Default is disabled.
enable :sync
# Set the type of sync this model
# will use (default :incremental).
# Set to :bulk_only to disable incremental
# sync and only use bulk sync.
set :sync_type, :bulk_only
# Set the sync priority for this model.
# 1000 is default, set to lower number
# for a higher priority.
set :sync_priority, 1
# Instruct Rhom to send all attributes
# to RhoConnect when an object is updated.
# Default is disabled, only changed attributes
# are sent.
enable :full_update
# RhoConnect provides a simple way to keep data out of redis.
# If you have sensitive data that you do not want saved in redis,
# add the pass_through option in settings/settings.yml for each source.
# Add pass_through to client model definition
enable :pass_through
# model settings
# Define how data is partitioned for this model.
# For synced models default is :user.
# For non-synced models default is :local
# If you have an :app partition
# for your RhoConnect source adapter and use bulk sync,
# set this to :app also.
set :partition, :app
# Define blob attributes for the model.
# :blob Declare property as a blob type
#
# :overwrite (optional) Overwrite client copy
# of blob with new copy from server.
# This is useful when RhoConnect modifies
# images sent from Rhodes, for example
# zooming or cropping.
property :image_url, :blob, :overwrite
# You can define your own properties also
property :mycustomproperty, 'hello'
end
## Fixed Schema
With a fixed schema model, each model has a separate database table and each attribute exists as a column in the table. In this sense, fixed schema models are similar to traditional relational tables.
### Fixed Schema Advantages
* Smaller database size, indexes can be specified only on specific attributes.
* Sync process may perform faster because whole objects are inserted at a time.
### Fixed Schema Disadvantages
* Schema changes must be handled with data migrations.
* Database performance may be slow unless you specify proper indexes.
### Using Fixed Schema Models
Using a fixed schema model involves an additional step to using a property bag model.
First, generate the model using the `rhodes` command:
:::term
$ rhodes model product name,brand,price,quantity,sku
Next, change the include statement in `product.rb` to `include Rhom::FixedSchema` and add the attributes:
:::ruby
class Product
include Rhom::FixedSchema
# Uncomment the following line to enable sync with Product.
# enable :sync
property :name, :string
property :brand, :string
property :price, :string
property :quantity, :string
property :sku, :string
property :int_prop, :integer
property :float_prop, :float
property :date_prop, :date #translate to integer type
property :time_prop, :time #translate to integer type
end
That's it! Now your model is a fixed schema model, the table will be generated automatically for you when the application launches.
Below is a full list of options available to fixed schema models:
:::ruby
class SomeModel
include Rhom::FixedSchema
# rhoconnect settings
# Enable sync for this model.
# Default is disabled.
enable :sync
# Set the type of sync this model
# will use (default :incremental).
# Set to :bulk_only to disable incremental
# sync and only use bulk sync.
set :sync_type, :bulk_only
# Set the sync priority for this model.
# 1000 is default, set to lower number
# for a higher priority.
set :sync_priority, 1
# Instruct Rhom to send all attributes
# to RhoConnect when an object is updated.
# Default is disabled, only changed attributes
# are sent.
enable :full_update
# RhoConnect provides a simple way to keep data out of redis.
# If you have sensitive data that you do not want saved in redis,
# add the pass_through option in settings/settings.yml for each source.
# Add pass_through to client model definition
enable :pass_through
# model settings
# Define how data is partitioned for this model.
# Default is :user. If you have an :app partition
# for your RhoConnect source adapter and use bulk sync,
# set this to :app also.
set :partition, :app
# Set the current version of the fixed schema.
# Your application may use it for data migrations.
set :schema_version, '1.0'
# Define fixed schema attributes.
# :string and :blob types are supported.
property :name, :string
property :tag, :string
property :phone, :string
property :image_url, :blob
# Define a named index on a set of attributes.
# For example, this will create index for name and tag columns.
index :by_name_tag, [:name, :tag]
# Define a unique named index on a set of attributes.
# For example, this will create unique index for the phone column.
unique_index :by_phone, [:phone]
# Define blob attributes for the model.
# :blob Declare property as a blob type
#
# :overwrite (optional) Overwrite client copy
# of blob with new copy from server.
# This is useful when RhoConnect modifies
# images sent from Rhodes, for example
# zooming or cropping.
property :image_url, :blob, :overwrite
# You can define your own properties also
property :mycustomproperty, 'hello'
end
## Fixed Schema Data Migrations
Rhom provides an application hook to migrate the data manually. You can also use this hook to run business logic related to updating the database. For example, your application may want to display a customized alert notifying the user that a migration is in progress and it may take a few moments.
To use this hook, first we need to track the `:schema_version` in our model:
:::ruby
class Product
include Rhom::FixedSchema
set :schema_version, '1.1'
end
Next, we will implement the following hook in our `application.rb` class:
#### `on_migrate_source(old_version, new_src)`
This is called on application start when `:schema_version` has changed.
:::ruby
class AppApplication < Rho::RhoApplication
# old_version String containing old version value (i.e. '1.0')
# new_src Hash with source information:
# 'schema_version', 'name', 'schema'
# new_src['schema']['sql'] contains new schema sql
def on_migrate_source(old_version, new_src)
# ... do something like alert user ...
db = Rho::RHO.get_src_db(new_src['name'])
db.execute_sql("ALTER TABLE #{new_src['name']} ADD COLUMN mytest VARCHAR DEFAULT null")
true # does not create table
end
end
**NOTE: To modify schema without recreate table, you can use only ADD COLUMN command, you cannot remove column or change type(This is sqlite limitation) **
Return `false` to run the custom sql specified by the new_src['schema']['sql'] string:
:::ruby
def on_migrate_source(old_version, new_src)
# ... do something like alert user ...
false # create table by source schema - useful only for non-synced models
end
**NOTE: For sync sources, you cannot just recreate table without data copy. Because server will not send this data at sync time. **
## Property Bag Data Migrations
No data migration required, since all attributes are dynamic.
If you want to remove all local data when upgrading to new application version: change `app_db_version` in `rhoconfig.txt`.
This scenario will work for Property Bag and Fixed Schema models.
## Rhom API
Below is the full list of links to the [Rhom API methods](/rhodesapi/rhom-api) available to Rhom models.
* [client_id](/rhodesapi/rhom-api#clientid) - Returns the current sync client id.
* [clear_notification](/rhodesapi/rhom-api#clearnotification) - Clear notification for the object.
* [delete_all](/rhodesapi/rhom-api#deleteall) - Delete all Rhodes model objects for a source, filtering by conditions.
* [destroy](/rhodesapi/rhom-api#destroy) - Destroy a Rhodes model object.
* [find](/rhodesapi/rhom-api#find) - Find Rhodes model objects.
* [find_all](/rhodesapi/rhom-api#findall) - Alias for modelname.find(:all, argument list).
* [find_by_sql](/rhodesapi/rhom-api#findbysql) - Returns Rhodes model object(s) based on sql_query. This method works only for schema models.
* [new](/rhodesapi/rhom-api#new) - Create a new Rhodes model object.
* [create](/rhodesapi/rhom-api#create) - Create a new Rhodes model object and save it to the database.
* [paginate](/rhodesapi/rhom-api#paginate) - Call `find` with a limit on the number of records.
* [sync](/rhodesapi/rhom-api#sync) - Start the sync process for a Rhodes model.
* [can_modify](/rhodesapi/rhom-api#canmodify) - Returns true if the Rhodes model object is not currently being synced (if it is being synced, you should disable editing of the object).
* [changed?](/rhodesapi/rhom-api#changed) - Returns true if a Rhodes model object has local database changes that need to be synchronized, false otherwise.
* [have_local_changes?](/rhodesapi/rhom-api#havelocalchanges) - Returns true if any of the Rhodes model objects have local database changes that need to be synchronized, false otherwise.
* [save](/rhodesapi/rhom-api#save) - Saves the current Rhodes model object to the database.
* [set_notification](/rhodesapi/rhom-api#setnotification) - Set a notification to be called when the sync is complete for this model.
* [update_attributes](/rhodesapi/rhom-api#updateattributes) - Updates the current Rho model object attributes and saves it to the database.
* [database_fullclient_reset_and_logout](/rhodesapi/rhom-api#databasefullclientresetandlogout) - Reset the Rhodes model database and logout.
* [database_full_reset](/rhodesapi/rhom-api#databasefullreset) - Deletes all records from the property bag and model tables.
* [database_full_reset_and_logout](/rhodesapi/rhom-api#databasefullresetandlogout) - Perform a full reset, then logout the RhoConnect client.
* [database_local_reset](/rhodesapi/rhom-api#databaselocalreset) - Reset only local (non-sync-enabled) models.
* [metadata](/rhodesapi/rhom-api#metadata) - Returns the metadata definition for a given model as a hash.
* [metadata=(metadata_def)](/rhodesapi/rhom-api#metadatametadatadef) - Assigns the JSON metadata definition for a given model.
### Rhom Delete Method Examples
Deletes all rhom objects for a source, optionally filtering by conditions:<a id="rhom-delete-example" />
:::ruby
# :conditions Delete only objects matching these criteria.
# Supports find() conditions.
# :op See advanced find syntax
Account.delete_all(:conditions => {'industry'=>'electronics'})
Delete a rhom object.
:::ruby
@account = Account.find(:all).first
@account.destroy
### Rhom Find Method Examples
Find Rhom objects.<a id="rhom-find-example" />
:::ruby
acct = Account.find "3560c0a0-ef58-2f40-68a5-48f39f63741b"
acct.name
#=> "A.G. Parr PLC 37862"
accts = Account.find(:all, :select => ['name','address'])
accts[0].name
#=> "A.G. Parr PLC 37862"
accts[0].telephone
#=> nil
**NOTE: Use SQL fragments with caution. They are considerably slower than advanced queries [described below](/rhodes/rhom#advanced-queries). You also have to specify :select parameter.**
#### Order Examples
The `:order` argument for `find` accepts several forms.
`:order` by one attribute.
:::ruby
@accts = Account.find(
:all,
:order => 'name',
:orderdir => 'DESC'
)
`:order` by one attribute with a block.
:::ruby
@accts = Account.find(:all, :order => 'name') do |x,y|
y <=> x
end
`:order` with a block.
:::ruby
@accts = Account.find(:all) do |item1,item2|
item2.name <=> item1.name
end
`:order` by multiple attributes.
:::ruby
@accts = Account.find(
:all,
:order => ['name', 'industry'],
:orderdir => ['ASC', 'DESC']
)
`find_by_sql(sql_query)` returns rhom object(s) based on sql_query. This method works only for schema models.<a id="rhom-findsql-example" />
:::ruby
@accts = Account.find_by_sql("SELECT * FROM Account")
### Rhom New and Create Method Examples
Create a new rhom object and assign given attributes.<a id="rhom-new-example" />
:::ruby
@account = Account.new(
{"name" => "ABC Inc.","address" => "555 5th St."}
)
@account.name
#=> "ABC Inc."
Create a new rhom object and save to the database.<a id="rhom-create-example" />
:::ruby
@account = Account.create(
{"name" => "some new record", "industry" => "electronics"}
)
### Rhom Paginate Method Example
Paginate calls `find` with a limit on the # of records. This emulates rails' classic pagination syntax. Default page size is 10.<a id="rhom-paginate-example" />
Account.paginate(:page => 0)
#=> returns first 10 records
Account.paginate(:page => 1, :per_page => 20)
#=> returns records 21-40
Account.paginate(
:page => 5,
:conditions => {'industry' => 'Technology'},
:order => 'name'
) #=> you can have :conditions and :order as well
### Rhom Sync and set_notification Method Example
Start the sync process for a model. In this example, the value for @params["sku"] -- the value of the sku -- must be URL encoded.<a id="rhom-sync-example" />
:::ruby
Product.sync( url_for(:action => :sync_callback), "", false, "query=#{@params["sku"]}" )
Set a notification to be called when the sync is complete for this model. This is useful for example if you want to refresh the current list page or display an alert when new data is synchronized. See the [sync notification docs](/rhodes/synchronization#notifications) for more information.
:::ruby
Account.set_notification( url_for(:action => :sync_notify) )
### Rhom update_attributes Method Example
Update the current rhom object's attributes and saves it to the database.<a id="rhom-update-example" />
**NOTE: This is the fastest way to add or update item attributes.**
:::ruby
@account = Account.find(
:all,
:conditions => {'name' => 'ABC Inc.'}
)
@account.update_attributes(
{"name" => "ABC Inc.", "industry" => "Technology"}
)
@account.industry
#=> "Technology"
Saves the current rhom object to the database.<a id="rhom-save-example" />
:::ruby
@account = Account.new(
{"name" => "some new record", "industry" => "electronics"}
)
@account.save
### Rhom can_modify Method Example
Before displaying an edit page for an object, your application can check if the object is currently being accessed by the sync process. If it is, you should disable editing of the object. `can_modify` could return true, for example, on a new local record that was created and sent to the RhoConnect application, but no response has been received yet.<a id="rhom-canmodify-example" />
:::ruby
def edit
@product = Product.find(@params['id'])
if @product && !@product.can_modify
render :action => :show_edit_error
else
render :action => :edit
end
end
### Rhom changed? Method Example
Determine if a rhom model has local database changes that need to be synchronized.<a id="rhom-changed-example" />
:::ruby
def should_sync_product_object
if Product.changed?
#... do stuff ...
end
end
### Rhom metadata Method Examples
Returns the [metadata](/rhoconnect/metadata) for a given model.<a id="rhom-metadata-example" />
:::ruby
Product.metadata
#=> {'foo' => 'bar'}
Assigns the [metadata](/rhoconnect/metadata) for a given model.
:::ruby
Product.metadata = { 'foo' => 'bar' }.to_json
## Associations
Rhom does not support Ruby on Rails associations. However, Rhom has a [sync association](/rhodes/rhom#sync-associations) called `belongs_to` which you can use to trigger updates on sync-enabled models.
## Sync Associations
For sync-enabled models, Rhom has a `belongs_to` sync association as a means to automatically trigger sync updates for dependent objects. This is useful where you have relationships between backend service objects.
For example, you can have a list of customers who have purchased a product, and thus you can have them belong to that product:
:::ruby
class Customer
include Rhom::PropertyBag
# Declare container model and attribute.
belongs_to :product_id, 'Product'
end
In your `product_controller.rb`, assign the `belongs_to` attribute when a product is created:
:::ruby
def create
@product = Product.new(@params['product'])
@product.save
cust = Customer.find(:first) # find the customer
cust.product_id = @product.object
cust.save
redirect :action => :index
end
You can also define polymorphic sync associations, or sync associations across multiple classes.
Using array notation:
:::ruby
belongs_to :parent_id, ['Product', 'Cases']
Or multiple declarations:
:::ruby
belongs_to :parent_id, 'Product'
belongs_to :parent_id, 'Cases'
**NOTE: After a new product is created, the `:product_id` for the `Customer` records will be updated to the new value.**
If you are planning to use bulk sync feature for your associated models, then you should take into consideration
corresponding support on RhoConnect server side. See [RhoConnect Bulk Sync associations](/rhoconnect/bulk-sync#bulk-sync-associations).
## Freezed models
If you want to limit model attributes by specific list - you can 'freeze' model:
:::ruby
class Customer
include Rhom::PropertyBag
enable :sync
set :freezed, true
property :address, :string
property :city, :string
property :email, :string
end
For such models if you try to add not listed property - you will get ArgumentError exception:
:::ruby
obj = Customer.new( :wrong_address => 'test') #will raise ArgumentError exception
obj = Customer.create( :wrong_address => 'test') #will raise ArgumentError exception
obj = Customer.new
obj.wrong_address = 'test' #will raise ArgumentError exception
obj = Customer.new
obj.update_attributes(:wrong_address => 'test') #will raise ArgumentError exception
**NOTE: FixedSchema models are 'freezed' by default.**
## Accessing Sync Info with RhomSource
Rhom exposes sync information as a `RhomSource` object. You can use this information for alerts, status pages, etc.
To access a RhomSource, load it by name:
:::ruby
@source = RhomSource.find('source_name')
Then you can get statistics and information about the source, such as the name.
:::ruby
@source.name
#=> "Product"
Here are the available statistics, from the [rhomsource API](/rhodesapi/rhomsource-api).
* [find](/rhodesapi/rhomsource-api#find) - Returns a RhomSource object.
* [source-id](/rhodesapi/rhomsource-api#sourceid) - Returns the id number of a source.
* [name](/rhodesapi/rhomsource-api#name) - Returns the name of the source.
* [last_updated](/rhodesapi/rhomsource-api#lastupdated) - returns the last time the source was successfully synchronized (in `Time.at` format).
* [last_inserted_size](/rhodesapi/rhomsource-api#lastinsertedsize) - returns the number of records inserted on last sync.
* [last_deleted_size](/rhodesapi/rhomsource-api#lastdeletedsize) - returns the number of records deleted on the last sync.
* [last_sync_duration](/rhodesapi/rhomsource-api#lastsyncduration) - returns the duration in milliseconds of the last sync for this source.
* [last_sync_success](/rhodesapi/rhomsource-api#lastsyncsuccess) - returns 1 if last sync was successful, 0 if it failed.
* [distinct_objects](/rhodesapi/rhomsource-api#distinctobjects) - returns the number of records for this source.
This example shows the formatted time for the `Product` model.
:::ruby
RhomSource.find(
Product.get_source_name
).last_updated.strftime("%m/%d/%Y, %I:%M%p")
#=> "01/19/2011, 06:40PM"
## Resetting the Database
Rhodes provides the following functions for recovering the database from a bad or corrupt state, or if the RhoConnect server returns errors.
### `Rhom::Rhom.database_full_reset(reset_client_info=false, reset_local_models=true)`
Deletes all records from the property bag and model tables.
:::ruby
# reset_client_info If set to true, client_info
# table will be cleaned.
#
# reset_local_models If set to true, local(non-synced models)
# will be cleaned.
Rhom::Rhom.database_full_reset(false,true)
### `Rhom::Rhom.database_full_reset_and_logout`
Perform a full reset and then logout the RhoConnect client.
:::ruby
Rhom::Rhom.database_full_reset_and_logout
### `Rhom::Rhom.database_fullclient_reset_and_logout`
Equivalent to `Rhom::Rhom.database_full_reset(true)` followed by `SyncEngine.logout`.
:::ruby
Rhom::Rhom.database_fullclient_reset_and_logout
**NOTE: If you receive a sync error "Unknown client" message in your sync callback, this means that the RhoConnect server no longer knows about the client and a `Rhom::Rhom.database_fullclient_reset_and_logout` is recommended. This error requires proper intervention in your app so you can handle the state before resetting the client. For example, your sync notification could contain the following:**
:::ruby
if @params['error_message'].downcase == 'unknown client'
puts "Received unknown client, resetting!"
Rhom::Rhom.database_fullclient_reset_and_logout
end
### `Rhom::Rhom.database_local_reset`
Reset only local(non-sync-enabled) models.
:::ruby
Rhom::Rhom.database_local_reset
### `Rhom::Rhom.database_full_reset_ex( :models => [model_name1, model_name2], :reset_client_info=>false, :reset_local_models => true)`
Deletes all records from the property bag and model tables, if models are set then reset only selected models
:::ruby
# models Array of models names to reset
# reset_client_info If set to true, client_info
# table will be cleaned.
#
# reset_local_models If set to true, local(non-synced models)
# will be cleaned.
Rhom::Rhom.database_full_reset_ex(:models => ['Product', 'Customer'])
## Seeding the Database
If your application requires seeding some initial data, you can use [RhoUtils.load_offline_data](/rhodesapi/rhoutils-api#loadofflinedata).
For example, in the rhodes/spec/framework_spec, we use `load_offline_data` to seed the device database for each test:
:::ruby
Rho::RhoUtils.load_offline_data(
['client_info','object_values'], 'spec'
)
In this example, there is a 'spec/fixtures' directory which contains a `client_info.txt` and `object_values.txt` pipe-delimited files. These files are structured as follows:
`client_info.txt`:
<pre>
client_id|last_sync_success
67320d31-e42e-4156-af91-5d9bd7175b08|
</pre>
`object_values.txt`:
<pre>
source_name|attrib|object|value
Case|status|4900dc4c072c|New|
Case|assigned_user_id|4900dc4c072c|48fce5e9fb16|
Case|work_log|4900dc4c072c||
Case|priority|4900dc4c072c|High|
...
</pre>
**NOTE: The column names are always the first line of the file.**
## Advanced Queries
### `find(*args)` (advanced conditions)
Rhom also supports advanced find `:conditions`. Using advanced `:conditions`, rhom can optimize the query for the property bag table.
Let's say we have the following SQL fragment condition:
:::ruby
Product.find(
:all,
:conditions => [
"LOWER(description) like ? or LOWER(title) like ?",
query,
query
],
:select => ['title','description']
)
Using advanced `:conditions`, this becomes:
:::ruby
Product.find(
:all,
:conditions => {
{
:func => 'LOWER',
:name => 'description',
:op => 'LIKE'
} => query,
{
:func => 'LOWER',
:name => 'title',
:op => 'LIKE'
} => query
},
:op => 'OR',
:select => ['title','description']
)
You can also use the 'IN' operator:
:::ruby
Product.find(
:all,
:conditions => {
{
:name => "image_uri",
:op => "IN"
} => "'15704','15386'"
}
)
# or use array notation
Product.find(
:all,
:conditions => {
{
:name => "image_uri",
:op => "IN"
} => ["15704","15386"]
}
)
You can also group `:conditions`:
:::ruby
cond1 = {
:conditions => {
{
:func => 'UPPER',
:name => 'name',
:op => 'LIKE'
} => query,
{
:func => 'UPPER',
:name => 'industry',
:op => 'LIKE'
} => query
},
:op => 'OR'
}
cond2 = {
:conditions => {
{
:name => 'description',
:op => 'LIKE'
} => 'Hello%'
}
}
@accts = Account.find(
:all,
:conditions => [cond1, cond2],
:op => 'AND',
:select => ['name','industry','description']
)
## Find by numeric field
To use number comparison conditions in find use CAST :
:::ruby
@accts = Account.find(:all,
:conditions => { {:func=> 'CAST', :name=>'rating as INTEGER', :op=>'<'} => 3 } )
#or using sql query:
size = 3
@accts = Account.find(:all,
:conditions => ["CAST(rating as INTEGER)< ?", "#{size}"], :select => ['rating'] )
## Database Encryption
**NOTE: As of Rhodes version 3.3.3, [Rhom data encryption](/rhodes/rhom#database-encryption) is removed from Rhodes. This feature is only supported in RhoMobile Suite. If you wish to use this feature, you will need to [upgrade to RhoMobile Suite](/rhomobile-install). Your application's build.yml will also need to be modified to [indicate the application type is 'Rhoelements']. Additionally, a [RhoElements license](/rhoelements/licensing) is required.**
If your application requires that the local database is encrypted on the filesystem, you can enable it by setting a flag in `build.yml`:
:::yaml
encrypt_database: 1
**NOTE: Database encryption is not supported for applications that use bulk sync at this time.**
### Platform Notes
* iOS: Uses AES 128 encryption algorithm from iOS SDK.
* Android: Uses AES 128 ecryption algorithm from Android SDK.
* Windows Mobile: Uses RC4 algorithm from Windows Mobile SDK.
### Blackberry Notes
* Any Blackberry versions: Uses AES 128 ecryption algorithm from Blackberry JDK with HSQL database
* Blackberry JDE >= 5.0 with SQLITE: Uses built-in encryption library for SQLite database.
**NOTE: Bulk sync is not supported in this mode.**
* Any Blackberry Version: The user can turn on memory encryption (device memory and sdcard), This policy can also can be enforced by the Blackberry enterprise server: <a href="http://docs.blackberry.com/en/admin/deliverables/3940/file_encryption_STO.pdf"/>
**NOTE: In this case you have to use HSQLDB even on Blackberry device OS >= 5.0, because SQLite does not encrypt database file. You can force Rhodes to use HSQLDB for all Blackberry OS versions by adding the following to `build.yml`:**
:::yaml
bb:
use_sqlite: 0
## Performance Tips
* Before test application for performance set warning log level in rhoconfig.txt(MUST set for Blackberry testing):
MinSeverity = 3
* All database modification operations can be slow, especially on big databases. So optimize object modification - prepare data and call create/update_attributes once
* Do not use sql conditions in Model.find, use Advanced Queries.
* Use Model.create to insert object to database
* Use update_attributes to add or modify object attributes
* To insert/update multiple object/models use database transaction
db = ::Rho::RHO.get_src_db('Model')
db.start_transaction
begin
items.each do |item|
# create hash of attribute/value pairs
data = {
:field1 => item['value1'],
:field2 => item['value2']
}
# Creates a new Model object and saves it
new_item = Model.create(data)
end
db.commit
rescue
db.rollback
end
*NOTE: If ::Rho::RHO.get_src_db('Model') returns nil, it means that you never called this model's methods before (models are loaded by demand). To fix it, call 'require_source'.
:::ruby
require_source 'Model'
*NOTE: see more tips here: [How can I create a lot of objects in controller action](/faq#how-can-i-create-a-lot-of-objects-in-controller-action).