Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeORM 버그 fix 및 entity 설정 #47

Merged
merged 2 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions server/ormconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ const common = {
dropSchema: false,
maxQueryExecutionTime: 1000, // milliseconds
entities: [
"src/entity/**/*.ts"
process.env.BASE_DIR + "entity/**/*.{ts,js}"
],
migrations: [
"src/migration/**/*.ts"
process.env.BASE_DIR + "migration/**/*.{ts,js}"
],
subscribers: [
"src/subscriber/**/*.ts"
process.env.BASE_DIR + "subscriber/**/*.{ts,js}"
],
migrationsRun: false,
cli: {
Expand All @@ -73,7 +73,7 @@ const common = {
*
**/
const setUpDbOptions = () => {
let profile = process.env.PROFILE;
let profile = process.env.NODE_ENV;
if (!Object.keys(profiles).includes(profile)) {
profile = "local";

Expand Down
8 changes: 3 additions & 5 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
"version": "1.0.0",
"private": true,
"scripts": {
"serve": "node dist/server.js",
"serve": "tsc && node dist/app.js",
"test": "jest",
"lint": "eslint src/**/*.ts",
"start": "ts-node --project tsconfig.json -r tsconfig-paths/register src/app.ts",
"typeorms": "ts-node ./node_modules/.bin/typeorm"
"start": "ts-node src/app.ts",
"typeorms": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js"
},
"dependencies": {
"cookie-parser": "^1.4.4",
"debug": "^4.1.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"http-errors": "^1.7.3",
"morgan": "^1.9.1",
"mysql": "^2.14.1",
"reflect-metadata": "^0.1.10",
Expand Down
41 changes: 15 additions & 26 deletions server/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import "dotenv/config";
import "reflect-metadata";
import express from "express";
import morgan from "morgan";
import cookieParser from "cookie-parser";
import "reflect-metadata";
import {createConnection} from "typeorm";
import indexRouter from "./routes/index";

/**
*
* express app 설정
*
**/
const initialize = () => {
const app = express();
app.set("port", process.env.PORT || 3000);
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use("/", indexRouter);
app.listen(app.get("port"), () => {
console.log("listen port 3000");
});
};
/**
*
* TypeOrm Connection 설정
*
**/
createConnection()
.then(initialize)
.catch(error => console.error("TypeORM Connection Error: ", error));
createConnection().then(async connection =>{
const app = express();
app.set("port", process.env.PORT || 3000);
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser(process.env.COOKIE_SECRET));

app.use("/", indexRouter);

app.listen(app.get("port"), () => {
console.log("listen port 3000");
});
}).catch(error => console.error("TypeORM Connection Error: ", error));
18 changes: 0 additions & 18 deletions server/src/entity/Channel.ts

This file was deleted.

39 changes: 39 additions & 0 deletions server/src/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, OneToMany} from "typeorm";
import {Profile} from "./Profile";
import {Room} from "./Room"


@Entity()
export class Post {

@PrimaryGeneratedColumn()
id: number;

@Column()
contents: string;

@Column()
imgSrc: string;

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;

// 생성자
@ManyToOne(type => Profile, profile => profile.posts)
owner: Profile;

// 소속된 Room
@ManyToOne(type => Room, room => room.posts)
room: Room;

// parent는 여러개의 child를 갖는다.
@OneToMany(type => Post, post => post.child)
parent: Post;

// child는 하나의 parent를 가는다.
@ManyToOne(type => Post, post => post.parent)
child: Post[];
}
51 changes: 51 additions & 0 deletions server/src/entity/Profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export type UserRoleType = "admin" | "participant";

import {Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, OneToMany, ManyToMany} from "typeorm";
import {Room} from "./Room";
import {Post} from "./Post";

@Entity()
export class Profile {

@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@Column()
status: string;

@Column()
thumbnail: string;

@Column()
description: string;

@Column({
type: "enum",
enum: ["admin", "participant"]
})
role: UserRoleType;

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;

@Column()
deletedAt: Date;

// 생성한 room 목록
@OneToMany(type => Room, room => room.owner)
myRooms: Room[];

// 참여하고 있는 room 목록
@ManyToMany(type => Room, room => room.participants)
rooms: Room[];

// 작성한 Post 목록
@OneToMany(type => Post, post => post.owner)
posts: Post[];
}
38 changes: 38 additions & 0 deletions server/src/entity/Room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, ManyToMany, OneToMany} from "typeorm";
import {Profile} from './Profile';
import { Post } from "./Post";


@Entity()
export class Room {

@PrimaryGeneratedColumn()
id: number;

@Column()
description: string;

@Column()
isPrivate: string;

@Column()
isChannel: boolean;

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;

// 생성자
@ManyToOne(type => Profile, profile => profile.myRooms)
owner: Profile;

// 참여자
@ManyToMany(type => Profile, profile => profile.rooms)
participants: Profile[];

// 게시된 Post 목록
@OneToMany(type => Post, post => post.room)
posts: Post[];
}
9 changes: 7 additions & 2 deletions server/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import {Router, Request, Response} from "express";
import {Channel} from "src/entity/Channel";
import {Post} from "../entity/Post";
import {getConnection} from "typeorm";

const router = Router();
router.get("/", async (req: Request, res: Response) => {
res.json(await Channel.find());
let post = new Post();
post.contents = 'kkr';
post.imgSrc = 'kkr.img'
await getConnection().manager.save(post);
res.send('hello world');
});
export default router;
21 changes: 4 additions & 17 deletions server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
{
"compilerOptions": {
"lib": [
"es5",
"es6"
],
"target": "es5",
"lib": ["es5", "es6"],
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"rootDir": "./",
"outDir": "./dist",
"sourceMap": true,
"resolveJsonModule": true,
"baseUrl": ".",
"paths": {
"src/*": [
"src/*"
]
},
"baseUrl": "./src",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true
},
"include": [
"src",
"test"
]
"include": ["src/**/*.ts"]
}