mirror of
https://github.com/wassname/talk.git
synced 2026-07-06 05:17:19 +08:00
30 lines
890 B
TypeScript
30 lines
890 B
TypeScript
import { Db, MongoClient } from "mongodb";
|
|
|
|
import { Config } from "coral-server/config";
|
|
import { InternalError } from "coral-server/errors";
|
|
|
|
export async function createMongoClient(config: Config): Promise<MongoClient> {
|
|
try {
|
|
return await MongoClient.connect(config.get("mongodb"), {
|
|
useNewUrlParser: true,
|
|
ignoreUndefined: true,
|
|
});
|
|
} catch (err) {
|
|
throw new InternalError(err, "could not connect to mongodb");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* create will connect to the MongoDB instance identified in the configuration.
|
|
*
|
|
* @param config application configuration.
|
|
*/
|
|
export async function createMongoDB(config: Config): Promise<Db> {
|
|
// Connect and create a client for MongoDB.
|
|
const client = await createMongoClient(config);
|
|
|
|
// Return the database handle, which defaults to the database name provided
|
|
// in the config connection string.
|
|
return client.db();
|
|
}
|