从零实现一套低代码(保姆级教程)【后端服务】 --- 【6】实现数据库的动态建表等接口

摘要

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

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

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

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

  1. 添加数据库表
  2. 获取全部数据库表
  3. 删除数据库表

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

1.实现创建实体的接口

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

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

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

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

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

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

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

为了后面方便叙述,我们统称数据库表为实体
数据库表的Schema:代表的是这个表需要什么字段,比如一张学生表需要学生姓名,学生学号,学生年级等。学生姓名是String类型,学生年龄是Number类型。而用来描述学生表信息的,就是学生表的Schema。


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

javascript 复制代码
export class createEntity {
  readonly entityName: string
  readonly entityCode: string
  readonly entitySchema: Object
}

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

javascript 复制代码
export interface Entity extends Document {
  readonly entityName: string
  readonly entityCode: string
  readonly entitySchema: Object
}

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

javascript 复制代码
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

javascript 复制代码
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<Entity>){}

  async addEntity(body: createEntity): Promise<void> {
    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

javascript 复制代码
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

javascript 复制代码
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中,我们写一下获取实体列表和删除实体的方法: 删除接口也需要做两件事:

  1. 删除entities中的对应信息
  2. 删除对应的数据库表
javascript 复制代码
  async getEntityList(): Promise<Entity []> {
    const entityList = await this.EntityModule.find();
    return entityList
  }

  async delEntityItem(body: EntityInfo): Promise<void> {
    if(!body.entityCode){
      return;
    }
    await this.EntityModule.deleteOne({entityCode: body.entityCode});
    DBRootModule.dropCollection(body.entityCode)
  }

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

javascript 复制代码
  @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上:
github.com/TeacherXin/...
commit: fix: 第四节:实现添加,查询,删除实体的接口

相关推荐
工业甲酰苯胺2 小时前
低代码开发:助力制造业数字化高质量发展
低代码
工业甲酰苯胺2 小时前
数字化转型背景下低代码开发模式变革的研究
低代码
BPM_宏天低代码2 小时前
低代码技术:简化应用开发的未来
低代码
白总Server6 小时前
MongoDB解说
开发语言·数据库·后端·mongodb·golang·rust·php
Kenneth風车7 小时前
【第十三章:Sentosa_DSML社区版-机器学习聚类】
人工智能·低代码·机器学习·数据分析·聚类
码爸8 小时前
spark读mongodb
大数据·mongodb·spark
有颜有货1 天前
低代码开发平台系统架构概述
低代码·系统架构
lgbisha1 天前
828华为云征文|华为云Flexus X实例docker部署最新Appsmith社区版,搭建自己的低代码平台
低代码·docker·华为云
一个很帅的帅哥1 天前
实现浏览器的下拉加载功能(类似知乎)
开发语言·javascript·mysql·mongodb·node.js·vue·express
快乐非自愿1 天前
一文详解低代码开发如何成为学校低成本数字化转型的新引擎
低代码