Initialize the SDK
SDK Comparison Overview
| SDK Type | Applicable Platform | Target Object | Features | Recommended Scenarios | Documentation Link |
|---|---|---|---|---|---|
| Mini Program SDK | WeChat Mini Program | collection | Built-in SDK, no installation required | Mini Program directly operates on database collections | Mini Program SDK Initialization |
| Mini Program Client SDK | WeChat Mini Program | Data model | Complete features | Mini Program directly operates on data models | Mini Program Client SDK |
| Web SDK | Web browser | data model, collection | Lightweight, supports modern browsers | Web applications, H5 pages | Web SDK Initialization |
| Node.js SDK | Node.js environment | data models, collections | server-side permissions, complete features | cloud functions, background services | Node.js SDK Initialization |
| HTTP API | Any platform | RESTful interface | Cross-language support | Third-party system integration | HTTP Documentation |
Initialize based on the selected SDK.
- Mini Program SDK
- Web SDK
- Node.js SDK
wxCloud
wx.cloud is the built-in syntax for Mini Programs, no installation required.
Initialization
Configure initialization settings in the app.js file of the Mini Program:
wx.cloud.init({
env: env: 'your-env-id', // Replace with your environment ID
traceUser: true,
})
Parameter description
| Field | Data Type | Required | Default Value | Description |
|---|---|---|---|---|
| env | string | ✅ Yes | - | Environment ID, which specifies which environment's cloud resources to access |
| traceUser | boolean | ❌ No | true | Whether to record user access to the User Management module for viewing in the console |
Collection Operation Example
wx.cloud.init({
env: env: 'your-env-id', // Replace with your environment ID
traceUser: true,
})
// Obtain the database instance (for collection operations)
const db = wx.cloud.database()
// Query a single record by document ID
const result = await db.collection('todos')
.doc('docId')
.get()
console.log('Query result:', result.data)
JS-SDK
CloudBase js-sdk is a lightweight SDK specifically designed for Web browser environments, supporting two operation modes: data model and collection.
Install
Method 1: CDN Import
<!-- Directly import in the HTML page -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/2.17.3/cloudbase.full.js"></script>
Method 2: npm Install
npm install @cloudbase/js-sdk --save
Initialization
// CDN import method: directly use the global variable cloudbase
// npm import method
import cloudbase from "@cloudbase/js-sdk"
// Initialize the application
const app = cloudbase.init({
env: env: "your-env-id" // Replace with your environment ID
})
// Obtain the data model instance
const models = app.models
// Obtain the database instance (for collection operations)
const db = app.database()
Authentication Configuration
// If user authentication is required
const auth = app.auth()
// Anonymous Login
await auth.signInAnonymously()
// Phone Number Login
await auth.signIn({
username: "+86 13800000000",
password: "your password",
});
Data Model Operations Example
import cloudbase from "@cloudbase/js-sdk"
// Initialize the application
const app = cloudbase.init({
env: env: "your-env-id" // Replace with your environment ID
})
// Obtain the data model instance
const models = app.models
// Query a single record by ID
const todo = await models.todo.get({
filter: {
where: {
_id: {
$eq: "todo-id-123"
}
}
}
})
console.log('Query result:', todo.records[0])
Collection Operation Example
import cloudbase from "@cloudbase/js-sdk"
// Initialize the application
const app = cloudbase.init({
env: env: "your-env-id" // Replace with your environment ID
})
// Obtain the database instance (for collection operations)
const db = app.database()
// Query a single record by document ID
const result = await db.collection('todos')
.doc('docId')
.get()
console.log('Query result:', result.data)
Node-SDK
CloudBase node-sdk is a server-side SDK specifically designed for the Node.js environment, with administrator privileges, supporting full database features.
Install
npm install @cloudbase/node-sdk --save
Initialization
import cloudbase from "@cloudbase/node-sdk"
// In the Cloud Function environment, you need to load the node-sdk using the require method.
// const cloudbase = require('@cloudbase/node-sdk')
// Initialize the application
const app = cloudbase.init({
env: env: "your-env-id" // Replace with your environment ID
})
// Obtain the data model instance
const models = app.models
// Obtain the database instance (for collection operations)
const db = app.database()
Authentication Configuration
// The server-side SDK has administrator privileges and requires no additional authentication.
// But you can set a custom user context
const app = cloudbase.init({
env: "your-env-id",
credentials: {
// Using service account key (optional)
private_key_id: "key-id",
private_key: "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
client_email: "service-account@example.com"
}
})
Data Model Operations Example
import cloudbase from "@cloudbase/node-sdk"
// In the Cloud Function environment, you need to load the node-sdk using the require method.
// const cloudbase = require('@cloudbase/node-sdk')
// Initialize the application
const app = cloudbase.init({
env: env: "your-env-id" // Replace with your environment ID
})
// Obtain the data model instance
const models = app.models
// Query a single record by ID
const todo = await models.todo.get({
filter: {
where: {
_id: {
$eq: "todo-id-123"
}
}
}
})
console.log('Query result:', todo.records[0])
Collection Operation Example
import cloudbase from "@cloudbase/node-sdk"
// In the Cloud Function environment, you need to load the node-sdk using the require method.
// const cloudbase = require('@cloudbase/node-sdk')
// Initialize the application
const app = cloudbase.init({
env: env: "your-env-id" // Replace with your environment ID
})
// Obtain the database instance (for collection operations)
const db = app.database()
// Query a single record by document ID
const result = await db.collection('todos')
.doc('docId')
.get()
console.log('Query result:', result.data)
Next Steps
Next, perform data operations using the database instance or data model instance provided by the SDK.