Bansari Akhani
get user with associations - role & it's permissions
2f7395b
import {
DataTypes,
Model,
InferAttributes,
InferCreationAttributes,
CreationOptional,
} from 'sequelize';
import { sequelize } from './index';
import { RoleInterface } from '../shared/interfaces/role.interface';
import Permission from './permissions';
import RolePermission from './rolePermissions';
class Role extends Model<InferAttributes<Role>, InferCreationAttributes<Role>> implements RoleInterface {
declare id: CreationOptional<number>;
declare name: string;
}
Role.init(
{
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
unique: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
}
},
{
sequelize,
tableName: 'roles',
underscored: true,
freezeTableName: true,
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
}
);
Role.belongsToMany(Permission, { through: 'role_permission', as : 'permissions' });
export default Role;