feat: add entity and swagger
This commit is contained in:
1 parent
0416c356b8
commit
346742b157
12 files changed
+652
-66
No files matched your search
@@ -1,22 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
describe('AppController', () => {
|
||||
let appController: AppController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,12 +0,0 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
}
|
||||
+4
-5
@@ -1,10 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { CoreModule } from './core/core.module';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
imports: [CoreModule],
|
||||
controllers: [],
|
||||
providers: [],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
envFilePath: [
|
||||
'.env', // 通用配置,最低优先级
|
||||
`.env.${process.env.NODE_ENV}`, // 环境配置,如 .env.development
|
||||
`.env.${process.env.NODE_ENV}.local`, // 本地配置,最高优先级
|
||||
],
|
||||
}),
|
||||
TypeOrmModule.forRootAsync({
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
type: 'mysql',
|
||||
host: configService.get<string>('DB_HOST'),
|
||||
port: configService.get<number>('DB_PORT'),
|
||||
username: configService.get<string>('DB_USERNAME'),
|
||||
password: configService.get<string>('DB_PASSWORD'),
|
||||
database: configService.get<string>('DB_NAME'),
|
||||
autoLoadEntities: true,
|
||||
synchronize: process.env.NODE_ENV === 'development',
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
],
|
||||
})
|
||||
@Global()
|
||||
export class CoreModule {}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { CreateDateColumn, PrimaryColumn, UpdateDateColumn } from 'typeorm';
|
||||
import { SnowflakeUtil } from '../util/snowflake.util';
|
||||
|
||||
export abstract class BaseEntity {
|
||||
@PrimaryColumn()
|
||||
id: string = SnowflakeUtil.generateId();
|
||||
|
||||
@CreateDateColumn()
|
||||
createdTime: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedTime: Date;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { BaseEntity } from './base.entity';
|
||||
import { Entity, ManyToOne } from 'typeorm';
|
||||
import { User } from './user.entity';
|
||||
|
||||
@Entity()
|
||||
export abstract class TraceableEntity extends BaseEntity {
|
||||
@ManyToOne(() => User)
|
||||
createBy: User;
|
||||
|
||||
@ManyToOne(() => User)
|
||||
updateBy: User;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Column, Entity } from 'typeorm';
|
||||
import { BaseEntity } from './base.entity';
|
||||
|
||||
@Entity()
|
||||
export class User extends BaseEntity {
|
||||
@Column({
|
||||
unique: true,
|
||||
})
|
||||
username: string;
|
||||
|
||||
@Column()
|
||||
encryptedPassword: string;
|
||||
|
||||
@Column({
|
||||
default: false,
|
||||
})
|
||||
locked: boolean;
|
||||
|
||||
@Column({
|
||||
default: true,
|
||||
})
|
||||
enabled: boolean;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import SnowflakeID from 'snowflake-id';
|
||||
|
||||
export class SnowflakeUtil {
|
||||
private static instance: SnowflakeID;
|
||||
|
||||
static initialize(workerId: number = 1, datacenterId: number = 1): void {
|
||||
this.instance = new SnowflakeID({
|
||||
workerId, // 机器 ID
|
||||
datacenterId, // 数据中心 ID
|
||||
});
|
||||
}
|
||||
|
||||
static generateId(): string {
|
||||
if (!this.instance) {
|
||||
throw new Error('SnowflakeUtil is not initialized.');
|
||||
}
|
||||
return this.instance.generate().toString();
|
||||
}
|
||||
}
|
||||
+21
@@ -1,8 +1,29 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { SnowflakeUtil } from './core/util/snowflake.util';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
if (['development', 'test'].includes(process.env.NODE_ENV)) {
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle(process.env.APP_TITLE)
|
||||
.setDescription('The api description')
|
||||
.setVersion('1.0')
|
||||
.build();
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('docs', app, document);
|
||||
}
|
||||
SnowflakeUtil.initialize(
|
||||
parseInt(process.env.WORKER_ID, 10),
|
||||
parseInt(process.env.DATACENTER_ID, 10),
|
||||
);
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
transform: true,
|
||||
}),
|
||||
);
|
||||
await app.listen(3000);
|
||||
}
|
||||
bootstrap();
|
||||
Reference in new issue
Block a user