Skip to content

Commit fecab65

Browse files
committed
Add MongoModelStorage.Configuration
1 parent 2131d30 commit fecab65

1 file changed

Lines changed: 57 additions & 27 deletions

File tree

Sources/MongoDBModel/MongoDatabase.swift

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,53 @@ public struct MongoModelStorage: ModelStorage {
88

99
public let model: Model
1010

11+
public let options: Configuration
12+
1113
public init(
1214
database: MongoDatabase,
13-
model: Model
15+
model: Model,
16+
options: Configuration = Configuration()
1417
) {
1518
self.database = database
1619
self.model = model
20+
self.options = options
1721
}
1822

1923
/// Fetch managed object.
2024
public func fetch(_ entityName: EntityName, for id: ObjectID) async throws -> ModelData? {
2125
let entity = try model(for: entityName)
22-
return try await database.fetch(entity, for: id)
26+
let options = options.collections[entity.id]
27+
return try await database.fetch(entity, for: id, options: options)
2328
}
2429

2530
/// Fetch managed objects.
2631
public func fetch(_ fetchRequest: FetchRequest) async throws -> [ModelData] {
2732
let entity = try model(for: fetchRequest.entity)
28-
return try await database.fetch(fetchRequest, entity: entity)
33+
let options = options.collections[entity.id]
34+
return try await database.fetch(fetchRequest, entity: entity, options: options)
2935
}
3036

3137
/// Fetch and return result count.
3238
public func count(_ fetchRequest: FetchRequest) async throws -> UInt {
33-
try await database.count(fetchRequest)
39+
let options = options.collections[fetchRequest.entity]
40+
return try await database.count(fetchRequest, options: options)
3441
}
3542

3643
/// Create or edit a managed object.
3744
public func insert(_ value: ModelData) async throws {
38-
try await database.insert(value)
45+
let options = options.collections[value.entity]
46+
try await database.insert(value, options: options)
3947
}
4048

4149
/// Create or edit multiple managed objects.
4250
public func insert(_ values: [ModelData]) async throws {
43-
try await database.insert(values)
51+
try await database.insert(values, options: options.collections)
4452
}
4553

4654
/// Delete the specified managed object.
4755
public func delete(_ entity: EntityName, for id: ObjectID) async throws {
48-
try await database.delete(entity, for: id)
56+
let options = options.collections[entity]
57+
try await database.delete(entity, for: id, options: options)
4958
}
5059

5160
private func model(for entityName: EntityName) throws -> EntityDescription {
@@ -56,14 +65,27 @@ public struct MongoModelStorage: ModelStorage {
5665
}
5766
}
5867

59-
public extension MongoDatabase {
68+
public extension MongoModelStorage {
69+
70+
struct Configuration {
71+
72+
public var collections: [EntityName: MongoCollectionOptions]
73+
74+
public init(collections: [EntityName : MongoCollectionOptions] = [:]) {
75+
self.collections = collections
76+
}
77+
}
78+
}
79+
80+
internal extension MongoDatabase {
6081

6182
/// Fetch managed object.
6283
func fetch(
6384
_ entity: EntityDescription,
64-
for id: ObjectID
85+
for id: ObjectID,
86+
options: MongoCollectionOptions?
6587
) async throws -> ModelData? {
66-
guard let document = try await find(entity.id, for: id) else {
88+
guard let document = try await find(entity.id, for: id, options: options) else {
6789
return nil
6890
}
6991
return try ModelData(bson: document, model: entity)
@@ -72,37 +94,47 @@ public extension MongoDatabase {
7294
/// Fetch managed objects.
7395
func fetch(
7496
_ fetchRequest: FetchRequest,
75-
entity: EntityDescription
97+
entity: EntityDescription,
98+
options: MongoCollectionOptions?
7699
) async throws -> [ModelData] {
77-
try await fetchDocuments(fetchRequest)
100+
try await fetchDocuments(fetchRequest, options: options)
78101
.map { try ModelData(bson: $0, model: entity) }
79102
}
80103

81104
/// Fetch and return result count.
82-
func count(_ fetchRequest: FetchRequest) async throws -> UInt {
105+
func count(
106+
_ fetchRequest: FetchRequest,
107+
options: MongoCollectionOptions?
108+
) async throws -> UInt {
83109
let entityName = fetchRequest.entity
84-
let collection = self.collection(entityName)
110+
let collection = self.collection(entityName, options: options)
85111
let filter = fetchRequest.predicate.map { BSONDocument(filter: $0) } ?? [:]
86112
let options = CountDocumentsOptions(fetchRequest: fetchRequest)
87113
let count = try await collection.countDocuments(filter, options: options)
88114
return UInt(count)
89115
}
90116

91117
/// Create or edit a managed object.
92-
func insert(_ value: ModelData) async throws {
118+
func insert(
119+
_ value: ModelData,
120+
options: MongoCollectionOptions?
121+
) async throws {
93122
let entityName = value.entity
94-
let collection = self.collection(entityName)
123+
let collection = self.collection(entityName, options: options)
95124
let document = BSONDocument(model: value)
96125
try await collection.insertOne(document)
97126
}
98127

99-
func insert(_ values: [ModelData]) async throws {
128+
func insert(
129+
_ values: [ModelData],
130+
options: [EntityName: MongoCollectionOptions]
131+
) async throws {
100132
var collections = [EntityName: [ModelData]]()
101133
for value in values {
102134
collections[value.entity, default: []].append(value)
103135
}
104136
for (entity, values) in collections {
105-
let collection = self.collection(entity)
137+
let collection = self.collection(entity, options: options[entity])
106138
let documents = values.map { BSONDocument(model: $0) }
107139
try await collection.insertMany(documents)
108140
}
@@ -111,23 +143,21 @@ public extension MongoDatabase {
111143
/// Delete the specified managed object.
112144
func delete(
113145
_ entity: EntityName,
114-
for id: ObjectID
146+
for id: ObjectID,
147+
options: MongoCollectionOptions?
115148
) async throws {
116-
let collection = self.collection(entity)
149+
let collection = self.collection(entity, options: options)
117150
let options: DeleteOptions? = nil
118151
let filter: BSONDocument = [
119152
BSONDocument.BuiltInProperty.id.rawValue: .string(id.rawValue)
120153
]
121154
try await collection.deleteOne(filter, options: options)
122155
}
123-
}
124-
125-
internal extension MongoDatabase {
126156

127157
func find(
128158
_ entityName: EntityName,
129159
for id: ObjectID,
130-
options: MongoCollectionOptions? = nil
160+
options: MongoCollectionOptions?
131161
) async throws -> BSONDocument? {
132162
let collection = self.collection(entityName, options: options)
133163
let options: FindOneOptions? = nil
@@ -139,7 +169,7 @@ internal extension MongoDatabase {
139169

140170
func fetchDocuments(
141171
_ fetchRequest: FetchRequest,
142-
options: MongoCollectionOptions? = nil
172+
options: MongoCollectionOptions?
143173
) async throws -> [BSONDocument] {
144174
let entityName = fetchRequest.entity
145175
let collection = self.collection(entityName, options: options)
@@ -156,7 +186,7 @@ internal extension MongoDatabase {
156186
func create(
157187
_ entityName: EntityName,
158188
for id: ObjectID,
159-
options: MongoCollectionOptions? = nil
189+
options: MongoCollectionOptions?
160190
) async throws -> BSONDocument {
161191
let collection = self.collection(entityName, options: options)
162192
let document: BSONDocument = [
@@ -172,7 +202,7 @@ fileprivate extension MongoDatabase {
172202

173203
func collection(
174204
_ name: EntityName,
175-
options: MongoCollectionOptions? = nil
205+
options: MongoCollectionOptions?
176206
) -> MongoCollection<BSONDocument> {
177207
let tableName = name.rawValue.lowercased() + "s"
178208
return collection(tableName, options: options)

0 commit comments

Comments
 (0)