@cloudbase/wx-cloud-client-sdk 1.8.3 → 1.8.4
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 +207 -36
- package/lib/auth/index.d.ts +3 -1
- package/lib/index.d.ts +2 -0
- package/lib/types/index.d.ts +2 -2
- package/lib/wxCloudClientSDK.cjs.js +9 -1
- package/lib/wxCloudClientSDK.esm.js +9 -1
- package/lib/wxCloudClientSDK.umd.js +9 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -12,31 +12,70 @@ npm install @cloudbase/wx-cloud-client-sdk
|
|
|
12
12
|
|
|
13
13
|
### 初始化
|
|
14
14
|
|
|
15
|
+
SDK 提供两种初始化方式:
|
|
16
|
+
|
|
17
|
+
#### 1. 微信云开发中使用(默认)
|
|
18
|
+
|
|
15
19
|
```javascript
|
|
16
20
|
import { init } from '@cloudbase/wx-cloud-client-sdk';
|
|
17
21
|
|
|
18
22
|
// 使用微信云开发实例初始化
|
|
19
23
|
const client = init(wx.cloud, {
|
|
20
|
-
envId: 'your-env-id',
|
|
24
|
+
envId: 'your-env-id', // 可选,当 cloud 为 new wx.cloud.Cloud() 实例时需要提供
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
#### 2. NodeJs 服务端使用
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
const adapter = require('@cloudbase/adapter-node');
|
|
32
|
+
const cloudbase = require('@cloudbase/js-sdk'); // 内置 @cloudbase/wx-cloud-client-sdk
|
|
33
|
+
cloudbase.useAdapters(adapter);
|
|
34
|
+
|
|
35
|
+
const client = cloudbase.init({
|
|
36
|
+
env,
|
|
21
37
|
});
|
|
22
38
|
```
|
|
23
39
|
|
|
40
|
+
初始化后,`client` 将拥有以下属性:
|
|
41
|
+
|
|
42
|
+
- `client.models` - 数据模型操作接口
|
|
43
|
+
- `client.mysql()` / `client.rdb()` - MySQL/RDB 数据库客户端
|
|
44
|
+
- `client.auth` - 用户认证接口
|
|
45
|
+
|
|
24
46
|
### 数据模型操作
|
|
25
47
|
|
|
48
|
+
数据模型提供完整的 CRUD 操作,支持生产环境 (`envType: 'prod'`) 和预发环境 (`envType: 'pre'`)。
|
|
49
|
+
|
|
26
50
|
```javascript
|
|
27
51
|
// 创建数据
|
|
28
52
|
const { data } = await client.models.yourModel.create({
|
|
29
53
|
data: { title: 'Hello', content: 'World' },
|
|
54
|
+
envType: 'prod', // 可选,默认 'prod'
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// 批量创建
|
|
58
|
+
const { data } = await client.models.yourModel.createMany({
|
|
59
|
+
data: [
|
|
60
|
+
{ title: 'Post 1', content: 'Content 1' },
|
|
61
|
+
{ title: 'Post 2', content: 'Content 2' },
|
|
62
|
+
],
|
|
30
63
|
});
|
|
31
64
|
|
|
32
|
-
//
|
|
65
|
+
// 查询单条数据
|
|
66
|
+
const { data } = await client.models.yourModel.get({
|
|
67
|
+
filter: { where: { _id: { $eq: 'record-id' } } },
|
|
68
|
+
select: { $master: true }, // 默认包含主模型字段
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// 查询列表(支持分页、总数)
|
|
33
72
|
const {
|
|
34
73
|
data: { records, total },
|
|
35
74
|
} = await client.models.yourModel.list({
|
|
36
75
|
filter: { where: { title: { $eq: 'Hello' } } },
|
|
37
76
|
pageSize: 10,
|
|
38
77
|
pageNumber: 1,
|
|
39
|
-
getCount: true,
|
|
78
|
+
getCount: true, // 获取总数
|
|
40
79
|
});
|
|
41
80
|
|
|
42
81
|
// 更新数据
|
|
@@ -45,18 +84,49 @@ await client.models.yourModel.update({
|
|
|
45
84
|
filter: { where: { _id: { $eq: 'record-id' } } },
|
|
46
85
|
});
|
|
47
86
|
|
|
87
|
+
// 批量更新
|
|
88
|
+
await client.models.yourModel.updateMany({
|
|
89
|
+
data: { status: 'published' },
|
|
90
|
+
filter: { where: { status: { $eq: 'draft' } } },
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// 创建或更新(upsert)
|
|
94
|
+
await client.models.yourModel.upsert({
|
|
95
|
+
data: { title: 'New or Updated' },
|
|
96
|
+
filter: { where: { _id: { $eq: 'record-id' } } },
|
|
97
|
+
});
|
|
98
|
+
|
|
48
99
|
// 删除数据
|
|
49
100
|
await client.models.yourModel.delete({
|
|
50
101
|
filter: { where: { _id: { $eq: 'record-id' } } },
|
|
51
102
|
});
|
|
103
|
+
|
|
104
|
+
// 批量删除
|
|
105
|
+
await client.models.yourModel.deleteMany({
|
|
106
|
+
filter: { where: { status: { $eq: 'deleted' } } },
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// 执行 SQL 模板
|
|
110
|
+
const { data } = await client.models.yourModel.runSQLTemplate({
|
|
111
|
+
templateId: 'your-template-id',
|
|
112
|
+
parameters: { param1: 'value1' },
|
|
113
|
+
});
|
|
52
114
|
```
|
|
53
115
|
|
|
54
116
|
### MySQL/RDB 数据库
|
|
55
117
|
|
|
118
|
+
MySQL/RDB 客户端基于 PostgREST 风格,提供链式查询构建器。
|
|
119
|
+
|
|
120
|
+
#### 获取数据库客户端
|
|
121
|
+
|
|
56
122
|
```javascript
|
|
57
|
-
//
|
|
58
|
-
const mysql = client.mysql({
|
|
59
|
-
//
|
|
123
|
+
// 获取 MySQL 客户端(mysql 和 rdb 是别名), 参数为可选
|
|
124
|
+
const mysql = client.mysql({
|
|
125
|
+
database: 'your-database', // 数据库名,默认为环境 ID
|
|
126
|
+
instance: 'default', // 实例名,默认为 'default'
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// 或使用 rdb 别名, 参数为可选
|
|
60
130
|
const rdb = client.rdb({ database: 'your-database' });
|
|
61
131
|
```
|
|
62
132
|
|
|
@@ -69,46 +139,74 @@ const users = await mysql.from('users').select('*');
|
|
|
69
139
|
// 查询指定字段
|
|
70
140
|
const users = await mysql.from('users').select('id, name, email');
|
|
71
141
|
|
|
72
|
-
//
|
|
73
|
-
const activeUsers = await mysql
|
|
142
|
+
// 条件查询(支持 eq、neq、gt、gte、lt、lte、in、like 等)
|
|
143
|
+
const activeUsers = await mysql
|
|
144
|
+
.from('users')
|
|
145
|
+
.select('*')
|
|
146
|
+
.eq('status', 'active')
|
|
147
|
+
.neq('role', 'admin')
|
|
148
|
+
.gt('age', 18)
|
|
149
|
+
.lt('age', 65)
|
|
150
|
+
.like('name', '%John%')
|
|
151
|
+
.in('id', [1, 2, 3]);
|
|
74
152
|
|
|
75
|
-
//
|
|
153
|
+
// 排序和分页
|
|
76
154
|
const result = await mysql
|
|
77
155
|
.from('users')
|
|
78
156
|
.select('*')
|
|
79
157
|
.eq('status', 'active')
|
|
80
158
|
.gte('age', 18)
|
|
81
159
|
.order('created_at', { ascending: false })
|
|
82
|
-
.limit(10)
|
|
160
|
+
.limit(10)
|
|
161
|
+
.range(0, 9); // 范围查询
|
|
162
|
+
|
|
163
|
+
// 计数查询
|
|
164
|
+
const { count } = await mysql.from('users').select('*', { count: 'exact', head: true }).eq('status', 'active');
|
|
83
165
|
```
|
|
84
166
|
|
|
85
167
|
#### 插入数据
|
|
86
168
|
|
|
87
169
|
```javascript
|
|
88
170
|
// 插入单条
|
|
89
|
-
await mysql.from('users').insert({
|
|
171
|
+
await mysql.from('users').insert({
|
|
172
|
+
name: 'test',
|
|
173
|
+
email: 'test@example.com',
|
|
174
|
+
});
|
|
90
175
|
|
|
91
176
|
// 插入多条
|
|
92
177
|
await mysql.from('users').insert([
|
|
93
178
|
{ name: 'user1', email: 'user1@example.com' },
|
|
94
179
|
{ name: 'user2', email: 'user2@example.com' },
|
|
95
180
|
]);
|
|
181
|
+
|
|
182
|
+
// 插入并返回数据
|
|
183
|
+
const { data } = await mysql.from('users').insert({ name: 'test', email: 'test@example.com' }).select();
|
|
96
184
|
```
|
|
97
185
|
|
|
98
186
|
#### 更新数据
|
|
99
187
|
|
|
100
188
|
```javascript
|
|
101
|
-
|
|
189
|
+
// 更新并返回更新后的数据
|
|
190
|
+
const { data } = await mysql.from('users').update({ status: 'inactive' }).eq('id', 1).select();
|
|
191
|
+
|
|
192
|
+
// 批量更新
|
|
193
|
+
await mysql.from('users').update({ status: 'inactive' }).in('id', [1, 2, 3]);
|
|
102
194
|
```
|
|
103
195
|
|
|
104
196
|
#### 删除数据
|
|
105
197
|
|
|
106
198
|
```javascript
|
|
107
|
-
|
|
199
|
+
// 删除并返回删除的数据
|
|
200
|
+
const { data } = await mysql.from('users').delete().eq('id', 1).select();
|
|
201
|
+
|
|
202
|
+
// 批量删除
|
|
203
|
+
await mysql.from('users').delete().in('id', [1, 2, 3]);
|
|
108
204
|
```
|
|
109
205
|
|
|
110
206
|
### 用户认证
|
|
111
207
|
|
|
208
|
+
认证模块提供微信小程序 OpenID 静默登录和用户信息管理。在 NodeJs 端提供更多的登录方式,参考[JS SDK 文档](https://docs.cloudbase.net/api-reference/webv3/authentication)。
|
|
209
|
+
|
|
112
210
|
```javascript
|
|
113
211
|
// 获取 auth 实例
|
|
114
212
|
const auth = client.auth;
|
|
@@ -116,19 +214,24 @@ const auth = client.auth;
|
|
|
116
214
|
// 微信小程序 OpenID 静默登录,用户不存在则注册成外部注册用户
|
|
117
215
|
await auth.signInWithOpenId();
|
|
118
216
|
|
|
119
|
-
//
|
|
217
|
+
// 更新用户信息(昵称、性别等)
|
|
120
218
|
const { data, error } = await auth.updateUser({
|
|
121
219
|
nickname: '新昵称',
|
|
122
220
|
gender: 'MALE',
|
|
123
221
|
});
|
|
124
222
|
|
|
125
223
|
// 更新 email 或 phone(需要验证)
|
|
126
|
-
const { data, error } = await
|
|
224
|
+
const { data, error } = await auth.updateUser({
|
|
127
225
|
email: 'new@example.com',
|
|
128
226
|
});
|
|
129
227
|
|
|
130
|
-
// 调用 verifyOtp
|
|
131
|
-
|
|
228
|
+
// 调用 verifyOtp 回调验证邮箱/手机
|
|
229
|
+
if (data?.verifyOtp) {
|
|
230
|
+
await data.verifyOtp({
|
|
231
|
+
email: 'new@example.com',
|
|
232
|
+
token: '123456',
|
|
233
|
+
});
|
|
234
|
+
}
|
|
132
235
|
|
|
133
236
|
// 获取当前用户信息
|
|
134
237
|
const { data, error } = await auth.getUser();
|
|
@@ -136,35 +239,31 @@ const { data, error } = await auth.getUser();
|
|
|
136
239
|
// 获取登录状态
|
|
137
240
|
const { data, error } = auth.getSession();
|
|
138
241
|
|
|
139
|
-
// 登出
|
|
140
|
-
await auth.signOut();
|
|
141
|
-
|
|
142
242
|
// 刷新登录状态
|
|
143
243
|
await auth.refreshSession();
|
|
244
|
+
|
|
245
|
+
// 登出
|
|
246
|
+
await auth.signOut();
|
|
144
247
|
```
|
|
145
248
|
|
|
146
|
-
## API
|
|
249
|
+
## API 参考
|
|
147
250
|
|
|
148
|
-
###
|
|
251
|
+
### 初始化函数
|
|
149
252
|
|
|
150
|
-
|
|
253
|
+
#### `init(cloud, options)`
|
|
151
254
|
|
|
152
|
-
|
|
153
|
-
| ------------- | ----------------- | ---- | -------------- |
|
|
154
|
-
| cloud | CloudBaseInstance | 是 | 微信云开发实例 |
|
|
155
|
-
| options.envId | string | 否 | 环境 ID |
|
|
255
|
+
使用云函数调用初始化 SDK。
|
|
156
256
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
- `getSession()` - 获取登录状态
|
|
163
|
-
- `refreshSession()` - 刷新登录状态
|
|
164
|
-
- `signOut()` - 登出
|
|
257
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
258
|
+
| --------------- | ------------------- | ---- | ---------------------------------------------------------- |
|
|
259
|
+
| cloud | `CloudBaseInstance` | 是 | 微信云开发实例 |
|
|
260
|
+
| options.envId | `string` | 否 | 环境 ID,当 cloud 为 `new wx.cloud.Cloud()` 实例时需要提供 |
|
|
261
|
+
| options.adapter | `CloudbaseAdapter` | 否 | 自定义适配器 |
|
|
165
262
|
|
|
166
263
|
### 数据模型方法
|
|
167
264
|
|
|
265
|
+
所有数据模型方法都支持 `envType` 参数(可选,默认 `'prod'`),用于指定操作环境(`'prod'` 或 `'pre'`)。
|
|
266
|
+
|
|
168
267
|
- `create(params)` - 创建单条数据
|
|
169
268
|
- `createMany(params)` - 批量创建数据
|
|
170
269
|
- `update(params)` - 更新单条数据
|
|
@@ -173,10 +272,82 @@ await auth.refreshSession();
|
|
|
173
272
|
- `delete(params)` - 删除单条数据
|
|
174
273
|
- `deleteMany(params)` - 批量删除数据
|
|
175
274
|
- `get(params)` - 获取单条数据
|
|
176
|
-
- `list(params)` -
|
|
275
|
+
- `list(params)` - 获取数据列表(支持分页、过滤、排序)
|
|
177
276
|
- `runSQLTemplate(params)` - 执行 SQL 模板
|
|
178
277
|
|
|
278
|
+
### MySQL/RDB 方法
|
|
279
|
+
|
|
280
|
+
#### 查询构建器方法
|
|
281
|
+
|
|
282
|
+
- `from(tableName)` - 指定表名
|
|
283
|
+
- `select(columns, options)` - 选择字段(支持 `count: 'exact'` 等选项)
|
|
284
|
+
- `eq(column, value)` - 等于
|
|
285
|
+
- `neq(column, value)` - 不等于
|
|
286
|
+
- `gt(column, value)` - 大于
|
|
287
|
+
- `gte(column, value)` - 大于等于
|
|
288
|
+
- `lt(column, value)` - 小于
|
|
289
|
+
- `lte(column, value)` - 小于等于
|
|
290
|
+
- `like(column, pattern)` - LIKE 匹配
|
|
291
|
+
- `ilike(column, pattern)` - ILIKE 不区分大小写匹配
|
|
292
|
+
- `in(column, values)` - IN 查询
|
|
293
|
+
- `is(column, value)` - IS 查询(如 `is('deleted_at', null)`)
|
|
294
|
+
- `or(filters)` - OR 条件
|
|
295
|
+
- `not(filter)` - NOT 条件
|
|
296
|
+
- `order(column, options)` - 排序(`{ ascending: boolean }`)
|
|
297
|
+
- `limit(count)` - 限制返回条数
|
|
298
|
+
- `range(start, end)` - 范围查询
|
|
299
|
+
- `insert(data)` - 插入数据
|
|
300
|
+
- `update(data)` - 更新数据
|
|
301
|
+
- `delete()` - 删除数据
|
|
302
|
+
|
|
303
|
+
### 认证方法
|
|
304
|
+
|
|
305
|
+
- `signInWithOpenId()` - 微信小程序 OpenID 静默登录,用户不存在则注册成外部注册用户
|
|
306
|
+
- `updateUser(params)` - 更新用户信息(返回可能包含 `verifyOtp` 回调)
|
|
307
|
+
- `getUser()` - 获取当前用户信息
|
|
308
|
+
- `getSession()` - 获取登录状态
|
|
309
|
+
- `refreshSession()` - 刷新登录状态
|
|
310
|
+
- `signOut()` - 登出
|
|
311
|
+
|
|
312
|
+
## 类型声明
|
|
313
|
+
|
|
314
|
+
SDK 提供完整的 TypeScript 类型定义。使用时可以导入相关类型:
|
|
315
|
+
|
|
316
|
+
```typescript
|
|
317
|
+
import type {
|
|
318
|
+
CloudBaseInstance,
|
|
319
|
+
ExtendedCloudBaseInstance,
|
|
320
|
+
OrmClient,
|
|
321
|
+
Auth,
|
|
322
|
+
IMySqlOptions,
|
|
323
|
+
} from '@cloudbase/wx-cloud-client-sdk';
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## 错误处理
|
|
327
|
+
|
|
328
|
+
SDK 使用统一的错误类型 `WxCloudSDKError`,包含错误码、请求 ID 和原始错误信息。
|
|
329
|
+
|
|
330
|
+
```javascript
|
|
331
|
+
try {
|
|
332
|
+
await client.models.yourModel.create({ data: {} });
|
|
333
|
+
} catch (error) {
|
|
334
|
+
if (error.code === 'NotSupported') {
|
|
335
|
+
// 处理不支持的操作
|
|
336
|
+
}
|
|
337
|
+
console.error('Request ID:', error.requestId);
|
|
338
|
+
console.error('Original error:', error.originError);
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
## 环境支持
|
|
343
|
+
|
|
344
|
+
- **生产环境** (`envType: 'prod'`) - 默认环境,操作生产数据
|
|
345
|
+
- **预发环境** (`envType: 'pre'`) - 用于测试,操作预发数据
|
|
346
|
+
|
|
347
|
+
可以在每个数据模型操作中通过 `envType` 参数指定环境。
|
|
348
|
+
|
|
179
349
|
## 参考文档
|
|
180
350
|
|
|
181
351
|
- [初始化 SDK](https://docs.cloudbase.net/model/init-sdk)
|
|
182
352
|
- [类型声明](https://docs.cloudbase.net/model/sdk-reference/globals)
|
|
353
|
+
- [微信云开发文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html)
|
package/lib/auth/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { CloudBaseInstance, Auth } from '../types';
|
|
2
|
+
import type { CloudbaseAdapter } from '@cloudbase/adapter-interface';
|
|
2
3
|
export declare const generateAuthClient: (cloud: CloudBaseInstance, options?: {
|
|
3
4
|
envId?: string;
|
|
4
|
-
|
|
5
|
+
adapter?: CloudbaseAdapter;
|
|
6
|
+
}) => Auth | null;
|
package/lib/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { CloudBaseInstance, ExtendedCloudBaseInstance, IMySqlOptions, OrmClient
|
|
|
2
2
|
import { generateHTTPClient } from './orm/http-orm-client';
|
|
3
3
|
import { MySqlClient } from './db/mysql';
|
|
4
4
|
import { Fetch } from './db/postgrest/types';
|
|
5
|
+
import type { CloudbaseAdapter } from '@cloudbase/adapter-interface';
|
|
5
6
|
export { getAccessToken } from './utils';
|
|
6
7
|
export declare function initHTTPOverCallFunction(cloud: CloudBaseInstance, options?: {
|
|
7
8
|
envId?: string;
|
|
@@ -11,6 +12,7 @@ export declare function initHTTPOverCallFunction(cloud: CloudBaseInstance, optio
|
|
|
11
12
|
}): ExtendedCloudBaseInstance;
|
|
12
13
|
export declare function init(cloud: CloudBaseInstance, options?: {
|
|
13
14
|
envId?: string;
|
|
15
|
+
adapter?: CloudbaseAdapter;
|
|
14
16
|
}): ExtendedCloudBaseInstance;
|
|
15
17
|
declare function generateMySQLClient(cloud: CloudBaseInstance, options?: {
|
|
16
18
|
envId?: string;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -714,7 +714,7 @@ export interface RunMysqlCommandParams {
|
|
|
714
714
|
*/
|
|
715
715
|
export type CloudBaseInstance = {
|
|
716
716
|
callFunction: CallFunction;
|
|
717
|
-
auth: Auth;
|
|
717
|
+
auth: Auth | null;
|
|
718
718
|
};
|
|
719
719
|
export type IMySqlClient = (options?: IMySqlOptions) => MySqlClient;
|
|
720
720
|
export interface Auth {
|
|
@@ -733,7 +733,7 @@ export interface ExtendedCloudBaseInstance extends CloudBaseInstance {
|
|
|
733
733
|
models: OrmClient & OrmRawQueryClient;
|
|
734
734
|
mysql: IMySqlClient;
|
|
735
735
|
rdb: IMySqlClient;
|
|
736
|
-
auth: Auth;
|
|
736
|
+
auth: Auth | null;
|
|
737
737
|
}
|
|
738
738
|
/**
|
|
739
739
|
* 云函数调用方法定义。
|