Created
July 6, 2012 05:49
-
-
Save ofl/3058304 to your computer and use it in GitHub Desktop.
Titanium Mobile用のJsonDBを扱うクラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @_yagiさんのhttps://gist.github.com/3039968にインスパイアされてJsonDBで作ってみた | |
mix = () -> | |
child = {} | |
for arg in arguments | |
for prop of arg | |
if arg.hasOwnProperty prop | |
child[prop] = arg[prop] | |
return child | |
class DataStoreModel | |
constructor: (params) -> | |
@initialize() | |
throw new Error 'DB not connected' if @_db is null or typeof @_db is 'undefined' | |
obj = mix @_defaults, params | |
for key, value of obj | |
@[key] = value | |
initialize:()-> #いらないか? | |
@init() | |
init:()-> | |
return | |
save: ()-> | |
#日時はgetTime()で数値化して保存 | |
@created_at = (new Date()).getTime() if !@created_at | |
@updated_at = (new Date()).getTime() | |
data = {} | |
for name in @_columns | |
data[name] = @[name] | |
if @$id | |
data.$id = @$id | |
@_db.save data | |
@_db.commit() | |
return | |
destroy: ()-> | |
@_db.remove $id: @$id | |
@_db.commit() | |
return | |
#ここよりClass変数 | |
@db : null | |
@defaults : null | |
@columns: ['created_at', 'updated_at'] | |
#ここよりClassメソッド | |
@initializeDB:(table_name, scheme)-> | |
#scheme: プロパティとデフォルト値のオブジェクト | |
#例 {name: 'Jojo', gender: 'man'} | |
JSONDB = require 'com.irlgaming.jsondb' | |
JSONDB.debug true | |
@db = JSONDB.factory table_name, 'yourscecretkey' | |
@defaults = scheme | |
for key, value of scheme | |
@columns.push key | |
return | |
@save_all:(array)-> | |
for params in array | |
params.created_at = (new Date()).getTime() if !params.created_at | |
params.updated_at = (new Date()).getTime() | |
data = {} | |
for name in @columns | |
data[name] = params[name] | |
if params.$id | |
data.$id = params.$id | |
@db.save data | |
@db.commit() | |
@destroy_all:(array)-> | |
for params in array | |
@db.remove $id: params.$id | |
@db.commit() | |
@all:()-> | |
results = @db.find {}, {$sort:{created_at: -1}} | |
data = [] | |
for result in results | |
data.push new @ result | |
return data | |
@one:(query, condition)-> | |
results = @db.find query, condition | |
result = results.length > 0 && new @ results[0] || null | |
return result | |
@first:()-> | |
@one {}, {$limit:1, $sort:{created_at: 1}} | |
@last:()-> | |
@one {}, {$limit:1, $sort:{created_at: -1}} | |
@find_by_id:(id)-> | |
@one {$id: id}, {$limit: 1} | |
@create_if_not_exist:(params)-> | |
data = @find_by_id(params.id) | |
if data | |
return data | |
else | |
return new @() | |
module.exports = DataStoreModel | |
##################テスト####################### | |
class Entry extends DataStoreModel | |
init: ()-> | |
# クラス変数をインスタンス変数に代入。これでいいのか? | |
@_db = Entry.db | |
@_defaults = Entry.defaults | |
@_columns = Entry.columns | |
return | |
Entry.initializeDB 'entries', | |
title: 'Hello' | |
body: 'Its me' | |
#この状態をexportsすれば良いはず | |
#module.exports = Entry | |
class Person extends DataStoreModel | |
init: ()-> | |
@_db = Person.db | |
@_defaults = Person.defaults | |
@_columns = Person.columns | |
return | |
Person.initializeDB 'people', | |
name: 'Adam' | |
gender: 'man' | |
entry = new Entry() | |
entry.save() | |
entry1 = new Entry {title: "I wish I was a fisherman", body: "Whew!"} | |
entry1.save() | |
entry2 = new Entry {title: 'I wish I was a brakeman', body: "Woooo!"} | |
entry2.save() | |
man = new Person {name: 'Jojo', gender: 'man'} | |
man.save() | |
man2 = new Person {name: 'Dio', gender: 'man'} | |
man2.save() | |
entries = Entry.all() | |
Ti.API.info entries.length | |
for ent in entries | |
Ti.API.info ent.title | |
ent.destroy() | |
Ti.API.info Person.first().name | |
Ti.API.info Person.last().name | |
people = Person.all() | |
Person.destroy_all people | |
Ti.API.info Entry.all().length | |
Ti.API.info Person.all().length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment