1.0.38 • Published 5 months ago

exmd v1.0.38

Weekly downloads
2
License
ISC
Repository
-
Last release
5 months ago

exmd

exmd的核心代码, 包含的功能有: 1. startProject 启动项目 2. BaseModel model基类 3. migrate 数据库表转model 4. asyncdb model转数据库表 5. renderRequestType controller方法的请求配置

  • 配置请求
  • 配置swagger及swagger中的参数和返回参数描述
  1. handleClientAdapter 持久层适配器,它能指定数据源,对数据源中的表对应的model操作
  2. ResultVo 返回的结果集合
  3. AuthController 内置的登录注册controller
  4. UploadController 内置的文件上传控制器

数据源配置 目前只支持postgresql数据库,后续将会扩展

module.exports = {
  // 数据库1
  postgresql1: {
    type: 'postgresql',
    data: {
      user: 'bserverx',
      database: 'bserver',
      password: 'bserverx123',
      host: 'localhost',
      port: '5432',
      poolSize: 5,
      poolIdleTimeout: 30000,
      reapIntervalMillis: 10000
    }
  }
}

启动项目

startProject({
  /**
   * 是否开启调试模式
   * 调试模式将会打印sql
   * 调试模式下不会发送邮件
   */
  isDebugger: true,
  /**
   * api地址路由配置
   */
  router,
  /**
   * 数据源
   */
  Datasource,
  /**
   * 用户model
   */
  authUserModel: UserModel,
  /**
   * 登录注册使用的数据源对应key
   */
  authDataSourceKey: 'postgresql1',
  /**
   * 注册邮箱使用的内容模板
   * 没有配置将会使用默认模板
   * @param {*} code 
   * @returns 
   */
  authEmailContent: (code) => {
    return `
      <p>您好:</p>
      <p>您的验证码是:<strong style="color:orangered;">${code}</strong></p>
      <p>嘿嘿</p>
    `
  },
  /**
   * 登录注册验证码超时时间
   */
  authEmailCodeMaxTime: '1minute', 
  // 一个邮箱最多允许绑定多少个用户
  authEmailBindUserMax: 3,
  // token生成私钥
  authPrivateKey: 'abc123',
  // hour/minute/second
  authTokenTime: '1minute',
  /**
   * 发邮件库,
   * 目前只支持 tencenmail
   */
  emailLib: 'tencenmail',
  /**
   * 邮箱配置
   */
  emailConfig,
  /**
   * 返回数据的结果集合模板
   */
  ResultVo,
  // swagger的路径配置
  swaggerUrl: '/swagger/data',
  // 文件上传的保存路径
  uploadBaseUrl: '/Users/wujianfei/test-upload-files',
  /**
   * 文件上传模块划分, 必须传
   * 上传的文件路径:基础路径下 文件名称: [模块名称_时间戳],
   * 比如default下的文件名称:
   * test1_1657007903331.mp4
   */
  uploadFileDirectorys: [{
    name: 'test1'
  }, {
    name: 'default'
  }],
  // 文件限制配置
  uploadLimits: {
    fileSize: 10 * 1024 * 1024 * 1024
  }
})

文件上传和登录模块使用 可参考提供的后端模板项目中的路由,看注释即可

const { AuthController, UploadController } = require('exmd')
const router = [
  {
    url: '/bserve/',
    controller: UploadController
  },
  {
    url: '/bserve/',
    controller: AuthController
  }
  ...自己定义的controller
]

ResultVo 可以根据该ResultVo自定义

/**
 * 返回实体类
 * @param {*} params 
 */
function ResultVo(params) {
  const { status, data, message } = params
  if (status === undefined) {
    throw new Error('status is undefined')
  }
  if (data === undefined && message === undefined) {
    throw new Error('both and data are undefined')
  }
  return {
    status,
    data,
    message
  }
}
ResultVo.prototype.swaggerDescription = {
  status: '状态 0正常',
  data: '',
  message: '异常信息'
}
module.exports = ResultVo

UserModel 可以根据该model自定义模板放到models目录下

const { BaseModel } = require('exmd')
const fieldsMap = {
  id: {
    type: 'uuid',
    length: 16,
    lengthvar: -1,
    notnull: true,
    comment: null,
    isPrimary: true,
    pk_name: 'omuser_pkey'
  },
  username: {
    type: 'text',
    length: -1,
    lengthvar: -1,
    notnull: true,
    comment: null
  },
  password: {
    type: 'text',
    length: -1,
    lengthvar: -1,
    notnull: true,
    comment: null
  },
  email: {
    type: 'text',
    length: -1,
    lengthvar: -1,
    notnull: true,
    comment: null
  },
  fullName: {
    type: 'text',
    length: -1,
    lengthvar: -1,
    notnull: true,
    comment: null
  },
  isactive: {
    type: 'int4',
    length: 4,
    lengthvar: -1,
    notnull: true,
    comment: null
  },
  realname: {
    type: 'text',
    length: -1,
    lengthvar: -1,
    notnull: false,
    comment: null
  }
}

function omuser() {
  const vm = this
  BaseModel.apply(vm, arguments)
  vm.fields = fieldsMap
  vm.tableName = 'omuser'
}

;(function () {
  const TempFun = function () {}
  TempFun.prototype = BaseModel.prototype
  omuser.prototype = new TempFun()
})()

module.exports = omuser

构建

npm run build

提交所有代码

npm run push:all

发布

npm run publish:exmd
npm run publish:aux

源码仓库已提交申请中,请耐心等待

1.0.38

5 months ago

1.0.37

7 months ago

1.0.36

7 months ago

1.0.35

7 months ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.34

2 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.11

4 years ago

1.0.12

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago