Skip to content

Commit

Permalink
feature: DB 설정 및 모델 정의 (#205)
Browse files Browse the repository at this point in the history
* chore: typeorm, typeorm-naming-strategies, mysql2 설치

* chore: TypeORM 설정

* feat: 데이터 모델 정의

* chore: redis 연결 성공시 콘솔 메시지 추가

* chore: yarn install 정리

* chore: 모든 DB 스키마에 updateAt 필드 추가

* chore: 패키지 충돌 해결
  • Loading branch information
bbearcookie authored Dec 27, 2023
1 parent 2f73e80 commit fe1a13e
Show file tree
Hide file tree
Showing 43 changed files with 949 additions and 17 deletions.
420 changes: 420 additions & 0 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions packages/slack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-async-errors": "^3.1.1",
"mysql2": "^3.6.1",
"redis": "^4.6.8",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typeorm": "^0.3.17",
"typeorm-naming-strategies": "^4.1.0",
"typescript": "^5.2.2",
"zod": "^3.22.2"
},
Expand Down
7 changes: 6 additions & 1 deletion packages/slack/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import 'dotenv/config';
import '@/configs/redis';
import app from '@/configs/express';
import AppDataSource from '@/configs/database';
import router from '@/routes';
import '@/configs/redis';

const PORT = process.env.PORT;

app.use('/', router);

AppDataSource.initialize()
.then(() => console.log('Database connected!'))
.catch((err) => console.log(err));

app.listen(PORT, () => {
console.log(`Server is running in ${PORT} port.`);
});
22 changes: 22 additions & 0 deletions packages/slack/src/configs/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DataSource } from 'typeorm';
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
import { User } from '@/models/User';
import { Post } from '@/models/Post';
import { Comment } from '@/models/Comment';

const AppDataSource = new DataSource({
type: 'mysql',
port: 3306,
host: process.env.NODE_ENV === 'production' ? process.env.DEPLOY_DB_HOST : 'localhost',
username: process.env.NODE_ENV === 'production' ? process.env.DEPLOY_DB_USERNAME : process.env.LOCAL_DB_USERNAME,
password: process.env.NODE_ENV === 'production' ? process.env.DEPLOY_DB_PASSWORD : process.env.LOCAL_DB_PASSWORD,
database: process.env.DB_DATABASE,
synchronize: true,
// logging: true,
entities: [User, Post, Comment],
subscribers: [],
migrations: [],
namingStrategy: new SnakeNamingStrategy()
});

export default AppDataSource;
3 changes: 2 additions & 1 deletion packages/slack/src/configs/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const redisClient = createClient({
password: process.env.NODE_ENV === 'production' ? process.env.DEPLOY_REDIS_PASSWORD : undefined
});

redisClient.on('error', (err) => console.log('Redis Client Error', err));
redisClient.on('connect', () => console.log('Redis Connected'));
redisClient.on('error', (err) => console.log('Redis Error', err));
redisClient.connect();

export default redisClient;
34 changes: 34 additions & 0 deletions packages/slack/src/models/Comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
import { Post } from './Post';

@Entity()
export class Comment {
@PrimaryGeneratedColumn()
id!: number;

@Column({ length: 50 })
author!: string;

@Column({ length: 500 })
content!: string;

@Column({ length: 50 })
imageName!: string;

@Column()
positionX!: number;

@Column()
positionY!: number;

@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
createdAt!: Date;

@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
updatedAt!: Date;

@ManyToOne(() => Post, (post) => post.comments, {
cascade: true
})
post!: Post;
}
32 changes: 32 additions & 0 deletions packages/slack/src/models/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne } from 'typeorm';
import { User } from './User';
import { Comment } from './Comment';

@Entity()
export class Post {
@PrimaryGeneratedColumn()
id!: number;

@Column({ length: 50 })
title!: string;

@Column({ length: 500 })
content!: string;

@Column({ length: 50, nullable: true })
imageName!: string;

@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
createdAt!: Date;

@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
updatedAt!: Date;

@ManyToOne(() => User, (user) => user.posts, {
cascade: true
})
author!: User;

@OneToMany(() => Comment, (comment) => comment.id)
comments!: Comment[];
}
44 changes: 44 additions & 0 deletions packages/slack/src/models/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
import { Post } from './Post';

@Entity()
export class User {
@PrimaryGeneratedColumn()
id!: number;

@Column({ length: 50, unique: true })
username!: string;

@Column()
password!: string;

@Column()
salt!: string;

@Column({ length: 20 })
role!: string;

@Column({ length: 20 })
name!: string;

@Column()
introduce!: string;

@Column({ length: 50 })
imageName!: string;

@Column({ length: 50 })
slackId!: string;

@Column({ length: 20 })
slackWorkspace!: string;

@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
createdAt!: Date;

@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
updatedAt!: Date;

@OneToMany(() => Post, (post) => post.author)
posts!: Post[];
}
7 changes: 6 additions & 1 deletion packages/slack/src/types/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ declare namespace NodeJS {
SLACK_HOOK_ENDPOINT: string;
SLACK_BOT_TOKEN: string;
SLACK_SIGNING_SECRET: string;
SESSION_SECRET: string;
DB_DATABASE: string;
LOCAL_DB_USERNAME: string;
LOCAL_DB_PASSWORD: string;
DEPLOY_DB_HOST: string;
DEPLOY_DB_USERNAME: string;
DEPLOY_DB_PASSWORD: string;
}
}
10 changes: 5 additions & 5 deletions packages/slack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
"ts-node": {
"files": true
},
"files": [
"src/types/env.d.ts",
"src/types/express.d.ts"
],
"files": ["src/types/env.d.ts", "src/types/express.d.ts"],
"compilerOptions": {
"baseUrl": "src",
"rootDir": "src",
"outDir": "dist",
"paths": {
"@/*": ["./*"],
"@/*": ["./*"]
},
"target": "ES2020",
"useDefineForClassFields": true,
Expand All @@ -28,6 +25,9 @@
"isolatedModules": true,
// "noEmit": true,

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

/* Linting */
"strict": true,
"noFallthroughCasesInSwitch": true
Expand Down
Loading

0 comments on commit fe1a13e

Please sign in to comment.