tms-koa-bucket
v0.1.2
Published
tms-koa实现基于bucket的租户分离
Downloads
145
Readme
通过创建 bucket,实现数据逻辑隔离。
环境变量
| 环境变量 | 说明 | 默认值 | | ---------------------------------------- | --------------------------------------------------------------------------------- | --------- | | TMS_KOA_BUCKET_DB | 存放管理数据的数据库。 | tms_admin | | TMS_KOA_META_ADMIN_DB | 存放管理数据的数据库。 | tms_admin | | TMS_KOA_BUCKET_CL | 存放空间管理数据的集合。 | bucket | | TMS_KOA_BUCKET_COWORKER_CL | 存放空间要求管理数据的集合。 | bucket | | TMS_KOA_BUCKET_COWORKER_INVITE_EXPIRE_MS | 邀请有效期。 | 30 分钟 | | TMS_KOA_BUCKET_COWORKER_USERNAME_AS_ID | 用户名称与用户 ID 一致,并且作为接受邀请检查条件。可选值:no/yes. | yes | | TMS_KOA_BUCKET_RANDOM_NAME_SIZE | 自动生成的空间名称的长度。如果不指定,就不自动生成。最少 6 位,建议设置为 21 位。 | 无 |
api
新建 bucket
curl 'http://localhost:3009/api/bucket/admin/bucket/create' -H 'content-type: application/json' -H 'authorization: Bearer accesstoken01' -d '{"name": "test01", "title": "测试01"}'{
"msg": "正常",
"code": 0,
"result": {
"name": "test01",
"title": "测试01",
"creator": "app01",
"createAt": "2025-04-04T09:09:40.299Z",
"_id": "67efa1d44b21209ee232dbd8"
}
}注意:createAt是 ISO 格式,差 8 小时。
查看用户的 bucket
curl 'http://localhost:3009/api/bucket/admin/bucket/list' -H 'content-type: application/json' -H 'authorization: Bearer accesstoken01' -d '{"name": "test01", "title": "测试01"}'{
"msg": "正常",
"code": 0,
"result": [
{
"_id": "67ecf83aa9aae9785a138db9",
"name": "test01",
"title": "测试01",
"creator": "app01",
"createAt": "2025-04-04T09:09:20.199Z"
},
{
"_id": "67efa1d44b21209ee232dbd8",
"name": "test02",
"title": "测试02",
"creator": "app01",
"createAt": "2025-04-04T09:09:40.299Z"
}
]
}包含用户创建的空间和被邀请加入的空间。
更新 bucket
curl 'http://localhost:3009/api/bucket/admin/bucket/update?bucket=test01' -H 'content-type: application/json' -H 'authorization: Bearer accesstoken01' -d '{"title": "测试01_1"}'{ "msg": "正常", "code": 0, "result": { "title": "测试05_1" } }注意:无法修改name和creator字段。
删除 bucket
curl 'http://localhost:3009/api/bucket/admin/bucket/remove?bucket=test01' -H 'authorization: Bearer accesstoken01'{ "msg": "正常", "code": 0, "result": "ok" }邀请加入 bucket
curl 'http://localhost:3009/api/bucket/admin/coworker/invite?bucket=test01' -H "authorization: Bearer accesstoken01" -H 'content-type: application/json' -d '{"username": "alice"}'注意:只允许空间的创建人发起邀请。
{ "msg": "正常", "code": 0, "result": "xxxx" }查看所有邀请
curl 'http://localhost:3009/api/bucket/admin/coworker/list?bucket=test01' -H 'authorization: Bearer accesstoken01'{
"msg": "正常",
"code": 0,
"result": [
{
"_id": "67ef95c71c3b85fb3140b2da",
"inviter": "app01",
"bucket": "test01",
"code": "l5AF",
"createAt": "2025-04-04T08:18:15.431Z",
"coworker": {
"username": "alice",
"id": "alice"
},
"acceptAt": "2025-04-04T08:18:34.737Z"
}
]
}接受邀请
curl 'http://localhost:3009/api/bucket/admin/coworker/accept?bucket=test01' -H 'content-type: application/json' -H "authorization: Bearer $access_token" -d '{"code": "98Ef", "username": "alice"}'{ "msg": "正常", "code": 0, "result": "ok" }查看空间(含被邀请的)
curl 'http://localhost:3009/api/bucket/admin/bucket/list' -H "authorization: Bearer $access_token"{
"msg": "正常",
"code": 0,
"result": [
{
"name": "test01",
"title": "测试01",
"creator": "app01",
"createAt": "2025-04-04T09:09:30.199Z"
},
{
"name": "test05",
"title": "测试05",
"creator": "app01",
"createAt": "2025-04-04T09:09:40.299Z"
}
]
}注意:没有提供bucket的_id。
取消邀请
curl 'http://localhost:3009/api/bucket/admin/coworker/remove?bucket=test01&username=alice' -H 'authorization: Bearer accesstoken01'{ "msg": "正常", "code": 0, "result": "ok" }自定义属性
配置文件bucket.js
export default {
disabled: true,
schemas: {},
schemasRootName: '',
}