Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

This project exists in an effort to make it as simple as possible to create models in Appcelerator much like ActiveRecord does for Rails without having to duplicate basic finders and CRUD operations.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 helpers
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.rdoc updates to count(). Can now pass in the same params as findAllBy() November 29, 2010
Octocat-spinner-32 appcelerator_database.js added sql logging November 09, 2010
Octocat-spinner-32 appcelerator_record.js
README.rdoc

AppceleratorRecord

(ActiveRecord for Appcelerator)

This project exists in an effort to make it as simple as possible to create models in Appcelerator much like ActiveRecord does for Rails without having to duplicate basic finders and CRUD operations.

Any help on this project would be appreciated.

Includes

I use this as a global include file which gets added to each page (THIS IS AN EXAMPLE)

var pathToRoot = '../';

var files = [
  // helper files
  'AppceleratorRecord/prototype_extensions.js',
  'AppceleratorRecord/array.js',
  'helpers/application.js', // this file doesn't exist, this is an example of another file you could have

  //models
  'AppceleratorRecord/appcelerator_database.js',
  'AppceleratorRecord/appcelerator_record.js',
  'models/order.js'
];

for(var i=0; i<files.length; i++){ Ti.include(pathToRoot + files[i]); }

Usage

In app.js call your models once to create the database and tables

var models = [new Order(), new OtherModel()];
models.each(function(model){ model.init(); }); //run init() once! inside of app.js to create the table, columns and indexes

A new model would look like

//only need to include these once, preferably in a global includes file
Titanium.include('helpers/prototype_extensions.js');
Titanium.include('helpers/array.js');
Titanium.include('appcelerator_database.js');
Titanium.include('appcelerator_record.js');

var Order = function() {
  this.klass = 'Order';
  this.tableName = 'orders';
  this.databaseName = 'mydatabase';
  this.createSQL = "(id integer PRIMARY KEY AUTOINCREMENT DEFAULT NULL, uuid Varchar, name Varchar, placed_at Timestamp)"; //just the columns are needed
  this.indexes = [
    ['user_id', 'item_id'']
  ];
  this.migrations = [
    ['resource_type', 'varchar'] // Added after release, Nov 4th 2010
  ]; //columns that didn't make it into the initial release.  Use this if your app has already been deployed to production.

  var ar = new AppceleratorRecord({databaseName: this.databaseName, tableName: this.tableName, createSQL: this.createSQL, indexes: this.indexes, migrations: this.migrations });
  this.merge(ar);
  this.addLocalMethods(this);

  this.orderExists = function(){
    // custom code here
  };

  return this;
};

We now have full CRUD on a new Order object.

CRUD

var o = new Order();
o.name = "Test Order";
o.save(); //create
o.id // would return the new objects id.
o.name = "Updated Name";
o.save(); // update
o.update('name', 'my updated value'); //update single column
o.destroy(); // destroy

Finders

new Order().all();
new Order().find(id);
new Order().findBy('name', 'Test Order');
new Order().findAllBy('name', 'Test Order'); // array
new Order().findAllBy(['name', 'user_id'], ['Test Order', 1]); // accepts multiple conditions
new Order().findBy('uuid', 'IS NULL'); //find by null/is not null
new Order().first();
new Order().last();
new Order().count();
new Order().count(['name', 'user_id'], ['Test Order', 1]); // accepts multiple conditions
new Order().findOrCreateBy(['name'], ['myValue']); // find or create new object

Destroyers

new Order().destroyAll();
new Order().destroyAll(['user_id'], [123]); //uses findAllBy()

Callbacks

this.destroyCallback = function(){}; // runs after a destroy is called

Indexes

See above in the Usage

Migrations

See above in the Usage

Database Logging

You can set a global variable to enable database logging which will print all SQL calls to the Titanium Developer console

var AppceleratorRecordConfig = { logging: false }; // enable Database Logging

To Do

new Order.sum('columnName');
Something went wrong with that request. Please try again.