摘要

在前面的两篇文章,我们主要是通过Upload组件和Image组件,真正的做到了设计器和后端服务之间的数据传递。

有了这个例子,应该比较清楚,对于低代码项目的服务端,它主要做的是一个抽象的数据存储。 因为正常做开发,后端主要是对某个字段的数据进行存储,例如姓名,年龄等。但是对于低代码的后端,它的存储没有具体到某个字段,也没有具体的数量,它是和设计器里面的字段进行关系映射。

当然,这是后话,目前我们能做的,就是说如何在我们的项目里面,可以创建数据库的表,例如我创建一张学生表,创建一张教师表。用于后面我们拖拽页面的时候,作为数据存储的地方(当然具体怎么用,我们后面再说)。

当下,我们需要把数据库的动态操作给做出来。我们需要完成的事情有:

添加数据库表获取全部数据库表删除数据库表

这里因为数据库的操作可能比较频繁,所以建议读者下载一个可视化操作数据库的软件。我这里使用的是Studio 3T Free for MongoDB。

1.实现创建实体的接口

首先,我们在src下新增和数据库表相关的模块:

现在我们想一下,我们创建的数据库表应该在哪里,其实我们就可以放在localData这个数据库里面,但是因为我们之前这里有存放页面信息的数据库,所以localData数据库中的全量表,并非全都是通过这个模块创建的。

所以我们需要有一张表来记录,我们通过可视化建表的方式,创建的数据库表有哪些。

OK,现在我们可以实现创建数据库表的接口了,创建表我们需要做两件事情:

创建一张新的数据库表将数据库表的ID放在Data-Base表中

创建一张数据库表需要的数据结构是什么样子的呢?

我们需要数据库表的名字,数据库表的编码,数据库表的Schema。

为了后面方便叙述,我们统称数据库表为实体

数据库表的Schema:代表的是这个表需要什么字段,比如一张学生表需要学生姓名,学生学号,学生年级等。学生姓名是String类型,学生年龄是Number类型。而用来描述学生表信息的,就是学生表的Schema。

所以我们在dto.ts, sechema.ts和interface.ts中,可以写出创建实体需要的数据结构了:

export class createEntity {

readonly entityName: string

readonly entityCode: string

readonly entitySchema: Object

}

xin-builder-server2\src\data-base\data-base.interface.ts

export interface Entity extends Document {

readonly entityName: string

readonly entityCode: string

readonly entitySchema: Object

}

xin-builder-server2\src\data-base\data-base.schema.ts

import { Schema } from 'mongoose';

export const EntitySchema = new Schema({

entityName: { type: String, required: true },

entityCode: { type: String, required: true },

entitySchema: {type: Object, required: true}

});

有了数据结构之后,我们开始编写创建实体的方法,来到service中: xin-builder-server2\src\data-base\data-base.service.ts

import { Injectable } from "@nestjs/common";

import { InjectModel } from "@nestjs/mongoose";

import mongoose, { Model,Schema } from 'mongoose'

import {createEntity} from './data-base.dto'

import { Entity } from "./data-base.interface";

import { EntityInfo } from "./data-base.dto";

const DBRootModule = mongoose.createConnection('mongodb://127.0.0.1/localData');

@Injectable()

export class EntityService {

constructor(@InjectModel('Entity') private readonly EntityModule:Model){}

async addEntity(body: createEntity): Promise {

const DBRootModule = mongoose.createConnection('mongodb://127.0.0.1/localData');

await this.EntityModule.create(body)

DBRootModule.model(body.entityCode,new Schema(body.entitySchema),body.entityCode);

}

}

await this.EntityModule.create(body): 在entities表中,加入当前新建实体的信息。

DBRootModule.model(body.entityCode,new Schema(body.entitySchema),body.entityCode): 就是创建一张新的数据库表。

来到controller中: xin-builder-server2\src\data-base\data-base.controller.ts

import { Body, Controller, Post } from "@nestjs/common";

import {EntityService} from './data-base.service'

import { createEntity, EntityInfo } from "./data-base.dto";

import { ApiTags, ApiOperation } from '@nestjs/swagger'

@Controller('entity')

@ApiTags('实体管理')

export class EntityController {

constructor(private readonly EntityService: EntityService){}

@Post('addEntity')

@ApiOperation({summary: '创建实体'})

async addEntity(@Body() createEntity: createEntity){

return {

code: 200,

data: await this.EntityService.addEntity(createEntity),

message: 'Success.'

};

}

}

最后来到module中: xin-builder-server2\src\data-base\data-base.module.ts

import { Module } from "@nestjs/common";

import { EntityController } from "./data-base.controller";

import { EntityService } from "./data-base.service";

import { MongooseModule } from '@nestjs/mongoose';

import { EntitySchema } from "./data-base.schema";

const EntitySchemaTable = MongooseModule.forFeature([{ name: 'Entity', schema: EntitySchema }]);

@Module({

imports: [EntitySchemaTable],

controllers: [EntityController],

providers: [EntityService]

})

export class EntityModal {}

这样我们创建实体的接口就完成了。

2.实现删除实体和获取实体列表的接口

来到service中,我们写一下获取实体列表和删除实体的方法: 删除接口也需要做两件事:

删除entities中的对应信息删除对应的数据库表

async getEntityList(): Promise {

const entityList = await this.EntityModule.find();

return entityList

}

async delEntityItem(body: EntityInfo): Promise {

if(!body.entityCode){

return;

}

await this.EntityModule.deleteOne({entityCode: body.entityCode});

DBRootModule.dropCollection(body.entityCode)

}

在来到controller中,我们实现一下这两个方法的调用:

@Post('getEntityList')

@ApiOperation({summary: '获取实体列表'})

async getEntityList(){

return {

code: 200,

data: await this.EntityService.getEntityList(),

message: 'Sucess'

}

}

@Post('delEntityItem')

@ApiOperation({summary: '删除实体'})

async delEntityItem(@Body() EntityInfo: EntityInfo){

return {

code: 200,

data: await this.EntityService.delEntityItem(EntityInfo),

message: 'Sucess'

}

}

最终我们可以在Swagger文档进行测试一下:

这里,我们就实现了数据库表的添加,查询,和删除了。

相关的代码提交在github上: https://github.com/TeacherXin/XinBuilderServer2 commit: fix: 第四节:实现添加,查询,删除实体的接口

精彩文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: