refactor(database): Remove remaining create/update decorators and use time base class

This commit is contained in:
FoxxMD
2022-03-28 20:00:41 -04:00
parent 78be8c6a54
commit b91f287df0
3 changed files with 77 additions and 12 deletions

View File

@@ -6,12 +6,14 @@ import {
VersionColumn,
ManyToOne,
JoinColumn,
PrimaryColumn, CreateDateColumn, UpdateDateColumn
PrimaryColumn, CreateDateColumn, UpdateDateColumn, BeforeInsert, BeforeUpdate
} from "typeorm";
import {Action} from "./Action";
import {ActionResultEntity} from "./ActionResultEntity";
import objectHash from "object-hash";
import {ObjectPremise} from "../interfaces";
import {TimeAwareBaseEntity} from "./Base/TimeAwareBaseEntity";
import dayjs, {Dayjs} from "dayjs";
export interface ActionPremiseOptions {
action: Action
@@ -19,7 +21,7 @@ export interface ActionPremiseOptions {
}
@Entity()
export class ActionPremise {
export class ActionPremise extends TimeAwareBaseEntity {
@ManyToOne(() => Action, undefined,{cascade: ['insert'], eager: true})
@JoinColumn({name: 'actionId'})
@@ -40,13 +42,32 @@ export class ActionPremise {
@VersionColumn()
version!: number;
@CreateDateColumn()
createdAt!: number
@Column({ type: 'bigint', width: 13, nullable: false, readonly: true, unsigned: true })
updatedAt: Dayjs = dayjs();
@UpdateDateColumn()
updatedAt!: number
convertToDayjs() {
if(this.createdAt !== undefined) {
this.createdAt = dayjs(this.createdAt);
}
if(this.updatedAt !== undefined) {
this.updatedAt = dayjs(this.createdAt);
}
}
public convertToUnix() {
// @ts-ignore
this.createdAt = this.createdAt.valueOf();
// @ts-ignore
this.updatedAt = this.updatedAt.valueOf();
}
@BeforeUpdate()
public updateTimestamp() {
this.updatedAt = dayjs();
}
constructor(data?: ActionPremiseOptions) {
super();
if(data !== undefined) {
this.action = data.action;
this.config = data.config;

View File

@@ -0,0 +1,23 @@
import {BeforeInsert, Entity, Column, AfterLoad, BeforeUpdate} from "typeorm";
import dayjs, {Dayjs} from "dayjs";
import {RandomIdBaseEntity} from "./RandomIdBaseEntity";
export abstract class TimeAwareBaseEntity {
@Column({ type: 'bigint', width: 13, nullable: false, readonly: true, unsigned: true })
createdAt: Dayjs = dayjs();
@AfterLoad()
convertToDayjs() {
if(this.createdAt !== undefined) {
this.createdAt = dayjs(this.createdAt);
}
}
@BeforeInsert()
@BeforeUpdate()
public convertToUnix() {
// @ts-ignore
this.createdAt = this.createdAt.valueOf();
}
}

View File

@@ -7,12 +7,14 @@ import {
VersionColumn,
ManyToOne,
PrimaryColumn,
CreateDateColumn, UpdateDateColumn
CreateDateColumn, UpdateDateColumn, BeforeUpdate
} from "typeorm";
import {RuleResultEntity} from "./RuleResultEntity";
import {Rule} from "./Rule";
import {ObjectPremise} from "../interfaces";
import objectHash from "object-hash";
import {TimeAwareBaseEntity} from "./Base/TimeAwareBaseEntity";
import dayjs, {Dayjs} from "dayjs";
export interface RulePremiseOptions {
rule: Rule
@@ -20,7 +22,7 @@ export interface RulePremiseOptions {
}
@Entity()
export class RulePremise {
export class RulePremise extends TimeAwareBaseEntity {
@ManyToOne(() => Rule, undefined,{cascade: ['insert'], eager: true})
@JoinColumn({name: 'ruleId'})
@@ -41,13 +43,32 @@ export class RulePremise {
@VersionColumn()
version!: number;
@CreateDateColumn()
createdAt!: number
@Column({ type: 'bigint', width: 13, nullable: false, readonly: true, unsigned: true })
updatedAt: Dayjs = dayjs();
@UpdateDateColumn()
updatedAt!: number
convertToDayjs() {
if(this.createdAt !== undefined) {
this.createdAt = dayjs(this.createdAt);
}
if(this.updatedAt !== undefined) {
this.updatedAt = dayjs(this.createdAt);
}
}
public convertToUnix() {
// @ts-ignore
this.createdAt = this.createdAt.valueOf();
// @ts-ignore
this.updatedAt = this.updatedAt.valueOf();
}
@BeforeUpdate()
public updateTimestamp() {
this.updatedAt = dayjs();
}
constructor(data?: RulePremiseOptions) {
super();
if(data !== undefined) {
this.rule = data.rule;
this.config = data.config;