关于mysql:Postman如何通过xmysql工具的Restful-API-接口访问MySQL

4次阅读

共计 5393 个字符,预计需要花费 14 分钟才能阅读完成。

  • GreatSQL 社区原创内容未经受权不得随便应用,转载请分割小编并注明起源。

导语

有时候用 Postman 接口测试须要获取 MySQL 的查问后果做接口输入的校验,这里介绍下 Postman 通过 Restful API 接口拜访 MySQL 的工具 xmysql 的应用办法。

步骤

一、应用 nmp 装置 xmysql

注:npm 的装置和配置自行百度,这里次要讲 xmysql,就不赘述了

C:\Users\wmp>node -v
v12.16.3
C:\Users\wmp>
C:\Users\wmp> npm install -g xmysql
C:\Users\wmp\AppData\Roaming\npm\xmysql -> C:\Users\wmp\AppData\Roaming\npm\node_modules\xmysql\bin\index.js

> es5-ext@0.10.59 postinstall C:\Users\wmp\AppData\Roaming\npm\node_modules\xmysql\node_modules\es5-ext
> node -e "try{require('./_postinstall')}catch(e){}"

+ xmysql@0.5.1
added 131 packages from 68 contributors in 22.806s

装置完输出 xmysql 能够查看具体参数,同时也示意装置胜利

C:\Users\wmp>xmysql
Usage: index [options]

Options:
  -V, --version            output the version number
  -h, --host <n>           hostname of database / localhost by default
  -u, --user <n>           username of database / root by default
  -p, --password <n>       password of database / empty by default
  -d, --database <n>       database schema name
  -r, --ipAddress <n>      IP interface of your server / localhost by default
  -n, --portNumber <n>     port number for app / 3000 by default
  -o, --port <n>           port number for mysql / 3306 by default
  -S, --socketPath <n>     unix socket path / not used by default
  -s, --storageFolder <n>  storage folder / current working dir by default / available only with local
  -i, --ignoreTables <n>   comma separated table names to ignore
  -a, --apiPrefix <n>      api url prefix / "/api/" by default
  -y, --readOnly           readonly apis / false by default
  -c, --useCpuCores <n>    use number of CPU cores (using cluster) / 1 by default
  -h, --help               output usage information

  Examples:

    $ xmysql -u username -p password -d databaseSchema

Error: password for database is missing
Error: database name is missing

C:\Users\wmp>

连贯数据库:
xmysql -h host_name -o port -u user_name -p user_password -d database_name

C:\Users\wmp>xmysql -h 192.168.5.103 -u root -p 123456 -o 3301 -d test

          Generating REST APIs at the speed of your thought..

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

          Database              :    test
          Number of Tables      :    3【test 库表的数量】REST APIs Generated   :    62【生成的 api 数量】Xmysql took           :    0 seconds
          API's base URL        :    localhost:3000【api 的拜访形式,端口 3000 能够通过参数 - n 进行批改为其余】- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

xmysql 的默认端口是 3000,上面用 postman 调用 api 的形式试下数据库的增删改查 (注: 在 postman 中执行 sql 语句时,cmd 窗口和 msqll 都不能够敞开)

API Overview

HTTP Type API URL Comments
GET / 获取所有 REST API
GET /api/tableName 列出表的行
POST /api/tableName 创立一个新行
PUT /api/tableName 用新行替换现有行
POST /api/tableName/bulk 创立多行 – 在申请注释中发送对象数组
GET /api/tableName/bulk 列出多行 – /api/tableName/bulk?_ids=1,2,3
DELETE /api/tableName/bulk 删除多行 – /api/tableName/bulk?_ids=1,2,3
GET /api/tableName/:id 按主键检索一行
PATCH /api/tableName/:id 按主键更新行元素
DELETE /api/tableName/:id 按主键删除一行
GET /api/tableName/findOne 作为列表工作,但取得单个记录匹配条件
GET /api/tableName/count 计算表中的行数
GET /api/tableName/distinct 表中的不同行 – /api/tableName/distinct?_fields=col1
GET /api/tableName/:id/exists 是否存在行
GET /api/parentTable/:id/childTable 获取具备父表外键的子表行列表
GET /api/tableName/aggregate 汇总数字列的后果
GET /api/tableName/groupby 按列的后果分组
GET /api/tableName/ugroupby 应用一次调用按后果进行多个分组
GET /api/tableName/chart 基于 (min,max,step) 或 (step array) 或 (automagic) 的数值列散布
GET /api/tableName/autochart 与 Chart 雷同,但会自动识别哪些是数字列
GET /api/xjoin 解决连贯
GET /dynamic 应用参数执行动静 mysql 语句
GET /upload 上传单个文件
GET /uploads 上传多个文件
GET /download 下载文件
GET /api/tableName/describe 形容每个表的列
GET /api/tables 获取数据库中的所有表
GET /_health 获取过程和 mysql 的运行状况
GET /_version 获取 Xmysql、mysql、node 的版本

查看对象表下所有可用的接口,能够参数调用并查看后果

http://localhost:3000/

以下是 t1 表可用的接口:

用 t1 表做一个简略的查问测试:

1、查问 t1 的整表数据

GET /api/t1
SQL: select  *  from `t1` ;

2、带条件查问

GET /api/t1?_where=(ida,eq,1)
SQL: select  *  from `t1`  where `ida`=1;

3、分页查问

4、in 查问

GET /api/t1/bulk?_ids=1,2,3
SQL: select  *  from `t1`  where `ida` in (1,2,3)  limit 0,20;

其余的增删改参考上述的 API Overview,上面演示下 xmysql 在 postman 接口测试中的利用:

利用一:接口测试数据校验

  1. 启动 xmysql 服务:(测试时放弃服务启动状态不要敞开)
xmysql -h 192.168.5.103 -u root -p Great@123 -o 3306 -d SCOTT

  1. 创立 postman 测试 collection,蕴含测试接口和 xmysql 获取 mysql 数据接口
# 测试接口 1:获取全国行政区划
http://127.0.0.1:8369/query?fid={{pregion_code}}
#接口 2:xmysql 获取 mysql 数据接口
http://localhost:3000/api/AREAS?_where=(parent_region_code,eq,{{pregion_code}})

接口参数化,可在 postman collection 中设置全局参数并赋值

// 定义变量做接口的输出传参并设置为全局变量
var pregion_code=110100;
pm.environment.set("pregion_code", pregion_code);

接口输入参数化设置:

在接口 1Tests 中用 js 脚本对接口输入后果须要断言的局部转参数并设置环境变量,以便在接口 2 的数据库输入中进行比拟

// 返回 body 转 json
var jsonData = JSON.parse(responseBody);
var list=jsonData.result;
var regionCode=[];

num=list.length;

for (let i=0;i<num;i++){regionCode.push(list[i].id);

}
// 设置为全局变量,用于数据库查问接口的数据校验
pm.environment.set("regionCode", regionCode);

//console.log(regionCode)

接口 2Tests 中用 js 脚本对数据库查问到的 json 脚本进行解决,获取须要的数据,参数化后与接口 1 的输入进行比拟

// 返回 body 转 json
var jsonData = JSON.parse(responseBody);
var res_id=[];
num=jsonData.length;

// 获取数据库查问后果的 region_code 列
for (let i=0;i<num;i++){res_id.push(jsonData[i].region_code+'');

}

// 获取接口 1 输入的后果参数 regionCode
var regionCode=pm.environment.get("regionCode");

// 断言 比拟接口和数据库的输入后果
tests["接口断言胜利!!"] = res_id.sort().toString === regionCode.sort().toString ; 


//console.log(res_id);

  1. 运行测试 collection

利用二:利用 postman+xmysql 实现 MySQL 疾速生成大量测试数据

建表脚本

drop table if exists t1;

CREATE TABLE  t1
(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY comment '用户 ID', 
person_name varchar(30) comment '用户名称'

) 

这里演示插入 10000 条:

  1. 创立 insert 接口

  1. 设置 collection 并执行

3、执行结束,查看表数量

调用表行数查问接口查问后果:

申明:xmysql 工具次要用于测试环境,联合接口测试等,次要用于内部测试,在生产还是不倡议应用,因为这款工具会造成重大的数据安全问题。

参考文档:

https://gitee.com/thinkyoung/…

Enjoy GreatSQL :)

文章举荐:

面向金融级利用的 GreatSQL 正式开源
https://mp.weixin.qq.com/s/cI…

Changes in GreatSQL 8.0.25 (2021-8-18)
https://mp.weixin.qq.com/s/qc…

MGR 及 GreatSQL 资源汇总
https://mp.weixin.qq.com/s/qX…

GreatSQL MGR FAQ
https://mp.weixin.qq.com/s/J6…

在 Linux 下源码编译装置 GreatSQL/MySQL
https://mp.weixin.qq.com/s/WZ…

# 对于 GreatSQL

GreatSQL 是由万里数据库保护的 MySQL 分支,专一于晋升 MGR 可靠性及性能,反对 InnoDB 并行查问个性,是实用于金融级利用的 MySQL 分支版本。

Gitee:

https://gitee.com/GreatSQL/Gr…

GitHub:

https://github.com/GreatSQL/G…

Bilibili:

https://space.bilibili.com/13…

微信 &QQ 群:

可搜寻增加 GreatSQL 社区助手微信好友,发送验证信息“加群”退出 GreatSQL/MGR 交换微信群

QQ 群:533341697

微信小助手:wanlidbc

本文由博客一文多发平台 OpenWrite 公布!

正文完
 0