學習 NESTJS feat Part 2 === ![](https://i.imgur.com/1fldI8k.png) --- ###### tags: `Nestjs`,`TypeScript`,`TypeORM`,`Project` ## 開始塑造 你會發現有兩個檔案在你的SRC裏,一個是users,一個是reports。 各別都有controller, module , service. ![](https://i.imgur.com/gpMuVqE.png) 這是我們即將要進行的project的設計模樣。 ![](https://i.imgur.com/59ndfdA.png) 經過這圖,我們將分配USer和 Reports module 要串接的是: - User Module -> POST/auth/SignUp + POST/auth/SignIn - Reports Module -> GET/reports + POST/reports + PATCH/reports:id ### 設置 TypeORM 到 app.module.ts ```typescript= import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { AppController } from './app/controller'; import { AppService } from './app.service'; import { UsersModule } from './users/users.module'; import { ReportsModule } from './reports/reports.module'; import { User } from './users/user.entity'; import { Report } from './reports/report.entity'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'sqlite', //執行怎麽樣的類別 database: 'db.sqlite', //檔案要建立在哪裏,或者讀取的地方 entities: [], // 來自哪個出入口 synchronize: true, //是否同步 }), UsersModule, ReportsModule, ], controllers: [AppController], providers: [AppService], }) export class AppModule {} ``` 這樣TypeORM的root就先建立好了,如果是用Nodejs的話,那會跟用ENV去設置自己的root連接到SQL的動作是一樣的。 --- ### 手動建立Entity document 在動手做之前,我們要先知道接下來要建立的Table裏面會有什麽元素,例如: - 做 user 的 table,我們需要像 ID 這樣的 exclusive key - username 和 Password 這樣的 String - maybe 也來個 Date? 畫好了Table我們就手動做 Entity 了! ```typescript= // build report.entity.ts inside document : report import {Entity, Column , PrimaryGeneratedColumn} from 'typeorm'; @Entity() //標記告知Nest這是入口類 export class Report { @PrimaryGeneratedColumn() //這是像Sql一樣一個table只能有一個且無法重複 id: number; @Column() //第二個格子的title price: number; } ``` ```typescript= //build another one in users document with user.entity.ts import {Entity , Column , PrimaryGeneratedColumn} from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column() email:string; @Column() password:string; } ``` --- ### import 到各自 module 裏 先在report.module.ts : ```typescript= import { Module } from '@nestjs/common'; import {TypeOrmModule} from '@nestjs/typeorm'; // Import TypeORM import { ReportsController } from './reports.controller'; import { ReportsService } from './reports.service'; import { Report } from './report.entity'; // Import entity as Report @Module({ imports : [TypeOrmModule.forFeature([Report])], //imports TypeORM forFeature as Array type. controllers: [ReportsController], providers: [ReportsService] }) export class ReportsModule {} ``` 然後再到 users.module.ts: ```typescript= import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; // import TypeORM import { UsersController } from './users.controller'; import { UsersService } from './users.service'; import { User } from './user.entity'; @Module({ imports : [TypeOrmModule.forFeature([User])], // imports TypeORM for feature controllers: [UsersController], providers: [UsersService] }) export class UsersModule {} ``` --- ### 回到 app.module.ts 追加剛剛建立和設置好的Entity: ```typescript= import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { UsersModule } from './users/users.module'; import { ReportsModule } from './reports/reports.module'; import { User } from './users/user.entity'; // import User from entity import { Report } from './reports/report.entity'; //import Report from entity @Module({ imports: [ TypeOrmModule.forRoot({ type: 'sqlite', database: 'db.sqlite', entities: [User, Report],// add-on User and Report synchronize: true, }), UsersModule, ReportsModule, ], controllers: [AppController], providers: [AppService], }) export class AppModule {} ``` --- ### 在NPM START 之前: 因爲我們使用的database是db.sqlite 所以我們需要到Extension 下載可以看sqlite 的挂件 ![](https://i.imgur.com/IxLXW0k.png) 儅你裝好以後,就執行: - `npm start --watch` 然後你會發現在資源管理器(Explorer)的最下方發現 SQLITE EXPLORER ![](https://i.imgur.com/f3amHHQ.png) 那就表示,這個SQlite建立成功了。這裏就像SQL table的架構。