Map existing collection names to Swift schema

I’m trying to add pre-existing collections from MongoDB Atlas to my Realm schema in Swift. The problem I’m encountering is that the existing collection names are snake_cased, and Swift naming conventions are PascalCased, so Realm is creating new collections. How do I setup Realm to recognize the existing collection names (i.e. collection_name) to refer to the Realm object models in Swift (i.e. CollectionName)?

I don’t use Swift so I’m not sure this answer will work. But if your swift schema is the only thing using these collections can you just rename the collections so they are in the correct format?

db.collection.renameCollection()

Thanks for the reply! Unfortunately the pre-existing collections are already in use by other server side processes, which I’d like to avoid modifying if possible.

Hi Tom,

You should be able to keep the collection name unchanged and for it’s Sync Cloud Schema update the Title field to use a PascalCase format equivalent which is what the Swift class can refer to.

Regards
Manny

Perhaps I don’t fully understand the question but let me ask for clarification

There is no general requirement in Swift for specific formats for var, class or structure names. You can use snake_case and PascalCases (sometimes called CamelCase but it’s slightly different). Camel case for vars please if you use upper/lower case.

You can freely use whatever works for your use case - the important bit it to keep it consistent in whatever format you go with.

So can’t you just name the Realm objects models the same? e.g. if the object is “cool_task” in MongoDB, can there be a Realm model class cool_task: Object { that matches?

Can you clarify what you mean? A Collection is a set of Realm objects; either Results, List or MutableSet etc, which refer to objects in CODE, after they are lazily loaded from disk.

When using RealmSync objects on disk are stored by their partition key locally (for partition bases sync)

So are you asking about Objects or the partition files stored on disk? Here’s an example task class that uses all kinds of var name formats, all of which are totally fine.

class TaskClass: Object {
@Persisted var MyNamePascal = “”
@Persisted var my_name_snake = “”
@Persisted var myNameCamel = “”
}

You could even call the class

class Task_Class or class taskClass or class TaSkClAsS. Regardless of how it’s named or the var names, on disk its stored according to the objects partition.