Skip to content

Commit

Permalink
Add BSONDocument.init(predicate:)
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Aug 21, 2023
1 parent f2ac23c commit 61eb967
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
10 changes: 0 additions & 10 deletions Sources/MongoDBModel/FetchRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/MongoDBModel/MongoDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]()
Expand Down
61 changes: 61 additions & 0 deletions Sources/MongoDBModel/Predicate.swift
Original file line number Diff line number Diff line change
@@ -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: { <operator-expression> } } }
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: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
// { $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
self = [LogicalQueryOperator(predicate: predicate.type).rawValue: .array(array.map { .document($0) })]
}
}

public extension BSONDocument {

init?(predicate: Comparison) {
// { <field>: { $eq: <value> } }
return nil
}
}

public extension BSON {

init?(predicate: Expression) {
return nil
}
}

0 comments on commit 61eb967

Please sign in to comment.