SDK Initialization
When creating a data model, operation SDKs for multiple platforms such as Mini Programs/Cloud Functions/Web, etc., will be automatically generated. You can refer to the following documentation for initialization and usage.
Invoking in Mini Programs
Install Dependencies
Method 1: Download the SDK
You can directly download the SDK link file below to the Mini Program code directory (usually the miniprogram
directory) and save it as wxCloudClientSDK.umd.js
.
Method 2: Import via npm
In the directory where the Mini Program's package.json
is located (usually the miniprogram
directory), run the command to install the npm package:
npm install @cloudbase/wx-cloud-client-sdk --save
Then click on "Tools" -> "Build npm" in the toolbar of the WeChat Developer Tools.
Initializing and Using the SDK
Add the following code in app.js:
// If you are using the downloaded SDK method, change it to const { initHTTPOverCallFunction } = require('./wxCloudClientSDK.umd.js')
const { initHTTPOverCallFunction } = require("@cloudbase/wx-cloud-client-sdk");
// Specify the Cloud Base environment ID.
wx.cloud.init({
env: "some-env-id", // current Cloud Base environment ID
});
const client = initHTTPOverCallFunction(wx.cloud);
const models = client.models; // Alternatively, you can also get them directly from wx.cloud.models, though the type hinting will be weaker with this approach.
// Now you can call the CRUD methods on the models.
// models.post.create({
// data: {
// body: "Hello, world👋\n\nfrom china",
// title: "Hello, world👋",
// slug: "hello-world-cn",
// },
// }).then(({ data } => { console.log(data)}))
Calling in Cloud Functions
To use in cloud functions, install version 3.10.0 or higher of @cloudbase/node-sdk
in the corresponding cloud function directory.
Install Dependencies
When creating a cloud function, a package.json
file will be created by default in the cloud function directory, and the user will be prompted whether to install dependencies locally immediately. Please note that the cloud function runs in a Node.js environment; therefore, when installing dependencies locally, ensure Node.js is installed and both node
and npm
are in the environment variables. If you choose not to install dependencies locally, you can run the following command in that directory:
npm install --save @cloudbase/node-sdk@3.10
Initializing and Using the SDK
Before calling the data model SDK in cloud functions, execute the initialization method once:
const cloudbase = require("@cloudbase/node-sdk");
// Specify the Cloud Base environment ID.
const app = cloudbase.init({
env: "some-env-id",
});
exports.main = async (event, context) => {
const models = app.models;
// Now you can call the CRUD methods on the models.
// models.post.create({
// data: {
// body: "Hello, world👋\n\nfrom china",
// title: "Hello, world👋",
// slug: "hello-world-cn",
// },
// }).then(({ data } => { console.log(data)}))
};
Calling in Web Pages
Install Dependencies
In the root directory of the web project, use npm or yarn to install the required packages:
npm install @cloudbase/js-sdk --save
Initializing and Using the SDK
import cloudbase from "@cloudbase/js-sdk";
// Import the SDK.
const app = cloudbase.init({
env: "your-cloud-env-id", // Replace this value with your Cloud Base environment ID.
clientId: "your-cloud-env-id", // Replace this value with your Cloud Base environment ID.
});
const auth = app.auth({
persistence: "local",
});
await auth.signInAnonymously(); // Or use other login methods.
const models = app.models;
// Now you can call the CRUD methods on the models.
// Example: Create a post data record.
// models.post.create({
// data: {
// body: "Hello, world👋\n\nfrom china",
// title: "Hello, world👋",
// slug: "hello-world-cn",
// },
// }).then(({ data } => { console.log(data)}))
Multi-platform Support
@cloudbase/js-sdk supports multi-platform invocation. For details, refer to One Code, Multi-Platform Adaptation. For multi-platform development requirements involving both Web and Mini Programs, use @cloudbase/js-sdk. When using @cloudbase/js-sdk in Mini Programs, if you need to reduce the Mini Program package size, refer to On-demand Import of Functional Modules.