| Version 11 (modified by , 9 years ago) ( diff ) |
|---|
EdenMobile Database
Table of Contents
$emdb Service
EdenMobile provides the $emdb service to access the database.
To use the service, it must be included in the controller dependencies:
EdenMobile.controller("MyController", [
'$scope', '$stateParams', '$emdb',
function($scope, $stateParams, $emdb) {
// controller code goes here
}
]);
$emdb automatically creates the database and tables when it is initialized for the first time.
API
Remember that all database transactions are asynchronous!
Callback functions may therefore need to perform a $scope.$apply to trigger a digest cycle explicitly.
table
The table API provides fundamental methods to access data in database tables, examples:
// Instantiate the API with the table name
table = $emdb.table("mytable");
// Insert records into the table
table.insert({
// The data to insert
'name': 'Example',
'number': 4
}, function(recordID) {
// Callback function after successful insert
});
// Select records from the table - general pattern
var fields = ['id', 'name'],
query = 'number > 3';
table.select(fields, query, function(result) {
var rows = result.rows,
record;
for (var i=0, len=rows.length; i<len; i++) {
record = rows.item(i);
// do something with the record
}
});
// Other variants of select:
// Extract all records, but only certain fields
table.select(fields, function(result) {
// process the result
});
// Extract all fields, but only certain records
table.select(query, function(result) {
// process the result
});
// Extract all fields from all records
table.select(function(result) {
// process the result
});
Default Schema
The database schema is stored in the mobile database, in the em_schema table.
For the initial setup of the database, a static emDefaultSchema (in www/config/schema.js) is used. Once this schema has been written to the new database, it will be ignored.
The emDefaultSchema service also defines the meta-fields which are to be added to all user tables.
emSQL
$emDB uses the emSQL service to construct common SQL statements. It can be found in www/services/sql.js.

