@cloudbase/wx-cloud-client-sdk
微信云开发客户端 SDK,提供数据模型、MySQL/RDB 数据库和用户认证能力。
安装
npm install @cloudbase/wx-cloud-client-sdk
快速开始
初始化
import { init } from '@cloudbase/wx-cloud-client-sdk';
const client = init(wx.cloud, {
envId: 'your-env-id',
accessKey: 'your-access-key',
});
数据模型操作
const { data } = await client.models.yourModel.create({
data: { title: 'Hello', content: 'World' },
});
const {
data: { records, total },
} = await client.models.yourModel.list({
filter: { where: { title: { $eq: 'Hello' } } },
pageSize: 10,
pageNumber: 1,
getCount: true,
});
await client.models.yourModel.update({
data: { title: 'Updated' },
filter: { where: { _id: { $eq: 'record-id' } } },
});
await client.models.yourModel.delete({
filter: { where: { _id: { $eq: 'record-id' } } },
});
MySQL/RDB 数据库
const mysql = client.mysql({ database: 'your-database' });
const rdb = client.rdb({ database: 'your-database' });
查询数据
const users = await mysql.from('users').select('*');
const users = await mysql.from('users').select('id, name, email');
const activeUsers = await mysql.from('users').select('*').eq('status', 'active');
const result = await mysql
.from('users')
.select('*')
.eq('status', 'active')
.gte('age', 18)
.order('created_at', { ascending: false })
.limit(10);
插入数据
await mysql.from('users').insert({ name: 'test', email: 'test@example.com' });
await mysql.from('users').insert([
{ name: 'user1', email: 'user1@example.com' },
{ name: 'user2', email: 'user2@example.com' },
]);
更新数据
await mysql.from('users').update({ status: 'inactive' }).eq('id', 1);
删除数据
await mysql.from('users').delete().eq('id', 1);
用户认证
const auth = client.auth;
await auth.signInWithOpenId();
const { data } = auth.getSession();
await auth.signOut();
API
init(cloud, options)
初始化 SDK(使用云函数调用)。
| cloud | CloudBaseInstance | 是 | 微信云开发实例 |
| options.envId | string | 否 | 环境 ID |
| options.accessKey | string | 否 | 客户端 Publishable Key |
数据模型方法
create(params) - 创建单条数据
createMany(params) - 批量创建数据
update(params) - 更新单条数据
updateMany(params) - 批量更新数据
upsert(params) - 创建或更新数据
delete(params) - 删除单条数据
deleteMany(params) - 批量删除数据
get(params) - 获取单条数据
list(params) - 获取数据列表
runSQLTemplate(params) - 执行 SQL 模板
参考文档