feat(skydiver-profile): add SkydiverProfile MySQL model and service
Introduces the skydiver_profile table as an optional 1-to-1 extension of the user table, holding skydive-specific fields (licence, poids, bg_image). Wired up with Sequelize associations and a service with upsert support. Migration, model, relationships, and service layer all included.
This commit is contained in:
@@ -0,0 +1,52 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
async up(queryInterface, Sequelize) {
|
||||||
|
const transaction = await queryInterface.sequelize.transaction();
|
||||||
|
try {
|
||||||
|
await queryInterface.createTable('skydiver_profile', {
|
||||||
|
userId: {
|
||||||
|
type: Sequelize.STRING(36),
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'user',
|
||||||
|
key: 'id'
|
||||||
|
},
|
||||||
|
onDelete: 'CASCADE'
|
||||||
|
},
|
||||||
|
licence: {
|
||||||
|
type: Sequelize.INTEGER,
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
poids: {
|
||||||
|
type: Sequelize.FLOAT,
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
bg_image: {
|
||||||
|
type: Sequelize.STRING(255),
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
type: Sequelize.DATE,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
|
||||||
|
},
|
||||||
|
updatedAt: {
|
||||||
|
type: Sequelize.DATE,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
|
||||||
|
}
|
||||||
|
}, { transaction });
|
||||||
|
|
||||||
|
await transaction.commit();
|
||||||
|
} catch (err) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async down(queryInterface) {
|
||||||
|
await queryInterface.dropTable('skydiver_profile');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
const { DataTypes, Model } = require("sequelize");
|
||||||
|
const sequelize = require("../../config/sequelize");
|
||||||
|
|
||||||
|
class SkydiverProfile extends Model {}
|
||||||
|
|
||||||
|
const SkydiverProfileModel = () => {
|
||||||
|
SkydiverProfile.init(
|
||||||
|
{
|
||||||
|
userId: {
|
||||||
|
type: DataTypes.STRING(36),
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'user',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
licence: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
poids: {
|
||||||
|
type: DataTypes.FLOAT,
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
bg_image: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sequelize,
|
||||||
|
tableName: 'skydiver_profile',
|
||||||
|
timestamps: true
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
SkydiverProfile.prototype.toJSONFor = function () {
|
||||||
|
return {
|
||||||
|
licence: this.licence,
|
||||||
|
poids: this.poids,
|
||||||
|
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg'
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return SkydiverProfile;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = SkydiverProfileModel;
|
||||||
@@ -10,6 +10,7 @@ exports.Product = require('./Product');
|
|||||||
exports.ProductCategoryXref = require('./ProductCategoryXref');
|
exports.ProductCategoryXref = require('./ProductCategoryXref');
|
||||||
exports.ProductTagXref = require('./ProductTagXref');
|
exports.ProductTagXref = require('./ProductTagXref');
|
||||||
exports.Role = require('./Role');
|
exports.Role = require('./Role');
|
||||||
|
exports.SkydiverProfile = require('./SkydiverProfile');
|
||||||
exports.Tag = require('./Tag');
|
exports.Tag = require('./Tag');
|
||||||
exports.TagList = require('./TagList');
|
exports.TagList = require('./TagList');
|
||||||
exports.User = require('./User');
|
exports.User = require('./User');
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
const DB = require('../mysql');
|
const DB = require('../mysql');
|
||||||
|
|
||||||
const associate = () => {
|
const associate = () => {
|
||||||
|
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.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.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.Article.belongsToMany(DB.User, { as: 'articleFavoriteUsers', through: DB.Favorite, foreignKey: "articleId", otherKey: "userId", onDelete: "CASCADE" });
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
exports.ArticleService = require('./mysql/article.service');
|
exports.ArticleService = require('./mysql/article.service');
|
||||||
exports.CommentService = require('./mysql/comment.service');
|
exports.CommentService = require('./mysql/comment.service');
|
||||||
exports.ProductService = require('./mysql/product.service');
|
exports.ProductService = require('./mysql/product.service');
|
||||||
|
exports.SkydiverProfileService = require('./mysql/skydiver-profile.service');
|
||||||
exports.TagService = require('./mysql/tag.service');
|
exports.TagService = require('./mysql/tag.service');
|
||||||
exports.UserService = require('./mysql/user.service');
|
exports.UserService = require('./mysql/user.service');
|
||||||
//exports.HeroWarService = require('./mysql/herowars.service');
|
//exports.HeroWarService = require('./mysql/herowars.service');
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
const DB = require('../../database/mysql');
|
||||||
|
|
||||||
|
const { SkydiverProfile } = DB;
|
||||||
|
|
||||||
|
class SkydiverProfileService {
|
||||||
|
static async getByUserId(userId) {
|
||||||
|
return SkydiverProfile.findByPk(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
static async upsert(userId, data) {
|
||||||
|
const [profile] = await SkydiverProfile.upsert({ userId, ...data });
|
||||||
|
return profile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = SkydiverProfileService;
|
||||||
Reference in New Issue
Block a user