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
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user