3 个版本
0.1.0-beta.3 | 2023年2月8日 |
---|---|
0.1.0-beta.2 | 2023年2月4日 |
0.1.0-beta.1 | 2023年2月2日 |
#731 in 数据库接口
220KB
4.5K SLoC
一个开源的图数据库
*** 仅用于演示介绍 MintDB - 一个由 Rust 构建的强大、开源的嵌入式图数据库。使用 MintDB,您可以轻松地通过简化的类似 SQL 的 JSON 对象管理并查询数据,并订阅实时更新和事件。数据库支持关系,提供深度优先和广度优先搜索功能,并内置管理面板以可视化和管理数据。用户界面使用 Vanilla Javascript 构建,使其成为一个轻量级且功能强大的解决方案。MintDB 的简单 API 非常适合现代 Web、移动和桌面应用程序,提供了一种安全高效的数据管理方式。
功能
- 友好的数据管理仪表板
- SQL 和 WebSocket 游乐场,用于测试查询和订阅
- 支持 ACID 事务以确保数据完整性
- 用户认证以确保安全访问数据
- SQL API 端点用于查询数据
- WebSocket 消息发布端点用于实时更新
- 实时 WebSocket 订阅,用于监控表、文档和键的突变
- TypeScript SDK
依赖项
[dependencies]
futures = "0.3.25"
mintdb = { path = "lib"}
once_cell = "1.16.0"
serde = { version = "1.0.148", features = ["derive", "rc"] }
serde_json = "1.0.89"
tokio = { version = "1.22.0", features = ["macros", "sync", "rt-multi-thread", "signal"]}
tokio-stream = "0.1.11"
uuid = { version = "1.2.2", features = ["serde", "v4"]}
warp = "0.3.3"
thiserror = "1.0.37"
入门
安装 mintdb-server 二进制文件
cargo install [email protected]
运行服务器
mintdb-server
在浏览器中打开 http://127.0.0.1:8000
SQL API
向以下格式的 http://127.0.0.1:8000/sql 发送 POST 请求
{
"stmt": "String",
"tb": "String",
"doc": "String",
"data": {},
"topic": "String",
"user_id": 1,
"message": "String",
}
SELECT
获取一个
读取一个用户
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "SELECT",
"tb": "person",
"doc": "person:1",
"data": {},
"topic": "",
"user_id": 1,
"message": ""
}'
获取所有
读取所有用户
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "SELECT",
"tb": "person",
"doc": "*",
"data": {},
"topic": "",
"user_id": 1,
"message": ""
}'
CREATE
创建一个用户
如果该人已存在,则返回错误
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "CREATE",
"tb": "person",
"doc": "person:4",
"data": {
"name": "Ricky",
"email": "[email protected]",
"city": "Miami",
},
"topic": "",
"user_id": 1,
"message": ""
}'
MERGE
将数据合并到用户文档中。
如果不存在,这将创建一个新的表或文档。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MERGE",
"tb": "person",
"doc": "person:4",
"data": {
"phone": "305 578 5899"
},
"topic": "",
"user_id": 1,
"message": ""
}'
DELETE
删除一个文档
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "DELETE",
"tb": "person",
"doc": "person:4",
"data": {},
"topic": "",
"user_id": 1,
"message": ""
}'
FIND
通过传递一个要匹配的值的 Map 来查找数据。
返回匹配任何参数的文档的向量。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "FIND",
"tb": "person",
"doc": "",
"data": { "phone": "305 578 5899" },
"topic": "",
"user_id": 1,
"message": ""
}'
MATCH
通过传递一个要匹配的值的 Map 来查找数据。
返回匹配所有参数的文档的向量。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MATCH",
"tb": "person",
"doc": "",
"data": { "state": "FL", "name": "Lucy" },
"topic": "",
"user_id": 1,
"message": ""
}'
COMPARE
通过传递一个 Map 查找数据
{
"lhs": "string", // The key of the document to compare.
"op": "operator", // The operation to compare ("==", "!=" "<=" "<" ">=" ">" "contains" or "icontains")
"rhs": "string or number" // The value to compare. Contains and icontains must use String
}
返回匹配参数的文档的向量。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MATCH",
"tb": "car",
"doc": "",
"data": { "lhs": "make", "op": "==", "rhs": "Suzuki" },
"topic": "",
"user_id": 1,
"message": ""
}'
返回键不等于 rhs 的文档的向量。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MATCH",
"tb": "car",
"doc": "",
"data": { "lhs": "make", "op": "!=", "rhs": "Suzuki" },
"topic": "",
"user_id": 1,
"message": ""
}'
返回键大于等于 rhs 的文档的向量。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MATCH",
"tb": "car",
"doc": "",
"data": { "lhs": "year", "op": ">=", "rhs": 2000 },
"topic": "",
"user_id": 1,
"message": ""
}'
返回键大于 rhs 的文档的向量。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MATCH",
"tb": "car",
"doc": "",
"data": { "lhs": "year", "op": ">", "rhs": 2000 },
"topic": "",
"user_id": 1,
"message": ""
}'
返回一个键值小于或等于rhs的文档向量。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MATCH",
"tb": "car",
"doc": "",
"data": { "lhs": "year", "op": "<=", "rhs": 2000 },
"topic": "",
"user_id": 1,
"message": ""
}'
返回一个键值小于rhs的文档向量。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MATCH",
"tb": "car",
"doc": "",
"data": { "lhs": "year", "op": "<", "rhs": 2000 },
"topic": "",
"user_id": 1,
"message": ""
}'
返回一个键值为字符串且包含值的文档向量。区分大小写。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MATCH",
"tb": "car",
"doc": "",
"data": { "lhs": "model", "op": "contains", "rhs": "AMG" },
"topic": "",
"user_id": 1,
"message": ""
}'
返回一个键值为字符串且包含值的文档向量。不区分大小写。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "MATCH",
"tb": "car",
"doc": "",
"data": { "lhs": "model", "op": "icontains", "rhs": "amg" },
"topic": "",
"user_id": 1,
"message": ""
}'
INFO
获取数据库中所有表的键
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "INFO",
"tb": "",
"doc": "",
"data": {},
"topic": "",
"user_id": 1,
"message": ""
}'
ADD
如果表不存在,则添加表
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "ADD",
"tb": "car",
"doc": "",
"data": {},
"topic": "",
"user_id": 1,
"message": ""
}'
PUSH
将值推送到由键指定的数组中。
如果键存在且不是数组,将返回错误。如果键不存在,将创建一个新的数组并将键值对插入到文档中。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "PUSH",
"tb": "car",
"doc": "car:1",
"data": { "key":"parts", "value":"engine" },
"topic": "",
"user_id": 1,
"message": ""
}'
PUT
将值放入指定的键。
如果键存在,则将覆盖值。如果键不存在,则将键值对插入到文档中。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "PUT",
"tb": "car",
"doc": "car:1",
"data": { "key":"owner", "value":"person:1" },
"topic": "",
"user_id": 1,
"message": ""
}'
REL
使用指定的关系关联两个文档。
关系作为"rel:"键中的数组添加,可用于BFS和DFS。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "REL",
"tb": "person",
"doc": "person:1",
"data": { "rel_tb":"person", "rel_doc":"person:2", "rel":"like" },
"topic": "",
"user_id": 1,
"message": ""
}'
BFS
执行带有指定关系的BFS搜索以查找目标文档。
响应返回包含目标的第一节点,或者在未找到时返回错误信息。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "BFS",
"tb": "person",
"doc": "person:1",
"data": { "target":"person:2", "rel":"like" },
"topic": "",
"user_id": 1,
"message": ""
}'
DFS
执行带有指定关系的DFS搜索以查找目标文档。
响应返回包含目标的第一节点,或者在未找到时返回错误信息。
curl -X POST 'http://127.0.0.1:8000/sql' -H 'Content-Type: application/json' -d '{
"stmt": "DFS",
"tb": "person",
"doc": "person:1",
"data": { "target":"person:2", "rel":"like" },
"topic": "",
"user_id": 1,
"message": ""
}'
用户认证
以以下格式向http://127.0.0.1:8000/auth发送POST请求
{
"event": "String",
"username": "String",
"password": "String",
}
SignIn
返回JSON Web Token
curl -X POST 'http://127.0.0.1:8000/auth' -H 'Content-Type: application/json' -d '{
"event": "signin",
"username": "[email protected]",
"password": "abc123"
}'
SignUp
返回JSON Web Token
curl -X POST 'http://127.0.0.1:8000/auth' -H 'Content-Type: application/json' -d '{
"event": "signup",
"username": "[email protected]",
"password": "abc123"
}'
SignOut
curl -X POST 'http://127.0.0.1:8000/auth' -H 'Content-Type: application/json' -H 'Authorization: Bearer <JWT>' -d '{
"event": "signout",
"username": "",
"password": ""
}'
Websocket连接
以JSON有效负载{ "user_id": i32 }向http://127.0.0.1:8000/register发送POST请求
curl -X POST 'http://127.0.0.1:8000/register' -H 'Content-Type: application/json' -d '{ "user_id": 1 }'
响应将是客户端的websocket连接URL
{
"url": "ws://127.0.0.1:8000/ws/4cc7c8ba4c144788a0e9bf9ee6840489"
}
连接到websocket URL,并从websocket客户端添加订阅主题,作为JSON有效负载{ "topics": Vec< String > }
{ "topics": ["dogs", "sports"] }
通过向http://127.0.0.1:8000/publish发送POST请求将消息发送到主题通道,JSON有效负载为{ "user_id": i32, "message": String, topic: String }
{"user_id": "4cc7c8ba4c144788a0e9bf9ee6840489", "topic": "sports", "message": "Hello, world!"}
订阅
您可以对表、文档或键中的变更进行订阅。给定表"person",文档"person:1"和键"phone",以下订阅将在变更时接收到更新的文档。将JSON对象作为消息发送到websocket。
订阅表中的所有变更
{ "topics": ["person"] }
订阅文档person:1中的所有变更
{ "topics": ["person:1"] }
订阅对person:1文档的键"phone"所做的所有变更
{ "topics": ["person:1:phone"] }
注销客户端
向http://127.0.0.1:8000/register/{user_id}发送DELETE请求
curl -X DELETE 'http://127.0.0.1:8000/register/{user_id}'
端点
- 管理控制台:GET / 从Web UI管理数据库
- SQL API:POST /sql
- 认证API:POST /auth
- 发布API:POST /publish 发布消息到订阅者
- 健康API:POST /health 检查服务器状态
- 注册:POST /register 返回连接到websocket的端点
- WebSocket服务器 ws://127.0.0.1:8000/ws/{id}
依赖项
~16–31MB
~516K SLoC