An experiment using Swift's Propery Wrappers to build data model for Couchbase Lite Swift.
- The library is using EnclosingSelf feature which is still in proposal/experimental state.
- As the `EnclosingSelf feature is used, only Class (not Struct) can use the property wrapper.
- CouchbaseLiteSwift 2.8+
- iOS (9.0+) and macOS (10.11+)
- XCode 12+
class Person: CBLModel {
@CBLProperty(key: "name")
var name: String?
@CBLProperty(key: "address")
var address: Address?
@CBLProperty(key: "contacts")
var contacts: [Person]?
@CBLProperty(key: "dependents", defaultValue: 0)
var dependents: Int
}
class Address: CBLModel {
@CBLProperty(key: "street")
var street: String?
}
let person = Person(with: MutableDocument())
person.name = "Daniel"
let address = Address()
address.street = "1 Main st."
person.address = address
let contact = Person()
contact.name = "James"
person.contacts = [contact]
try person.save(into: self.database)
[ "name": "Daniel"
"address": ["street": "1 Main st."]
"contacts": [
["name": "James]
]
]