associations and db stuff

This commit is contained in:
counterweight 2025-03-08 02:47:37 +01:00
parent bf478bbbe9
commit 2d124a1ef4
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
3 changed files with 34 additions and 12 deletions

19
src/associations.js Normal file
View file

@ -0,0 +1,19 @@
class AssociationsDefiner {
constructor({ models, DataTypes }) {
this.models = models;
this.DataTypes = DataTypes;
}
define() {
this.models.OfferCreated.hasOne(this.models.OfferDeleted);
this.models.OfferDeleted.belongsTo(this.models.OfferCreated, {
foreignKey: {
name: 'offer_uuid',
type: this.DataTypes.UUID,
allowNull: false,
},
});
}
}
module.exports = AssociationsDefiner;

View file

@ -10,11 +10,11 @@ const sequelize = new Sequelize({
database: process.env.POSTGRES_DB,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
logging: (msg) => {
/* logging: (msg) => {
if (msg && (msg.includes('ERROR') || msg.includes('error'))) {
console.error(msg);
}
},
}, */
define: {
timestamps: false,
freezeTableName: true,
@ -23,13 +23,4 @@ const sequelize = new Sequelize({
},
});
sequelize
.sync()
.then(() => {
console.log('Database synced');
})
.catch((err) => {
console.error('Error syncing the database:', err);
});
module.exports = sequelize;

View file

@ -1,6 +1,6 @@
const express = require('express');
function buildDependencies() {
async function buildDependencies() {
const dependencies = {};
const errors = require('./errors');
const constants = require('./constants');
@ -10,6 +10,18 @@ function buildDependencies() {
const ModelsProvider = require('./models');
const models = new ModelsProvider({ sequelize, DataTypes }).provide();
const AssociationsDefiner = require('./associations');
new AssociationsDefiner({ models, DataTypes }).define();
sequelize
.sync({ alter: true })
.then(() => {
console.log('Database synced');
})
.catch((err) => {
console.error('Error syncing the database:', err);
});
const ServicesProvider = require('./services');
const services = new ServicesProvider({
models,