| | 1 | = EdenMobile Database = |
| | 2 | [[TOC]] |
| | 3 | |
| | 4 | == $emdb Service == |
| | 5 | |
| | 6 | EdenMobile provides the '''$emdb''' service to access the database. |
| | 7 | |
| | 8 | To use the service, it must be included in the controller dependencies: |
| | 9 | |
| | 10 | {{{ |
| | 11 | #!js |
| | 12 | EdenMobile.controller("MyController", [ |
| | 13 | '$scope', '$stateParams', '$emdb', |
| | 14 | function($scope, $stateParams, $emdb) { |
| | 15 | // controller code goes here |
| | 16 | } |
| | 17 | ]); |
| | 18 | }}} |
| | 19 | |
| | 20 | {{{$emdb}}} automatically bootstraps the database when it is first initialized. |
| | 21 | |
| | 22 | == Default Schema == |
| | 23 | |
| | 24 | The database schema is stored ''in'' in the database itself, in the {{{em_schema}}} table. |
| | 25 | |
| | 26 | For the initial setup of the database, a static {{{emDefaultSchema}}} (in www/js/modules/schema.js) is used: |
| | 27 | |
| | 28 | {{{ |
| | 29 | #!js |
| | 30 | var emSchemaVersion = '1'; |
| | 31 | |
| | 32 | var emDefaultSchema = { |
| | 33 | |
| | 34 | /** |
| | 35 | * Table to store the current schema version |
| | 36 | */ |
| | 37 | 'em_version': { |
| | 38 | 'version': { |
| | 39 | type: 'string', |
| | 40 | label: 'Version', |
| | 41 | notnull: true |
| | 42 | }, |
| | 43 | _records: [ |
| | 44 | {'version': emSchemaVersion} |
| | 45 | ] |
| | 46 | }, |
| | 47 | |
| | 48 | /** |
| | 49 | * Table to store table schemas |
| | 50 | */ |
| | 51 | 'em_schema': { |
| | 52 | 'name': { |
| | 53 | type: 'string', |
| | 54 | label: 'Name', |
| | 55 | notnull: true |
| | 56 | }, |
| | 57 | 'schema': { |
| | 58 | type: 'json', |
| | 59 | label: 'Schema', |
| | 60 | notnull: true |
| | 61 | } |
| | 62 | } |
| | 63 | }; |
| | 64 | }}} |
| | 65 | |
| | 66 | Once this schema has been written to the new database, it will be ignored. |
| | 67 | |
| | 68 | == emSQL == |
| | 69 | |
| | 70 | {{{emSQL}}} is a helper module to construct common SQL statements. It can be found in www/js/modules/sql.js. |
| | 71 | |
| | 72 | Examples: |
| | 73 | {{{ |
| | 74 | #!js |
| | 75 | |
| | 76 | // Schema definition |
| | 77 | var schema = { |
| | 78 | id: { |
| | 79 | type: 'id' |
| | 80 | }, |
| | 81 | name: { |
| | 82 | type: 'string', |
| | 83 | label: 'Name', |
| | 84 | notnull: true |
| | 85 | }, |
| | 86 | number: { |
| | 87 | type: 'integer', |
| | 88 | }, |
| | 89 | }; |
| | 90 | |
| | 91 | // Create an emSQL helper for this schema |
| | 92 | var table = emSQL.Table('testtable', schema); |
| | 93 | |
| | 94 | // Produce SQL to create the table |
| | 95 | // sql == 'CREATE IF NOT EXISTS "testtable" (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,number INTEGER)' |
| | 96 | var sql = table.create(); |
| | 97 | |
| | 98 | // Produce SQL to insert data |
| | 99 | // => produces a tuple: an SQL string with placeholders and an array with values (for use with db.executeSql) |
| | 100 | // sql == ['INSERT INTO "testtable" (name,number) VALUES (?,?)', ['testing',4]] |
| | 101 | sql = table.insert({name: 'testing', number: 4}); |
| | 102 | |
| | 103 | // Produce SQL to drop the table |
| | 104 | // sql == 'DROP TABLE IF EXISTS "testtable"' |
| | 105 | sql = table.drop(); |
| | 106 | |
| | 107 | }}} |