mirror of
https://github.com/FoxxMD/context-mod.git
synced 2026-04-19 03:00:07 -04:00
refactor(database): Remove remaining create/update decorators and use time base class
This commit is contained in:
@@ -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;
|
||||
|
||||
23
src/Common/Entities/Base/TimeAwareBaseEntity.ts
Normal file
23
src/Common/Entities/Base/TimeAwareBaseEntity.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user