46 lines
1006 B
Swift
46 lines
1006 B
Swift
//
|
|
// MongoController.swift
|
|
// MongoView
|
|
//
|
|
// Created by Shadowfacts on 1/8/20.
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import MongoSwift
|
|
import NIO
|
|
|
|
class MongoController {
|
|
let connectionString: String
|
|
|
|
var group: EventLoopGroup!
|
|
|
|
var client: MongoClient!
|
|
|
|
init(connectionString: String) {
|
|
self.connectionString = connectionString
|
|
}
|
|
|
|
deinit {
|
|
try! group.syncShutdownGracefully()
|
|
cleanupMongoSwift()
|
|
}
|
|
|
|
func setup() {
|
|
print("setting up mongo")
|
|
|
|
group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
|
|
|
client = try! MongoClient(connectionString, using: group)
|
|
}
|
|
|
|
func db(for collection: DatabaseCollection) -> MongoDatabase {
|
|
return client.db(collection.database)
|
|
}
|
|
|
|
func collection(_ collection: DatabaseCollection) -> MongoCollection<Document> {
|
|
return db(for: collection).collection(collection.name)
|
|
}
|
|
|
|
}
|