Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FluentKit 1.5.2 #20

Merged
merged 2 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
.library(name: "FluentMongo", targets: ["FluentMongo"])
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/vapor/fluent-kit.git", .upToNextMajor(from: "1.5.2")),
.package(url: "https://github.com/mongodb/mongo-swift-driver.git", .upToNextMajor(from: "1.0.0"))
],
targets: [
Expand Down
7 changes: 4 additions & 3 deletions Sources/FluentMongo/DatabaseQueryFilter+Mongo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension DatabaseQuery.Filter {
case .value(let field, let method, let value):
let key = try field.mongoKeyPath(namespace: field.schema != mainSchema)
let mongoOperator = try method.mongoOperator()
guard let bsonValue = try value.mongoValueFilter(encoder: encoder) else {
guard let bsonValue = try value.mongoValueFilter(method: method, encoder: encoder) else {
return nil
}

Expand Down Expand Up @@ -74,8 +74,9 @@ extension DatabaseQuery.Filter.Method {
}
case .subset(let inverse):
return inverse ? "$nin" : "$in"
case .contains(let inverse, _):
return inverse ? "$nin" : "$in"
case .contains(_, _):
// Prefer `$in`/`$nin` over double negation once this is fixed: https://github.com/mongodb/mongo-swift-driver/issues/517
return "$not"
case .custom(let value as String):
return value
default:
Expand Down
48 changes: 46 additions & 2 deletions Sources/FluentMongo/DatabaseQueryValue+Mongo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,34 @@ import FluentKit

extension DatabaseQuery.Value {

func mongoValueFilter(encoder: BSONEncoder) throws -> BSON? {
return try self.mongoValue(ignoreIfNil: false, encoder: encoder)
func mongoValueFilter(method: DatabaseQuery.Filter.Method, encoder: BSONEncoder) throws -> BSON? {

switch method {
case .contains(let inverse, let position):
guard let value = self.mongoValueString() else {
return nil
}

let pattern: String
switch position {
case .anywhere:
pattern = value
case .prefix:
pattern = "^\(value)"
case .suffix:
pattern = "\(value)$"
}

var document: BSONDocument = ["$regex": .string(pattern)]

if (!inverse) {
document = ["$not": .document(document)]
}

return .document(document)
default:
return try self.mongoValue(ignoreIfNil: false, encoder: encoder)
}
}

func mongoValueInsert(encoder: BSONEncoder) throws -> BSON? {
Expand Down Expand Up @@ -93,6 +119,24 @@ extension DatabaseQuery.Value {
throw Error.unsupportedValue
}
}

private func mongoValueString() -> String? {
switch self {
case .bind(let value):
switch value {
case let string as String:
return string
case let string as CustomStringConvertible:
return string.description
default:
return nil
}
case .custom(let value as String):
return value
default:
return nil
}
}
}

extension DatabaseQuery.Value {
Expand Down
21 changes: 17 additions & 4 deletions Sources/FluentMongo/FluentMongoConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import NIO
import AsyncKit
import FluentKit

Expand All @@ -16,6 +17,9 @@ struct FluentMongoConfiguration: DatabaseConfiguration {

let maxConnectionsPerEventLoop: Int

/// The amount of time to wait for a connection from the connection pool before timing out.
let connectionPoolTimeout: NIO.TimeAmount

var middleware: [AnyModelMiddleware]

func makeDriver(for databases: Databases) -> DatabaseDriver {
Expand All @@ -27,6 +31,7 @@ struct FluentMongoConfiguration: DatabaseConfiguration {
let pool = EventLoopGroupConnectionPool(
source: db,
maxConnectionsPerEventLoop: self.maxConnectionsPerEventLoop,
requestTimeout: self.connectionPoolTimeout,
on: databases.eventLoopGroup
)

Expand All @@ -44,7 +49,8 @@ extension DatabaseConfigurationFactory {
port: Int = 27017,
database: String,
options: MongoClientOptions? = nil,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10)
) throws -> Self {

let configuration = try MongoConfiguration(
Expand All @@ -61,6 +67,7 @@ extension DatabaseConfigurationFactory {
FluentMongoConfiguration(
configuration: configuration,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout,
middleware: []
)
}
Expand All @@ -69,7 +76,8 @@ extension DatabaseConfigurationFactory {
public static func mongo(
connectionString: String,
options: MongoClientOptions? = nil,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10)
) throws -> Self {

let configuration = try MongoConfiguration(
Expand All @@ -81,6 +89,7 @@ extension DatabaseConfigurationFactory {
FluentMongoConfiguration(
configuration: configuration,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout,
middleware: []
)
}
Expand All @@ -89,7 +98,8 @@ extension DatabaseConfigurationFactory {
public static func mongo(
connectionURL: URL,
options: MongoClientOptions? = nil,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10)
) throws -> Self {

let configuration = try MongoConfiguration(
Expand All @@ -101,14 +111,16 @@ extension DatabaseConfigurationFactory {
FluentMongoConfiguration(
configuration: configuration,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout,
middleware: []
)
}
}

public static func mongo(
environment: [String: String] = ProcessInfo.processInfo.environment,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10)
) throws -> Self {

let configuration = try MongoConfiguration(environment: environment)
Expand All @@ -117,6 +129,7 @@ extension DatabaseConfigurationFactory {
FluentMongoConfiguration(
configuration: configuration,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout,
middleware: []
)
}
Expand Down
64 changes: 0 additions & 64 deletions Sources/FluentMongo/QueryBuilder+Operators.swift

This file was deleted.

8 changes: 2 additions & 6 deletions Tests/FluentMongoTests/FluentMongoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,15 @@ class FluentMongoTests: XCTestCase {
XCTAssertEqual(r1.count, 1)
XCTAssertEqual(r1.first?.name, "User1")

let r2 = try User.query(on: database).filter(\.$nicknames, .subset(inverse: false), "a").all().wait()
let r2 = try User.query(on: database).filter(\.$nicknames, .subset(inverse: false), ["a"]).all().wait()
XCTAssertEqual(r2.count, 1)
XCTAssertEqual(r2.first?.name, "User1")
XCTAssertEqual(try User.query(on: database).filter(\.$nicknames ~~ "a").all().wait(), r2)

let r3 = try User.query(on: database).filter(\.$nicknames, .subset(inverse: false), "b").all().wait()
let r3 = try User.query(on: database).filter(\.$nicknames, .subset(inverse: false), ["b"]).all().wait()
XCTAssertEqual(r3.count, 2)
XCTAssertEqual(try User.query(on: database).filter(\.$nicknames ~~ "b").all().wait(), r3)

let r4 = try User.query(on: database).filter(\.$nicknames, .subset(inverse: false), ["a", "b"]).all().wait()
XCTAssertEqual(r4.count, 2)
// TODO: https://github.com/vapor/fluent-kit/issues/289
// XCTAssertEqual(try User.query(on: database).filter(\.$nicknames ~~ ["a", "b"]).all().wait(), r4)
} catch {
XCTFail(error.localizedDescription)
}
Expand Down