tethys.api/db.config.js

81 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-11-07 12:55:02 +00:00
import Sequelize from "sequelize";
import * as dotenv from "dotenv"; // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
dotenv.config();
// module.exports = {
// HOST: "localhost",
// USER: "tethys_admin",
// PASSWORD: "tethys_admin007",
// DB: "tethys",
// dialect: "postgres",
// pool: {
// max: 5,
// min: 0,
// acquire: 30000,
// idle: 10000
// }
// };
// const pg = require('pg');
// pg.types.setTypeParser(1114, (str) => new Date((str.split(' ').join('T'))+'Z'));
const dbSchema = process.env.DB_SCHEMA;
const dbName = process.env.DB_NAME;
const dbUser = process.env.DB_USER;
const dbPassword = process.env.DB_PASSWORD;
const dbHost = process.env.DB_HOST;
// const dbDriver = process.env.DB_DRIVER; // as Dialect
const sequelizeConnection = new Sequelize(
dbName,
dbUser,
dbPassword,
{
schema: dbSchema,
host: dbHost || "localhost",
port: process.env.DB_PORT || 5432,
dialect: "postgres",
dialectOptions: {
ssl: process.env.DB_SSL == "true",
},
pool: {
max: 10,
min: 0,
acquire: 30000,
idle: 10000,
},
logging: false,
dialectOptions: {
useUTC: false, //for reading from database
dateStrings: true,
typeCast: true
},
timezone: '+02:00' //for writing to database
// host: "localhost",
// dialect: 'postgres',
// logging: false
}
);
sequelizeConnection
.authenticate()
.then(() => {
console.log("Connection has been established successfully.");
})
.catch((err) => {
console.error("Unable to connect to the database:", err);
});
export default sequelizeConnection;
// export default sequelizeConnection;
// // relations
// Dataset.hasMany(Title, {
// as: "titles",
// foreignKey: "document_id"
// });
// Title.belongsTo(Dataset, {
// foreignKey: "document_id",
// as: "dataset",
// });