Update 'src/config/postgres.js'

tungd0 1 year ago
parent f44d590b40
commit 65b85f1640
  1. 80
      src/config/postgres.js

@ -1,40 +1,40 @@
import Sequelize from 'sequelize'; import Sequelize from 'sequelize';
import bluebird from 'bluebird'; import bluebird from 'bluebird';
import { postgres, env } from './vars'; import { postgres, env } from './vars';
Sequelize.Promise = bluebird; Sequelize.Promise = bluebird;
const defaultErrorHandler = (err) => { const defaultErrorHandler = (err) => {
console.log(`Connection to Postgres error: ${err}`); console.log(`Connection to Postgres error: ${err}`);
}; };
const app = { const app = {
sequelize: new Sequelize( sequelize: new Sequelize(
postgres.uri, postgres.uri,
{ {
dialect: 'postgres' dialect: 'postgres'
} }
), ),
connect(errorHandler = defaultErrorHandler) { connect(errorHandler = defaultErrorHandler) {
this.sequelize.authenticate() this.sequelize.authenticate()
.then(() => { .then(() => {
console.log('Postgres connection established!'); console.log('Postgres connection established!');
if (env === '1') { if (env === 'development') {
this.sequelize.sync({ this.sequelize.sync({
alter: true, alter: true,
logging: true logging: true
}); });
} }
}).catch((error) => { }).catch((error) => {
errorHandler(error); errorHandler(error);
}); });
return this.sequelize; return this.sequelize;
}, },
disconnect() { disconnect() {
// close connection // close connection
console.log('Closing postgres connection!'); console.log('Closing postgres connection!');
this.sequelize.close(); this.sequelize.close();
} }
}; };
export default app; export default app;

Loading…
Cancel
Save