现在可以添加实体数据记录

This commit is contained in:
Jeremy Yin 2019-06-27 22:22:50 +08:00
parent 359b7c9533
commit 607d3d86be
3 changed files with 32 additions and 4 deletions

View File

@ -1,4 +1,14 @@
import { Controller } from '@nestjs/common';
import { Controller, Post, Body } from '@nestjs/common';
import { PostService } from './post.service';
@Controller('posts')
export class PostController {}
export class PostController {
constructor(
private readonly postService: PostService,
) { }
@Post()
async store(@Body() data) {
return await this.postService.store(data);
}
}

View File

@ -1,9 +1,14 @@
import { Module } from '@nestjs/common';
import { PostController } from './post.controller';
import { PostService } from './post.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Post } from './post.entity';
@Module({
imports: [
TypeOrmModule.forFeature([Post]),
],
controllers: [PostController],
providers: [PostService]
providers: [PostService],
})
export class PostModule {}

View File

@ -1,4 +1,17 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { Post } from './post.entity';
import { InjectRepository } from '@nestjs/typeorm';
@Injectable()
export class PostService {}
export class PostService {
constructor(
@InjectRepository(Post)
private readonly postRepository: Repository<Post>,
) { }
async store(data) {
const entity = await this.postRepository.create(data);
return await this.postRepository.save(entity);
}
}