9.7.2 • Published 4 years ago

@ioa/rest v9.7.2

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

@ioa/rest

数据库查询通用rest api组件,集成角色、模型、字段级的权限管理,数据类型校验REST API。

在api开发过程中,经常需要向客户端提供一些纯数据库操作的CRUD接口,这些接口的共同特征是除访问权限外的其它业务逻辑几乎是相同的。为了避免编写重复的接口代码,减少接口数量,@ioa/rest通过在数据模型与用户api之间加入权限控制中间件,实现安全、快捷的抽象api。

api设计借鉴于postgrest简约的url查询表达式,@ioa/rest废除了原来臃肿的json方案,使用更为扁平化、更简洁、更易于读写的函数链表达式替换。

查询示例

select

http://localhost:9900/model/user?select=title,age,name

select! 反选

http://localhost:9900/model/user?select!=title,age,name

where

http://localhost:9900/model/user?where=name.eq(Wilburn);email.eq(Janae.Kiehn95@yahoo.com);age.eq(94580)

where、and

http://localhost:9900/model/user?where=name.eq(Wilburn)&and=name.eq(12);age.scope(1,45)

where、or

http://localhost:9900/model/user?where=name.eq(Wilburn)&or=name.eq(12);age.scope(1,45)

where、and、or

http://localhost:9900/model/user?where=age.gte(12).lt(100)&and=name.eq(12)&or=name.eq(12);age.scope(1,45)
http://localhost:9900/model/user?where=age.gte(12).lt(100);name.eq(12)||name.eq(12);age.gte(1);age.lte(100)

链式条件

http://localhost:9900/model/user?where=age.gte(10).lt(100)

分号分隔符

用于隔离多个字段

where=name.eq(Wilburn);email.eq(Janae.Kiehn95@yahoo.com)

点连接符

用函数链的语法为单个字段声明多个and条件,只是一种快捷方式

email.gte(12).lt(100) // 快捷函数链表达式

email.gte(12);email.lt(100) // 等效冗余表达式

ormv库运算符

@ioa/rest支持ormv中的所有运算符

参考链接:https://github.com/xiangle/ormv

编码转换

由于url参数存在保留关键字限制,当输入参数值中包含类似于&=()的保留关键字时需要使用encodeURIComponent()进行编码转换

(:%28

):%29

示例

// 错误,赋值中包含非法的保留字()
http://localhost:9900/model/user?where=phone.eq((559)-150-5961)

// 正确,()被转换为对应的url编码
http://localhost:9900/model/user?where=phone.eq(%28559%29-150-5961)

角色

当允许客户端通过api直接操作数据库时会涉及到很多安全问题,因此角色、权限的控制必不可少。

@ioa/rest中的角色是针对数据库的操作权限分组,并非常规的按照路由进行角色权限分组概念。

角色权限配置文件

为了方便集中管理权限配置项,@ioa/rest仅支持为$main组件扩展role目录,以角色名作为目录进行分组,每个配置文件名都需要有对应的数据模型。

配置示例

const common = {
   fields: {
      'name': true,
      'age': true,
   },
   where(ctx) {
      const { uid } = ctx.auth;
      return {
         'id': uid
      };
   }
}

module.exports = {
   'GET': { ...common },
   'POST': { ...common },
   'PUT': { ...common },
   'DELETE': { ...common },
}

辅助配置函数

由于role字段配置繁琐且容易出错,为了简化配置过程,可以通过辅助函数来提高工作效率

const { restModel } = require('@ioa/rest');

const fields = restModel('document').fields(); // 获取document模型的所有字段

const fields = restModel('document').fields('id', 'name', 'age', 'email'); // 获取document模型的指定字段

const fields = restModel('user').fieldsExclude('password'); // 获取user模型,不含password的所有字段

module.exports = {
   'GET': {
      fields,
      where(ctx) {
         const { uid } = ctx.auth;
         return {
            'id': uid
         };
      }
   },
   'POST': {
      fields,
      where(ctx) {
         const { uid } = ctx.auth;
         return {
            'id': uid
         };
      }
   },
}

query查询参数

支持Ormv中的所有运算符和查询语句。除此之外@ioa/rest还扩展了一部分专用选项,用于简化查询语句。以下凡标注为“扩展参数”的选项均为@ioa/rest私有。

通用参数

  • where “与”逻辑条件表达式,和sql语法类似,必须用where开头,否则and、or条件会被忽略

  • and “与”逻辑条件表达式

  • or “或”逻辑条件表达式

select专用参数

  • select 选择字段,select=name,title,email

  • select! 反选字段,select!=uid,password

select查询列表专用参数

  • order 限定排序条件,order=name.desc;title.desc;

  • offset 起始位置

  • page 限定当前第几页

  • limit 限制单页最大条数

  • total 是否显示总量

9.7.2

4 years ago

9.7.1

4 years ago

9.2.2

5 years ago

9.1.6

5 years ago

9.1.5

5 years ago

9.1.3

5 years ago

9.1.2

5 years ago

9.1.1

5 years ago

9.1.0

5 years ago

9.0.1

5 years ago

9.0.0

5 years ago

8.1.3

5 years ago

8.1.2

5 years ago

8.1.1

5 years ago

8.1.0

5 years ago

7.0.0-alpha.3

5 years ago

7.0.0-alpha.2

5 years ago

7.0.0-alpha.1

5 years ago

7.0.0-alpha.0

5 years ago

6.3.1-alpha.0

5 years ago

6.3.0-alpha.4

5 years ago

6.3.0-alpha.3

5 years ago

6.3.0-alpha.2

5 years ago

6.3.0-alpha.1

5 years ago

6.3.0-alpha.0

5 years ago