-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_record.lua
257 lines (198 loc) · 5.62 KB
/
test_record.lua
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
--- Record.lua
--- Easy Record Your CRUD Life!
--- Here's the demo using Users model
--- @author Jakit Liang 泊凛
--- @date 2023-09-16
--- @license MIT
local Users = require('model.users')
print('====== Print schema info ======')
print(Users.schema.table)
for _, v in ipairs(Users.schema.fields) do
print(v.name, v.type)
end
print('Users:create()', Users:create())
print('====== Insert ======')
for i = 1, 10 do
local u = Users()
u.name = 'demo'..i
u.age = i
print('User.save', u, u:save(), u.id)
end
local u = Users()
u.name = 'demo3'
u.age = 30
print('User.save', u, u:save(), u.id)
print('====== Destroy ======')
-- Users:destroy({id = 3})
-- u:destroy()
print('====== Update ======')
-- u.age = 50
-- print(u:update())
print('====== Fetch All Result ======')
-- local result = Users:fetch()
-- for i = 1, #result do
-- print('id', result[i].id)
-- print('name', result[i].name)
-- print('age', result[i].age)
-- end
print('====== Fetch One Result ======')
local result = Users:fetchOne()
if result then
print('id', result.id)
print('name', result.name)
print('age', result.age)
end
print('====== Fetch All Result ======')
print('====== Find All Result By Condition ======')
-- local result = Users:find({id = {'<', 6}})
-- for i = 1, #result do
-- print('id', result[i].id)
-- print('name', result[i].name)
-- print('age', result[i].age)
-- end
print('====== Find only 3 results and reverse the result order ======')
local result = Users:find({id = {'<', 6}}, 3, true)
for i = 1, #result do
print('id', result[i].id)
print('name', result[i].name)
print('age', result[i].age)
end
print('====== Find only 3 results and sort the result by age ======')
local result = Users:find({id = {'<', 6}}, 3, 'age')
for i = 1, #result do
print('id', result[i].id)
print('name', result[i].name)
print('age', result[i].age)
end
print('====== Find 2 results from index 1 ======')
local result = Users:find({id = {'<', 6}}, {1, 2})
for i = 1, #result do
print('id', result[i].id)
print('name', result[i].name)
print('age', result[i].age)
end
print('====== Find only One result ======')
local result = Users:findOne({id = {'<', 6}})
if result then
print('id', result.id)
print('name', result.name)
print('age', result.age)
end
print('====== Find id in set (2, 3) ======')
local result = Users:find({id = {'in', {2, 3}}})
for i = 1, #result do
print('id', result[i].id)
print('name', result[i].name)
print('age', result[i].age)
end
print('====== Find id in set (2, 3) ======')
local result = Users:find({id = {'in', {2, 3}}})
for i = 1, #result do
print('id', result[i].id)
print('name', result[i].name)
print('age', result[i].age)
end
print('====== Then update their "age" to 50 ======')
local result = Users:find({id = {'in', {2, 3}}})
for i = 1, #result do
print('id', result[i].id)
print('name', result[i].name)
print('age', result[i].age, ' => 50')
result[i].age = 50
result[i]:update() -- Update this data!
end
print('====== You can see the change now! ======')
local result = Users:find({id = {'in', {2, 3}}})
for i = 1, #result do
print('id', result[i].id)
print('name', result[i].name)
print('age', result[i].age)
end
print()
print('============================')
print('====== [Senior Usage] ======')
print('============================')
print()
print('====== Find One ======')
local row = Users:findBy({id = 3}):fetchOne()
if row then
print('id', row.id)
print('name', row.name)
print('age', row.age)
end
print('====== Find the user [id > 8] or [name = demo1] or [id = 2] ======')
local rows = Users:findBy({id = {'>', 8}}):orWhere({name = 'demo1', id = '2'}):fetchAll()
for i = 1, #rows do
print('id', rows[i].id)
print('name', rows[i].name)
print('age', rows[i].age)
end
print()
print('=========================================')
print('====== Boss task: Data Transaction ======')
print('=========================================')
print()
print('====== Find out data [id in (2, 3)] and modify them ======')
local rows = Users:find({id = {'in', {2, 3}}})
Users:begin()
for i = 1, #rows do
print('id', rows[i].id)
print('name', rows[i].name)
print('age', rows[i].age)
rows[i].age = 12345
rows[i]:update()
end
print('====== Rolling back!!!!! (Undo changes) ======')
Users:rollback()
local rows = Users:find({id = {'in', {2, 3}}})
for i = 1, #rows do
print('id', rows[i].id)
print('name', rows[i].name)
print('age', rows[i].age)
end
print('====== Find out data [id in (2, 3)] and modify them ======')
local rows = Users:find({id = {'in', {2, 3}}})
Users:begin()
for i = 1, #rows do
print('id', rows[i].id)
print('name', rows[i].name)
print('age', rows[i].age)
rows[i].age = 12345
rows[i]:update()
end
print('====== Commit it!!!!! (Commit changes) ======')
Users:commit()
local rows = Users:find({id = {'in', {2, 3}}})
for i = 1, #rows do
print('id', rows[i].id)
print('name', rows[i].name)
print('age', rows[i].age)
end
print('====== Promise style Transaction ======')
Users:transaction(function (onFail)
local rows = Users:find({id = {'in', {2, 3}}})
for i = 1, #rows do
print('id', rows[i].id)
print('name', rows[i].name)
print('age', rows[i].age)
rows[i].age = 999
rows[i]:update()
end
if true then
return onFail() -- Undo changes and return
end
-- The code below will not do
rows = Users:find({id = {'in', {2, 3}}})
for i = 1, #rows do
print('id', rows[i].id)
print('name', rows[i].name)
print('age', rows[i].age)
end
end)
-- You can see data is successfuly rollback
rows = Users:find({id = {'in', {2, 3}}})
for i = 1, #rows do
print('id', rows[i].id)
print('name', rows[i].name)
print('age', rows[i].age)
end