sqler sql 转rest api 的工具试用

sqler 从开源很快就获取了1k的star,使用起来很方便,而且也很灵活,支持的数据库也比较多。

支持的功能

  • 无需依赖,可独立使用;
  • 支持多种数据可类型,包括:SQL Server, MYSQL, SQLITE, PostgreSQL, Cockroachdb 等;
  • 内置 RESTful 服务器;
  • 内置 RESP Redis 协议,可以使用任何 redis 客户端连接到 SQLer;
  • 内置 Javascript 解释器,可轻松转换结果;
  • 内置验证器;
  • 自动使用预备语句;
  • 使用(HCL)配置语言;
  • 可基于 unix glob 模式加载多个配置文件;
  • 每条 SQL 查询可被命名为宏;
  • 在每个宏内可使用 Go text/template;
  • 每个宏都有自己的 Context(查询参数 + 正文参数)作为.Input(map [string] interface{}),而.Utils是辅助函数列表,目前它只包含 SQLEscape;
  • 可自定义授权程序,授权程序只是一个简单的 webhook,sqler 使用这个 webhook 验证是否应该完成某请求

测试环境准备

为了方便测试,我制作了一个比较简单的docker 镜像dalongrong/sqler:1.5,对于运行的参数可以通过环境
变量指定

  • docker-compose 文件
version: "3"
services:
  sqler:
    image: dalongrong/sqler:1.5
    environment:
    - "DSN=root:[email protected](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
  • 说明
    镜像dalongrong/sqler:1.5 包含一个默认的配置,也可以通过环境指定,配置内容来自官方
    内容如下:
// 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 {
    // what request method will this macro be called
    // default: ["ANY"]
    methods = ["POST"]
?
    // authorizers,
    // sqler will attempt to send the incoming authorization header
    // to the provided endpoint(s) as `Authorization`,
    // each endpoint MUST return `200 OK` so sqler can continue, other wise,
    // sqler will break the request and return back the client with the error occured.
    // each authorizer has a method and a url, if you ignored the method
    // it will be automatically set to `GET`.
    // authorizers = ["GET http://web.hook/api/authorize", "GET http://web.hook/api/allowed?roles=admin,root,super_admin"]
?
    // the validation rules
    // you can specifiy seprated rules for each request method!
    rules {
        user_name = ["required"]
        user_email = ["required", "email"]
        user_password = ["required", "stringlength: 5,50"]
    }
?
    // the query to be executed
    exec = <<SQL
        {{ template "_boot" }}
?
        /* let‘s bind a vars to be used within our internal prepared statment */
        {{ .BindVar "name" .Input.user_name }}
        {{ .BindVar "email" .Input.user_email }}
        {{ .BindVar "emailx" .Input.user_email }}
?
        INSERT INTO users(name, email, password, time) VALUES(
            /* we added it above */
            :name,
?
            /* we added it above */
            :email,
?
            /* it will be secured anyway because it is encoded */
            ‘{{ .Input.user_password | .Hash "bcrypt" }}‘,
?
            /* generate a unix timestamp "seconds" */
            {{ .UnixTime }}
        );
?
        SELECT * FROM users WHERE id = LAST_INSERT_ID();
    SQL
}
?
// list all databases, and run a transformer function
databases {
    exec = "SHOW DATABASES"
?
    transformer = <<JS
        // there is a global variable called `$result`,
        // `$result` holds the result of the sql execution.
        (function(){
            newResult = []
?
            for ( i in $result ) {
                newResult.push($result[i].Database)
            }
?
            return newResult
        })()
    JS
}

运行&&测试

  • 运行
docker-compose up  -d
  • 添加数据
    rest 接口地址为8025
    添加数据curl 命令
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":"[email protected]",
    "user_password":"dalongdemo"
}‘

返回结果

{
    "data": [
        {
            "ID": 1,
            "email": "[email protected]",
            "name": "dalong",
            "password": "$2a$10$nfPllaq3AqYDwu4SQTskWeN0BphHCoBzwmb4rj6Q0OB21voBHCZke",
            "time": 1547127497
        }
    ],
    "success": true
}

数据库数据

说明

sqler 的设计很方便,我们通过简单的配置就可以创建灵活的rest api 了,很强大,而且内置的认证处理,数据校验。。。

参考资料

https://www.infoq.cn/article/LZxqJd-ZcNiUKcG1APDz
https://github.com/alash3al/sqler
https://github.com/rongfengliang/sqler-docker-compose

原文地址:https://www.cnblogs.com/rongfengliang/p/10252632.html

时间: 2024-08-30 15:21:43

sqler sql 转rest api 的工具试用的相关文章

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

sqler sql 转rest api 的源码还是比较简单的,没有比较复杂的设计,大部分都是基于开源 模块实现的. 说明: 当前的版本为2.0,代码使用go mod 进行包管理,如果本地运行注意golang 版本,我使用docker 运行, 参考 https://github.com/rongfengliang/sqler-docker-compose/blob/master/Dockerfile 依赖的开源包 配置解析的(比如bind,exec,validates,include...) 使用

sqler sql 转rest api 的docker 镜像构建(续)使用源码编译

sqler 在社区的响应还是很不错的,已经添加了好多数据库的连接,就在早上项目的包管理还没有写明确, 下午就已经有go mod 构建的支持了,同时也调整下docker 镜像的构建,直接使用git clone + go mod 备注: go mod 是新的包管理方案,需要新版本的golang,使用容器就不存在这个问题了,同时对于国内 还有一个墙的问题,同时我push了1.7tag 的镜像,就是使用go mod 构建的. dockerfile FROM golang:alpine as build

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

macro 说明 macro 是sqler 的核心,当前的处理流程为授权处理,数据校验,依赖执行(include),聚合处理,数据转换 处理 授权处理 这个是通过golang 的js 包处理的,通过将golang 的http 请求暴露为js 的fetch 方法,放在js 引擎的执行,通过 http 状态吗确认是否是执行的权限,对于授权的处理,由宏的配置指定,建议通过http hreader处理 参考格式:    authorizer = <<JS       (function(){    

Api管理工具(spring-rest-docs)

对于app开发来说,必须需要有相应的api文档,一般最基础的就是用markdown工具来撰写api文档.当对于开发人员来说,是总会想着寻找更方便撰写,测试,对接前端开发的文档生成的工具. 其实这方面的工具很多:手动撰写的也很多,很多都带有mock测试功能.方便前端对接时使用. 国内的也不少: RAP:动态生成模拟数据,实时测试等功能. eoLinker:基于云端的api管理工具,同时提供在线协作,测试等的功能. SBDoc:SBDoc是一个商业化开源产品,完全免费.无论你是前端工程师,还是后端工

【Windows10&nbsp;IoT开发系列】API&nbsp;移植工具

原文:[Windows10 IoT开发系列]API 移植工具 Windows 10 IoT Core 中是否提供你的当前 Win32 应用程序或库所依赖的 API? 如果不提供,是否存在可使用的等效 API? 此工具可以为你回答这些问题,并协助你将你的当前 Win32 应用程序和库迁移到 Windows IoT Core. Windows 10 IoT 核心版 API 移植工具可在 ms-iot/iot-utilities github 存储库中找到.下载存储库 zip 并将 IoTAPIPor

Sql server 2008 R2 配置管理工具服务显示远程过程调用失败:0x800706be

今天在其他电脑配置 SQl server 2008 R2,安装完成后,发现打开配置管理工具服务 :显示远程过程调用失败:0x800706be 这样的错误提示 之前并没有遇到过这种问题,猜想,可能是这台电脑之前安装了 visual studio 2012,内置的SQl server 2012与之冲突,导致新的2008 R2 版本过低导致. 解决方法如下: 有软件管理器的打开软件管理器,没有的进入控制面板的程序卸载,将"Microsoft SQL Server 2012 Express LocalD

基于oracle SQL Developer Data Modeler建模工具,解析xml,生成etljet代码

今天准备在 @zhangkai05 写的小工具model_sql基础上,进一步完善功能,使其更加自动化.方便建模人员开发etl任务. 源代码更改后 ,会发布到 git(url地址)上,逐步完善. 描述现状及问题 团队 数据仓库建模在2013年下开始使用 oracle sql developser datamodeler建模工具,之前一直用 powerdesigner,但由于后者是商业收费软件,公司又不打算购买.故经过调研,转向oracle建模工具.虽然其 易用性与 power designer

spring MVC +freemarker + easyui 实现sql查询和执行小工具总结

项目中,有时候线下不能方便的连接项目中的数据源时刻,大部分的问题定位和处理都会存在难度,有时候,一个小工具就能实时的查询和执行当前对应的数据源的库.下面,就本人在项目中实际开发使用的小工具,实时的介绍开发使用过程.首先看图:大概的操作界面,基本使用easyui组件实现,欢迎大家吐槽: 界面包含了基本的sql查询 和 sql执行的小功能,把查询和执行分开,也是为了后台实现的需要,以及权限控制的考虑,毕竟执行的操作,会影响到系统的数据问题.查询和执行的菜单,是用easyui的手风琴式的菜单处理的.两

Atitit.&#160;Class&#160;&#160;元数据的反射操作&#160;api&#160;apache&#160;&#160;工具

Atitit. Class  元数据的反射操作 api apache  工具 1 BeanUtils & PropertyUtils & MethodUtils类使用方法 - 短裤党 ConstructorUtils1 2 New实例 ConstructorUtils.invokeConstructor1 3 调用方法 MethodUtils2 4 参考3 1 BeanUtils & PropertyUtils & MethodUtils类使用方法 - 短裤党 Constr