11个版本 (破坏性更新)
0.9.0 | 2024年4月7日 |
---|---|
0.7.0 | 2024年4月6日 |
0.6.0 | 2021年8月22日 |
0.5.0 | 2020年12月4日 |
0.2.0 | 2020年1月12日 |
#995 in 解析器实现
每月485次下载
240KB
6.5K SLoC
RestQ
在REST API中表达数据操作的 simplest 方法。
使用适当的HTTP方法、url和数据格式(csv)的组合实现。
示例:使用过滤、分组和分页查询简单的表 person
。
GET /person?age=lt.42&(student=eq.true|gender=eq.'M')&group_by=sum(age),grade,gender&having=min(age)=gt.42&order_by=age.desc,height.asc&page=20&page_size=100
这可以转换成SQL查询。
SELECT * FROM person
WHERE age < 42
AND (student = true OR gender = 'M')
GROUP BY sum(age), grade, gender
HAVING min(age) > 42
ORDER BY age DESC, height ASC
LIMIT 100 OFFSET 1900 ROWS
响应体将包含查询结果的 csv
格式数据。
RestQ 语法/语法
create = ["CREATE" | "PUT"], "/", table, column_def_list, "\n", csv
select = "GET", "/", table, [join_table], column_list, [ "?", condition]
delete = "DELETE", table, [ "?", condition ]
update = ["UPDATE | "PATCH"] table, set_expr_list, [ "?", condition]
drop = ["DROP" | "DELETE"] "-", table
alter = ["ALTER" | "PATCH"] table, { drop_column | add_column | alter_column }
drop_column = "-", column
add_column = "+", column_def
alter_column = column, "=", column_def
column_def_list = "{", { column_def }, "}"
| "(", { column_def }, ")"
column_def = [ { column_attributes } ], column, [ "(" foreign ")" ], ":", data_type, [ "(" default_value ")" ]
column_attributes = primary | index | unique
primary = "*"
index = "@"
unique = "&"
data_type = "bool" | "s8" | "s16" | "s32" | "s64" | "u8" | "u16", etc
default_value = value
value = number | string | bool ,..etc
column = string, ".", string
| string
table = string
foreign = table
insert = table, column_list ,"\n", csv
column_list = "{", { column }, "}"
join_table = table, join_type, table
join_type = right_join | left_join | inner_join | full_join
right_join = "->"
left_join = "<-"
inner_join = "-><-"
full_join = "<-->"
condition = expr
expr = column | value | binary_operation
binary_operation = expr, operator, expr
operator = "and" | "or" | "eq" | "gte" | "lte" ,..etc
数据类型
bool
: 布尔值s8
: 自动递增的 u8s16
: 自动递增的 u16s32
: 自动递增的 u32,序列号s64
: 自动递增的 u64,大序列号f32
: 4字节浮点数f64
: 8字节浮点数i8
、i16
、i32
、i64
: 有符号整数u8
、u16
、u32
、u64
: 无符号整数text
: utf8 字符串uuid
: 纯UUID,null时随机生成uuid_rand
: 随机生成的UUIDuuid_slug
: 创建一个新的UUID并从中生成一个url friend base64字符串。utc
: UTC时区的时间戳url
: URL类型json
: JSONbytes
: 二进制数据
在一个请求中创建表和插入记录。
PUT /+product{*product_id:s32,name:text,created_by(users):u32,created:utc,is_active:bool}
Content-Type: text/csv; charset=UTF-8
1,go pro,1,2019-10-31 11:59:59.872,,true
2,shovel,1,2019-11-01 07:30:00.462,,false
- HTTP方法为
PUT
。 - URL采用
restq
语法。 - 正文为
csv
。
对应的SQL语句
CREATE TABLE product (
product_id serial NOT NULL PRIMARY,
name character varying NOT NULL,
created_by integer NOT NULL REFERENCES users(user_id),
created timestamp with time zone NOT NULL DEFAULT now(),
is_active boolean NOT NULL
INSERT INTO product(product_id, name, created_by, is_active)
VALUES(
(1,'go pro',1,2019-10-31 11:59:59.872,DEFAULT,true)
(2,'shovel',1,2019-11-01 07:30:00.462,DEFAULT,false)
);
显示表定义
HEAD /product
显示所有表
HEAD /
查询记录
GET /product{product_id,name}?is_active=eq.true&order_by=created.desc
SELECT product_id,name FROM product WHERE is_active = true ORDER BY created DESC
插入记录
POST /product{product_id,name,created_by,created,is_active}
1,go pro,1,2019-10-31 11:59:59.872,,true
2,shovel,1,2019-11-01 07:30:00.462,,false
INSERT INTO product(product_id, name, created_by, is_active)
VALUES(
(1,'go pro',1,2019-10-31 11:59:59.872,true)
(2,'shovel',1,2019-11-01 07:30:00.462,false)
);
查询插入
POST /user{user_id,name,person_id(GET/person{id}?person.name=name)}
1,TOM JONES,,
INSERT INTO user(user_id, name, person_id)
VALUES(1, 'TOM JONES', (SELECT person.id FROM person WHERE person.name='TOM JONES'));
更新记录
PATCH /product{description="I'm the new description now"}?product_id=1
UPDATE product SET description = 'I\'m the new description now' WHERE product_id = 1;
批量更新记录
传递了同一记录的两个版本,第一个是原始版本,下一个是更新版本
PATCH /product{*product_id,name}
1,go pro,1,go pro hero4
2,shovel,2,slightly used shovel
UPDATE product SET name = 'go pro hero4' WHERE id = 1;
UPDATE product SET name = 'slightly used shovel' WHERE id = 2'
删除
DELETE /product?product_id=1
DELETE FROM product WHERE product_id = '1'
删除多个
DELETE /product{product_id}
1
2
3
DELETE FROM product WHERE product_id IN ('1','2','3')
按名称删除多个(无主键)
DELETE /product{name,is_active}
Go Pro,true
Shovel,true
Chair,true
DELETE FROM product WHERE name = 'Go Pro' AND is_active = 'true';
DELETE FROM product WHERE name = 'Shovel' AND is_active = 'true';
DELETE FROM product WHERE name = 'Chair' AND is_active = 'true';
删除表中的所有记录
DELETE /product
TRUNCATE product;
复杂选择(带连接)
GET /product<-users{product.*,users.user_name}?product_id=1&is_active=true&created=gt.2019-11-05T08:45:03.432
SELECT product.*, users.user_name
FROM product
LEFT JOIN users ON product.created_by = users.user_id
WHERE product_id = 1
AND is_active = true
AND created > '2019-11-05T08:45:03.432'
连接表
支持的连接类型
- INNER JOIN
table1-><-table2
- OUTER JOIN
- LEFT JOIN
table1<-table2
- RIGHT JOIN
table1->table2
- FULL JOIN
table1<-->table2
- LEFT JOIN
早期版本和灵感来源
请支持此项目
依赖项
~7MB
~112K SLoC