Protocolmongokitten 7.9.5Meow
MutableModel
The base specification of any readable Meow model, containing a collectionName and a _id. MutableModel must be implemented using Codable
, and always implements ReadableModel. MutableModels can be saved, whereas Readableodels cannot
protocol MutableModel : ReadableModel, Encodable
Two models may share the same collection, allowing for constructions where both AdminUser
Example:
struct AnyUser: MutableModel {
@Field var _id: ObjectId
@Field var username: String
@Field var role: Role
// Populted only for admin users
@Field var adminPowers: [AdminPrivilege]?
}
struct AdminUser: ReadableModel {
let _id: ObjectId
let username: String
// Role is always admin for this model, so it's not decoded
// Always populated
let adminPowers: [AdminPrivilege]
}
struct User: ReadableModel {
let _id: ObjectId
let username: String
@Field var role: Role
// `adminPowers` are not relevant
}
All Meow models must have an identifier that is Codable
and Hashable
, as well as representable by a Primitive.
When implementing PrimitiveEncodable
using BSONEncoder
, this allows you to use any struct
as an _id
so long as it’s not an “unkeyedContainer”. Array-like (sequence) types are rejected on insertion by MongoDB.
Supertypes
protocol BaseModel
The base specification of any Meow model, containing a collectionName and a _id
protocol Decodable
A type that can decode itself from an external representation.
protocol Encodable
A type that can encode itself to an external representation.
protocol ReadableModel
The base specification of any readable Meow model, containing a collectionName and a _id. ReadableModel must be implemented using
Decodable
. ReadableModel is currently used for any entity that is queryable. MutableModels can be saved, whereas Readableodels cannot
Requirements
Type members
Instance members
Citizens in Meow
Type members
Instance members
func create(in: MeowDatabase
) async throws -> MeowOperationResult Creates this model using an
insert
operation. Fails if the entity already existsfunc encode(to: Document.Type
) throws -> Document func replaceModel(in: MeowDatabase
) async throws -> Self? Replaces the old model matching this
_id
with the current model. Returns the old model if the entity existedfunc save(in: MeowDatabase
) async throws -> MeowOperationResult Saves this model using an
upsert
operation, updating if it exists, creating if it’s new