feat: 标签切回mysql

This commit is contained in:
Mozzie 2023-09-08 15:56:16 +08:00
parent abdf14cbee
commit cf79216a6e
6 changed files with 60 additions and 29 deletions

View File

@ -0,0 +1,7 @@
/**
*
*/
export enum LabelSource {
SYSTEM = 'SYSTEM',
USER = 'USER',
}

View File

@ -0,0 +1,30 @@
import { LabelSource } from 'src/app.constant';
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
@Entity()
export class Label {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column({
type: 'enum',
enum: LabelSource,
default: LabelSource.SYSTEM, // 设置一个默认值,如果需要的话
})
source: LabelSource;
@CreateDateColumn({ type: 'timestamp' })
createAt: Date;
@UpdateDateColumn({ type: 'timestamp' })
updateAt: Date;
}

View File

@ -8,6 +8,6 @@ export class LabelController {
@EventPattern({ cmd: 'dicom.label.create' }) @EventPattern({ cmd: 'dicom.label.create' })
async createLabel(payload) { async createLabel(payload) {
return await this.labelService.createLabel(); return await this.labelService.createLabel(payload);
} }
} }

View File

@ -1,14 +1,24 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { LabelController } from './label.controller'; import { LabelController } from './label.controller';
import { Label, LabelSchema } from './schema/label.schema';
import { LabelService } from './label.service'; import { LabelService } from './label.service';
import { MongooseModule } from '@nestjs/mongoose'; import { TypeOrmModule } from '@nestjs/typeorm';
import { Label } from './entity/label.entity';
@Module({ @Module({
controllers: [LabelController], controllers: [LabelController],
imports: [ imports: [
MongooseModule.forRoot('mongodb://test:test@localhost:27017/tavi'), TypeOrmModule.forRoot({
MongooseModule.forFeature([{ name: Label.name, schema: LabelSchema }]), type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'dicom',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
timezone: 'Asia/Shanghai', // 这里设置了时区
}),
TypeOrmModule.forFeature([Label]),
], ],
providers: [LabelService], providers: [LabelService],
}) })

View File

@ -1,22 +1,19 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Label, LabelDocument } from './schema/label.schema'; import { InjectRepository } from '@nestjs/typeorm';
import { InjectModel } from '@nestjs/mongoose'; import { Label } from './entity/label.entity';
import { Model } from 'mongoose'; import { Repository } from 'typeorm';
@Injectable() @Injectable()
export class LabelService { export class LabelService {
constructor( constructor(
@InjectModel(Label.name) @InjectRepository(Label)
private readonly labelModel: Model<LabelDocument>, private readonly labelRepository: Repository<Label>,
) {} ) {}
async createLabel() { async createLabel(payload) {
// 创建一个新的 Label 实例 console.log(payload);
const newLabel = new this.labelModel({ return await this.labelRepository.save({
name: '钙化', name: '钙化',
description: '钙化的关键词', description: '钙化的关键词',
}); });
// 使用 save 方法保存实例到数据库
return await newLabel.save();
} }
} }

View File

@ -1,13 +0,0 @@
import { Schema, Document, model } from 'mongoose';
export interface LabelDocument extends Document {
name: string;
description?: string;
}
export const LabelSchema = new Schema({
name: { type: String, required: true },
description: String,
});
export const Label = model<LabelDocument>('Label', LabelSchema);