aboutsummaryrefslogtreecommitdiff
path: root/test/utils/dbConnection.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test/utils/dbConnection.ts')
-rw-r--r--test/utils/dbConnection.ts26
1 files changed, 26 insertions, 0 deletions
diff --git a/test/utils/dbConnection.ts b/test/utils/dbConnection.ts
new file mode 100644
index 0000000..ee1e9e0
--- /dev/null
+++ b/test/utils/dbConnection.ts
@@ -0,0 +1,26 @@
+import mongoose from 'mongoose';
+import Promise from 'bluebird';
+
+mongoose.Promise = Promise;
+
+const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/mongo-cronjob-test';
+const { MONGODB_USER, MONGODB_PASSWORD } = process.env;
+
+mongoose.connect(MONGODB_URL, {
+ user: MONGODB_USER,
+ pass: MONGODB_PASSWORD,
+ useNewUrlParser: true,
+ useUnifiedTopology: true,
+ useCreateIndex: true,
+ useFindAndModify: false,
+ family: 4 // Use IPv4, skip trying IPv6
+});
+
+const db = mongoose.connection;
+db.on('error', console.error.bind(console, 'connection error:'));
+db.once('open', () => {
+ console.log('Connection to MongoDB successful');
+});
+
+export default db;
+