This repository has been archived by the owner on Mar 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
244 lines (194 loc) · 7.35 KB
/
main.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
---@diagnostic disable: undefined-field
--------------------------------------------------------------------------------
-- EXAMPLE USAGE OF REALM LUA USING A LOCAL (NON-SYNC) REALM
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Import Realm
--------------------------------------------------------------------------------
local uv = require "luv"
local Realm = require "realm"
require "realm.scheduler.libuv"
local signal = uv.new_signal()
signal:start("sigint", function()
print("\nReceived interrupt signal. Exiting.")
uv.stop()
end)
signal:unref()
--------------------------------------------------------------------------------
-- Define Your Object Model
--------------------------------------------------------------------------------
local TeamSchema = {
name = "Team",
primaryKey = "_id",
properties = {
_id = "int",
teamName = "string",
tasks = "Task[]"
}
}
local TaskSchema = {
name = "Task",
primaryKey = "_id",
properties = {
_id = "int",
description = "string",
completed = "bool",
size = "string?"
}
}
-----------------------------------------------------------------------------
-- Open a Local (Non-Sync) Realm
-----------------------------------------------------------------------------
local realm = Realm.open({
schema = { TeamSchema, TaskSchema }
})
-----------------------------------------------------------------------------
-- Create Realm Objects
-----------------------------------------------------------------------------
local size = {
SMALL = "SMALL",
MEDIUM = "MEDIUM",
LARGE = "LARGE"
}
local smallTask = {
_id = math.random(1, 100000),
description = "Get started with Realm Lua",
completed = false,
size = size.SMALL,
}
local mediumTask = {
_id = math.random(1, 100000),
description = "Build an app using Atlas Device Sync",
completed = false,
size = size.MEDIUM
}
local luaTeam = {
_id = math.random(1, 100000),
teamName = "Lua",
}
-- Before the write transaction, `smallTask`, `mediumTask`
-- and `luaTeam` are only regular Lua objects.
realm:write(function ()
-- `realm:create()` returns the created Realm
-- object, so we can assign it to our variables.
smallTask = realm:create("Task", smallTask)
mediumTask = realm:create("Task", mediumTask)
luaTeam = realm:create("Team", luaTeam)
table.insert(luaTeam.tasks, smallTask)
table.insert(luaTeam.tasks, mediumTask)
assert(smallTask and smallTask.description == "Get started with Realm Lua")
assert(mediumTask and mediumTask.description == "Build an app using Atlas Device Sync")
assert(luaTeam and luaTeam.teamName == "Lua")
assert(#luaTeam.tasks == 2)
end)
-- After the write transaction, the same local
-- variables can now be used as Realm objects.
-----------------------------------------------------------------------------
-- Query Realm Objects
-----------------------------------------------------------------------------
local tasks = realm:objects("Task");
-----------------------------------------------------------------------------
-- Filter Realm Objects
-----------------------------------------------------------------------------
local uncompletedSmallTasks = tasks:filter(
"completed = $0 AND size = $1",
false, -- Replaces $0
size.SMALL -- Replaces $1
)
print("Number of uncompleted small tasks: " .. #uncompletedSmallTasks)
print(uncompletedSmallTasks[1].description)
-----------------------------------------------------------------------------
-- Update Realm Objects
-----------------------------------------------------------------------------
local largeTask = {
_id = math.random(1, 100000),
description = "Build a great IoT app",
completed = false,
size = size.LARGE
}
realm:write(function ()
-- Modify `smallTask`
smallTask.completed = true
-- Modify `largeTask`
largeTask = realm:create("Task", largeTask)
table.insert(luaTeam.tasks, largeTask)
end)
print("Number of uncompleted small tasks: " .. #uncompletedSmallTasks)
-----------------------------------------------------------------------------
-- Delete Realm Objects
-----------------------------------------------------------------------------
realm:write(function ()
realm:delete(largeTask)
largeTask = nil
end)
-----------------------------------------------------------------------------
-- Get Notified of Changes: Collection Changes
-----------------------------------------------------------------------------
local onTaskCollectionChange = function (collection, changes)
-- Handle deletions first
for _, deletedIndex in ipairs(changes.deletions) do
print("Deleted task at index " .. deletedIndex)
end
for _, insertedIndex in ipairs(changes.insertions) do
print("Added task: " .. collection[insertedIndex].description)
end
for _, modifiedIndex in ipairs(changes.modificationsNew) do
print("Modified task: " .. collection[modifiedIndex].description)
end
end
-- Add the listener (you currently need to save the return value to a variable)
local _ = tasks:addListener(onTaskCollectionChange)
print("Listening for changes on Task collection...")
-----------------------------------------------------------------------------
-- Get Notified of Changes: Object Changes
-----------------------------------------------------------------------------
local onTaskObjectChange = function (object, changes)
if changes.isDeleted then
print("Deleted a task")
elseif #changes.modifiedProperties > 0 then
print("Modified task: " .. object.description)
end
end
-- Add the listener (you currently need to save the return value to a variable)
local _ = smallTask:addListener(onTaskObjectChange)
print("Listening for changes on Task object with id " .. smallTask._id .. "...")
-----------------------------------------------------------------------------
-- Trigger Collection Change Notification
-----------------------------------------------------------------------------
print("\n-------- TRIGGER CHANGE NOTIFICATIONS --------\n")
local insertedTask = {
_id = math.random(1, 100000),
description = "Insert data and watch it notify you",
completed = false,
size = size.SMALL
}
-- (1) Trigger insertion notification:
realm:write(function ()
insertedTask = realm:create("Task", insertedTask)
end)
-- (2) Trigger modification notification:
realm:write(function ()
insertedTask.description = "Modify data and watch it notify you"
end)
-- (3) Trigger deletion notification:
realm:write(function ()
realm:delete(insertedTask)
end)
-----------------------------------------------------------------------------
-- Trigger Object Change Notification
-----------------------------------------------------------------------------
-- Since we added the listener to `smallTask`, that's the
-- object we need to change to trigger the notification.
-- (1) Trigger modification notification:
realm:write(function ()
smallTask.description = "Modify the small task"
end)
-- (2) Trigger deletion notification:
realm:write(function ()
realm:delete(smallTask)
end)
-- NOTE:
-- * If the object we added the listener to is also in the collection
-- that we added a listener to, both listeners will be called.
print("Press Ctrl+C to exit")
uv.run()