2021-07-26 10:58:58 -07:00
|
|
|
"use strict"
|
2021-12-07 10:33:19 -08:00
|
|
|
|
|
|
|
require("dotenv").config()
|
2021-07-26 10:58:58 -07:00
|
|
|
const Sequelize = require("sequelize")
|
|
|
|
const db = {}
|
2021-12-07 10:33:19 -08:00
|
|
|
var parse = require("pg-connection-string")
|
|
|
|
var readDBConfig = parse(process.env.TIMESCALEDB_URL_READ)
|
|
|
|
var writeDBConfig = parse(process.env.TIMESCALEDB_URL)
|
2021-07-26 10:58:58 -07:00
|
|
|
|
2021-12-07 10:33:19 -08:00
|
|
|
const sequelize = new Sequelize({
|
|
|
|
username: writeDBConfig.user,
|
|
|
|
password: writeDBConfig.password,
|
|
|
|
database: writeDBConfig.database,
|
|
|
|
port: parseInt(writeDBConfig.port),
|
|
|
|
replication: {
|
|
|
|
read: [{ host: readDBConfig.host }],
|
|
|
|
write: { host: writeDBConfig.host },
|
|
|
|
},
|
|
|
|
dialect: "postgres",
|
|
|
|
logging: false,
|
|
|
|
dialectOptions: {
|
|
|
|
ssl: {
|
|
|
|
require: true,
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-07-26 10:58:58 -07:00
|
|
|
|
|
|
|
sequelize
|
|
|
|
.authenticate()
|
|
|
|
.then(function (err) {
|
|
|
|
console.log("Connection has been established successfully.")
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
console.log("Unable to connect to the database:", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
db.sequelize = sequelize
|
|
|
|
db.Sequelize = Sequelize
|
|
|
|
|
|
|
|
module.exports = db
|