forked from stargate/stargate-mongoose-sample-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.js
36 lines (30 loc) · 987 Bytes
/
connect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
require('dotenv').config();
const mongoose = require('./mongoose');
require('./models');
const {createAstraUri} = require("stargate-mongoose");
let conn = null;
module.exports = async function connect() {
if (conn != null) {
return conn;
}
conn = mongoose.connection;
let uri = '';
let jsonApiConnectOptions = {};
if (process.env.IS_ASTRA === 'true') {
uri = createAstraUri(process.env.ASTRA_DBID, process.env.ASTRA_REGION, process.env.ASTRA_KEYSPACE, process.env.ASTRA_APPLICATION_TOKEN);
jsonApiConnectOptions = {
isAstra: true
}
} else {
uri = process.env.JSON_API_URL;
jsonApiConnectOptions = {
username: process.env.JSON_API_AUTH_USERNAME,
password: process.env.JSON_API_AUTH_PASSWORD,
authUrl: process.env.JSON_API_AUTH_URL
}
}
await mongoose.connect(uri, jsonApiConnectOptions);
await Promise.all(Object.values(mongoose.connection.models).map(Model => Model.init()));
return conn;
};