Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@cloudbase/wx-cloud-client-sdk

Package Overview
Dependencies
Maintainers
14
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cloudbase/wx-cloud-client-sdk

wx cloud client sdk

npmnpm
Version
1.8.1
Version published
Weekly downloads
11K
-8.98%
Maintainers
14
Weekly downloads
 
Created
Source

@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',
});

数据模型操作

// 创建数据
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);

用户认证

// 获取 auth 实例
const auth = client.auth;

// 微信小程序 OpenID 静默登录,用户不存在则注册成外部注册用户
await auth.signInWithOpenId();

// 获取登录状态
const { data } = auth.getSession();

// 登出
await auth.signOut();

API

init(cloud, options)

初始化 SDK(使用云函数调用)。

参数类型必填说明
cloudCloudBaseInstance微信云开发实例
options.envIdstring环境 ID

数据模型方法

  • create(params) - 创建单条数据
  • createMany(params) - 批量创建数据
  • update(params) - 更新单条数据
  • updateMany(params) - 批量更新数据
  • upsert(params) - 创建或更新数据
  • delete(params) - 删除单条数据
  • deleteMany(params) - 批量删除数据
  • get(params) - 获取单条数据
  • list(params) - 获取数据列表
  • runSQLTemplate(params) - 执行 SQL 模板

参考文档

  • 初始化 SDK
  • 类型声明

Keywords

typescript

FAQs

Package last updated on 26 Jan 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts