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.
Sorry to barge in, but does this mean there is no way to map an “ExampleRealmModel” in atlas to a swift “DifferentExampleRealmModel” ?? just as you can map property names?
@Gustavo_Parrado You are not barging in and are always welcome to comment/ask questions here on the forums - this is a two-year old post though so keep it in mind that the SDK and things can change over time.
In this case though, I don’t believe anything has changed; the requirement is the same as it’s been - the object name in Atlas must match the local object name and vice-versa. Object names are sensitive so they must stay consistent. Even while properties can be mapped, the actual property name in the object must be accounted for and match the property name on the server (see below)
Correct and a good point; with 10.33, Property name mapping is a function of the Object on the client side and without implementing that, non-matching property names will cause a client reset when there are breaking schema changes.
Thank you very much, this clarification makes things clear for what should I do in my project.
just a quick follow question tho. Dart does permit this with the @mapTo. Is it a Dart language thing or is this something about the API?
@Gustavo_Parrado I am not a Dart/Flutter person but I believe the MapTo applies to both classes and properties within the Realm SDK - but it is at the object level as with the other SDK’s. e.g. the object has to do the heavy lifting independently of the server.
I get that, what I mean is that flutter has
And swift doesn’t seem to have it
Correct - Swift offers mapping but only at the property level, not for the class. What is the use case where you need to map a Class? I can’t think of a single time in the last 10 years I’ve needed to do that outside of during development (maybe I am just lucky with class naming lol). Perhaps there’s another option?