Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>egg-mongoose-helperNew to Visual Studio Code? Get it now.
egg-mongoose-helper

egg-mongoose-helper

rootz

|
322 installs
| (0) | Free
自动生成基于egg-mongoose的CRUD代码
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

egg-mongoose-helper

Usage

model文件夹下,右键菜单第一项生成代码:

guide

手动添加router:

app/router.js

  app.resources('model-name', '/api/v1/model-name', app.controller.api.v1.modelName);

生成代码模板如下:

app/controller/model-name.js

'use strict';
const { Controller } = require('egg');

class ${controllerName} extends Controller {

  constructor(ctx) {
    super(ctx);

    this.${serviceName}IndexTransfer = {
      page: { type: 'number', required: false, allowEmpty: false },
      pageSize: { type: 'number', required: false, allowEmpty: false, min: 1 },
    };

  }

  /**
   * GET ${route}
   */
  async index() {
    const { ctx, service } = this;

    // 组装参数
    const payload = ctx.query;
    payload.page = payload.page ? Number(payload.page) - 1 : 0;
    payload.pageSize = payload.pageSize ? Number(payload.pageSize) : 10;

    // 校验参数
    ctx.validate(this.${serviceName}IndexTransfer, payload);

    // 调用 Service 进行业务处理
    const res = await service.api.v1.${serviceName}.index(payload);
    res.pageSize++;
    // 设置响应内容和响应状态码
    ctx.helper.success({ ctx, res });
  }

  /**
   * GET ${route}/:id
   */
  async show() {
    const { ctx } = this;
    const { id } = ctx.params;

    const res = await ctx.service.api.v1.${serviceName}.show(id);
    ctx.helper.success({ ctx, res });
  }

  /**
   * GET ${route}/:id/edit
   */
  async edit() {
    const { ctx } = this;
    const { id } = ctx.params;


    const res = await ctx.service.api.v1.${serviceName}.edit(ctx.query, id);
    ctx.helper.success({ ctx, res });
  }

  /**
   * POST ${route}
   */
  async create() {
    const { ctx } = this;

    const payload = ctx.request.body;

    const res = await ctx.service.api.v1.${serviceName}.create(payload);
    // 设置响应内容和响应状态码
    ctx.helper.success({ ctx, res });
  }

  /**
   * PUT ${route}/:id
   */
  async update() {
    const { ctx } = this;
    const { id } = ctx.params;
    const payload = ctx.request.body;

    const res = await ctx.service.api.v1.${serviceName}.update(id, payload);

    ctx.helper.success({ ctx, res });
  }

  /**
   * DELETE ${route}/:id
   */
  async destroy() {
    const { ctx } = this;
    const { id } = ctx.params;

    const res = await ctx.service.api.v1.${serviceName}.destroy(id);
    ctx.helper.success({ ctx, res });
  }
}

module.exports = ${controllerName};

app/service/model-name.js

'use strict';
const { Service } = require('egg');
const mongoose = require('mongoose');

class ${serviceName} extends Service {
  async index(payload = {}) {
    const { ctx } = this;
    const resParameter = [];
    const $and = [];
    // 这里写搜索条件

    if (payload._id) {
      $and.push({ _id: mongoose.Types.ObjectId(payload._id) });
    }

    let find = {};
    if ($and.length > 0) {
      find = {
        $and,
      };
    }

    resParameter.push({ $match: find });

    // 处理排序
    if (payload.sort) {
      resParameter.push({ $sort: payload.sort });
    } else {
      resParameter.push({
        $sort: {
          createdAt: -1,
        },
      });
    }

    // 处理页数
    resParameter.push({
      $skip: payload.page * payload.pageSize || 0,
    });

    // 处理条数
    if (payload.pageSize) {
      resParameter.push({
        $limit: payload.pageSize * 1,
      });
    }


    const res = await ctx.model.${modelName}.aggregate(resParameter);

    const count = await ctx.model.${modelName}.aggregate([{
      $match: find,
    }]);


    return { count: count.length, list: res, pageSize: payload.pageSize * 1 || 0, page: payload.pageSize || 0 };
  }

  async show(_id) {
    const { ctx } = this;

    const res = await ctx.model.${modelName}.aggregate([{
      $match: {
        _id: mongoose.Types.ObjectId(_id),
      },
    }]);

    if (res.length < 1) {
      ctx.throw(200, ctx.app.config.httpCodeHash[404001]);
    }

    return res;
  }

  async edit(_id, payload = {}) {
    const { ctx } = this;

    const res = await ctx.model.${modelName}.findByIdAndUpdate(_id, payload);
    return res;
  }

  async create(payload = {}) {
    const { ctx } = this;

    const newDocument = {};
    if (payload.state) {
      newDocument.state = payload.state;
    }

    const res = await ctx.model.${modelName}.create(newDocument);
    return res;
  }

  async update(_id, payload = {}) {
    const { ctx } = this;

    const res = await ctx.model.${modelName}.findByIdAndUpdate(_id, payload);
    return res;
  }

  async destroy(_id) {
    const {
      ctx,
    } = this;

    return ctx.model.${modelName}.findOneAndRemove({ _id });
  }
}

module.exports = ${serviceName};

Release Notes

1.0.0

基于egg-mongoose,根据model自动生成CRUD代码

其他

联系方式 576415369@qq.com

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft