| 21 | |
| 22 | == API == |
| 23 | |
| 24 | === table === |
| 25 | |
| 26 | The '''table''' API provides fundamental methods to access data in database tables, examples: |
| 27 | |
| 28 | {{{#!js |
| 29 | // Instantiate the API with the table name |
| 30 | table = $emdb.table("mytable"); |
| 31 | |
| 32 | // Insert records into the table |
| 33 | table.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 |
| 42 | var fields = ['id', 'name'], |
| 43 | query = 'number > 3'; |
| 44 | table.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 |
| 54 | table.select(fields, function(result) { |
| 55 | // process the result |
| 56 | }); |
| 57 | |
| 58 | // Extract all fields, but only certain records |
| 59 | table.select(query, function(result) { |
| 60 | // process the result |
| 61 | }); |
| 62 | |
| 63 | // Extract all fields from all records |
| 64 | table.select(function(result) { |
| 65 | // process the result |
| 66 | }); |
| 67 | |
| 68 | }}} |
| 69 | |
| 70 | Remember that '''all database transactions are asynchronous''', so callback functions may need to perform a {{{$scope.$apply}}} to trigger a digest cycle explicitly. |