b3a31e4725
- Add Sequelize migration creating the 'application' table
- Add Application Sequelize model with slugify, toJSONFor, toAuthJSON
- Rewrite passport-headerapikey strategy to use DB.Application
- Rewrite applications.routes.js to use Sequelize (findAll, count,
build/save, destroy) instead of Mongoose
- Fix pre-existing bug: passport was querying { encryptedKey } but the
stored field is named 'apikey'
- Add Application to MySQL models index and relationships
- Remove Application from Mongoose models index and delete the schema
76 lines
6.3 KiB
JavaScript
76 lines
6.3 KiB
JavaScript
const DB = require('../mysql');
|
|
|
|
const associate = () => {
|
|
DB.Application.belongsTo(DB.User, { as: 'author', foreignKey: 'authorId' });
|
|
DB.User.hasMany(DB.Application, { as: 'applications', foreignKey: 'authorId', onDelete: 'SET NULL' });
|
|
|
|
DB.User.hasOne(DB.SkydiverProfile, { as: 'skydiverProfile', foreignKey: 'userId', onDelete: 'CASCADE' });
|
|
DB.SkydiverProfile.belongsTo(DB.User, { as: 'user', foreignKey: 'userId' });
|
|
|
|
DB.Article.belongsToMany(DB.Tag, { as: 'articleTags', through: DB.TagList, foreignKey: "articleId", otherKey: "tagName", onDelete: "CASCADE" });
|
|
DB.Product.belongsToMany(DB.Tag, { as: 'productTags', through: DB.ProductTagXref, foreignKey: "productId", otherKey: "tagName", onDelete: "CASCADE" });
|
|
DB.Article.belongsToMany(DB.User, { as: 'articleFavoriteUsers', through: DB.Favorite, foreignKey: "articleId", otherKey: "userId", onDelete: "CASCADE" });
|
|
DB.Category.belongsToMany(DB.Product, { as: 'productIdProducts', through: DB.ProductCategoryXref, foreignKey: "categoryId", otherKey: "productId" });
|
|
DB.Product.belongsToMany(DB.Category, { as: 'productCategories', through: DB.ProductCategoryXref, foreignKey: "productId", otherKey: "categoryId" });
|
|
DB.Role.belongsToMany(DB.User, { as: 'userIdUserUserRoleXrefs', through: DB.UserRoleXref, foreignKey: "roleId", otherKey: "userId" });
|
|
DB.Tag.belongsToMany(DB.Article, { as: 'articleIdArticleTagLists', through: DB.TagList, foreignKey: "tagName", otherKey: "articleId" });
|
|
DB.Tag.belongsToMany(DB.Product, { as: 'productIdProductProductTagXrefs', through: DB.ProductTagXref, foreignKey: "tagName", otherKey: "productId" });
|
|
DB.User.belongsToMany(DB.Article, { as: 'articleIdArticles', through: DB.Favorite, foreignKey: "userId", otherKey: "articleId" });
|
|
DB.User.belongsToMany(DB.Role, { as: 'roleIdRoles', through: DB.UserRoleXref, foreignKey: "userId", otherKey: "roleId" });
|
|
DB.User.belongsToMany(DB.User, { as: 'followerIdUsers', through: DB.Follower, foreignKey: "userId", otherKey: "followerId" });
|
|
DB.User.belongsToMany(DB.User, { as: 'userIdUserFollowers', through: DB.Follower, foreignKey: "followerId", otherKey: "userId" });
|
|
DB.Comment.belongsTo(DB.Article, { as: "article", foreignKey: "articleId"});
|
|
DB.Article.hasMany(DB.Comment, { as: "comments", foreignKey: "articleId", onDelete: "CASCADE"});
|
|
DB.Favorite.belongsTo(DB.Article, { as: "article", foreignKey: "articleId"});
|
|
DB.Article.hasMany(DB.Favorite, { as: "favorites", foreignKey: "articleId", onDelete: "CASCADE"});
|
|
DB.TagList.belongsTo(DB.Article, { as: "article", foreignKey: "articleId"});
|
|
DB.Article.hasMany(DB.TagList, { as: "tagLists", foreignKey: "articleId", onDelete: "CASCADE"});
|
|
DB.Product.belongsTo(DB.Brand, { as: "brand", foreignKey: "brandId"});
|
|
DB.Brand.hasMany(DB.Product, { as: "products", foreignKey: "brandId"});
|
|
DB.ProductCategoryXref.belongsTo(DB.Category, { as: "category", foreignKey: "categoryId"});
|
|
DB.Category.hasMany(DB.ProductCategoryXref, { as: "productCategoryXrefs", foreignKey: "categoryId"});
|
|
DB.Product.belongsTo(DB.Packaging, { as: "packaging", foreignKey: "packagingId"});
|
|
DB.Packaging.hasMany(DB.Product, { as: "products", foreignKey: "packagingId"});
|
|
DB.ProductCategoryXref.belongsTo(DB.Product, { as: "product", foreignKey: "productId"});
|
|
DB.Product.hasMany(DB.ProductCategoryXref, { as: "productCategoryXrefs", foreignKey: "productId"});
|
|
DB.ProductTagXref.belongsTo(DB.Product, { as: "product", foreignKey: "productId"});
|
|
DB.Product.hasMany(DB.ProductTagXref, { as: "productTagXrefs", foreignKey: "productId"});
|
|
DB.UserRoleXref.belongsTo(DB.Role, { as: "role", foreignKey: "roleId"});
|
|
DB.Role.hasMany(DB.UserRoleXref, { as: "userRoleXrefs", foreignKey: "roleId"});
|
|
DB.ProductTagXref.belongsTo(DB.Tag, { as: "tagNameTag", foreignKey: "tagName"});
|
|
DB.Tag.hasMany(DB.ProductTagXref, { as: "productTagXrefs", foreignKey: "tagName"});
|
|
DB.TagList.belongsTo(DB.Tag, { as: "tagNameTag", foreignKey: "tagName"});
|
|
DB.Tag.hasMany(DB.TagList, { as: "tagLists", foreignKey: "tagName", onDelete: "CASCADE"});
|
|
DB.Article.belongsTo(DB.User, { as: "author", foreignKey: "authorId"});
|
|
DB.User.hasMany(DB.Article, { as: "articles", foreignKey: "authorId", onDelete: "CASCADE"});
|
|
DB.Comment.belongsTo(DB.User, { as: "author", foreignKey: "authorId"});
|
|
DB.User.hasMany(DB.Comment, { as: "comments", foreignKey: "authorId", onDelete: "CASCADE"});
|
|
DB.Favorite.belongsTo(DB.User, { as: "user", foreignKey: "userId"});
|
|
DB.User.hasMany(DB.Favorite, { as: "favorites", foreignKey: "userId"});
|
|
DB.Follower.belongsTo(DB.User, { as: "user", foreignKey: "userId"});
|
|
DB.User.hasMany(DB.Follower, { as: "followers", foreignKey: "userId"});
|
|
DB.Follower.belongsTo(DB.User, { as: "follower", foreignKey: "followerId"});
|
|
DB.User.hasMany(DB.Follower, { as: "followerFollowers", foreignKey: "followerId"});
|
|
DB.Product.belongsTo(DB.User, { as: "owner", foreignKey: "ownerId"});
|
|
DB.User.hasMany(DB.Product, { as: "products", foreignKey: "ownerId"});
|
|
DB.UserRoleXref.belongsTo(DB.User, { as: "user", foreignKey: "userId"});
|
|
DB.User.hasMany(DB.UserRoleXref, { as: "userRoleXrefs", foreignKey: "userId"});
|
|
/*
|
|
// Relations
|
|
DB.User.belongsToMany(DB.User, { as: "followers", through: "Followers", foreignKey: "userId", timestamps: false });
|
|
DB.User.belongsToMany(DB.User, { as: "following", through: "Followers", foreignKey: "followerId", timestamps: false });
|
|
DB.User.hasMany(DB.Article, { foreignKey: "authorId", onDelete: "CASCADE" });
|
|
DB.Article.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
|
|
DB.User.hasMany(DB.Comment, { foreignKey: "authorId", onDelete: "CASCADE" });
|
|
DB.Comment.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
|
|
DB.Article.hasMany(DB.Comment, { foreignKey: "articleId", onDelete: "CASCADE" });
|
|
DB.Comment.belongsTo(DB.Article, { foreignKey: "articleId" });
|
|
DB.User.belongsToMany(DB.Article, { as: "favorites", through: "Favorites", timestamps: false });
|
|
DB.Article.belongsToMany(DB.User, { through: "Favorites", foreignKey: "articleId", timestamps: false });
|
|
DB.Article.belongsToMany(DB.Tag, { through: "TagLists", as: "tagLists", foreignKey: "articleId", timestamps: false, onDelete: "CASCADE" });
|
|
DB.Tag.belongsToMany(DB.Article, { through: "ArticleTags", uniqueKey: false, timestamps: false });
|
|
*/
|
|
};
|
|
|
|
module.exports = associate;
|