diff --git a/Sources/MongoDBModel/FetchRequest.swift b/Sources/MongoDBModel/FetchRequest.swift index a665319..89a11d0 100644 --- a/Sources/MongoDBModel/FetchRequest.swift +++ b/Sources/MongoDBModel/FetchRequest.swift @@ -7,18 +7,8 @@ import Foundation import CoreModel -import Predicate import MongoSwift -public extension BSONDocument { - - init(filter predicate: Predicate) { - self.init() - // TODO: - assertionFailure("Not implemented") - } -} - public extension BSONDocument { init(sort sortDescriptors: [FetchRequest.SortDescriptor]) { diff --git a/Sources/MongoDBModel/MongoDatabase.swift b/Sources/MongoDBModel/MongoDatabase.swift index 472befa..ca17326 100644 --- a/Sources/MongoDBModel/MongoDatabase.swift +++ b/Sources/MongoDBModel/MongoDatabase.swift @@ -108,7 +108,7 @@ internal extension MongoDatabase { ) async throws -> UInt { let entityName = fetchRequest.entity let collection = self.collection(entityName, options: options) - let filter = fetchRequest.predicate.map { BSONDocument(filter: $0) } ?? [:] + let filter = fetchRequest.predicate.flatMap { BSONDocument(predicate: $0) } ?? [:] let options = CountDocumentsOptions(fetchRequest: fetchRequest) let count = try await collection.countDocuments(filter, options: options) return UInt(count) @@ -173,7 +173,7 @@ internal extension MongoDatabase { ) async throws -> [BSONDocument] { let entityName = fetchRequest.entity let collection = self.collection(entityName, options: options) - let filter = fetchRequest.predicate.map { BSONDocument(filter: $0) } ?? [:] + let filter = fetchRequest.predicate.flatMap { BSONDocument(predicate: $0) } ?? [:] let options = FindOptions(fetchRequest: fetchRequest) let stream = try await collection.find(filter, options: options) var results = [BSONDocument]() diff --git a/Sources/MongoDBModel/Predicate.swift b/Sources/MongoDBModel/Predicate.swift new file mode 100644 index 0000000..1c32915 --- /dev/null +++ b/Sources/MongoDBModel/Predicate.swift @@ -0,0 +1,61 @@ +// +// Predicate.swift +// +// +// Created by Alsey Coleman Miller on 8/21/23. +// + +import Foundation +import CoreModel +import Predicate +import MongoSwift + +public extension BSONDocument { + + init?(predicate: Predicate) { + switch predicate { + case .comparison(let comparison): + self.init(predicate: comparison) + case .compound(let compound): + self.init(predicate: compound) + case .value: + return nil + } + } +} + +public extension BSONDocument { + + init?(predicate: Compound) { + guard predicate.type != .not else { + // { field: { $not: { } } } + return nil + } + var array = [BSONDocument]() + array.reserveCapacity(predicate.subpredicates.count) + for subpredicate in predicate.subpredicates { + guard let document = BSONDocument(predicate: subpredicate) else { + return nil + } + array.append(document) + } + // { $and: [ { }, { } , ... , { } ] } + // { $or: [ { }, { }, ... , { } ] } + self = [LogicalQueryOperator(predicate: predicate.type).rawValue: .array(array.map { .document($0) })] + } +} + +public extension BSONDocument { + + init?(predicate: Comparison) { + // { : { $eq: } } + return nil + } +} + +public extension BSON { + + init?(predicate: Expression) { + return nil + } +}