Uecko_ERP/apps/server/src/contexts/auth/infraestructure/sequelize/auth-user.model.ts

75 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-02-03 19:50:16 +00:00
import { DataTypes, InferAttributes, InferCreationAttributes, Model, Sequelize } from "sequelize";
2025-01-30 10:45:31 +00:00
export type AuthUserCreationAttributes = InferCreationAttributes<AuthUserModel>;
2025-02-03 19:50:16 +00:00
export class AuthUserModel extends Model<
2025-01-30 10:45:31 +00:00
InferAttributes<AuthUserModel>,
InferCreationAttributes<AuthUserModel>
> {
2025-02-03 19:50:16 +00:00
// To avoid table creation
/*static async sync(): Promise<any> {
return Promise.resolve();
}*/
static associate(connection: Sequelize) {}
2025-02-03 18:03:23 +00:00
declare id: string;
declare username: string;
declare email: string;
declare password: string;
declare roles: string[];
declare isActive: boolean;
2025-01-30 10:45:31 +00:00
}
2025-02-03 19:50:16 +00:00
export default (sequelize: Sequelize) => {
AuthUserModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
2025-02-03 18:03:23 +00:00
},
2025-02-03 19:50:16 +00:00
username: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
roles: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: "USER",
get(this: AuthUserModel): string[] {
const rawValue = this.getDataValue("roles") as any;
return String(rawValue).split(";");
},
set(this: AuthUserModel, value: string[]) {
const rawValue = value.join(";") as any;
this.setDataValue("roles", rawValue);
},
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true,
2025-02-03 18:03:23 +00:00
},
2025-01-30 10:45:31 +00:00
},
2025-02-03 19:50:16 +00:00
{
sequelize,
tableName: "users",
paranoid: true, // softs deletes
timestamps: true,
2025-01-30 10:45:31 +00:00
2025-02-03 19:50:16 +00:00
createdAt: "created_at",
updatedAt: "updated_at",
deletedAt: "deleted_at",
2025-01-30 10:45:31 +00:00
2025-02-03 19:50:16 +00:00
indexes: [{ name: "email_idx", fields: ["email"], unique: true }],
}
);
return AuthUserModel;
};