Hi,
It wasn’t my intention to bring out ORM library for titanium, but some how i got it out. still lotta features to implement.
My current implementation is very basic with “hasOne”, finder, save, update, destroy and model before/after observer. My goal is not to implement all of them together rather i’ve been implementing them one by one. so i could persist my patient to debug thems thoroughly.
Let’s see how you can use the current version -
Include this library in your app.js
Ti.include('lib/tasawr-jsrecord/database_service.js');
Define your model –
var AccountService = {
TABLE_NAME: 'accounts',
Account: function(name) {
this.id = 0;
this.active = false;
this.name = name;
this.description = "";
this.safetyBalance = 0;
this.currentBalance = 0;
this.totalExpenses = 0;
this.totalIncomes = 0;
this.currency = 'BDT';
this.userId = 0;
// Use this method right after declaring fields otherwise it won't work
DatabaseService.introduceDaoSupport(this, AccountService.TABLE_NAME, {
primaryKey: 'id DESC',
// None database fields could be defined through noDbFields: ['field1', 'field2']
});
// You can map relationship, hasOne: model name, this will be converted to "givenModelNameId"
// Also define model class type.
// this.hasOne('user', {modelClass: UserService.User});
// You can define this.beforeFilter('eventType', callback);
// You can also define this.afterFilter('eventType', callback);
findAccounts: function(options) {
return DatabaseService.Finder.findRows(AccountService.TABLE_NAME, AccountService.Account, options);
}
}
You can see i’ve added AccountService to provide more utility methods for account model specific task. Also defined Account with the account service scope. (you can use it any way you want, there is no convention constraint).
Now you can use the following line of codes –
// Create new account
var a = new AccountService.Account();
a.name = 'Personal';
a.save();
// Update record
a.updateAttributes({ name: 'New name' });
// Find items
var arrayOfAccounts = AccountService.findAccounts({conditions: {name: 'New Name'}});
// Destroy item
a.destroy();
Let me guide you through the code so you could understand how these things are working –
- TABLE_NAME is just holding the preferable database table name
- Defining Account model with attributes and their sample value
- It’s important to provide sample data otherwise all fields will be treated as text
-
DatabaseService.introduceDaoSupport(this, AccountService.TABLE_NAME, {
primaryKey: 'id DESC'
});
This code is introducing all “save, update, destroy, hasOne and other” related functionality
- Finder methods (ie. findAccounts) are kept under AccountService scope so those could be accessed statically
These are the features i’ve already implemented –
- Record save
- Record updateAttributes
- Record finder
- Query builder with fluent interface
- Observer with before and after hook for create, destroy and update
- Relationship with hasOne
I’ve plan to implement the following features as well –
- belongs to
- Has many
- scope
- More default caching behavior
Let’s see how far it goes