feat: 标签接入mongodb

This commit is contained in:
Mozzie 2023-09-08 14:14:31 +08:00
parent df826308dc
commit 72d3fc8619
5 changed files with 65 additions and 1 deletions

View File

@ -3,8 +3,8 @@ import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { NacosModule } from './nacos/nacos.module'; import { NacosModule } from './nacos/nacos.module';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { RetrievalModule } from './retrieval/retrieval.module'; import { RetrievalModule } from './retrieval/retrieval.module';
import { LabelModule } from './label/label.module';
@Module({ @Module({
imports: [ imports: [
@ -14,6 +14,7 @@ import { RetrievalModule } from './retrieval/retrieval.module';
}), }),
NacosModule, NacosModule,
RetrievalModule, RetrievalModule,
LabelModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService],

View File

@ -0,0 +1,13 @@
import { Controller } from '@nestjs/common';
import { EventPattern } from '@nestjs/microservices';
import { LabelService } from './label.service';
@Controller()
export class LabelController {
constructor(private readonly labelService: LabelService) {}
@EventPattern({ cmd: 'dicom.label.create' })
async createLabel(payload) {
return await this.labelService.createLabel();
}
}

View File

@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { LabelController } from './label.controller';
import { Label, LabelSchema } from './schema/label.schema';
import { LabelService } from './label.service';
import { MongooseModule } from '@nestjs/mongoose';
@Module({
controllers: [LabelController],
imports: [
MongooseModule.forRoot('mongodb://test:test@localhost:27017/tavi'),
MongooseModule.forFeature([{ name: Label.name, schema: LabelSchema }]),
],
providers: [LabelService],
})
export class LabelModule {}

View File

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

View File

@ -0,0 +1,13 @@
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);