associations and db stuff
This commit is contained in:
parent
bf478bbbe9
commit
2d124a1ef4
3 changed files with 34 additions and 12 deletions
19
src/associations.js
Normal file
19
src/associations.js
Normal 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;
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue