Version 7 (modified by Dominic König, 6 years ago) ( diff )


EdenMobile Database

$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.


Remember that all database transactions are asynchronous! Callback functions may therefore need to perform a $scope.$apply to trigger a digest cycle explicitly.


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
    // 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';, query, function(result) {
    var rows = result.rows,
    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, function(result) {
    // process the result

// Extract all fields, but only certain records, function(result) {
    // process the result

// Extract all fields from all records {
    // process the result

Default Schema

The database schema is stored in in the database itself, in the em_schema table.

For the initial setup of the database, a static emDefaultSchema (in www/js/modules/schema.js) is used:

var emSchemaVersion = '1';

var emDefaultSchema = {

     * Table to store the current schema version
    'em_version': {
        'version': {
            type: 'string',
            label: 'Version',
            notnull: true
        _records: [
            {'version': emSchemaVersion}

     * Table to store table schemas
    'em_schema': {
        'name': {
            type: 'string',
            label: 'Name',
            notnull: true
        'schema': {
            type: 'json',
            label: 'Schema',
            notnull: true

Once this schema has been written to the new database, it will be ignored.


emSQL is a helper module to construct common SQL statements. It can be found in www/js/modules/sql.js.


// Schema definition
var schema = {
    id: {
        type: 'id'
    name: {
        type: 'string',
        label: 'Name',
        notnull: true
    number: {
        type: 'integer',

// Create an emSQL helper for this schema
var table = emSQL.Table('testtable', schema);

// Produce SQL to create the table
var sql = table.create();

// Produce SQL to insert data
// => produces a tuple: an SQL string with placeholders and an array with values (for use with db.executeSql)
// sql == ['INSERT INTO "testtable" (name,number) VALUES (?,?)', ['testing',4]]
sql = table.insert({name: 'testing', number: 4});

// Produce SQL to select data
// sql == 'SELECT, FROM "testtable" WHERE number>2'
sql =['id', 'name'], 'number>2');

// Produce SQL to drop the table
// sql == 'DROP TABLE IF EXISTS "testtable"'
sql = table.drop();

Note: See TracWiki for help on using the wiki.