Skip to content

Commit

Permalink
Setup Database
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhbao922003 authored and PluckySquirrel committed Oct 8, 2024
1 parent b83c368 commit 77a0b28
Show file tree
Hide file tree
Showing 17 changed files with 497 additions and 7 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
node_modules/
dist/
**/**/*.log
env/*.env
src/migration/*.ts

# Logs
logs
Expand Down
2 changes: 1 addition & 1 deletion env/development.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ DB_HOST=''
DB_PORT=''
DB_USERNAME=''
DB_PASSWORD=''
DB_DATABASE=''
DB_DATABASE=''
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default tseslint.config(
{ files: ['**/*.ts'] },
{
rules: {
'@typescript-eslint/explicit-member-accessibility': 'warn',
'@typescript-eslint/explicit-member-accessibility': 'off',
'@typescript-eslint/no-misused-promises': 0,
'@typescript-eslint/no-floating-promises': 0,
'@typescript-eslint/no-confusing-void-expression': 0,
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"lint:tests": "npx eslint ./spec",
"start": "NODE_ENV=development node -r ./preload.js ./dist",
"test": "NODE_ENV=test npx ts-node ./spec",
"test:hot": "nodemon --exec \"npm run test\" --watch ./src --watch ./spec -e ts"
"test:hot": "nodemon --exec \"npm run test\" --watch ./src --watch ./spec -e ts",
"add:migration": "npm run typeorm -- -d ./src/repos/db.ts migration:generate ./src/migration/$npm_config_name",
"revert:migration": "npm run typeorm migration:revert -- -d ./src/repos/db.ts",
"apply:migration": "npm run typeorm migration:run -- -d ./src/repos/db.ts",
"typeorm": "ts-node --transpile-only -r tsconfig-paths/register ./node_modules/typeorm/cli.js"
},
"engines": {
"node": ">=16.0.0"
Expand Down
38 changes: 38 additions & 0 deletions src/entity/Comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
JoinColumn,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm';
import { Review } from './Review';

@Entity('comments')
export class Comment {
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
id!: number;

@ManyToOne(() => Review)
@JoinColumn({ name: 'review_id' })
review!: Review;

@Column({ type: 'bigint' })
parent_id!: number;

@Column('text')
comment_text!: string;

@Column({ type: 'bigint' })
review_id!: number;

@CreateDateColumn({ type: 'datetime' })
created_at!: Date;

@UpdateDateColumn({ type: 'datetime' })
updated_at!: Date;

constructor(commentData?: Partial<Comment>) {
commentData && Object.assign(this, commentData);
}
}
38 changes: 38 additions & 0 deletions src/entity/Component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
JoinColumn
} from 'typeorm';
import { Lesson } from './Lesson';

@Entity('components')
export class Component {
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
id!: number;

@ManyToOne(() => Lesson)
@JoinColumn({ name: 'lesson_id' })
lesson!: Lesson;

@Column({
type: 'enum',
enum: ['video', 'url', 'pdf', 'text'],
})
type!: string;

@Column('text')
content!: string;

@CreateDateColumn({ type: 'datetime' })
created_at!: Date;

@UpdateDateColumn({ type: 'datetime' })
updated_at!: Date;

constructor(componentData?: Partial<Component>) {
componentData && Object.assign(this, componentData);
}
}
44 changes: 44 additions & 0 deletions src/entity/Course.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
JoinColumn
} from 'typeorm';
import { User } from './User';

@Entity('courses')
export class Course {
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
id!: number;

@ManyToOne(() => User)
@JoinColumn({ name: 'professor_id' })
professor!: User;

@Column()
name!: string;

@Column('double')
price!: number;

@Column('text')
description!: string;

@Column('float')
average_rating!: number;

@Column({ type: 'bigint' })
professor_id!: number;

@CreateDateColumn({ type: 'datetime' })
created_at!: Date;

@UpdateDateColumn({ type: 'datetime' })
updated_at!: Date;

constructor(courseData?: Partial<Course>) {
courseData && Object.assign(this, courseData);
}
}
49 changes: 49 additions & 0 deletions src/entity/Enrollment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
JoinColumn
} from 'typeorm';
import { User } from './User';
import { Course } from './Course';

@Entity('enrollments')
export class Enrollment {
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
id!: number;

@ManyToOne(() => User)
@JoinColumn({ name: 'user_id' })
user!: User;

@ManyToOne(() => Course)
@JoinColumn({ name: 'course_id' })
course!: Course;

@Column({ type: 'datetime' })
enrollment_date!: Date;

@Column('integer')
progress!: number;

@Column({ type: 'datetime' })
completion_date!: Date;

@Column({ type: 'bigint' })
user_id!: number;

@Column({ type: 'bigint' })
course_id!: number;

@CreateDateColumn({ type: 'datetime' })
created_at!: Date;

@UpdateDateColumn({ type: 'datetime' })
updated_at!: Date;

constructor(enrollmentData?: Partial<Enrollment>) {
enrollmentData && Object.assign(this, enrollmentData);
}
}
46 changes: 46 additions & 0 deletions src/entity/EnrollmentLesson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
JoinColumn
} from 'typeorm';
import { Enrollment } from './Enrollment';
import { Lesson } from './Lesson';

@Entity('enrollments_lessons')
export class EnrollmentLesson {
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
id!: number;

@ManyToOne(() => Enrollment)
@JoinColumn({ name: 'enrollment_id' })
enrollment!: Enrollment;

@ManyToOne(() => Lesson)
@JoinColumn({ name: 'lesson_id' })
lesson!: Lesson;

@Column('integer')
progress!: number;

@Column({ type: 'datetime' })
completion_date!: Date;

@Column({ type: 'bigint' })
enrollment_id!: number;

@Column({ type: 'bigint' })
lesson_id!: number;

@CreateDateColumn({ type: 'datetime' })
created_at!: Date;

@UpdateDateColumn({ type: 'datetime' })
updated_at!: Date;

constructor(enrollmentLessonData?: Partial<EnrollmentLesson>) {
enrollmentLessonData && Object.assign(this, enrollmentLessonData);
}
}
41 changes: 41 additions & 0 deletions src/entity/Lesson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
JoinColumn
} from 'typeorm';
import { Section } from './Section';

@Entity('lessons')
export class Lesson {
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
id!: number;

@ManyToOne(() => Section)
@JoinColumn({ name: 'section_id' })
section!: Section;

@Column()
name!: string;

@Column('text')
description!: string;

@Column('integer')
time!: number;

@Column({ type: 'bigint' })
section_id!: number;

@CreateDateColumn({ type: 'datetime' })
created_at!: Date;

@UpdateDateColumn({ type: 'datetime' })
updated_at!: Date;

constructor(lessonData?: Partial<Lesson>) {
lessonData && Object.assign(this, lessonData);
}
}
52 changes: 52 additions & 0 deletions src/entity/Payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
JoinColumn
} from 'typeorm';
import { User } from './User';
import { Course } from './Course';

@Entity('payments')
export class Payment {
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
id!: number;

@ManyToOne(() => User)
@JoinColumn({ name: 'user_id' })
user!: User;

@ManyToOne(() => Course)
@JoinColumn({ name: 'course_id' })
course!: Course;

@Column('double')
amount!: number;

@Column({ type: 'date' })
payment_date!: Date;

@Column({
type: 'enum',
enum: ['pending', 'done'],
})
status!: string;

@Column({ type: 'bigint' })
user_id!: number;

@Column({ type: 'bigint' })
course_id!: number;

@CreateDateColumn({ type: 'datetime' })
created_at!: Date;

@UpdateDateColumn({ type: 'datetime' })
updated_at!: Date;

constructor(paymentData?: Partial<Payment>) {
paymentData && Object.assign(this, paymentData);
}
}
43 changes: 43 additions & 0 deletions src/entity/Review.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Entity,
Column,
PrimaryGeneratedColumn,
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
JoinColumn
} from 'typeorm';
import { User } from './User';
import { Course } from './Course';

@Entity('reviews')
export class Review {
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
id!: number;

@ManyToOne(() => User)
@JoinColumn({ name: 'user_id' })
user!: User;

@ManyToOne(() => Course)
@JoinColumn({ name: 'course_id' })
course!: Course;

@Column('integer')
rating!: number;

@Column({ type: 'bigint' })
user_id!: number;

@Column({ type: 'bigint' })
course_id!: number;

@CreateDateColumn({ type: 'datetime' })
created_at!: Date;

@UpdateDateColumn({ type: 'datetime' })
updated_at!: Date;

constructor(reviewData?: Partial<Review>) {
reviewData && Object.assign(this, reviewData);
}
}
Loading

0 comments on commit 77a0b28

Please sign in to comment.