11import Foundation
22@_exported import CoreModel
3- @_exported import MongoSwift
3+ @_exported @ preconcurrency import MongoSwift
44
5- public struct MongoModelStorage : ModelStorage {
6-
5+ extension MongoCollectionOptions : @unchecked Sendable { }
6+
7+ public actor MongoModelStorage : ModelStorage {
8+
79 public let database : MongoDatabase
810
911 public let model : Model
@@ -23,43 +25,49 @@ public struct MongoModelStorage: ModelStorage {
2325 /// Fetch managed object.
2426 public func fetch( _ entityName: EntityName , for id: ObjectID ) async throws -> ModelData ? {
2527 let entity = try model ( for: entityName)
26- let options = options. collections [ entity. id]
27- return try await database. fetch ( entity, for: id, options: options )
28+ let collectionOptions = options. collections [ entity. id]
29+ return try await database. fetch ( entity, for: id, options: collectionOptions )
2830 }
2931
3032 /// Fetch managed objects.
3133 public func fetch( _ fetchRequest: FetchRequest ) async throws -> [ ModelData ] {
3234 let entity = try model ( for: fetchRequest. entity)
33- let options = options. collections [ entity. id]
34- return try await database. fetch ( fetchRequest, entity: entity, options: options )
35+ let collectionOptions = options. collections [ entity. id]
36+ return try await database. fetch ( fetchRequest, entity: entity, options: collectionOptions )
3537 }
3638
3739 /// Fetch and return result count.
3840 public func count( _ fetchRequest: FetchRequest ) async throws -> UInt {
39- let options = options. collections [ fetchRequest. entity]
40- return try await database. count ( fetchRequest, options: options )
41+ let collectionOptions = options. collections [ fetchRequest. entity]
42+ return try await database. count ( fetchRequest, options: collectionOptions )
4143 }
4244
4345 /// Create or edit a managed object.
4446 public func insert( _ value: ModelData ) async throws {
45- let options = options. collections [ value. entity]
46- try await database. insert ( value, options: options )
47+ let collectionOptions = options. collections [ value. entity]
48+ try await database. insert ( value, options: collectionOptions )
4749 }
4850
4951 /// Create or edit multiple managed objects.
5052 public func insert( _ values: [ ModelData ] ) async throws {
51- try await database. insert ( values, options: options. collections)
53+ let collections = options. collections
54+ try await database. insert ( values, options: collections)
5255 }
5356
5457 /// Delete the specified managed object.
5558 public func delete( _ entity: EntityName , for id: ObjectID ) async throws {
56- let options = options. collections [ entity]
57- try await database. delete ( entity, for: id, options: options)
59+ let collectionOptions = options. collections [ entity]
60+ try await database. delete ( entity, for: id, options: collectionOptions)
61+ }
62+
63+ public func delete( _ entity: CoreModel . EntityName , for ids: [ CoreModel . ObjectID ] ) async throws {
64+ let collectionOptions = options. collections [ entity]
65+ try await database. delete ( entity, for: ids, options: collectionOptions)
5866 }
5967
6068 public func fetchID( _ fetchRequest: FetchRequest ) async throws -> [ ObjectID ] {
61- let options = options. collections [ fetchRequest. entity]
62- return try await database. fetchIDs ( fetchRequest, options: options )
69+ let collectionOptions = options. collections [ fetchRequest. entity]
70+ return try await database. fetchIDs ( fetchRequest, options: collectionOptions )
6371 }
6472
6573 private func model( for entityName: EntityName ) throws -> EntityDescription {
@@ -72,9 +80,9 @@ public struct MongoModelStorage: ModelStorage {
7280
7381public extension MongoModelStorage {
7482
75- struct Configuration {
83+ struct Configuration : Sendable {
7684
77- public var collections : [ EntityName : MongoCollectionOptions ]
85+ public let collections : [ EntityName : MongoCollectionOptions ]
7886
7987 public init ( collections: [ EntityName : MongoCollectionOptions ] = [ : ] ) {
8088 self . collections = collections
@@ -167,6 +175,19 @@ internal extension MongoDatabase {
167175 ]
168176 try await collection. deleteOne ( filter, options: options)
169177 }
178+
179+ /// Delete the specified managed object.
180+ func delete(
181+ _ entity: EntityName ,
182+ for ids: [ ObjectID ] ,
183+ options: MongoCollectionOptions ?
184+ ) async throws {
185+ let collection = self . collection ( entity, options: options)
186+ let filter : BSONDocument = [
187+ BSONDocument . BuiltInProperty. id. rawValue: . array( ids. map ( { . string( $0. rawValue) } ) )
188+ ]
189+ try await collection. deleteMany ( filter)
190+ }
170191
171192 func find(
172193 _ entityName: EntityName ,
0 commit comments