orlandos-nl / MongoKitten

Native MongoDB driver for Swift, written in Swift

Home Page:https://orlandos.nl/docs/mongokitten/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't add MongoKitten database as a service in Vapor

dannymout opened this issue · comments

I'm trying to add a MongoKitten/MongoDB database as a service in Vapor, and I can't seem to get it to work as shown in the readme.

/// Called before your application initializes.
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
   ...
    // Register database
    let connectionURI = "mongodb://localhost:27017/test"
    services.register { container -> MongoKitten.Database in
        return try MongoKitten.Database.lazyConnect(connectionURI, on: )
    }
  ...
}

From here, I cannot access container as a Container type, so I can't use container.eventLoop.

Try this

extension MongoKitten.Database: Service {}

//Inside configure
services.register(MongoKitten.Database.self) { container in
        return try MongoKitten.Database.lazyConnect(settings: mongoSettings, on: container.eventLoop)
    }

@Andrewangeta Adding that extension seemed to fix it, thanks!