@cloudbase/wx-cloud-client-sdk 1.7.1 → 1.8.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 CHANGED
@@ -1,9 +1,155 @@
1
- # 微信云开发客户端 SDK
1
+ # @cloudbase/wx-cloud-client-sdk
2
2
 
3
- ## 使用
3
+ 微信云开发客户端 SDK,提供数据模型、MySQL/RDB 数据库和用户认证能力。
4
4
 
5
- [初始化 SDK](https://docs.cloudbase.net/model/init-sdk)
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @cloudbase/wx-cloud-client-sdk
9
+ ```
10
+
11
+ ## 快速开始
12
+
13
+ ### 初始化
14
+
15
+ ```javascript
16
+ import { init } from '@cloudbase/wx-cloud-client-sdk';
17
+
18
+ // 使用微信云开发实例初始化
19
+ const client = init(wx.cloud, {
20
+ envId: 'your-env-id',
21
+ accessKey: 'your-access-key', // 可选,客户端 Publishable Key
22
+ });
23
+ ```
24
+
25
+ ### 数据模型操作
26
+
27
+ ```javascript
28
+ // 创建数据
29
+ const { data } = await client.models.yourModel.create({
30
+ data: { title: 'Hello', content: 'World' },
31
+ });
32
+
33
+ // 查询列表
34
+ const {
35
+ data: { records, total },
36
+ } = await client.models.yourModel.list({
37
+ filter: { where: { title: { $eq: 'Hello' } } },
38
+ pageSize: 10,
39
+ pageNumber: 1,
40
+ getCount: true,
41
+ });
42
+
43
+ // 更新数据
44
+ await client.models.yourModel.update({
45
+ data: { title: 'Updated' },
46
+ filter: { where: { _id: { $eq: 'record-id' } } },
47
+ });
48
+
49
+ // 删除数据
50
+ await client.models.yourModel.delete({
51
+ filter: { where: { _id: { $eq: 'record-id' } } },
52
+ });
53
+ ```
54
+
55
+ ### MySQL/RDB 数据库
56
+
57
+ ```javascript
58
+ // 获取数据库客户端
59
+ const mysql = client.mysql({ database: 'your-database' });
60
+ // 或使用别名
61
+ const rdb = client.rdb({ database: 'your-database' });
62
+ ```
63
+
64
+ #### 查询数据
65
+
66
+ ```javascript
67
+ // 查询所有字段
68
+ const users = await mysql.from('users').select('*');
69
+
70
+ // 查询指定字段
71
+ const users = await mysql.from('users').select('id, name, email');
72
+
73
+ // 条件查询
74
+ const activeUsers = await mysql.from('users').select('*').eq('status', 'active');
75
+
76
+ // 多条件查询
77
+ const result = await mysql
78
+ .from('users')
79
+ .select('*')
80
+ .eq('status', 'active')
81
+ .gte('age', 18)
82
+ .order('created_at', { ascending: false })
83
+ .limit(10);
84
+ ```
85
+
86
+ #### 插入数据
87
+
88
+ ```javascript
89
+ // 插入单条
90
+ await mysql.from('users').insert({ name: 'test', email: 'test@example.com' });
91
+
92
+ // 插入多条
93
+ await mysql.from('users').insert([
94
+ { name: 'user1', email: 'user1@example.com' },
95
+ { name: 'user2', email: 'user2@example.com' },
96
+ ]);
97
+ ```
98
+
99
+ #### 更新数据
100
+
101
+ ```javascript
102
+ await mysql.from('users').update({ status: 'inactive' }).eq('id', 1);
103
+ ```
104
+
105
+ #### 删除数据
106
+
107
+ ```javascript
108
+ await mysql.from('users').delete().eq('id', 1);
109
+ ```
110
+
111
+ ### 用户认证
112
+
113
+ ```javascript
114
+ // 获取 auth 实例
115
+ const auth = client.auth;
116
+
117
+ // 微信小程序 OpenID 静默登录,用户不存在则注册成外部注册用户
118
+ await auth.signInWithOpenId();
119
+
120
+ // 获取登录状态
121
+ const { data } = auth.getSession();
122
+
123
+ // 登出
124
+ await auth.signOut();
125
+ ```
126
+
127
+ ## API
128
+
129
+ ### init(cloud, options)
130
+
131
+ 初始化 SDK(使用云函数调用)。
132
+
133
+ | 参数 | 类型 | 必填 | 说明 |
134
+ | ----------------- | ----------------- | ---- | ---------------------- |
135
+ | cloud | CloudBaseInstance | 是 | 微信云开发实例 |
136
+ | options.envId | string | 否 | 环境 ID |
137
+ | options.accessKey | string | 否 | 客户端 Publishable Key |
138
+
139
+ ### 数据模型方法
140
+
141
+ - `create(params)` - 创建单条数据
142
+ - `createMany(params)` - 批量创建数据
143
+ - `update(params)` - 更新单条数据
144
+ - `updateMany(params)` - 批量更新数据
145
+ - `upsert(params)` - 创建或更新数据
146
+ - `delete(params)` - 删除单条数据
147
+ - `deleteMany(params)` - 批量删除数据
148
+ - `get(params)` - 获取单条数据
149
+ - `list(params)` - 获取数据列表
150
+ - `runSQLTemplate(params)` - 执行 SQL 模板
6
151
 
7
152
  ## 参考文档
8
153
 
9
- [类型声明](https://docs.cloudbase.net/model/sdk-reference/globals)
154
+ - [初始化 SDK](https://docs.cloudbase.net/model/init-sdk)
155
+ - [类型声明](https://docs.cloudbase.net/model/sdk-reference/globals)
@@ -6,5 +6,5 @@ export declare const enum EQUERY_PARAM_TYPE {
6
6
  OBJECT = "OBJECT",
7
7
  STRING = "STRING"
8
8
  }
9
- export declare const callDataSource: ({ dataSourceName, methodName, params, realMethodName, callFunction, envType, mode, }: CallDataSourceParams) => Promise<MethodResponse<any>>;
10
- export declare const runMysqlCommand: ({ sql, params, config, callFunction, unsafe }: RunMysqlCommandParams) => Promise<MethodResponse<any>>;
9
+ export declare const callDataSource: ({ dataSourceName, methodName, params, realMethodName, callFunction, envType, mode, cloud, }: CallDataSourceParams) => Promise<MethodResponse<any>>;
10
+ export declare const runMysqlCommand: ({ sql, params, config, callFunction, unsafe, cloud, }: RunMysqlCommandParams) => Promise<MethodResponse<any>>;
@@ -0,0 +1,6 @@
1
+ import { CloudBaseInstance } from '../types';
2
+ import { Auth } from '@cloudbase/auth';
3
+ export declare const generateAuthClient: (cloud: CloudBaseInstance, options?: {
4
+ envId?: string;
5
+ accessKey?: string;
6
+ }) => Auth;
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
+ export { getAccessToken } from './utils';
5
6
  export declare function initHTTPOverCallFunction(cloud: CloudBaseInstance, options?: {
6
7
  envId?: string;
7
8
  baseUrl?: string;
@@ -10,6 +11,7 @@ export declare function initHTTPOverCallFunction(cloud: CloudBaseInstance, optio
10
11
  }): ExtendedCloudBaseInstance;
11
12
  export declare function init(cloud: CloudBaseInstance, options?: {
12
13
  envId?: string;
14
+ accessKey?: string;
13
15
  }): ExtendedCloudBaseInstance;
14
16
  declare function generateMySQLClient(cloud: CloudBaseInstance, options?: {
15
17
  envId?: string;
@@ -22,6 +24,7 @@ declare const _default: {
22
24
  init: typeof init;
23
25
  generateHTTPClient: (callFunction: import("./types").CallFunction, fetch: (options: import("@cloudbase/adapter-interface").IFetchOptions) => any, baseUrl: string, options?: {
24
26
  sqlBaseUrl?: string | undefined;
27
+ cloud?: CloudBaseInstance | undefined;
25
28
  } | undefined) => OrmClient;
26
29
  generateMySQLClient: typeof generateMySQLClient;
27
30
  };
@@ -1,4 +1,4 @@
1
- import { OrmClient, CallFunction, ModelFetch } from '../types';
1
+ import { OrmClient, CallFunction, ModelFetch, CloudBaseInstance } from '../types';
2
2
  export declare const enum EQUERY_PARAM_TYPE {
3
3
  ARRAY = "ARRAY",
4
4
  BOOLEAN = "BOOLEAN",
@@ -8,4 +8,5 @@ export declare const enum EQUERY_PARAM_TYPE {
8
8
  }
9
9
  export declare const generateHTTPClient: (callFunction: CallFunction, fetch: ModelFetch, baseUrl: string, options?: {
10
10
  sqlBaseUrl?: string;
11
+ cloud?: CloudBaseInstance;
11
12
  }) => OrmClient;
@@ -1,3 +1,3 @@
1
- import { OrmClient, CallFunction } from '../types';
2
- export declare const generateClientByDataSourceName: (dataSourceName: string, callFunction: CallFunction) => OrmClient;
3
- export declare const generateClient: (callFunction: CallFunction) => OrmClient;
1
+ import { OrmClient, CallFunction, CloudBaseInstance } from '../types';
2
+ export declare const generateClientByDataSourceName: (dataSourceName: string, callFunction: CallFunction, cloud?: CloudBaseInstance) => OrmClient;
3
+ export declare const generateClient: (callFunction: CallFunction, cloud?: CloudBaseInstance) => OrmClient;
@@ -1,2 +1,2 @@
1
- import type { CallFunction, OrmRawQueryClient } from '../types';
2
- export declare const createRawQueryClient: (callFunction: CallFunction) => OrmRawQueryClient;
1
+ import type { CallFunction, CloudBaseInstance, OrmRawQueryClient } from '../types';
2
+ export declare const createRawQueryClient: (callFunction: CallFunction, cloud?: CloudBaseInstance) => OrmRawQueryClient;
@@ -1,5 +1,6 @@
1
1
  import { SDKRequestInterface } from '@cloudbase/adapter-interface';
2
2
  import { MySqlClient } from '../db/mysql';
3
+ import { Auth } from '@cloudbase/auth';
3
4
  /**
4
5
  * 基础 Model 类型定义
5
6
  */
@@ -690,6 +691,10 @@ export interface CallDataSourceParams {
690
691
  * @deprecated 使用 dataSourceName 替代。
691
692
  */
692
693
  mode?: string;
694
+ /**
695
+ * 云实例,用于执行云函数调用。
696
+ */
697
+ cloud?: CloudBaseInstance;
693
698
  }
694
699
  /**
695
700
  * 调用runMySQLCoommand 参数的结构定义,用于封装调用云函数时所需的参数。
@@ -701,6 +706,7 @@ export interface RunMysqlCommandParams {
701
706
  callFunction: CallFunction;
702
707
  config?: any;
703
708
  unsafe?: boolean;
709
+ cloud?: CloudBaseInstance;
704
710
  }
705
711
  /**
706
712
  * 云函数调用接口,包含调用函数和认证信息。
@@ -718,6 +724,8 @@ export type IMySqlClient = (options?: IMySqlOptions) => MySqlClient;
718
724
  export interface ExtendedCloudBaseInstance extends CloudBaseInstance {
719
725
  models: OrmClient & OrmRawQueryClient;
720
726
  mysql: IMySqlClient;
727
+ rdb: IMySqlClient;
728
+ auth: Auth;
721
729
  }
722
730
  /**
723
731
  * 云函数调用方法定义。
package/lib/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { CloudBaseInstance } from './types';
1
2
  /**
2
3
  * 获取全局对象 window
3
4
  * 小程序中可用, 但小程序中对象信息残缺, 无法访问 navigator 对象, ua 信息也无意义
@@ -9,3 +10,4 @@ export declare function getReferrer(): any;
9
10
  export declare function getUserAgent(): any;
10
11
  export declare const VERSION: string | undefined;
11
12
  export declare function getRandomString(): string;
13
+ export declare const getAccessToken: (cloud?: CloudBaseInstance) => Promise<string>;