This commit is contained in:
陶林 2022-04-08 13:16:51 +08:00
parent 14e6d6ffee
commit 2f587c8f1d
6 changed files with 45 additions and 34 deletions

View File

@ -4,8 +4,10 @@ import { AppService } from './app.service';
import { ScheduleModule } from '@nestjs/schedule';
import { TypeOrmModule } from '@nestjs/typeorm';
import User from './entity/user';
import { TechModule } from './apps/tech';
@Module({
imports: [
TechModule,
ScheduleModule.forRoot(),
TypeOrmModule.forRoot({
type: 'mysql',
@ -15,7 +17,9 @@ import User from './entity/user';
password: 'ne5BbDCiYwx3n6nE',
database: 'nest_test',
entities: [User],
synchronize: true,
synchronize: true, // 是否开启同步
autoLoadEntities: true, // 是否自动导入
logging: true, // 打印日志
}),
],
controllers: [AppController],

View File

@ -0,0 +1,20 @@
import { Controller, Get } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import TechEntity from '../entity/tech';
@Controller('/tech')
export default class TechController {
@InjectRepository(TechEntity)
private tech: Repository<TechEntity>;
@Get('/t1')
getT1() {
return 'T1';
}
/// 数据库查询 裙查询
@Get('/t2')
async getT2() {
const techs = await this.tech.find();
return techs;
}
}

View File

@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export default class Tech {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}

10
src/apps/tech/index.ts Normal file
View File

@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import Tech from './entity/tech';
import TechController from './controller/tech';
@Module({
imports: [TypeOrmModule.forFeature([Tech])], // 此模块使用 forFeature() 方法定义在当前范围中注册哪些存储库。这样,我们就可以使用 @InjectRepository()装饰器将 UsersRepository 注入到 UsersService 中:
controllers: [TechController],
providers: [],
})
export class TechModule {}

View File

@ -1,24 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
});
});

View File

@ -1,9 +0,0 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}