sqler 的迭代还是很快的,已经2.0 了,2.0 有好多新功能的添加,同时也有好多不兼容的修改
说明: 测试使用docker-compose,同时我已经push 了docker 镜像 dalongrong/sqler

发布说明

  • 添加 aggregate
  • 移除 authorizers hooks
  • 添加 authorizer script
  • 移除 rules
  • 添加 validators as array of scripts
  • 添加 Go text/template
  • 添加 include
  • 添加 bind

环境准备

  • docker-compose 文件
 
version: "3"
services:
  sqler:
    image: dalongrong/sqler:2.0
    volumes:
    - "./config/config-2-0-example.hcl:/app/config.example.hcl"
    environment:
    - "DSN=root:dalongrong@tcp(mysqldb:3306)/test?multiStatements=true"
    ports:
    - "3678:3678"
    - "8025:8025"
  mysqldb:
    image: mysql:5.7.16
    ports:
      - 3306:3306
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: dalongrong
      MYSQL_DATABASE: test
      MYSQL_USER: test
      MYSQL_PASSWORD: test
      TZ: Asia/Shanghai
 
 
  • 配置说明
    使用了2.0 的参考配置,为了方便,我注释了授权的处理
 
// create a macro/endpoint called "_boot",
// this macro is private "used within other macros" 
// because it starts with "_".
_boot {
    // the query we want to execute
    exec = <<SQL
        CREATE TABLE IF NOT EXISTS `users` (
            `ID` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `name` VARCHAR(30) DEFAULT "@anonymous",
            `email` VARCHAR(30) DEFAULT "@anonymous",
            `password` VARCHAR(200) DEFAULT "",
            `time` INT UNSIGNED
        );
    SQL
}
// adduser macro/endpoint, just hit `/adduser` with
// a `?user_name=&user_email=` or json `POST` request
// with the same fields.
adduser {
    validators {
        user_name_is_empty = "$input.user_name && $input.user_name.trim().length > 0"
        user_email_is_empty = "$input.user_email && $input.user_email.trim(' ').length > 0"
        user_password_is_not_ok = "$input.user_password && $input.user_password.trim(' ').length > 5"
    }
    bind {
        name = "$input.user_name"
        email = "$input.user_email"
        password = "$input.user_password"
    }
    methods = ["POST"]
    // authorizer = <<JS
    // (function(){
    // log("use this for debugging")
    // token = $input.http_authorization
    // response = fetch("http://requestbin.fullcontact.com/zxpjigzx", {
    // headers: {
    // "Authorization": token
    // }
    // })
    // if ( response.statusCode != 200 ) {
    // return false
    // }
    // return true
    // })()
    // JS
    // include some macros we declared before
    include = ["_boot"]
    exec = <<SQL
        INSERT INTO users(name, email, password, time) VALUES(:name, :email, :password, UNIX_TIMESTAMP());
        SELECT * FROM users WHERE id = LAST_INSERT_ID();
    SQL
}
// list all databases, and run a transformer function
databases {
    exec = "SHOW DATABASES"
}
// list all tables from all databases
tables {
    exec = "SELECT `table_schema` as `database`, `table_name` as `table` FROM INFORMATION_SCHEMA.tables"
}
// a macro that aggregates `databases` macro and `tables` macro into one macro
databases_tables {
    aggregate = ["databases", "tables"]
}
 
 

运行&&测试

  • 启动
docker-compose up -d
  • 测试
    添加数据
 
curl -X POST \
  http://localhost:8025/adduser \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: a7784ea1-9f50-46ee-92ac-1d850334f3f1' \
  -H 'cache-control: no-cache' \
  -d '{
    "user_name":"dalong",
    "user_email":"1141591465@qq.com",
    "user_password":"dalongdemo"
}'
 
 

返回结果

curl -X POST \
  http://localhost:8025/adduser \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: a7784ea1-9f50-46ee-92ac-1d850334f3f1' \
  -H 'cache-control: no-cache' \
  -d '{
    "user_name":"dalong",
    "user_email":"1141591465@qq.com",
    "user_password":"dalongdemo"
}'
{"data":[{"ID":2,"email":"1141591465@qq.com","name":"dalong","password":"dalongdemo","time":1547433588}],"success":true}% 
 
 
  • 聚合功能试用
curl http://localhost:8025/databases_tables | jq
 

返回结果
数据比较多,截取部分

 
{
  "data": {
    "databases": [
      {
        "Database": "information_schema"
      },
      {
        "Database": "mysql"
      },
      {
        "Database": "performance_schema"
      },
      {
        "Database": "sys"
      },
      {
        "Database": "test"
      }
    ],
。。。。。。
 
 
  • redis 集成
    使用redis_cli
 
redis-cli -p 3678
 

列出宏列表

list
1) "tables"
2) "databases_tables"
3) "_boot"
4) "adduser"
5) "databases"
 
 

调用宏
databases_tables
数据较多,返回部分

 
databases_tables
1) (integer) 1
2) "{\"databases\":[{\"Database\":\"information_schema\"},{\"Database\":\"mysql\"},{\"Database\":\"performance_schema\"},{\"Database\":\"sys\"},{\"Database\":\"test\"}],\"tables\":[{\"database\":\"information_schema\",\"table\":\"CHARACTER_SETS\"},{\"database\":\"information_schema\",\"table\":\"COLLATIONS\"},{\"database\":\"information_s

说明

2.0 的功能是越来越方便了

参考资料

https://github.com/alash3al/sqler
https://github.com/rongfengliang/sqler-docker-compose
https://cloud.docker.com/repository/docker/dalongrong/sqler/

sqler sql 转rest api 2.0 试用的更多相关文章

  1. sqler sql 转rest api 的工具试用

    sqler 从开源很快就获取了1k的star,使用起来很方便,而且也很灵活,支持的数据库也比较多. 支持的功能 无需依赖,可独立使用: 支持多种数据可类型,包括:SQL Server, MYSQL, ...

  2. sqler sql 转rest api 源码解析(一)应用的启动入口

    sqler sql 转rest api 的源码还是比较简单的,没有比较复杂的设计,大部分都是基于开源 模块实现的. 说明: 当前的版本为2.0,代码使用go mod 进行包管理,如果本地运行注意gol ...

  3. sqler sql 转rest api javascript 试用

    sqler 内嵌了一个js 引擎的实现(基于goja,当我们配置了exec的配置之后 调用宏(redis 接口)或者rest api 的时候会有一个全局变量$result ,保存了执行的结果,我们可以 ...

  4. sqler sql 转rest api 数据聚合操作

    sqler 2.0 提供了一个新的指令aggregate,注意这个和sql 的聚合函数不是一个概念,这个只是为了 方便api数据的拼接 参考格式   databases {    exec = &qu ...

  5. sqler sql 转rest api redis 接口使用

    sqler 支持redis 协议,我们可以用过redis client 连接sqler,他会将宏住转换为redis command 实现上看源码我们发现是基于一个开源的redis 协议的golang ...

  6. sqler sql 转rest api 源码解析(四)macro 的执行

    macro 说明 macro 是sqler 的核心,当前的处理流程为授权处理,数据校验,依赖执行(include),聚合处理,数据转换 处理,sql 执行以及sql 参数绑定 授权处理 这个是通过go ...

  7. sqler sql 转rest api 数据校验的处理

    早期版本(2.0 之前)使用rules 进行数据校验处理,2.0 之后进行了修改使用 validators,这样更加明确 参考格式   addpost {    // if any rule retu ...

  8. sqler sql 转rest api 防止sql 注入

    sqler 对于sql Sanitization 的处理,我们可以使用bind 指令 说明: 这个是2.0 的功能,注意版本的使用 参考格式   addpost {    // $input is a ...

  9. sqler sql 转rest api 授权处理

    我们可以使用内置的authorizer 以及js 脚本,方便的进行api 接口的授权处理 说明: 这个是2.0 的功能,注意版本的使用 参考格式 addpost {    authorizer = & ...

随机推荐

  1. :代理模式:proxy

    #ifndef __PROXY_H__ #define __PROXY_H__ class Subject { public: Subject(){} virtual ~Subject(){} vir ...

  2. ubuntu多显示器单触摸屏校准

    多显示器单触摸屏屏幕校准 0.触摸屏重定向 sudo xinput map-to-output 13 DP1  #将触摸屏映射到指定的显示器 其中:13为触摸屏设备id,可通过 xinput命令查看 ...

  3. mybatis mapper配置文件 CustomerMapper.xml

    Dao @Repositorypublic interface CustomerDAO {    public void create(CustomerModel cm);    public voi ...

  4. js 唤起APP

    常常有这样的场景,咱们开发出来的APP需要进行推广,比如在页面顶部来一张大Banner图片,亦或一张二维码.但往往我们都是直接给推广图片加了一个下载链接(App Store中的).所以咱们来模拟一下用 ...

  5. tensorflow-优化器

    优化器可以通俗的理解成梯度下降那一套流程. 梯度下降 基本流程 a. 损失函数 b. 求导,计算梯度 c. 更新参数 d. 迭代下一批样本 可以对照着理解tf. tf 优化器 正常使用流程 a. 创建 ...

  6. 20165214 学习基础与C语言基础调查

    读后感 我花时间仔细阅读了娄老师公众号上的那5篇关于自己各个方面学习经历的文章,随后深为震撼又自愧不如. 我实在没想到,这套学习方法竟有如此巨大的力量!纵使在娄老师不熟悉的乒乓球领域,娄老师也能通过这 ...

  7. thinkphp5.1学习笔记

    由于新公司使用的框架是thinkphp5.1,有必要进一步学习,看来我要把php框架使用完全了,之前就用过laravel和CI框架了. 现在打算只是一个学习的记录,结构会比较凌乱,基本我估计只能自己看 ...

  8. 如何将 gitbook上的开源书转换为mobi

    看更新后的gitbook官网说不支持pdf.mobi导出,还以为用gitbook-cli也没法导出.几轮尝试之后发现还是能导出的,使用gitbook-cli即可实现. 按如下步骤进行操作,未能完成导出 ...

  9. Android大作业 --音乐播放器

    1.项目成员(本次作业主要对上一次的音乐播放器进行完善) 韦家城 学号:1600802026 班级:161  博客:https://www.cnblogs.com/ln9969cc/ 邓乾尧 学号:1 ...

  10. 使用scrapy ImagesPipeline爬取图片资源

    这是一个使用scrapy的ImagesPipeline爬取下载图片的示例,生成的图片保存在爬虫的full文件夹里. scrapy startproject DoubanImgs cd DoubanIm ...