bjchy-udg-sdk 1.4.5 → 1.5.0
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 +10 -2
- package/package.json +1 -1
- package/src/collection.js +51 -0
package/README.md
CHANGED
|
@@ -303,9 +303,17 @@ await db.collection('users').doc(id).update({ data: { score: _.inc(10), tags: _.
|
|
|
303
303
|
// 删除(filter 必须非空,防整表误删)
|
|
304
304
|
await db.collection('users').where({ status: 9 }).remove() // → { stats: { removed } }
|
|
305
305
|
await db.collection('users').doc(id).remove()
|
|
306
|
+
|
|
307
|
+
// set(upsert):有则更新、无则新增 → { _id, stats: { created, updated } }
|
|
308
|
+
await db.collection('users').doc('u10086').set({ data: { name: 'alice', status: 1 } })
|
|
309
|
+
await db.collection('users').set({ id: 'u10086', data: { name: 'alice' } })
|
|
310
|
+
// MySQL / Oracle 也可显式传主键列名(Gateway 自动查表主键校验)
|
|
311
|
+
await db.collection('users').set({ user_id: '10086', data: { name: 'alice' } })
|
|
306
312
|
```
|
|
307
313
|
|
|
308
|
-
|
|
314
|
+
`set` 语义:MongoDB / CloudBase **整文档替换**(与微信云 `doc.set` 一致);MySQL / Oracle 按表**主键列** upsert(自动从数据字典读取主键,无需配置;复合主键表不支持)。`id` 参数即主键值,不必知道列名;也可传 `user_id` 等主键列名显式指定。
|
|
315
|
+
|
|
316
|
+
用户登录(非 apiKey)时,Gateway 在 `add` / `set` 时自动写入 `_openid`(取自 token 的 `openId`,覆盖客户端传入值);`apiKey` 登录不注入,无需在 `data` 里手动传 `_openid`。
|
|
309
317
|
|
|
310
318
|
### 批量写入的事务保证(≤5000 条)
|
|
311
319
|
|
|
@@ -395,7 +403,7 @@ if (client.now() < new Date(activity.bookEnd)) { /* 可以报名 */ }
|
|
|
395
403
|
|
|
396
404
|
## 返回与错误
|
|
397
405
|
|
|
398
|
-
返回兼容微信云开发:`get → {data}`、`count → {total}`、`aggregate → {list}`、`add → {_id}/{ids}`、`update → {stats:{updated}}`、`remove → {stats:{removed}}`。
|
|
406
|
+
返回兼容微信云开发:`get → {data}`、`count → {total}`、`aggregate → {list}`、`add → {_id}/{ids}`、`set → {_id, stats:{created,updated}}`、`update → {stats:{updated}}`、`remove → {stats:{removed}}`。
|
|
399
407
|
|
|
400
408
|
失败抛统一错误对象:`{ errorCode, errorMessage, requestId, timestamp }`(如 `UNSUPPORTED_OPERATOR`、`TENANT_NOT_FOUND`、`DB_NOT_FOUND`(dbKey 未在租户 bindings[] 中登记)、`READONLY_DATABASE`(对只读 dbKey 或表级只读的表执行了写操作)、`APPEND_ONLY_TABLE`(表被设为 `append` 只增表,只能 `add`,不能 `update`/`remove`)、`TABLE_ACCESS_DENIED`(表被租户表级权限拒绝:例外模式 denied 的表 / 白名单模式未列出的表,含聚合 `$lookup` 引用的表)、`DUPLICATE_KEY`(409,唯一键冲突;批量写入时整批已回退,可修正数据后整批重发)、`FORBIDDEN`(Token 无效/过期,会自动 refresh 重试)、`TIMEOUT`)。
|
|
401
409
|
|
package/package.json
CHANGED
package/src/collection.js
CHANGED
|
@@ -201,6 +201,27 @@ class DocumentReference {
|
|
|
201
201
|
return { stats: { updated: data.updated || 0 }, errMsg: 'document.update:ok' }
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
+
/**
|
|
205
|
+
* set({ data }) — 有则更新、无则新增(整文档替换,兼容微信云 doc.set)
|
|
206
|
+
*/
|
|
207
|
+
async set(options) {
|
|
208
|
+
const payload = options && options.data
|
|
209
|
+
if (payload === undefined || payload === null) {
|
|
210
|
+
throw new Error('set 需要 data 字段')
|
|
211
|
+
}
|
|
212
|
+
const data = await post(this._ctx, '/api/set', {
|
|
213
|
+
collection: this._collection,
|
|
214
|
+
db: this._db,
|
|
215
|
+
id: this._id,
|
|
216
|
+
data: payload
|
|
217
|
+
})
|
|
218
|
+
return {
|
|
219
|
+
_id: data._id || this._id,
|
|
220
|
+
stats: data.stats || { created: 0, updated: 0 },
|
|
221
|
+
errMsg: 'document.set:ok'
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
204
225
|
async remove() {
|
|
205
226
|
const data = await post(this._ctx, '/api/delete', {
|
|
206
227
|
collection: this._collection,
|
|
@@ -241,6 +262,36 @@ class Collection extends Query {
|
|
|
241
262
|
}
|
|
242
263
|
return { _id: ids[0], id: ids[0], errMsg: 'collection.add:ok' }
|
|
243
264
|
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* set({ id, data }) 或 set({ [主键列名]: id值, data }) — upsert 单条
|
|
268
|
+
* 返回 { _id, stats: { created, updated } }
|
|
269
|
+
*/
|
|
270
|
+
async set(options) {
|
|
271
|
+
if (!options || typeof options !== 'object') {
|
|
272
|
+
throw new Error('set 需要 options 对象')
|
|
273
|
+
}
|
|
274
|
+
const payload = options.data
|
|
275
|
+
if (payload === undefined || payload === null) {
|
|
276
|
+
throw new Error('set 需要 data 字段')
|
|
277
|
+
}
|
|
278
|
+
const body = {
|
|
279
|
+
collection: this._collection,
|
|
280
|
+
db: this._db,
|
|
281
|
+
data: payload
|
|
282
|
+
}
|
|
283
|
+
if (options.id !== undefined) body.id = options.id
|
|
284
|
+
const reserved = new Set(['data', 'id'])
|
|
285
|
+
for (const key of Object.keys(options)) {
|
|
286
|
+
if (!reserved.has(key)) body[key] = options[key]
|
|
287
|
+
}
|
|
288
|
+
const data = await post(this._ctx, '/api/set', body)
|
|
289
|
+
return {
|
|
290
|
+
_id: data._id,
|
|
291
|
+
stats: data.stats || { created: 0, updated: 0 },
|
|
292
|
+
errMsg: 'collection.set:ok'
|
|
293
|
+
}
|
|
294
|
+
}
|
|
244
295
|
}
|
|
245
296
|
|
|
246
297
|
module.exports = { Collection, Query, DocumentReference }
|