Presupuestador_web/server/src/contexts/users/infrastructure/sequelize/user.model.ts

87 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-05-15 19:56:22 +00:00
import {
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
2024-05-16 18:16:00 +00:00
Op,
2024-05-15 19:56:22 +00:00
Sequelize,
} from "sequelize";
export type TCreationUser_Attributes = InferCreationAttributes<User_Model>;
export class User_Model extends Model<
InferAttributes<User_Model>,
InferCreationAttributes<User_Model>
> {
// To avoid table creation
/*static async sync(): Promise<any> {
return Promise.resolve();
}*/
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;
}
export default (sequelize: Sequelize) => {
User_Model.init(
{
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,
},
},
{
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-16 18:16:00 +00:00
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
scopes: {
quickSearch(value) {
return {
where: {
[Op.or]: {
name: {
[Op.like]: `%${value}%`,
},
email: {
[Op.like]: `%${value}%`,
},
},
},
};
},
},
2024-05-15 19:56:22 +00:00
},
);
return User_Model;
};