yORM is an ORM built specifically for Titanium Appcelerator.
The entry point is BaseEntity. Write a javascript class that extends BaseEntity using prototypes. This class serves as your Entity class (or a table in database).
###Sample Code
Include the single entity.js file
Ti.include('yORM/entity.js');
Declare a function like this
function UserEntity() {
//add some of your own methods specific to User
};
Then extend BaseEntity like this
var dbName = "Your Database Name";
var tableName = "Your Table Name (used in the database)";
var fields = {
'field1': 'SQLLite_DataType',
'field2: 'SQLLite_DataType',
'field3: 'SQLLite_DataType'
};
UserEntity.prototype = new BaseEntity(dbName, tableName, fields);
var user = new UserEntity();
The rest is easy
var user = new UserEntity();
user.field1 = 'field1_value';
user.field2 = 'field2_value';
user.field2 = 'field3_value';
user.save();
Ti.API.info(user.id);
Entity Methods.
save()
- Insert into the table that this entity represents. Call this method after setting the values of the Entity. If you down't set any value, the default is null. The entity's id field is automatically updated with the id of inserted item.
removeAll()
- Deletes from the table WITHOUT a where clause
remove()
- Deletes from the table where id is the id of this entity
all()
- Retrieves all records in the table
find(id, clauses)
- Find a single record by id. Optionally you can specify clauses. clauses can bewhere
,order
,limit
, etc clauses supported by SQLLite. Method always return a valid Entity. If there are no records found for the id, the Entity fields will be null.
findBy(fieldsAndValuesObject)
- Find a single record. The where clause is generated from the object fieldsAndValuesObject. The return value is the first record found. If there are no records found, a valid entity will still be returned but the fields will be null. fieldsAndValuesObject is a javascript object. For example to find where name is 'Don' and age is 19 we will writenew User().findBy({'name':'Don', 'age':19})
findAllBy(fieldsAndValuesObject)
- Find all records. The where clause is as in findBy(fieldsAndValuesObject) method. The method returns a list. If there are no records found, an empty list is returned.