public
Created

Titanium Mobile用のJsonDBを扱うクラス

  • Download Gist
gistfile1.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
# @_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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.