Changes between Version 2 and Version 3 of DeveloperGuidelines/EdenMobile/Database


Ignore:
Timestamp:
09/27/16 09:27:06 (9 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/EdenMobile/Database

    v2 v3  
    1919
    2020{{{$emdb}}} automatically bootstraps the database when it is first initialized.
     21
     22== API ==
     23
     24=== table ===
     25
     26The '''table''' API provides fundamental methods to access data in database tables, examples:
     27
     28{{{#!js
     29// Instantiate the API with the table name
     30table = $emdb.table("mytable");
     31
     32// Insert records into the table
     33table.insert({
     34    // The data to insert
     35    'name': 'Example',
     36    'number': 4
     37}, function(recordID) {
     38    // Callback function after successful insert
     39});
     40
     41// Select records from the table - general pattern
     42var fields = ['id', 'name'],
     43    query = 'number > 3';
     44table.select(fields, query, function(result) {
     45    var rows = result.rows,
     46    for (var i=0, len=rows.length; i<len; i++) {
     47        record = rows.item(i);
     48        // do something with the record
     49    }
     50});
     51
     52// Other variants of select:
     53// Extract all records, but only certain fields
     54table.select(fields, function(result) {
     55    // process the result
     56});
     57
     58// Extract all fields, but only certain records
     59table.select(query, function(result) {
     60    // process the result
     61});
     62
     63// Extract all fields from all records
     64table.select(function(result) {
     65    // process the result
     66});
     67
     68}}}
     69
     70Remember that '''all database transactions are asynchronous''', so callback functions may need to perform a {{{$scope.$apply}}} to trigger a digest cycle explicitly.
    2171
    2272== Default Schema ==