@cloudbase/wx-cloud-client-sdk 1.7.2 → 1.8.1

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,153 @@
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
+ });
22
+ ```
23
+
24
+ ### 数据模型操作
25
+
26
+ ```javascript
27
+ // 创建数据
28
+ const { data } = await client.models.yourModel.create({
29
+ data: { title: 'Hello', content: 'World' },
30
+ });
31
+
32
+ // 查询列表
33
+ const {
34
+ data: { records, total },
35
+ } = await client.models.yourModel.list({
36
+ filter: { where: { title: { $eq: 'Hello' } } },
37
+ pageSize: 10,
38
+ pageNumber: 1,
39
+ getCount: true,
40
+ });
41
+
42
+ // 更新数据
43
+ await client.models.yourModel.update({
44
+ data: { title: 'Updated' },
45
+ filter: { where: { _id: { $eq: 'record-id' } } },
46
+ });
47
+
48
+ // 删除数据
49
+ await client.models.yourModel.delete({
50
+ filter: { where: { _id: { $eq: 'record-id' } } },
51
+ });
52
+ ```
53
+
54
+ ### MySQL/RDB 数据库
55
+
56
+ ```javascript
57
+ // 获取数据库客户端
58
+ const mysql = client.mysql({ database: 'your-database' });
59
+ // 或使用别名
60
+ const rdb = client.rdb({ database: 'your-database' });
61
+ ```
62
+
63
+ #### 查询数据
64
+
65
+ ```javascript
66
+ // 查询所有字段
67
+ const users = await mysql.from('users').select('*');
68
+
69
+ // 查询指定字段
70
+ const users = await mysql.from('users').select('id, name, email');
71
+
72
+ // 条件查询
73
+ const activeUsers = await mysql.from('users').select('*').eq('status', 'active');
74
+
75
+ // 多条件查询
76
+ const result = await mysql
77
+ .from('users')
78
+ .select('*')
79
+ .eq('status', 'active')
80
+ .gte('age', 18)
81
+ .order('created_at', { ascending: false })
82
+ .limit(10);
83
+ ```
84
+
85
+ #### 插入数据
86
+
87
+ ```javascript
88
+ // 插入单条
89
+ await mysql.from('users').insert({ name: 'test', email: 'test@example.com' });
90
+
91
+ // 插入多条
92
+ await mysql.from('users').insert([
93
+ { name: 'user1', email: 'user1@example.com' },
94
+ { name: 'user2', email: 'user2@example.com' },
95
+ ]);
96
+ ```
97
+
98
+ #### 更新数据
99
+
100
+ ```javascript
101
+ await mysql.from('users').update({ status: 'inactive' }).eq('id', 1);
102
+ ```
103
+
104
+ #### 删除数据
105
+
106
+ ```javascript
107
+ await mysql.from('users').delete().eq('id', 1);
108
+ ```
109
+
110
+ ### 用户认证
111
+
112
+ ```javascript
113
+ // 获取 auth 实例
114
+ const auth = client.auth;
115
+
116
+ // 微信小程序 OpenID 静默登录,用户不存在则注册成外部注册用户
117
+ await auth.signInWithOpenId();
118
+
119
+ // 获取登录状态
120
+ const { data } = auth.getSession();
121
+
122
+ // 登出
123
+ await auth.signOut();
124
+ ```
125
+
126
+ ## API
127
+
128
+ ### init(cloud, options)
129
+
130
+ 初始化 SDK(使用云函数调用)。
131
+
132
+ | 参数 | 类型 | 必填 | 说明 |
133
+ | ------------- | ----------------- | ---- | -------------- |
134
+ | cloud | CloudBaseInstance | 是 | 微信云开发实例 |
135
+ | options.envId | string | 否 | 环境 ID |
136
+
137
+ ### 数据模型方法
138
+
139
+ - `create(params)` - 创建单条数据
140
+ - `createMany(params)` - 批量创建数据
141
+ - `update(params)` - 更新单条数据
142
+ - `updateMany(params)` - 批量更新数据
143
+ - `upsert(params)` - 创建或更新数据
144
+ - `delete(params)` - 删除单条数据
145
+ - `deleteMany(params)` - 批量删除数据
146
+ - `get(params)` - 获取单条数据
147
+ - `list(params)` - 获取数据列表
148
+ - `runSQLTemplate(params)` - 执行 SQL 模板
6
149
 
7
150
  ## 参考文档
8
151
 
9
- [类型声明](https://docs.cloudbase.net/model/sdk-reference/globals)
152
+ - [初始化 SDK](https://docs.cloudbase.net/model/init-sdk)
153
+ - [类型声明](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,4 @@
1
+ import { CloudBaseInstance, Auth } from '../types';
2
+ export declare const generateAuthClient: (cloud: CloudBaseInstance, options?: {
3
+ envId?: string;
4
+ }) => 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;
@@ -22,6 +23,7 @@ declare const _default: {
22
23
  init: typeof init;
23
24
  generateHTTPClient: (callFunction: import("./types").CallFunction, fetch: (options: import("@cloudbase/adapter-interface").IFetchOptions) => any, baseUrl: string, options?: {
24
25
  sqlBaseUrl?: string | undefined;
26
+ cloud?: CloudBaseInstance | undefined;
25
27
  } | undefined) => OrmClient;
26
28
  generateMySQLClient: typeof generateMySQLClient;
27
29
  };
@@ -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 as AuthInstance } 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
  * 云函数调用接口,包含调用函数和认证信息。
@@ -711,6 +717,12 @@ export type CloudBaseInstance = {
711
717
  auth: any;
712
718
  };
713
719
  export type IMySqlClient = (options?: IMySqlOptions) => MySqlClient;
720
+ export interface Auth {
721
+ signInWithOpenId: AuthInstance['signInWithOpenId'];
722
+ getUser: AuthInstance['getUser'];
723
+ getSession: AuthInstance['getSession'];
724
+ refreshSession: AuthInstance['refreshSession'];
725
+ }
714
726
  /**
715
727
  * 扩展的云实例接口,扩展了云函数调用接口并包含ORM客户端。
716
728
  * @hidden
@@ -719,6 +731,7 @@ export interface ExtendedCloudBaseInstance extends CloudBaseInstance {
719
731
  models: OrmClient & OrmRawQueryClient;
720
732
  mysql: IMySqlClient;
721
733
  rdb: IMySqlClient;
734
+ auth: Auth;
722
735
  }
723
736
  /**
724
737
  * 云函数调用方法定义。
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>;