From ff80da0a93d1518f8a651564fe3e50debb498426 Mon Sep 17 00:00:00 2001 From: kumuditha-udayanga Date: Fri, 11 Aug 2023 20:12:38 +0530 Subject: [PATCH] Update created_at and updated_at columns --- src/entity/category.entity.ts | 27 ++++++++++++++++++++++++++- src/entity/email.entity.ts | 30 ++++++++++++++++++++++++++---- src/entity/mentee.entity.ts | 33 +++++++++++++++++++++++++-------- src/entity/mentor.entity.ts | 32 ++++++++++++++++++++++++-------- src/entity/platform.entity.ts | 29 +++++++++++++++++++++++------ src/types.ts | 1 + 6 files changed, 125 insertions(+), 27 deletions(-) diff --git a/src/entity/category.entity.ts b/src/entity/category.entity.ts index b3a57e01..5cc9efec 100644 --- a/src/entity/category.entity.ts +++ b/src/entity/category.entity.ts @@ -1,22 +1,47 @@ import { + BeforeInsert, + BeforeUpdate, Column, Entity, PrimaryGeneratedColumn } from "typeorm"; +import {v4 as uuidv4} from 'uuid'; @Entity("category") class Category { @PrimaryGeneratedColumn('uuid') - id!: bigint + uuid!: string + + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + created_at: Date | undefined; + + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + updated_at: Date | undefined; @Column({type: 'varchar', length: 255}) category: string + constructor( category: string, ) { this.category = category; } + + @BeforeInsert() + @BeforeUpdate() + updateTimestamps() { + this.updated_at = new Date(); + if (!this.uuid) { + this.created_at = new Date(); + } + } + @BeforeInsert() + async generateUuid() { + if (!this.uuid) { + this.uuid = uuidv4(); + } + } } export default Category; diff --git a/src/entity/email.entity.ts b/src/entity/email.entity.ts index 3a948543..9d09888d 100644 --- a/src/entity/email.entity.ts +++ b/src/entity/email.entity.ts @@ -1,15 +1,18 @@ import { + BeforeInsert, + BeforeUpdate, Column, - CreateDateColumn, Entity, PrimaryGeneratedColumn } from "typeorm"; import {EmailStatusTypes} from "../enums/enums"; +import {v4 as uuidv4} from 'uuid'; + @Entity("email") class Email { @PrimaryGeneratedColumn('uuid') - id!: bigint + uuid!: string @Column({type: 'varchar', length: 255}) recipient: string @@ -23,8 +26,11 @@ class Email { @Column({type: 'enum', enum: EmailStatusTypes}) state: EmailStatusTypes - @CreateDateColumn() - created_at: Date | undefined + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + created_at: Date | undefined; + + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + updated_at: Date | undefined; constructor( recipient: string, @@ -37,6 +43,22 @@ class Email { this.content = content; this.state = state; } + + @BeforeInsert() + @BeforeUpdate() + updateTimestamps() { + this.updated_at = new Date(); + if (!this.uuid) { + this.created_at = new Date(); + } + } + + @BeforeInsert() + async generateUuid() { + if (!this.uuid) { + this.uuid = uuidv4(); + } + } } export default Email; diff --git a/src/entity/mentee.entity.ts b/src/entity/mentee.entity.ts index 09f72417..a0ea770e 100644 --- a/src/entity/mentee.entity.ts +++ b/src/entity/mentee.entity.ts @@ -1,22 +1,23 @@ import { + BeforeInsert, + BeforeUpdate, Column, - CreateDateColumn, Entity, JoinColumn, ManyToOne, OneToOne, - PrimaryGeneratedColumn, - UpdateDateColumn + PrimaryGeneratedColumn } from "typeorm"; import {MenteeApplication} from "../types"; import Mentor from "./mentor.entity"; import profileEntity from "./profile.entity"; import {MenteeStateTypes} from "../enums/enums"; +import {v4 as uuidv4} from "uuid"; @Entity('mentee') class Mentee { @PrimaryGeneratedColumn() - id!: bigint + uuid!: string @Column({type:"enum"}) state: MenteeStateTypes @@ -37,11 +38,11 @@ class Mentee { @ManyToOne(() => Mentor, mentor => mentor.mentees) mentor: Mentor - @CreateDateColumn() - created_at: Date | undefined + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + created_at: Date | undefined; - @UpdateDateColumn() - updated_at: Date | undefined + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + updated_at: Date | undefined; constructor( state: MenteeStateTypes, @@ -58,6 +59,22 @@ class Mentee { this.profile = profile; this.mentor = mentor; } + + @BeforeInsert() + @BeforeUpdate() + updateTimestamps() { + this.updated_at = new Date(); + if (!this.uuid) { + this.created_at = new Date(); + } + } + + @BeforeInsert() + async generateUuid() { + if (!this.uuid) { + this.uuid = uuidv4(); + } + } } export default Mentee; diff --git a/src/entity/mentor.entity.ts b/src/entity/mentor.entity.ts index af65fa3d..57efec9c 100644 --- a/src/entity/mentor.entity.ts +++ b/src/entity/mentor.entity.ts @@ -1,21 +1,21 @@ import { + BeforeInsert, BeforeUpdate, Column, - CreateDateColumn, Entity, JoinColumn, OneToMany, OneToOne, - PrimaryGeneratedColumn, - UpdateDateColumn + PrimaryGeneratedColumn } from "typeorm"; import profileEntity from "./profile.entity"; import Mentee from "./mentee.entity"; import Category from "./category.entity"; +import {v4 as uuidv4} from "uuid"; @Entity("mentor") class Mentor { @PrimaryGeneratedColumn('uuid') - id!: bigint + uuid!: string @Column({type: 'varchar', length: 255}) state: string @@ -36,11 +36,11 @@ class Mentor { @OneToMany(() => Mentee, mentee => mentee.mentor) mentees: Mentee[]; - @CreateDateColumn() - created_at: Date | undefined + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + created_at: Date | undefined; - @UpdateDateColumn() - updated_at: Date | undefined + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + updated_at: Date | undefined; constructor( state: string, @@ -57,6 +57,22 @@ class Mentor { this.profile = profile; this.mentees =mentees; } + + @BeforeInsert() + @BeforeUpdate() + updateTimestamps() { + this.updated_at = new Date(); + if (!this.uuid) { + this.created_at = new Date(); + } + } + + @BeforeInsert() + async generateUuid() { + if (!this.uuid) { + this.uuid = uuidv4(); + } + } } export default Mentor; diff --git a/src/entity/platform.entity.ts b/src/entity/platform.entity.ts index 05921490..dbe95d65 100644 --- a/src/entity/platform.entity.ts +++ b/src/entity/platform.entity.ts @@ -1,15 +1,17 @@ import { + BeforeInsert, BeforeUpdate, Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm"; +import {v4 as uuidv4} from "uuid"; @Entity("platform") class Platform { @PrimaryGeneratedColumn('uuid') - id!: bigint + uuid!: string @Column() description: string @@ -29,11 +31,11 @@ class Platform { @Column({type: 'varchar', length: 255}) title: string - @CreateDateColumn() - created_at: Date | undefined + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + created_at: Date | undefined; - @UpdateDateColumn() - updated_at: Date | undefined + @Column({type: 'timestamp', default:() => 'CURRENT_TIMESTAMP' }) + updated_at: Date | undefined; constructor( description: string, @@ -42,7 +44,6 @@ class Platform { landing_page_url: string, email_templates: JSON, title: string, - category: string, ) { this.description = description; this.mentor_questions = mentor_questions; @@ -51,6 +52,22 @@ class Platform { this.email_templates = email_templates; this.title = title; } + + @BeforeInsert() + @BeforeUpdate() + updateTimestamps() { + this.updated_at = new Date(); + if (!this.uuid) { + this.created_at = new Date(); + } + } + + @BeforeInsert() + async generateUuid() { + if (!this.uuid) { + this.uuid = uuidv4(); + } + } } export default Platform; diff --git a/src/types.ts b/src/types.ts index 08da5538..f7226a9d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,7 @@ interface Option { // todo: To be determined } + interface Question { question: string answer: string