Presupuestador_web/server/src/contexts/auth/infrastructure/sequelize/authUser.model.ts

79 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-06-10 17:16:39 +00:00
import { USER_ROLES } from "@/contexts/users";
2024-05-26 17:09:43 +00:00
import { DataTypes, InferAttributes, InferCreationAttributes, Model, Sequelize } from "sequelize";
2024-05-15 19:56:22 +00:00
2024-05-26 17:09:43 +00:00
export type AuthUserCreationAttributes = InferCreationAttributes<AuthUser_Model>;
2024-05-15 19:56:22 +00:00
2024-05-16 18:16:00 +00:00
export class AuthUser_Model extends Model<
InferAttributes<AuthUser_Model>,
InferCreationAttributes<AuthUser_Model>
2024-05-15 19:56:22 +00:00
> {
// To avoid table creation
/*static async sync(): Promise<any> {
return Promise.resolve();
}*/
2024-05-26 17:09:43 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2024-05-15 19:56:22 +00:00
static associate(connection: Sequelize) {}
declare id: string;
2024-05-16 11:56:46 +00:00
declare name: string;
2024-05-15 19:56:22 +00:00
declare email: string;
declare password: string;
2024-06-10 17:16:39 +00:00
declare roles: string[];
2024-05-15 19:56:22 +00:00
}
export default (sequelize: Sequelize) => {
2024-05-16 18:16:00 +00:00
AuthUser_Model.init(
2024-05-15 19:56:22 +00:00
{
id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
2024-05-16 11:56:46 +00:00
name: {
type: DataTypes.STRING,
},
2024-05-15 19:56:22 +00:00
email: {
type: DataTypes.STRING,
allowNull: false,
},
2024-05-16 11:56:46 +00:00
2024-05-15 19:56:22 +00:00
password: {
type: DataTypes.STRING,
allowNull: false,
},
2024-06-10 17:16:39 +00:00
roles: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: USER_ROLES.ROLE_USER,
get(this: AuthUser_Model): string[] {
const rawValue = this.getDataValue("roles") as any;
return String(rawValue).split(";");
},
set(this: AuthUser_Model, value: string[]) {
const rawValue = value.join(";") as any;
this.setDataValue("roles", rawValue);
},
},
2024-05-15 19:56:22 +00:00
},
{
sequelize,
tableName: "users",
2024-05-16 11:56:46 +00:00
paranoid: true, // softs deletes
timestamps: true,
2024-05-15 19:56:22 +00:00
//version: true,
2024-05-16 11:56:46 +00:00
createdAt: "created_at",
updatedAt: "updated_at",
deletedAt: "deleted_at",
2024-05-15 19:56:22 +00:00
2024-05-16 11:56:46 +00:00
indexes: [{ name: "email_idx", fields: ["email"] }],
2024-05-26 17:09:43 +00:00
}
2024-05-15 19:56:22 +00:00
);
2024-05-16 18:16:00 +00:00
return AuthUser_Model;
2024-05-15 19:56:22 +00:00
};