tethys.backend/app/models/user_role.ts

77 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-03-14 19:25:27 +00:00
import { column, BaseModel, belongsTo, SnakeCaseNamingStrategy } from '@adonisjs/lucid/orm';
2023-03-03 15:54:28 +00:00
import User from '#models/user';
import Role from '#models/role';
import { DateTime } from 'luxon';
2024-03-14 19:25:27 +00:00
import type { BelongsTo } from "@adonisjs/lucid/types/relations";
2023-03-03 15:54:28 +00:00
// import moment from 'moment'
export default class UserRole extends BaseModel {
public static namingStrategy = new SnakeCaseNamingStrategy();
public static primaryKey = 'id';
public static table = 'user_roles';
public static selfAssignPrimaryKey = false;
2023-03-03 15:54:28 +00:00
@column({
isPrimary: true,
})
public id: number;
2023-03-03 15:54:28 +00:00
@column({})
public user_id: number;
2023-03-03 15:54:28 +00:00
@column({})
public role_id: number;
2023-03-03 15:54:28 +00:00
@column({
// serialize: (value: DateTime | null) => {
// return value ? moment(value).format('lll') : value
// },
})
public created_at: DateTime;
2023-03-03 15:54:28 +00:00
@column({
// serialize: (value: DateTime | null) => {
// return value ? moment(value).format('lll') : value
// },
})
public updated_at: DateTime;
2023-03-03 15:54:28 +00:00
public static boot() {
super.boot();
2023-03-03 15:54:28 +00:00
this.before('create', async (_modelInstance) => {
_modelInstance.created_at = this.formatDateTime(_modelInstance.created_at);
_modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at);
});
this.before('update', async (_modelInstance) => {
_modelInstance.created_at = this.formatDateTime(_modelInstance.created_at);
_modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at);
});
}
2023-03-03 15:54:28 +00:00
2024-03-14 19:25:27 +00:00
private static formatDateTime(datetime: any) {
let value = new Date(datetime);
return datetime
? value.getFullYear() +
'-' +
(value.getMonth() + 1) +
'-' +
value.getDate() +
' ' +
value.getHours() +
':' +
value.getMinutes() +
':' +
value.getSeconds()
: datetime;
}
2023-03-03 15:54:28 +00:00
@belongsTo(() => User)
public user: BelongsTo<typeof User>;
2023-03-03 15:54:28 +00:00
@belongsTo(() => Role)
public role: BelongsTo<typeof Role>;
2023-03-03 15:54:28 +00:00
}