tethys.backend/app/Models/Subject.ts
Arno Kaimbacher ebb24cc75c
Some checks failed
CI Pipeline / japa-tests (push) Failing after 51s
- add classes inside app/library for creting Tethys xml: Field.ts, Strategy.ts, XmlModel.ts
- added model DocumentXmlCache.ts
- npm updates
- changed all models inside app/Models to use corrected BaseModel.ts
- added extra extension class DatasetExtension.ts for app/dataset.ts for caching internal and external fields
2023-09-26 17:53:00 +02:00

78 lines
2.1 KiB
TypeScript

import { column, SnakeCaseNamingStrategy, manyToMany, ManyToMany, beforeCreate, beforeUpdate } from '@ioc:Adonis/Lucid/Orm';
import BaseModel from './BaseModel';
import { DateTime } from 'luxon';
import dayjs from 'dayjs';
import Dataset from './Dataset';
export default class Subject extends BaseModel {
public static namingStrategy = new SnakeCaseNamingStrategy();
public static table = 'dataset_subjects';
public static selfAssignPrimaryKey = false;
@column({
isPrimary: true,
})
public id: number;
@column({})
public language: string;
@column({})
public type: string;
@column({})
public value: string;
@column({})
public external_key: string;
@column.dateTime({
serialize: (value: Date | null) => {
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
},
autoCreate: true,
})
public created_at: DateTime;
@column.dateTime({
serialize: (value: Date | null) => {
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
},
autoCreate: true,
autoUpdate: true,
})
public updated_at: DateTime;
@beforeCreate()
@beforeUpdate()
public static async resetDate(role) {
role.created_at = this.formatDateTime(role.created_at);
role.updated_at = this.formatDateTime(role.updated_at);
}
private static formatDateTime(datetime) {
let value = new Date(datetime);
return datetime
? value.getFullYear() +
'-' +
(value.getMonth() + 1) +
'-' +
value.getDate() +
' ' +
value.getHours() +
':' +
value.getMinutes() +
':' +
value.getSeconds()
: datetime;
}
@manyToMany(() => Dataset, {
pivotForeignKey: 'subject_id',
pivotRelatedForeignKey: 'document_id',
pivotTable: 'link_dataset_subjects',
})
public datasets: ManyToMany<typeof Dataset>;
}