๐งฑ ์ด๊ธฐ ์ค์น
- npm init : package.json ํ์ผ ์์ฑ
- npm install -g typescript : grobal๋ก typescript ์ค์น, ์ฒ์ ํ๋ก์ ํธ์์๋ง ์ค์นํ๋ฉด ๋๋ค.
- tsc --init : tsconfig.json ํ์ผ ์์ฑ
- tsconfic.json ํ์ผ ์์
- Express์์ ํ์ํ ๋ชจ๋ ์ค์น
- npm install --save-dev @types/node
- npm install --save express body-parser
- npm install @types/express --save-dev
- ts-node ์ค์น
- package.json์์ npm start์ ์์ํ ํ์ผ ์ง์
- typeorm ์ค์น
- npm install typeorm --save
- npm install mysql --save
- typeorm init --database mysql: ormconfig.jsonํ์ผ์ด ์์ฑ๋๊ณ , tsconfig.json ํ์ผ์ด ์์ ๋๋ค.
- โ ์ด ์์ ์ด์ ์ ํ๋ tsconfig.json ํ์ผ ์์ ์ ๋ค ์์ด์ง๊ณ ์์ ๋ฉ๋๋ค. ๊ผญ typeorm์ค์น ์ดํ์ ํ ๊ฒ!
๐ tsconfig.json ์๋ฏธ
โ tsconfig.json ํ์ผ์ ํ๋ก์ ํธ์ root ๋๋ ํ ๋ฆฌ์ ์กด์ฌํด์ผ ํฉ๋๋ค.
์ค์น ์ดํ tsconfig.json ํ์ผ
{
"compilerOptions": {
"lib": [ "es5", "es6" ],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
}
}
์์ ํ tsconfig.json ํ์ผ
{
"compilerOptions": {
"lib": [ "es5", "es6" ],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "./dist",
"rootDir": "./src",
"esModuleInterop": true
}
}
- rootDir: tsc๋ฅผ ์คํ ํ์๋ ์ปดํ์ผํ tsํ์ผ์ ํด๋. rootDir๋ก ์ง์ ํ ๊ฒฝ๋ก ์์ tsํ์ผ๋ค ์์ ์ ํ๋ฉด ๋ฉ๋๋ค.
- outDir : tsc๋ฅผ ์คํ ํ์๋ ์ปดํ์ผํ jsํ์ผ์ด ์ ์ฅ๋ ํด๋. tsc๋ฅผ ์คํํ๋ฉด ์๋์ผ๋ก ์์ฑ๋๋ฏ๋ก ๋ณ๋์ ์์ ์ ํด์ค ํ์์์ต๋๋ค.
- sourceMap : true๋ก ์ค์ ํ๋ฉด ๋ชจ๋ jsํ์ผ๋ง๋ค ๋ณ๋์ js.mapํ์ผ์ด ์๊น๋๋ค.
๐ซ ts-node ์ค์น
https://www.npmjs.com/package/ts-node
npm install -g ts-node
ts-node๋ tsc๋ฅผ ์คํํ๊ณ node๋ก ์์ํด์ผํ๋ ๋ฒ๊ฑฐ๋ก์์ ํด๊ฒฐ์ค๋๋ค. node app.js๋ก ์์ํ๋ ๋์ ์ ts-node app.ts๋ก ์์ํฉ๋๋ค. package.json์์ npm start ์คํ์ ๋ช ๋ น์ด๋ฅผ ์๋์ ๊ฐ์ด ์ง์ ํด์ค๋๋ค.
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "ts-node src/index.ts" },
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node src/index.ts"
},
๐ ormconfig.json ์ค์
โ ormconfig.json ํ์ผ์ ํ๋ก์ ํธ์ root ๋๋ ํ ๋ฆฌ์ ์กด์ฌํด์ผ ํฉ๋๋ค.
์ฒ์ typeorm์ค์ ์ ๋ง์น๋ฉด ์๋์ ๊ฐ์ด ormconfig.jsonํ์ผ์ด ์์ฑ๋ฉ๋๋ค. ๊ฐ ์ค์ ๊ฐ์ ๋ง๊ฒ ์์ ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
๋ง์ฝ ts-node๋ฅผ ์ฌ์ฉํ์ง ์์ ๊ฑฐ๋ผ๋ฉด(node์ฌ์ฉ ์) entities, migrations, subscribers์ ๊ฒฝ๋ก์ง์ ์์ "dist/entity/**/*.js" ์ ๊ฐ์ด jsํ์ผ๋ก ์ค์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test",
"synchronize": true,
"logging": false,
"entities": [ "src/entity/**/*.ts" ],
"migrations": [ "src/migration/**/*.ts" ],
"subscribers": [ "src/subscriber/**/*.ts" ],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
โ ์ ์ฒด ํ๋ก์ ํธ ๊ตฌ์ฑ
- controllers์ routes๋ก ๋ผ์ฐํ ๊ณผ ๊ธฐ๋ฅ์ ๊ตฌํํฉ๋๋ค.
- entityํด๋๋ node ํ๋ก์ ํธ์์ models ํด๋์ ์ญํ ์ด ๊ฐ์ต๋๋ค. typeorm์ผ๋ก ์์ฑํ ํ ์ด๋ธ์ ๋ํ ํ์ผ์ ํฌํจํฉ๋๋ค.
'typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TypeOrm ์์ํ๊ธฐ (entity, Table์์ฑ) (0) | 2021.04.01 |
---|