I want to create database collection name as “car_details” instead of “CarDetails” (which is model class name)
Is there any annotation which will internally map to my own custom name instead of model class name?
class CarDetails : RealmObject {
var make: String = “”
var model: String = “”
var miles: Int = 0
}
Is it possible to create collections from console and insert data from mobile application (kotlin)?
There will not be any schema initialization from kotlin code. It has to get list of collections and insert data into the right collection
This way i can use my own collections names instead of model class name
The TLDR is that MongoDB has a Namespace (database.collection) and Realm has a TableName. The Schemas tab is where you can define the mapping between these two. By default we use the collection name to be the table name, but if you add a “title” to the json schema mapping then you can effectively map which table maps to which collection. Then you can have your car_details collection have a schema with the title “CarDetails” and I think that will get you what you want.
But practically this is not possible with Kotlin SDK. Kotlin sdk always deals with data classes. I dont find any code to get list of collections and insert data into specific collection
Hi, like I linked above, if you want to map that class to a synced collection car_details, you can add a new schema in the “schemas” tab for the car_details collection and give the json schema a “title” field of CarDetails. Then you would have a Realm Class with the title CarDetails that maps to a MongoDB namespace DB_NAME.Car_Details.
You can edit it right in that page if you want. Just note that updating the title is a “breaking” change since it is functionally “removing” the old table.
Yes, I understand. Your model doesnt need to even know about what the collection name is though. It can just know that its table name is “CarDetails” and Device Sync knows the mapping it has to the “car_details” field through the “title” field in the JSON Schema.
Yes, this is not possible from the Kotlin side I do not think, but my point is that it should not matter if you setup the backend like I said above. You do not need to modify the “title” in the Kotlin code, you want to define it in the JSON Schema in the backend.
But I want to maintain local database and sync data based on online/offline conditions.
I should maintain the same schema which is there at server level.
It would be great if i get any example kotlin code to understad this
I guess we have a provision to give custom schema titles using java annotations.
Can we create models in java language use it in kotlin realm? Is this feasible ?
@Raviteja_Aketi: No, that wouldn’t make sense as you are using Realm Kotlin SDK which doesn’t have implementation for this annotation but should be out very soon.
On a separate note, why do you want the collection name precisely like car_detail? Camel is normally used in MongoDB naming for collections as shown in the sample dataset if this is the primary concern.
I just gave an example. I need my custom collection name instead of model class name.
I just came across below example from Java SDK but don’t know whether same kotlin annotation works with kotlin SDK or not. Do you have any idea?