Skip to content

Commit f9be42d

Browse files
authored
Merge pull request #13 from Code-4-Community/4-Database-schema-and-migration
4 database schema and migration
2 parents 0a675a9 + ba9e67c commit f9be42d

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

apps/backend/src/data-source.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DataSource } from 'typeorm';
22
import { PluralNamingStrategy } from './strategies/plural-naming.strategy';
3+
import { Donation } from './donations/donation.entity';
34
import { User } from './users/user.entity';
45
import * as dotenv from 'dotenv';
56

@@ -12,8 +13,8 @@ const AppDataSource = new DataSource({
1213
username: process.env.NX_DB_USERNAME,
1314
password: process.env.NX_DB_PASSWORD,
1415
database: process.env.NX_DB_DATABASE,
15-
entities: [User],
16-
migrations: ['apps/backend/src/migrations/*.js'],
16+
entities: [User, Donation],
17+
migrations: ['apps/backend/src/migrations/*.ts'],
1718
// Setting synchronize: true shouldn't be used in production - otherwise you can lose production data
1819
synchronize: false,
1920
namingStrategy: new PluralNamingStrategy(),
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
2+
3+
export enum donationType {
4+
'one_time',
5+
'recurring',
6+
}
7+
8+
export enum recurringInterval {
9+
'monthly',
10+
'yearly',
11+
'weekly',
12+
'bimonthly',
13+
'biweekly',
14+
}
15+
16+
@Entity()
17+
export class Donation {
18+
@PrimaryGeneratedColumn('identity', {
19+
generatedIdentity: 'ALWAYS',
20+
})
21+
id: number;
22+
23+
@Column()
24+
firstName: string;
25+
26+
@Column()
27+
lastName: string;
28+
29+
@Column()
30+
email: string;
31+
32+
@Column({ type: 'numeric', precision: 10, scale: 2 })
33+
amount: number;
34+
35+
@Column({ default: false })
36+
isAnonymous: boolean;
37+
38+
@Column()
39+
donationType: donationType;
40+
41+
@Column({ nullable: true })
42+
recurringInterval: recurringInterval;
43+
44+
@Column({ nullable: true })
45+
dedicationMessage: string;
46+
47+
@Column()
48+
createdAt: Date;
49+
50+
@Column()
51+
updatedAt: Date;
52+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class AddUsers1759151412730 implements MigrationInterface {
4+
name = 'AddUsers1759151412730';
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE TABLE "users" ("id" integer NOT NULL, "status" character varying NOT NULL, "firstName" character varying NOT NULL, "lastName" character varying NOT NULL, "email" character varying NOT NULL, CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))`,
9+
);
10+
}
11+
12+
public async down(queryRunner: QueryRunner): Promise<void> {
13+
await queryRunner.query(`DROP TABLE "users"`);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class AddDonations1759151447065 implements MigrationInterface {
4+
name = 'AddDonations1759151447065';
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE TABLE "donations" ("id" integer GENERATED ALWAYS AS IDENTITY NOT NULL, "firstName" character varying NOT NULL, "lastName" character varying NOT NULL, "email" character varying NOT NULL, "amount" numeric(10,2) NOT NULL, "isAnonymous" boolean NOT NULL DEFAULT false, "donationType" integer NOT NULL, "recurringInterval" integer, "dedicationMessage" character varying, "createdAt" TIMESTAMP NOT NULL, "updatedAt" TIMESTAMP NOT NULL, CONSTRAINT "PK_c01355d6f6f50fc6d1b4a946abf" PRIMARY KEY ("id"))`,
9+
);
10+
}
11+
12+
public async down(queryRunner: QueryRunner): Promise<void> {
13+
await queryRunner.query(`DROP TABLE "donations"`);
14+
}
15+
}

0 commit comments

Comments
 (0)