Skip to content
This repository was archived by the owner on Mar 28, 2024. It is now read-only.

Commit 1d34d8a

Browse files
committed
db: add base entity for common columns
1 parent 69630ab commit 1d34d8a

File tree

7 files changed

+58
-43
lines changed

7 files changed

+58
-43
lines changed

packages/server/src/db/entities/author.entity.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
1+
import { Column, Entity, OneToMany } from 'typeorm';
22

3+
import { BaseEntity } from './base.entity';
34
import { PodcastEntity } from './podcast.entity';
45

56
@Entity('author')
6-
export class AuthorEntity {
7-
@PrimaryGeneratedColumn()
8-
id: number;
9-
7+
export class AuthorEntity extends BaseEntity {
108
@Column()
119
name: string;
1210

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {
2+
CreateDateColumn,
3+
PrimaryGeneratedColumn,
4+
UpdateDateColumn,
5+
} from 'typeorm';
6+
7+
export abstract class BaseEntity {
8+
@PrimaryGeneratedColumn()
9+
id: number;
10+
11+
@CreateDateColumn()
12+
createdAt: Date;
13+
14+
@UpdateDateColumn()
15+
updatedAt: Date;
16+
}

packages/server/src/db/entities/category.entity.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
import {
2-
Column,
3-
Entity,
4-
ManyToMany,
5-
PrimaryGeneratedColumn,
6-
JoinTable,
7-
} from 'typeorm';
1+
import { Column, Entity, ManyToMany, JoinTable } from 'typeorm';
2+
import { BaseEntity } from './base.entity';
83
import { PodcastEntity } from './podcast.entity';
94

105
@Entity('category')
11-
export class CategoryEntity {
12-
@PrimaryGeneratedColumn()
13-
id: number;
14-
6+
export class CategoryEntity extends BaseEntity {
157
@Column()
168
name: string;
179

packages/server/src/db/entities/episode.entity.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
2+
import { BaseEntity } from './base.entity';
23
import { PodcastEntity } from './podcast.entity';
34

45
@Entity('episode')
5-
export class EpisodeEntity {
6-
@PrimaryGeneratedColumn()
7-
id: number;
8-
6+
export class EpisodeEntity extends BaseEntity {
97
@Column()
108
title: string;
119

Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
import {
2-
Column,
3-
CreateDateColumn,
4-
Entity,
5-
ManyToMany,
6-
ManyToOne,
7-
OneToMany,
8-
PrimaryGeneratedColumn,
9-
UpdateDateColumn,
10-
} from 'typeorm';
1+
import { Column, Entity, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
2+
import { BaseEntity } from './base.entity';
113
import { AuthorEntity } from './author.entity';
124
import { EpisodeEntity } from './episode.entity';
135
import { CategoryEntity } from './category.entity';
146

157
@Entity('podcast')
16-
export class PodcastEntity {
17-
@PrimaryGeneratedColumn()
18-
id: number;
19-
8+
export class PodcastEntity extends BaseEntity {
209
@Column()
2110
title: string;
2211

@@ -44,12 +33,6 @@ export class PodcastEntity {
4433
@ManyToMany(() => CategoryEntity, (category) => category.podcasts)
4534
categories: CategoryEntity[];
4635

47-
@CreateDateColumn()
48-
createdAt: Date;
49-
50-
@UpdateDateColumn()
51-
updatedAt: Date;
52-
5336
@Column({ nullable: true })
5437
authorId: number;
5538
}

packages/server/src/db/entities/queue.entity.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
1+
import { Column, Entity } from 'typeorm';
22
import { FilterableField } from '@nestjs-query/query-graphql';
33
import { Field, ObjectType } from '@nestjs/graphql';
4+
import { BaseEntity } from './base.entity';
45

56
@Entity('queue')
67
@ObjectType('Queue')
7-
export class QueueEntity {
8-
@PrimaryGeneratedColumn()
8+
export class QueueEntity extends BaseEntity {
99
@FilterableField()
1010
id: number;
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {MigrationInterface, QueryRunner} from "typeorm";
2+
3+
export class AddBaseEntity1589291828794 implements MigrationInterface {
4+
name = 'AddBaseEntity1589291828794'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE "episode" ADD "createdAt" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
8+
await queryRunner.query(`ALTER TABLE "episode" ADD "updatedAt" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
9+
await queryRunner.query(`ALTER TABLE "category" ADD "createdAt" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
10+
await queryRunner.query(`ALTER TABLE "category" ADD "updatedAt" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
11+
await queryRunner.query(`ALTER TABLE "author" ADD "createdAt" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
12+
await queryRunner.query(`ALTER TABLE "author" ADD "updatedAt" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
13+
await queryRunner.query(`ALTER TABLE "queue" ADD "createdAt" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
14+
await queryRunner.query(`ALTER TABLE "queue" ADD "updatedAt" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
15+
}
16+
17+
public async down(queryRunner: QueryRunner): Promise<void> {
18+
await queryRunner.query(`ALTER TABLE "queue" DROP COLUMN "updatedAt"`, undefined);
19+
await queryRunner.query(`ALTER TABLE "queue" DROP COLUMN "createdAt"`, undefined);
20+
await queryRunner.query(`ALTER TABLE "author" DROP COLUMN "updatedAt"`, undefined);
21+
await queryRunner.query(`ALTER TABLE "author" DROP COLUMN "createdAt"`, undefined);
22+
await queryRunner.query(`ALTER TABLE "category" DROP COLUMN "updatedAt"`, undefined);
23+
await queryRunner.query(`ALTER TABLE "category" DROP COLUMN "createdAt"`, undefined);
24+
await queryRunner.query(`ALTER TABLE "episode" DROP COLUMN "updatedAt"`, undefined);
25+
await queryRunner.query(`ALTER TABLE "episode" DROP COLUMN "createdAt"`, undefined);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)