BaseModel

SQLite ORM & Inheritance Functions for Titanium


Boydlee Pollentine / @boydleep

Accenture, Bangalore



Structure of a Model


On Initialisation:

  1. Creates all of the objects required by the Model locally
  2. Requires and sets Utilities, Database, Validation & Sync Libs
  3. Fetches the associated table info via the Database.describeTableReader() function
  4. Assigns the Model's columns Array and primary key properties based on information from describeTableReader()
  5. Auto-assigns foreign keys to the foreignKeys Array if requested
  6. Instantiates and performs a login check to the SyncAdapter library.

Creating a New Model


                             var table = "my_sqlite_table";

                             var _init = require("BaseModel"), 

                                               Model = new _init.Model(table);


                             exports.Model = Model;



Extending a Model


                             var table "my_sqlite_table";

                             var _init require("BaseModel"), 

                                               Model = new _init.Model(table);


                            Model.myCustomFunction = function() {

                                var newRecord = this.newRecord();

                                newRecord.dateRightNow = new Date();

                                return newRecord;

                            };


                             exports.Model = Model;


Fetching A Record


      var ContactModel = AppEngine.loadModel('contactModel');
 //this wraps:  require('/sqlModels/contactModel').Model;    
      
      var myRecord = ContactModel.fetchByPrimaryKey('123456');
      Ti.API.info(myRecord);
//is a proper JavaScript object{   id: "123456",
   firstname: "Boydlee",   lastname: "Pollentine",   gendercode: 1,   fullname: "Boydlee Pollentine",   address1_line1: "5 Rigbys Court",   address1_city: "Norwich",   ..........
} 

Fetching Multiple Records


      var allRecords = ContactModel.fetchAll();
      Ti.API.info(allRecords);
//is JavaScript Array of Objects[  { id: "123456", firstname: "Boydlee", lastname: "Pollentine", gendercode: 1,       fullname: "Boydlee Pollentine" .......... },  { id: "9032190", firstname: "Harry", lastname: "Styles", .... },  { id: "9032190", firstname: "Barry", lastname: "Johnson", .... },   { id: "9032190", firstname: "Margeret", lastname: "Braithwaite", .... }

      var _query = "SELECT * FROM contact WHERE city = 'Norwich';
      var someRecords = ContactModel.fetchAllBySqlQuery(_query);
//returns a sqlite result set //use the second optional parameter (BOOLEAN) to objectify the results //e.g. ContactModel.fetchAllBySqlQuery(_query, true); //objectifies results to JS 

Creating a New Record


      var newRecord = ContactModel.newRecord();
      Ti.API.info(newRecord);
//is JavaScript Object that has a set of pre-defined object properties which match//those of the table in the database
{ id: "123456",  firstname: "",  lastname: "",  gendercode: 1,   //will pre-fill if DEFAULT Value exists fullname: "Boydlee Pollentine"  .......... }

Inserting a New Record


      var newRecord = ContactModel.newRecord();
             newRecord.firstname = 'John';
             newRecord.lastname = 'Smith'; 
             newRecord.gendercode = 1;
             //etc etc....

     ContactModel.insert(newRecord);
     //primary keys are auto-generated as GUID's if not provided

Updating a Record


      var newRecord = ContactModel.newRecord();
             newRecord.firstname = 'John';
             newRecord.lastname = 'Smith'; 
             newRecord.gendercode = 1;
             //etc etc....

     ContactModel.insert(newRecord);
     //primary keys are auto-generated as GUID's if not provided

     newRecord.firstname = 'Barry';
     ContactModel.update(newRecord);

Objectify()


Reads in the table column names and database record, 
mapping each SQLite resultset column to a JavaScript object property.

E.g. a table/recordset with the columns "id", "name" and "gender" will get mapped to a JavaScript object that looks like:

{
   id: 1,
   name: "Boydlee",
   gender: "M"
}  

If there is more than 1 row in the SQLite result set, objectify() will produce an Array of objects instead.

BaseModel

By boydlee

BaseModel

  • 1,481