USE [IronManNest]
GO
/****** Object: Table [dbo].[Users] Script Date: 2017/12/20 下午 10:49:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Age] [int] NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Users] ON
INSERT [dbo].[Users] ([ID], [Name], [Age]) VALUES (1, N'Michael', 26)
INSERT [dbo].[Users] ([ID], [Name], [Age]) VALUES (2, N'Mary', 16)
INSERT [dbo].[Users] ([ID], [Name], [Age]) VALUES (3, N'Mary', 30)
SET IDENTITY_INSERT [dbo].[Users] OFF
'use strict';
import { Users } from '../users.entity';
import { IUsers } from './IUsers';
export interface IUsersService {
findAll(): Promise<Array<Users>>;
findById(ID: number): Promise<Users | null>;
findOne(options: Object): Promise<Users | null>;
//今天先完成Select部分,所以註解以下方法。
/*create(users: IUsers): Promise<Users>;
update(ID: number, newValue: IUsers): Promise<Users | null>;
delete(ID: number): Promise<number>;*/
}
import { Component, Inject } from '@nestjs/common';
import { Users } from './users.entity';
import { Model } from 'sequelize-typescript';
import { IUsers, IUsersService } from './interfaces/index';
@Component()
export class UsersServices implements IUsersService {
constructor(
@Inject('UsersRepository') private readonly usersRepository: typeof Users) { }
public async findAll():Promise<Array<Users>>{
return await this.usersRepository.findAll<Users>();
}
//findOne()可以加入各種option,以下示範常見的where
//注意findOne() 找到一筆就會立即return data,不會繼續往下找。
public async findOne(options: Object): Promise<Users | null> {
return await this.usersRepository.findOne<Users>(options);
}
//restful API很常用。
public async findById(id: number): Promise<Users | null> {
return await this.usersRepository.findById<Users>(id);
}
}
'use strict';
import { Controller, Get, Response, HttpStatus, Param, Body } from '@nestjs/common';
import { UsersServices } from '../users.service';
@Controller()
export class UsersController {
constructor(private readonly usersServices: UsersServices) { }
@Get('users')
public async getUsers( @Response() res) {
const users = await this.usersServices.findAll();
return res.status(HttpStatus.OK).json(users);
}
@Get('users/find')
public async findUser( @Response() res) {
//給定where條件
let queryCondition = { where: { Name: 'Mary' } };
const users = await this.usersServices.findOne(queryCondition);
return res.status(HttpStatus.OK).json(users);
}
@Get('users/:id')
public async getUser( @Response() res, @Param() param) {
const users = await this.usersServices.findById(param.id);
return res.status(HttpStatus.OK).json(users);
}
}
'use strict';
import { Module } from '@nestjs/common';
import { UsersServices } from '../users.service';
import { UsersProvider } from '../users.providers';
import { DatabaseModule } from '../../database.module';
import { UsersController } from './users.controller';
@Module({
modules: [DatabaseModule],
controllers: [UsersController],
components: [
UsersServices,
UsersProvider
]
})
export class UsersModule { }
要注意一下findOne()這方法,只要找到一筆資料就會立即回傳,不會繼續往下找,所以我where Name = Mary 是不會傳兩筆資料的。
以上簡單的SELECT 情境已經實現,明天再完成create、update、delete。
程式碼都在github