bjchy-udg-sdk 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# UDG SDK
|
|
2
2
|
|
|
3
|
-
跨端统一数据访问 SDK(微信小程序 / Web / Node.js ≥18),API 兼容微信云开发 `wx.cloud.database()`,连接 UDG Gateway 访问 CloudBase
|
|
3
|
+
跨端统一数据访问 SDK(微信小程序 / Web / Node.js ≥18),API 兼容微信云开发 `wx.cloud.database()`,连接 UDG Gateway 访问 CloudBase / MongoDB / MySQL / Oracle 业务库。零运行时依赖。
|
|
4
|
+
|
|
5
|
+
> 本文档即线上 `GET {endpoint}/llms.txt`(llms.txt 约定,AI 开发工具可直接抓取;`/llms-full.txt` 为同一内容的兼容别名)。人类可读版:`{endpoint}/admin/sdk-guide.html`。
|
|
4
6
|
|
|
5
7
|
## 安装 / 引入
|
|
6
8
|
|
|
@@ -24,7 +26,7 @@ npm install bjchy-udg-sdk
|
|
|
24
26
|
|
|
25
27
|
### 发版(维护者)
|
|
26
28
|
|
|
27
|
-
`sdk/` 有代码改动 push 到 master 时,`.githooks/pre-push` 自动跑 `scripts/publish-sdk.sh
|
|
29
|
+
`sdk/` 有代码改动 push 到 master 时,`.githooks/pre-push` 自动跑 `scripts/publish-sdk.sh`:**自动算出一个比当前 npm 最新版本高一个 patch 的版本号并 `npm publish`**,不需要手动改 `sdk/package.json` 的 `version`。这个 `version` 字段只是仓库里的"基线"——平时不用管,只有想主动打一个语义化更明显的 minor/major 版本时才手动改,改了之后自动发布会以这个更高的号为准。详见 `docs/bjchy-udg-sdk-npm-publish.md`。
|
|
28
30
|
|
|
29
31
|
新克隆本仓库、要参与 SDK 开发的机器,需执行一次 `scripts/setup-hooks.sh` 激活 `.githooks/`。
|
|
30
32
|
|
|
@@ -168,16 +170,46 @@ const { list } = await db.collection('orders').aggregate()
|
|
|
168
170
|
.end()
|
|
169
171
|
```
|
|
170
172
|
|
|
171
|
-
支持 stage:`match group project sort limit skip count lookup unwind addFields replaceRoot sample sortByCount`。stage 内表达式使用 MongoDB 原生写法(`$sum` / `$cond` / `$dateToString`
|
|
173
|
+
支持 stage:`match group project sort limit skip count lookup unwind addFields replaceRoot sample sortByCount`。stage 内表达式使用 MongoDB 原生写法(`$sum` / `$cond` / `$dateToString` …),各数据源写法一致;MySQL / Oracle 支持其中核心子集(`match group project addFields sort limit skip count`),矩阵外直接报错。
|
|
174
|
+
|
|
175
|
+
## 实时订阅(SSE)
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
const watcher = db.collection('logs')
|
|
179
|
+
.where({ status: 'waiting' }) // 可选:带条件订阅
|
|
180
|
+
.watch({
|
|
181
|
+
onChange: (evt) => { /* insert / update / delete 变更事件 */ },
|
|
182
|
+
onError: (err) => { /* 断线等错误(内部已自动重连) */ }
|
|
183
|
+
})
|
|
184
|
+
watcher.close() // 退出页面时关闭
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
基于 SSE:经 Gateway 的全部写入(四类数据源)都会推送;外部直连写入在 MongoDB(Change Stream)/ CloudBase(原生 watch)上补充监听,MySQL / Oracle 暂不支持外部监听。`onChange` 首个 `watch-status` 事件的 `external` 字段指示外部监听状态(`active | degraded | unsupported`)。
|
|
188
|
+
|
|
189
|
+
运行环境:浏览器 EventSource / Node ≥ 18;微信小程序不支持(抛 `ENV_NOT_SUPPORTED`,请用轮询 / 定时 get 代替)。
|
|
190
|
+
|
|
191
|
+
扫码登录专用:`client.subscribeScanLogin(sessionId)`——SSE 推送确认结果(亚秒级),SSE 不可用时自动降级为 `pollScanLogin` 轮询,返回值语义一致。
|
|
192
|
+
|
|
193
|
+
## 运行时自查表结构
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
POST {endpoint}/api/describe
|
|
197
|
+
Authorization: Bearer <token>
|
|
198
|
+
body: { "db": "<dbKey>", "collection": "<表名>" }
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
返回该表的字段/类型/中文说明/枚举样本(来自管理后台维护的数据字典);也可在管理后台「导出开发文档」得到完整 Markdown 文档包。
|
|
202
|
+
|
|
203
|
+
**配套 MCP Server**(UDG 仓库 `mcp/` 目录):只读工具 `udg_list_collections`(列可访问的表)、`udg_describe_collection`(查表结构)、`udg_query` / `udg_aggregate`(只读试查询)、`udg_nl_query`(自然语言查询),环境变量 `UDG_ENDPOINT` / `UDG_APPID` / `UDG_APIKEY`,适合接入 Claude Code / Cursor 等 AI 开发工具。
|
|
172
204
|
|
|
173
205
|
## 返回与错误
|
|
174
206
|
|
|
175
207
|
返回兼容微信云开发:`get → {data}`、`count → {total}`、`aggregate → {list}`、`add → {_id}/{ids}`、`update → {stats:{updated}}`、`remove → {stats:{removed}}`。
|
|
176
208
|
|
|
177
|
-
失败抛统一错误对象:`{ errorCode, errorMessage, requestId, timestamp }`(如 `UNSUPPORTED_OPERATOR`、`TENANT_NOT_FOUND`、`DB_NOT_FOUND`(dbKey 未在租户 bindings[] 中登记)、`READONLY_DATABASE`(对只读 dbKey
|
|
209
|
+
失败抛统一错误对象:`{ errorCode, errorMessage, requestId, timestamp }`(如 `UNSUPPORTED_OPERATOR`、`TENANT_NOT_FOUND`、`DB_NOT_FOUND`(dbKey 未在租户 bindings[] 中登记)、`READONLY_DATABASE`(对只读 dbKey 或表级只读的表执行了写操作)、`TABLE_ACCESS_DENIED`(表被租户表级权限拒绝:例外模式 denied 的表 / 白名单模式未列出的表,含聚合 `$lookup` 引用的表)、`FORBIDDEN`(Token 无效/过期,会自动 refresh 重试)、`TIMEOUT`)。
|
|
178
210
|
|
|
179
211
|
## 说明
|
|
180
212
|
|
|
181
213
|
- 日期:SDK 与 Gateway 以 `{ $date: 毫秒 }` 传输,业务代码直接用 `Date` 对象读写即可
|
|
182
214
|
- `db.serverDate()` V1 以客户端时间近似
|
|
183
|
-
- distinct /
|
|
215
|
+
- distinct / 文件上传:V1 不提供
|