orm-sequelize-utils

1.0.5 • Public • Published

orm-sequelize-utils

常用方法封装说明

entity方法说明

根据模型定义结构通过sequelize进行模型创建,使用小驼峰进行模型定义,entity会自动把小驼峰写法在数据库中解析成下划线,然后对操作模型的方法进行了拦截封装,对参数数据进行类型检查和默认值赋值等操作

模型定义结构 字段,表名称小驼峰写法

 {
  name: 'tableName',
  fields: {
    id: { type: Sequelize.BIGINT, allowNull: false, primaryKey: true, defaultFn: 'id' },
    creatorId: { type: Sequelize.BIGINT, allowNull: true, field: 'creator_id', defaultFn: 'userId' },
    creatorName: { type: Sequelize.STRING(64), allowNull: true, field: 'creator_name', defaultFn: 'userName' },
    createdAt: { type: Sequelize.DATE, allowNull: true, field: 'created_at' },
    modifierId: { type: Sequelize.BIGINT, allowNull: true, field: 'modifier_id', defaultEqual: 'creatorId', updateFn: 'userId' },
    modifierName: { type: Sequelize.STRING(64), allowNull: true, field: 'modifier_name', defaultFn: 'userName', updateFn: 'userName' },
    updatedAt: { type: Sequelize.DATE, allowNull: true, field: 'updated_at' },
    isRemoved: { type: Sequelize.BOOLEAN, field: 'is_removed', defaultValue: '0' },
    version: { type: Sequelize.BIGINT, allowNull: true, defaultEqual: 'id', updateFn: 'id' }
  },
  uniques: {
      props: ['name'],
      memo: '属性名称'
    }
}

配置说明:
  字段定义自定义属性说明:
    defaultFn:默认值函数,支持的列类型 id:主键列, now 当前日期, userId: 当前用户主键, userName: 当前用户名称, fullId: 树形表主键链列, fullname: 树形表全称列, level: 树形表层级
    defaultEqual:默认值等值列,一般配置表属性
    updateFn:更新值函数  支持的列类型函数同defaultFn
  uniques:
    props: 参与重复验证的属性字段
    memo: 属性名

sequelize操作模型的主要方法

getCondition 解析通过问号传参的方式传进来的参数,形成options对象
访问路径:/items?limit=20&order=id&attributes=name,shortName&offset=10   
解析结果对象:
{
  limit: 20,
  order
}
postCondition 解析通过post方式传进来的参数,形成options
参数对象:
{
  condtionItems: [ {
    filedName: 'name',
    op: 'eq',
    filedValue: 'lp'
  } ],
  order: [ [ 'id' ] ],
  limit: 100,
  offset: 100,
  attributes: [ 'name', 'shortName' ],
  isNotReturnCount: false // 新修改增加
}
解析对象:
{

}
findOne 通过参数对象查询单条数据
参数对象options:
{
  where: {
    id: ctx.params.id,
    orgId: ctx.params.orgId
  }
}
findAll 通过参数对象查询多条数据
参数对象options:
{
  where: {
    id: ctx.params.id
  },
  limit: 100,
  offset: 100,
  order: [ [ 'id' ] ],
  attributes: ['name','shortName']
}
findAndCountAll 分页查找返回总行数与分页后对象
参数对象options:
{
  where: {
    id: ctx.params.id
  },
  limit: 100,
  offset: 100,
  order: [ [ 'id' ] ],
  attributes: ['name','shortName']
}
create 添加操作
参数对象说明:
  values:要添加的对象
  extras: 上下文信息,里面有租户id,组织id,用户id,扩展添加用户名称
  options: 条件对象,添加一般不使用
    where: 查询数据的条件
    returnData: 默认为false 表示添加成功后返回数据的id,为true,返回新增的完整数据
update 修改操作
参数对象说明:
  values:要添加的对象
  extras: 上下文信息,里面有租户id,组织id,用户id,扩展添加用户名称
  options: 条件对象,一般使用{ where: { id: ctx.params.id } }
  reservedProps:保留字段值,不进行更新的字段列表
destroy 删除操作,默认为更新isRemoved字段值
参数对象说明:
options: 条件对象,一般使用{ where: { id: ctx.params.id } }
extras: 上下文信息,里面有租户id,组织id,用户id,扩展添加用户名称
realDesctroy 删除操作,真正从数据库删除
参数对象说明:
options: 条件对象,一般使用{ where: { id: ctx.params.id } }
bulkCreate 批量添加操作
参数对象说明:
  values:要添加的对象
  extras: 上下文信息,里面有租户id,组织id,用户id,扩展添加用户名称
  options: 条件对象,一般使用{ where: { id: ctx.params.id } }
  reservedProps:保留字段值,不进行更新的字段列表
  

Readme

Keywords

Package Sidebar

Install

npm i orm-sequelize-utils

Weekly Downloads

1

Version

1.0.5

License

none

Unpacked Size

30.2 kB

Total Files

6

Last publish

Collaborators

  • danielmlc