Mongo Kitten Support #175
Answered
by
Joannis
nonfungibletunji
asked this question in
Q&A
-
how does one use mongo kitten for MongoDB support ? |
Beta Was this translation helpful? Give feedback.
Answered by
Joannis
Mar 15, 2023
Replies: 3 comments
-
Hey @nonfungibletunji , you can follow MongoKitten's own instructions. It covers both Vapor and Hummingbird. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
nonfungibletunji
-
Here's a full import Hummingbird
import MongoKitten
import Meow
@main
public struct HBTest {
public static func main() async throws {
let app = HBApplication()
app.mongo = try MongoDatabase.lazyConnect(to: "mongodb://localhost/poc")
app.router.get("/") { req -> [Document] in
return try await req.mongo["esblog"].find().drain()
}
try app.start()
try await app.server.channel?.closeFuture.get()
}
}
extension HBApplication {
public var mongo: MongoDatabase {
get { extensions.get(\.mongo) }
set { extensions.set(\.mongo, value: newValue) }
}
}
extension HBRequest {
public var mongo: MongoDatabase {
application.mongo.adoptingLogMetadata([
"hb_id": self.id
])
}
}
extension HBApplication {
public var meow: MeowDatabase {
MeowDatabase(mongo)
}
}
extension HBRequest {
public var meow: MeowDatabase {
MeowDatabase(mongo)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
I saw that after I posted in the discussions. Thanks
…On Mar 15, 2023 at 4:06 AM -0400, Joannis Orlandos ***@***.***>, wrote:
Here's a full main.swift file that uses Hummingbird 1.x and MongoKitten 7.x:
import Hummingbird
import MongoKitten
import Meow
@main
public struct HBTest {
public static func main() async throws {
let app = HBApplication()
app.mongo = try MongoDatabase.lazyConnect(to: "mongodb://localhost/poc")
app.router.get("/") { req -> [Document] in
return try await req.mongo["esblog"].find().drain()
}
try app.start()
try await app.server.channel?.closeFuture.get()
}
}
extension HBApplication {
public var mongo: MongoDatabase {
get { extensions.get(\.mongo) }
set { extensions.set(\.mongo, value: newValue) }
}
}
extension HBRequest {
public var mongo: MongoDatabase {
application.mongo.adoptingLogMetadata([
"hb_id": logger[metadataKey: "hb_id"] ?? "unknown"
])
}
}
extension HBApplication {
public var meow: MeowDatabase {
MeowDatabase(mongo)
}
}
extension HBRequest {
public var meow: MeowDatabase {
MeowDatabase(mongo)
}
}
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @nonfungibletunji , you can follow MongoKitten's own instructions. It covers both Vapor and Hummingbird.