-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.js
165 lines (142 loc) · 3.87 KB
/
setup.js
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
/* include the dist version
cd into activerecord folder, run
$ rake dist to build
*/
Ti.include('test_helper.js');
Ti.include('../../activejs-1584174/dist/active_record.js');
var win = Ti.UI.currentWindow
ActiveRecord.logging = true;
// ActiveRecord.autoMigrate = true;
ActiveSupport.log = Ti.API.debug;
/* Set the Adapter to Titanium and connect to the defauly 'app.sqlite' database */
ActiveRecord.connect(ActiveRecord.Adapters.Titanium);
ActiveRecord.execute('DROP TABLE IF EXISTS posts');
ActiveRecord.execute('DROP TABLE IF EXISTS comments');
ActiveRecord.execute('DROP TABLE IF EXISTS users');
ActiveRecord.execute('DROP TABLE IF EXISTS credit_cards');
ActiveRecord.execute('DROP TABLE IF EXISTS string_dates');
ActiveRecord.execute('DROP TABLE IF EXISTS dates');
ActiveRecord.execute('DROP TABLE IF EXISTS articles');
ActiveRecord.execute('DROP TABLE IF EXISTS categories');
ActiveRecord.execute('DROP TABLE IF EXISTS categorizations');
ActiveRecord.execute('DROP TABLE IF EXISTS field_type_testers');
ActiveRecord.execute('DROP TABLE IF EXISTS singular_table_name');
ActiveRecord.execute('DROP TABLE IF EXISTS custom_table');
ActiveRecord.execute('DROP TABLE IF EXISTS guid');
ActiveRecord.execute('DROP TABLE IF EXISTS reserved');
//define Posts via SQL
ActiveRecord.execute('CREATE TABLE IF NOT EXISTS posts (id INTEGER PRIMARY KEY,user_id,title,body)');
var Post = ActiveRecord.create('posts');
Post.belongsTo('user',{ counter: 'post_count' });
Post.hasMany('comments',{ dependent: true });
//define Comments via Migrations
var Comment = ActiveRecord.create('comments',{
title: '',
post_id: 0,
user_id: 0,
body: {
type: 'text',
value: ''
},
test: {},
test_2: []
});
Comment.validatesPresenceOf('title');
Comment.belongsTo('user');
Comment.belongsTo(Post);
CreditCard = ActiveRecord.create('credit_cards',{
number: 0
});
var User = ActiveRecord.create('users',{
name: '',
password: '',
comment_count: 0,
post_count: 0,
credit_card_id: 0
});
//you can mix and match singular, plural, camelcase, normal
User.hasMany('Comment',{
dependent: true
});
User.hasMany('posts',{
dependent: true
});
User.hasOne(CreditCard,{
dependent: true
});
var ModelWithStringDates = ActiveRecord.create('string_dates',{
name: '',
created: '',
updated: ''
});
var ModelWithDates = ActiveRecord.create('dates',{
name: '',
created: {
type: 'DATETIME'
},
updated: {
type: 'DATETIME'
}
});
var Article = ActiveRecord.create('articles',{
name: ''
});
Article.hasMany('Categorization');
Article.hasMany('Category',{
through: 'Categorization'
});
var Category = ActiveRecord.create('categories',{
name: ''
});
Category.hasMany('Categorization');
Category.hasMany('Article',{
through: 'Categorization'
});
var Categorization = ActiveRecord.create('categorizations',{
article_id: 0,
category_id: 0
});
Categorization.belongsTo('Article',{
dependent: true
});
Categorization.belongsTo('Category',{
dependent: true
});
var FieldTypeTester = ActiveRecord.create('field_type_testers',{
string_field: '',
number_field: 0,
default_value_field: 'DEFAULT',
boolean_field: true,
custom_type_field: {
type: 'MEDIUMTEXT'
},
custom_type_field_with_default: {
type: 'MEDIUMTEXT',
value: 'DEFAULT'
}
});
var SingularTableName = ActiveRecord.create('singular_table_name',{
string_field: ''
});
log('Should created SingularTableName');
var Custom = ActiveRecord.create({
tableName: 'custom_table',
modelName: 'Orange'
},{
custom_id: {
primaryKey: true
},
name: ''
});
var Guid = ActiveRecord.create('guid',{
guid: {
primaryKey: true,
type: 'VARCHAR(255)'
},
data: ''
});
var Reserved = ActiveRecord.create('reserved',{
to: { primaryKey: true },
from: '',
select: ''
});