Adding Nested Objects & Lists from Realm (serverless) Functions

Unless I am missing a better way… I would like to confirm the process for adding nested Objects & List<>s to an Object from a Realm Function (since there is no Realm SDK accessible from a Realm Function).

Assumptions:
• The nested are of type ‘Object’ or ‘List’ (not ‘EmbeddedObject’)
• There is a proper relationship created in Realm UI (or CLI)

Steps:

  1. Create the nested Object or List<> (declared with ‘_id’ for the ‘foreign_key’ relationship)
  2. Insert such nested object’s documents to their Collections via ‘.insertOne()’ method
  3. Add the ‘_id’ value to the nested property field (for Object) or append the ‘_id’ value to the Array for the nested property (for List<>).
  4. Then add the object’s document (which contains such nested) to its Collection via ‘.insertOne()’ method

Basically, the Realm Object added in Step #4 will contain references (via ‘_id’) for the nested, while the full Object or List<> of nested were added in Step #2. Thereafter, simply depend on the reference of the ‘_id’ to make the connection between the two.

This seems to work but I want to make sure there is not something missing under-the-hood, or better yet if there is a more ideal Mongo method (instead of ‘.insertOne()’ and all steps) for Realm Objects to be inserted that have nested (non EmbeddedObject).

SAMPLE (RE-CREATED) CODE:

/// NOTE: Code shown is not actual code, rather recreated & verbose (not DRY) to show each step.

exports = function () {

    // NOTE: MongoDb Properties
    const clusterServiceName = "mongodb-atlas";
    const dbName = "Dev-DB";
    const db = context.services.get(clusterServiceName).db(dbName);
    // NOTE: Create Collection properties
    let useNestedCollection = db.collection("PersonRealm");
    let useCollection = db.collection("UsersRealm");

    // NOTE: Step #1 - Create Nested Object
    const personObject = context.values.get("PersonRealm_Schema");
    personObject._id = new BSON.ObjectId();
    personObject.name = "John Doe"

    // NOTE: Step #2 - Insert Nested Object to its Collection
    useNestedCollection.findOne({ "_id": personObject._id })
        .then(found => {
            if (found) {
                // return found;
            }
            else {
                useNestedCollection.insertOne(personObject)
                    .then(inserted => {
                        // return inserted;
                    })
                    .catch(error => console.error(`Failed to insertOne 'personObject' document. Here is error: ${error}`));
            }
        })
        .catch(error => console.error(`ERROR in trying to find already existing 'personObject' document check. Here is **ERROR: ${error}`));

    // NOTE: Step #3 - Create Top-level Object with references to '_id'
    const usersObject = context.values.get("UsersRealm_Schema");
    usersObject._id = new BSON.ObjectId();
        // This is supposed to reference to the 'personObject' object previously added
    usersObject.lastAddedPerson = personObject._id
        // Assuming this is the first time being added (so entire Array), and not a 'push' of an Element.
        // This is supposed to reference to the 'personObject' object previously added
    usersObject.allPersons = [personObject._id] 

    // NOTE: Step #4 - Add Top-level Object to its Collection
    useCollection.findOne({ "_id": usersObject._id })
        .then(found => {
            if (found) {
                // return found;
            }
            else {
                useCollection.insertOne(usersObject)
                    .then(inserted => {
                        // return inserted;
                    })
                    .catch(error => console.error(`Failed to insertOne 'usersObject' document. Here is error: ${error}`));
            }
        })
        .catch(error => console.error(`ERROR in trying to find already existing 'usersObject' document check. Here is ERROR: ${error}`));

}

1 Like

To help anyone with the same question… This process for Realm without Realm SDK (e.g.: Realm Functions) is correct. This was confirmed through paid MongoDB support.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.