mongodb#MongoClientOptions TypeScript Examples
The following examples show how to use
mongodb#MongoClientOptions.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts From integration-services with Apache License 2.0 | 6 votes |
/**
* Connect to the mongodb.
*
* @static
* @param {string} url The url to the mongodb.
* @param {string} dbName The name of the database.
* @return {*} {Promise<MongoClient>}
* @memberof MongoDbService
*/
static async connect(url: string, dbName: string): Promise<MongoClient | undefined> {
if (MongoDbService.client) {
return undefined;
}
return new Promise((resolve, reject) => {
const options: MongoClientOptions = {
useUnifiedTopology: true
} as MongoClientOptions;
MongoClient.connect(url, options, function (err: Error, client: MongoClient) {
if (err != null) {
logger.error('could not connect to mongodb');
reject(err);
return;
}
logger.log('Successfully connected to mongodb');
MongoDbService.client = client;
MongoDbService.db = client.db(dbName);
resolve(client);
});
});
}
Example #2
Source File: db.ts From monkeytype with GNU General Public License v3.0 | 5 votes |
export async function connect(): Promise<void> {
const {
DB_USERNAME,
DB_PASSWORD,
DB_AUTH_MECHANISM,
DB_AUTH_SOURCE,
DB_URI,
DB_NAME,
} = process.env;
if (!DB_URI || !DB_NAME) {
throw new Error("No database configuration provided");
}
const connectionOptions: MongoClientOptions = {
connectTimeoutMS: 2000,
serverSelectionTimeoutMS: 2000,
auth: !(DB_USERNAME && DB_PASSWORD)
? undefined
: {
username: DB_USERNAME,
password: DB_PASSWORD,
},
authMechanism: DB_AUTH_MECHANISM as AuthMechanism | undefined,
authSource: DB_AUTH_SOURCE,
};
const mongoClient = new MongoClient(
(DB_URI as string) ?? global.__MONGO_URI__, // Set in tests only
connectionOptions
);
try {
await mongoClient.connect();
db = mongoClient.db(DB_NAME);
} catch (error) {
Logger.error(error.message);
Logger.error(
"Failed to connect to database. Exiting with exit status code 1."
);
process.exit(1);
}
}