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
/
mainSync.lua
326 lines (265 loc) · 10.4 KB
/
mainSync.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
---@diagnostic disable: undefined-field
--------------------------------------------------------------------------------
-- EXAMPLE USAGE OF REALM LUA USING ATLAS DEVICE SYNC
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Import Realm and App
--------------------------------------------------------------------------------
local uv = require "luv"
local Realm = require "realm"
require "realm.scheduler.libuv"
local App = require "realm.app"
local signal = uv.new_signal()
signal:start("sigint", function()
print("\nReceived interrupt signal. Exiting.")
uv.stop()
end)
signal:unref()
--------------------------------------------------------------------------------
-- Initialize Your App Using the App ID Copied From the Atlas App Services UI
--------------------------------------------------------------------------------
local APP_ID = "application-0-oltdi" -- <--- TODO: INSERT YOUR APP ID <---
local app = App.new({ appId = APP_ID })
--------------------------------------------------------------------------------
-- Define Your Object Model
--------------------------------------------------------------------------------
local TeamSchema = {
name = "Team",
primaryKey = "_id",
properties = {
_id = "int",
-- The `_partition` field will store the team name:
_partition = "string",
teamName = "string",
tasks = "Task[]"
}
}
local TaskSchema = {
name = "Task",
primaryKey = "_id",
properties = {
_id = "int",
-- The `_partition` field will store the team name:
_partition = "string",
description = "string",
completed = "bool",
size = "string?",
assigneeId = "string?"
}
}
-----------------------------------------------------------------------------
-- Authenticate a User and Open a Synced Realm
-----------------------------------------------------------------------------
local realm --- @type Realm
local currentUser --- @type Realm.App.User | nil
local partitionValue = "Lua Sync"
---@param user Realm.App.User The realm user.
---@return Realm
local function openRealm(user)
assert(user)
return Realm.open({
schema = { TeamSchema, TaskSchema },
sync = {
user = user,
-- To sync all objects belonging to a team called "Lua Sync"
-- (i.e. all objects with the "_partition" field set to "Lua
-- Sync"), we add it as the partition value. (We will soon
-- create a team with this name.) The value must be the raw
-- Extended JSON string in order to be compatible with documents
-- stored in MongoDB Atlas. (Manually add `"` around the string.)
partitionValue = "\"" .. partitionValue .. "\""
}
})
end
local function registerAndLogIn(email, password)
app:registerEmail(email, password, function (err)
-- Called when registration is completed..
if not err or err == "name already in use" then
local credentials = App.credentials.emailPassword(email, password)
app:logIn(credentials, function (user, err)
-- Called when login is completed..
if not err then
currentUser = user
realm = openRealm(user)
else
error(err)
end
end)
else
error(err)
end
end)
end
-- The current App User can also be retrieved once logged in using:
-- local currentUser = app:currentUser()
local exampleEmail = "[email protected]"
local examplePassword = "123456"
-- If there is a current user, it must have been authenticated and
-- is logged in, whereafter we open the realm. Otherwise we first
-- register a new user, log in, then open the realm.
if currentUser then
realm = openRealm(currentUser)
else
registerAndLogIn(exampleEmail, examplePassword)
end
-----------------------------------------------------------------------------
-- Create Realm Objects
-----------------------------------------------------------------------------
local size = {
SMALL = "SMALL",
MEDIUM = "MEDIUM",
LARGE = "LARGE"
}
local smallTask = {
_id = math.random(1, 100000),
_partition = partitionValue,
description = "Get started with Realm Lua",
completed = false,
size = size.SMALL,
assigneeId = currentUser.identity
}
local mediumTask = {
_id = math.random(1, 100000),
_partition = partitionValue,
description = "Build an app using Atlas Device Sync",
completed = false,
size = size.MEDIUM
}
local luaSyncTeam = {
_id = math.random(1, 100000),
_partition = partitionValue,
teamName = "Lua Sync",
}
-- Before the write transaction, `smallTask`, `mediumTask`
-- and `luaSyncTeam` 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)
luaSyncTeam = realm:create("Team", luaSyncTeam)
table.insert(luaSyncTeam.tasks, smallTask)
table.insert(luaSyncTeam.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(luaSyncTeam and luaSyncTeam.teamName == "Lua Sync")
assert(#luaSyncTeam.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),
_partition = partitionValue,
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(luaSyncTeam.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),
_partition = partitionValue,
description = "Insert data and watch it sync across all devices",
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 sync across all devices"
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.assigneeId = currentUser.identity
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.
-- TIP:
-- * While this Lua app is running and you're connected, go to MongoDB Atlas
-- and modify or delete a document directly from the UI. If the document has
-- the same partition value as the one specified when opening the realm, you
-- should see the printouts in the terminal where you're running this code.
print("Press Ctrl+C to exit")
uv.run()