天天看点

ActiveRecord.JS 与 Google Gears

ActiveRecord是仿照Rails的ActiveRecord写的js orm框架, 我使用它操作gears 的sqlite数据库。看起来不错,但是文档比较恶心,我按照文档的操作ActiveRecord.define方法根本就不能用,去邮件列表里一看,有个老外和我一样的问题,正确的方法是用ActiveRecord.create方法。也可以用ActiveRecord.execute()方法来创建一个表,用ActiveRecord.create方法也比较省事。 代码如下:

   //创建一个model

   var Customer = ActiveRecord.create('customers',{

     first_name: '',

     updated_at: ''

   });

当然也可以:

   var Customer = ActiveRecord.create('customers');

如果想在create方法里定义字段类型,可以这么做:

var ContactData = ActiveRecord.create('contact_datas',{

   customer_id: {

    type: 'varchar(5)',

    value: ''

   }

});

添加一个实例:

var cu1 = Customer.create({

     first_name: 'Monkey!',

     updated_at: this.FormatDateTime()

         });

注意, Google Gears用的数据库是sqlite,而sqlite的主键自增方式有两种:

方法一:

CREATE TABLE table_name (

id INTEGER PRIMARY KEY,

name TEXT

);

方法二:

id INTEGER PRIMARY KEY AUTOINCREMENT,

兩者只有一點小小的差別,就是在那個 AUTOINCREMENT。

方法一在 INSERT 時,是從 table 裡挑下一個 autoincrement index (最大值 + 1),方法二則會自动生成 sqlite_sequence table ,并在里面紀錄各個 table 目前的 autoincrement index。

所以要達到 TRUNCATE 效果時,後者還要多一個指令把 sqlite_sequence 裡的值刪掉。

DELETE FROM table_name;

DELETE FROM sqlite_sequence WHERE name = table_name;

而ActiveRecord.js的create方法采用的是第一种方法。

我不明白AcriveRecord.js的migration有什么用,不是很方便。

还不清楚ActiveRecord支持不支持单表继承,还没有试。 

继续阅读