I am new to MongoDB Realm and am trying to understand the ‘correct’ approach to call and execute functions. For example, I want to create a very simple application (that can scale) where users can create an account and create an event.
Prior to Realm, I was using Node JS and created a function that would interact with my Atlas database by updating the User, Event, and OutlierEvents collections. The outlier collection is used in the instance where a user has over 100 events in the future (events 101+). The examples below are just snapshots into each collection. My question is focused on calling functions in Realm.
User Collection
_id: ObjectID
name: string
events: array of event ObjectIDs that have not occurred (up to 100 ids)
countCurrentEvents: number
hasOutlierEvents: boolean
Event Collection
_id: ObjectID
eventCreator: ObjectID (id will match the user's id)
name: string
description: string
eventDate: date
OutlierEvents Collection
_id: ObjectID (id will match a user's id)
events: array of event ObjectIDs that have not occurred (101+ ids)
Based on the example above, a user can create an event. All events in the future will be stored in the user’s events array, but if the user has more than 100 events in the future scheduled, a new outlier document is created for this user (id matches the user’s id) and the additional events are stored in the outlier collection. As events expire these outlier events will be moved to the main events array in the user’s document.
I previously had a function ‘createEvent’ working in Atlas through Node JS. In order to transition to Realm, I created a new function called ‘createEvent’. I then went into my mobile application (built on React Native) and created a button the executes the following:
const createEvent = async () => {
try {
await user.functions.createEvent()
catch (error) {
Alert.alert('error')
}
}
When I click this button, an event is created. If the number of current events exceeds 100, then events will be saved to the OutlierEvents collection. The ‘createEvent’ function looks something like this (not perfect but for demonstration purposes I hope will suffice):
exports = async function(payload, response) {
const userId = "61250a623a67bb23f55c1c11"
const users = context.services.get("mongodb-atlas").db("myDB").collection("users")
const events = context.services.get("mongodb-atlas").db("myDB").collection("events")
const currentUser = await users.findOne({ "_id": BSON.ObjectId(userId)
if(currentUser) {
const event = {
eventCreator: currentUser._id,
name: "New Event",
description: "Tell others about your event!",
eventDate: new Date()
}
const newEvent = await events.insertOne(event)
await users.updateOne(
{ "_id": BSON.ObjectId(userId)},
{ $push: {
events: {
_id: newEvent.insertedId
}
},
$inc: { countCurrentEvents: 1 }
}
)
if(currentUser.hasOutlierEvents) {
...
}
}
}
At a high-level, is this the right approach? It works exactly how I intended but I am worried I am not getting the benefits of Realm (offline first, sync, etc.). I ask because several tutorials may use a function like:
const createEvent = (newEvent) => {
const projectRealm = realmRef.current
projectRealm(() => {
projectRealm.create(
"Event",
new Event({
eventCreator: currentUser._id,
name: "New Event",
description: "Tell others about your event!",
eventDate: new Date()
)
... (I am not sure how I would execute the remainder of this function)
}
}
I think where I am getting lost is when I am trying to build more complex functions (e.g. interacting among multiple collections). Almost all of the tutorials / examples I can find online are for simple ToDo style apps.
Is it possible to 1) confirm my approach will work or 2) expand on how to make it work with Realm with code, if possible. I am really excited to use Realm but am struggling to find great resources so any help here would be immensely appreciated. Cheers!