Files
parcer/server/src/db/config.ts

36 lines
899 B
TypeScript
Raw Normal View History

2024-09-24 17:19:49 +05:30
import { Sequelize } from 'sequelize';
import dotenv from 'dotenv';
dotenv.config();
const sequelize = new Sequelize(
2024-09-24 17:24:24 +05:30
process.env.DB_NAME as string,
process.env.DB_USER as string,
process.env.DB_PASSWORD as string,
{
host: process.env.DB_HOST,
dialect: 'postgres',
logging: false,
}
2024-09-24 17:19:49 +05:30
);
export const connectDB = async () => {
2024-09-24 17:24:24 +05:30
try {
await sequelize.authenticate();
console.log('Database connected successfully');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
2024-09-24 17:19:49 +05:30
};
2024-09-24 18:00:25 +05:30
export const syncDB = async () => {
try {
await sequelize.sync({ force: false }); // force: true will drop and recreate tables on every run
console.log('Database synced successfully!');
} catch (error) {
console.error('Failed to sync database:', error);
}
};
2024-09-24 17:19:49 +05:30
export default sequelize;