diff --git a/src/app.module.ts b/src/app.module.ts index ff7a5ce..9631ab9 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -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], diff --git a/src/apps/tech/controller/tech.ts b/src/apps/tech/controller/tech.ts new file mode 100644 index 0000000..68175c5 --- /dev/null +++ b/src/apps/tech/controller/tech.ts @@ -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; + + @Get('/t1') + getT1() { + return 'T1'; + } + /// 数据库查询 裙查询 + @Get('/t2') + async getT2() { + const techs = await this.tech.find(); + return techs; + } +} diff --git a/src/apps/tech/entity/tech.ts b/src/apps/tech/entity/tech.ts new file mode 100644 index 0000000..20a5bd3 --- /dev/null +++ b/src/apps/tech/entity/tech.ts @@ -0,0 +1,10 @@ +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; + +@Entity() +export default class Tech { + @PrimaryGeneratedColumn() + id: number; + + @Column() + name: string; +} diff --git a/src/apps/tech/index.ts b/src/apps/tech/index.ts new file mode 100644 index 0000000..8d58b80 --- /dev/null +++ b/src/apps/tech/index.ts @@ -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 {} diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts deleted file mode 100644 index 50cda62..0000000 --- a/test/app.e2e-spec.ts +++ /dev/null @@ -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!'); - }); -}); diff --git a/test/jest-e2e.json b/test/jest-e2e.json deleted file mode 100644 index e9d912f..0000000 --- a/test/jest-e2e.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "moduleFileExtensions": ["js", "json", "ts"], - "rootDir": ".", - "testEnvironment": "node", - "testRegex": ".e2e-spec.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - } -}