refactor: rename to storage
This commit is contained in:
35
server/src/storage/config.ts
Normal file
35
server/src/storage/config.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import { Sequelize } from 'sequelize';
|
||||||
|
import dotenv from 'dotenv';
|
||||||
|
import setupAssociations from '../models/associations';
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
const sequelize = new Sequelize(
|
||||||
|
`postgresql://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`,
|
||||||
|
{
|
||||||
|
host: 'localhost',
|
||||||
|
dialect: 'postgres',
|
||||||
|
logging: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export const connectDB = async () => {
|
||||||
|
try {
|
||||||
|
await sequelize.authenticate();
|
||||||
|
console.log('Database connected successfully');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Unable to connect to the database:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const syncDB = async () => {
|
||||||
|
try {
|
||||||
|
//setupAssociations();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export default sequelize;
|
||||||
Reference in New Issue
Block a user