JaxerでActiveJS – ActiveRecordを使うと、簡単にDBをいじれます。そのサンプル。
SQLiteを使う時のサンプルです。
このサンプルでは、以下のようなDBを作ります。これが全部終わった後の状態。

コードちょい解説
とりあえずDBに接続。sqliteのファイル名を指定できる。最初にこれやらないとだめ。
/**
* Connect first
* If you use sqlite, Jaxer create 'appdata.sqlite' in db directory.
* You can specify this db file name by third argument.
*/
ActiveRecord.connect(ActiveRecord.Adapters.JaxerSQLite);
// following works too.
// ActiveRecord.connect(ActiveRecord.Adapters.JaxerSQLite,'filenamehere.sqlite');
logを有効にすると、Jaxer.Log.info()でログを取りまくってくれる。
/** * Logging * Following enables logging. * If true, ActiveRecord execute Jaxer.Log.info() for each sql execution or something like it. */ ActiveRecord.logging = true;
直接SQL文を実行させるには以下のような感じ
/**
* Execute simple sql
* After you executed ActiveRecord.connect(),
* You can use ActiveRecord.execute() to execute sql.
*/
ActiveRecord.execute('CREATE TABLE IF NOT EXISTS exampletable1 (id INTEGER PRIMARY KEY, username, mailaddr, profile)');
tableをラップしたモデルを作れます。モデルを作れば、createで簡単にrecord追加できます。
/**
* Create model
* ActiveRecord.create(tableName) creates a model object.
* You can use sql execution without writhing sql strings.
* Followings are the examples of create new records.
* 'exampletable1' is the table you created above.
*/
var ExampleModel1 = ActiveRecord.create('exampletable1');
ExampleModel1.create({
username: 'Takazudo',
mailaddr: 'takazudo@gmail.com',
profile: 'hey! I am Takazudo.'
});
ExampleModel1.create({
username: 'Hogezudo',
mailaddr: 'hogezudo@gmail.com',
profile: 'hey! I am Hogezudo.'
});
モデル作成時、インターフェイスを定義すると、findByメソッドらが使えます。
ActiveRecord.create()するとき、そのtableがなけりゃ勝手に作ります。
PRIMARY KEYはIDでこれは勝手に入ります。
/**
* Model - finder methods
* Followings are the examples of finder methods.
* Defining the interface by second argument of create allows you to use finder methods.
*/
var ExampleModel2 = ActiveRecord.create('exampletable2',{
username: '',
mailaddr: '',
profile: ''
});
ExampleModel2.create({
username: 'Takazudo',
mailaddr: 'takazudo@gmail.com',
profile: 'hey! I am Takazudo.'
});
ExampleModel2.create({
username: 'Hogezudo',
mailaddr: 'takazudo@gmail.com',
profile: 'hey! I am Hogezudo.'
});
ExampleModel2.create({
username: 'AnyOtherMan1',
mailaddr: 'someone@gmail.com',
profile: 'hey! I am AnyOtherMan.'
});
ExampleModel2.create({
username: 'AnyOtherMan2',
mailaddr: 'someone@gmail.com',
profile: 'hey! I am AnyOtherMan.'
});
findByHoge(val)で、マッチした最初のrecordをラップしたオブジェクトが戻ってきます。
各keyの値は普通にメンバ変数として格納されてる。
PRIMARY KEYのIDから探すときは、findByIdですが、これだけは、インターフェイスを定義しなくても使えます。
/**
* ModelInstanceObject.findBy{property}(val)
* findBy{property}(val) returns a object which wrapped a record.
* this method finds a record with limit=1.
*/
var res = ExampleModel2.findByUsername('Takazudo');
Jaxer.Log.info(res.username); // Takazudo
Jaxer.Log.info(res.mailaddr); // takazudo@gmail.com
Jaxer.Log.info(res.profile); // hey! I am Takazudo.
Jaxer.Log.info(res.id); // 1
/**
* ModelInstanceObject.findById(val)
* findById(val) is allowed specially without defining interface.
*/
var res = ExampleModel2.findById(1);
Jaxer.Log.info(res.username); // Takazudo
マッチしたやつ全部欲しいと言う場合はfindAllByHoge。
recordをラップしたオブジェクトを詰め込んだArrayが返ってくる。
/**
* ModelInstanceObject.findAllBy{property}(val)
* findAllBy{property}(val) returns an array of matched recoreds.
* each member of the array is the object which wrapped a record.
* these objects are same record wrapped instance objects as above.
*/
var res = ExampleModel2.findAllByMailaddr('takazudo@gmail.com');
Jaxer.Log.info('the matched records length is... '+res.length);
// the matched records length is... 2
for(var i=0,l=res.length; i<l; i++){
Jaxer.Log.info(res[i].username);
}
// Takazudo
// Hogezudo
find(条件のハッシュ)で、複数の条件でrecordの取得が可能。
戻ってくるのは上記と同じようにrecordラップしたオブジェクトが詰め込まれたArray。
/**
* ModelInstanceObject.find(hash)
* find(hash) returns matched objects' array.
* same as above. but you can specify multi conditions
*/
var res = ExampleModel2.find({
mailaddr: 'someone@gmail.com',
profile: 'hey! I am AnyOtherMan.'
});
Jaxer.Log.info('the matched records length is... '+res.length);
// the matched records length is... 2
for(var i=0,l=res.length; i<l; i++){
Jaxer.Log.info(res[i].username);
}
// AnyOtherMan1
// AnyOtherMan2
もっと色々機能あるのでまだ調べてみているところ。
