Sina/database/relation.js
2025-07-24 08:10:35 +03:00

63 lines
1.3 KiB
JavaScript

const books_db = require("../api/v1/books/db");
const authors_db = require("../api/v1/authors/db");
const quotes_db = require("../api/v1/quotes/db");
const user_db = require("../api/v1/users/db");
const login_db = require("../api/v1/logins/db");
books_db.belongsToMany(authors_db, {
through: "books_author",
as: "author",
foreignKey: "book_id"
});
authors_db.belongsToMany(books_db, {
through: "books_author",
as: "book",
foreignKey: "author_id"
});
books_db.hasMany(quotes_db, {
foreignKey: {
allowNull: false
}
});
quotes_db.belongsTo(books_db);
user_db.hasMany(quotes_db, {
foreignKey: {
allowNull: false
}
});
quotes_db.belongsTo(user_db);
user_db.hasMany(login_db, {
foreignKey: {
allowNull: false
}
});
login_db.belongsTo(user_db);
user_db.belongsToMany(quotes_db, {
through: "action_quotes",
as: "quote_quote",
foreignKey: "user_id"
});
quotes_db.belongsToMany(user_db, {
through: "action_quotes",
as: "quote_user",
foreignKey: "quote_id"
});
user_db.belongsToMany(authors_db, {
through: "follows_authors",
as: "followa_author",
foreignKey: "user_id"
});
authors_db.belongsToMany(user_db, {
through: "follows_authors",
as: "followa_user",
foreignKey: "author_id"
});