|
| 1 | +import { |
| 2 | + DataTypes, |
| 3 | + Model, |
| 4 | + Optional, |
| 5 | + NonAttribute, |
| 6 | + HasOneGetAssociationMixin, |
| 7 | + Association, |
| 8 | +} from 'sequelize'; |
| 9 | + |
| 10 | +import { sequelize } from '../sequelize'; |
| 11 | +import { UserModel } from '../user/user.model'; |
| 12 | + |
| 13 | +type Log = { |
| 14 | + id: number; |
| 15 | + recordId: number; |
| 16 | + recordTitle: string; |
| 17 | + difference: Record<string, any>; |
| 18 | + userId: number; |
| 19 | + action: string; |
| 20 | + resource: string; |
| 21 | + createdAt: Date; |
| 22 | + updatedAt: Date; |
| 23 | +}; |
| 24 | + |
| 25 | +type LogCreationAttributes = Optional<Log, 'id' | 'createdAt' | 'updatedAt'>; |
| 26 | + |
| 27 | +export class LogModel extends Model<Log, LogCreationAttributes> { |
| 28 | + declare id: number; |
| 29 | + declare recordId: number; |
| 30 | + declare recordTitle: string; |
| 31 | + declare difference: Record<string, any>; |
| 32 | + declare userId: number; |
| 33 | + declare action: string; |
| 34 | + declare resource: string; |
| 35 | + declare createdAt: Date; |
| 36 | + declare updatedAt: Date; |
| 37 | + |
| 38 | + declare user?: NonAttribute<UserModel>; |
| 39 | + |
| 40 | + declare getUser: HasOneGetAssociationMixin<UserModel>; |
| 41 | + |
| 42 | + declare static associations: { |
| 43 | + user: Association<UserModel>; |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +LogModel.init( |
| 48 | + { |
| 49 | + id: { |
| 50 | + type: DataTypes.INTEGER, |
| 51 | + autoIncrement: true, |
| 52 | + primaryKey: true, |
| 53 | + }, |
| 54 | + action: { |
| 55 | + type: new DataTypes.STRING(128), |
| 56 | + allowNull: false, |
| 57 | + }, |
| 58 | + resource: { |
| 59 | + type: new DataTypes.STRING(128), |
| 60 | + allowNull: false, |
| 61 | + }, |
| 62 | + userId: { |
| 63 | + type: DataTypes.INTEGER, |
| 64 | + allowNull: false, |
| 65 | + }, |
| 66 | + recordId: { |
| 67 | + type: DataTypes.INTEGER, |
| 68 | + allowNull: false, |
| 69 | + }, |
| 70 | + recordTitle: { |
| 71 | + type: new DataTypes.STRING(128), |
| 72 | + allowNull: false, |
| 73 | + }, |
| 74 | + difference: { |
| 75 | + type: DataTypes.JSONB, |
| 76 | + allowNull: true, |
| 77 | + }, |
| 78 | + createdAt: { |
| 79 | + type: DataTypes.DATE, |
| 80 | + }, |
| 81 | + updatedAt: { |
| 82 | + type: DataTypes.DATE, |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + sequelize, |
| 87 | + tableName: 'logs', |
| 88 | + modelName: 'Log', |
| 89 | + timestamps: true, |
| 90 | + } |
| 91 | +); |
| 92 | + |
| 93 | +LogModel.belongsTo(UserModel, { |
| 94 | + foreignKey: 'userId', |
| 95 | + targetKey: 'id', |
| 96 | + as: 'user', |
| 97 | +}); |
0 commit comments