Initialize the SDK
🎯 SDK Comparison Overview
SDK Type | Platform | Object | Features | Recommended Scenarios | Documentation Link |
---|---|---|---|---|---|
Mini Program SDK | WeChat Mini Program | Collection | Built-in SDK, no installation required | Direct database collection operations in Mini Programs | Mini Program SDK Initialization |
Web SDK | Web Browsers | Data Models, Collections | Lightweight, supports modern browsers | Web Applications, H5 Pages | Web SDK Initialization |
Node.js SDK | Node.js Environment | Data Models, Collections | Server-side permissions, full functionality | Cloud Functions, Background Services | Node.js SDK Initialization |
HTTP API | Any Platform | RESTful Interfaces | 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 a built-in feature of the Mini Program and does not require installation.
Initialization
Configure initialization in the app.js
file of the Mini Program:
wx.cloud.init({
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, specifies which environment's cloud resources to access |
traceUser | boolean | ❌ No | true | Whether to log user visits to user management for viewing in the console |
Collection Operations Example
wx.cloud.init({
env: 'your-env-id', // Replace with your environment ID
traceUser: true,
})
// Get 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 designed specifically for Web browser environments, supporting two operation modes: data models and collections.
Installation
Option 1: Include via CDN
<!-- Directly include in the HTML page -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/2.17.3/cloudbase.full.js"></script>
Option 2: Install via npm
npm install @cloudbase/js-sdk --save
Initialization
// Include via CDN: directly use the global variable cloudbase
// npm import method
import cloudbase from "@cloudbase/js-sdk"
// Initialize the app
const app = cloudbase.init({
env: "your-env-id" // Replace with your environment ID
})
// Get the data model instance
const models = app.models
// Get 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 Operation Examples
import cloudbase from "@cloudbase/js-sdk"
// Initialize the app
const app = cloudbase.init({
env: "your-env-id" // Replace with your environment ID
})
// Get 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 Operations Example
import cloudbase from "@cloudbase/js-sdk"
// Initialize the app
const app = cloudbase.init({
env: "your-env-id" // Replace with your environment ID
})
// Get 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 Node.js environments, featuring administrator privileges and supporting full database capabilities.
Installation
npm install @cloudbase/node-sdk --save
Initialization
import cloudbase from "@cloudbase/node-sdk"
// In a cloud function environment, you need to load node-sdk using the require method
// const cloudbase = require('@cloudbase/node-sdk')
// Initialize the app
const app = cloudbase.init({
env: "your-env-id" // Replace with your environment ID
})
// Get the data model instance
const models = app.models
// Get the database instance (for collection operations)
const db = app.database()
Authentication Configuration
// The server-side SDK has administrator privileges and does not require additional authentication
// but you can set a custom user context
const app = cloudbase.init({
env: "your-env-id",
credentials: {
// Use the 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 Operation Examples
import cloudbase from "@cloudbase/node-sdk"
// In a cloud function environment, you need to load node-sdk using the require method
// const cloudbase = require('@cloudbase/node-sdk')
// Initialize the app
const app = cloudbase.init({
env: "your-env-id" // Replace with your environment ID
})
// Get 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 Operations Example
import cloudbase from "@cloudbase/node-sdk"
// In a cloud function environment, you need to load node-sdk using the require method
// const cloudbase = require('@cloudbase/node-sdk')
// Initialize the app
const app = cloudbase.init({
env: "your-env-id" // Replace with your environment ID
})
// Get 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, based on the database instance or data model instance provided by the SDK, perform data operations.