Quick Start
🎯 Practical Case: Blog System
Let's demonstrate how to use these field types by creating a data model for a blog system:
{
"title": "My First Blog", // Article title
"content": "<p>This is rich text content</p>", // Article body content
"author": "Zhang San", // Author's name
"email": "zhangsan@example.com", // Author's email
"published": true, // Whether published
"viewCount": 1024, // View count
"tags": ["technology", "frontend", "Vue"], // Article tags
"metadata": { // Article metadata
"seo": {
"keywords": "Vue, frontend development", // SEO keywords
"description": "Vue frontend development tutorial" // SEO description
}
},
"coverImage": "https://example.com/cover.jpg", // Cover image
"publishedAt": "2024-01-15T10:30:00Z", // Publish time
"status": "published", // Article status
"articleNo": "A000001", // Article number
"location": { // Publish location
"type": "Point",
"coordinates": [116.4074, 39.9042] // Geographic coordinates (Beijing)
}
}
Step 1: Access the Data Model Management Page
- Access the console: Go to the CloudBase console
- Select database: Switch to Database/Document-based
- Create model: Click "New Model"
Step 2: Design Model Structure
Taking the blog system as an example, we need to create two related data models:
📝 Article Model (article)
Field Name | Field Type | Required | Description |
---|---|---|---|
title | Text | Yes | Article title |
content | Rich Text | Yes | Article content |
summary | Text | No | Article summary |
author | Text | Yes | Author name |
email | Yes | Author's email | |
coverImage | Image | No | Cover image |
tags | Array | No | Article tags |
published | Boolean | Yes | Whether published |
viewCount | Number | No | View count |
publishedAt | Date Time | No | Publish time |
status | Enum | Yes | Article status (draft/published/offline) |
💬 Comment Model (comment)
Field Name | Field Type | Required | Description |
---|---|---|---|
content | Text | Yes | Comment content |
author | Text | Yes | Commenter name |
email | Yes | Commenter's email | |
website | URL | No | Commenter's website |
article | Text | Yes | Associated article Id |
approved | Boolean | Yes | Whether approved |
📱 Data Operations with the SDK
Initialize the SDK
- Mini Program
- Web
- Cloud Function
// Download and import the SDK files
const cloudbase = require("@cloudbase/wx-cloud-client-sdk");
// Initialize CloudBase
wx.cloud.init({
env: "your-env-id", // Replace with your environment ID
});
// Initialize the data model client
const client = cloudbase.init(wx.cloud);
const models = client.models;
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()
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()
Basic Data Operations
Create Article
// Create a new article
const {
data
} = await models.article.create({
data: {
title: "Vue 3 Getting Started Tutorial",
content: "<h1>Vue 3 Introduction</h1><p>Vue 3 is a modern front-end framework...</p>",
summary: "This article introduces the basic concepts and usage of Vue 3",
author: "Zhang San",
email: "zhangsan@example.com",
tags: ["Vue", "frontend", "tutorial"],
published: true,
status: "published",
publishedAt: new Date().toISOString()
}
});
console.log("Article created successfully:", data.id);
Article List Query
// Query published articles
const {
data
} = await models.article.list({
filter: {
where: {
published: {
$eq: true
},
status: {
$eq: "published"
}
}
},
sort: {
publishedAt: 'desc' // Publication time, descending order
},
pageSize: 10,
pageNumber: 1
});
console.log("Article list:", data.records);
console.log("Total:", data.total);
Create Comment
// Create a comment for the article
const {
data
} = await models.comment.create({
data: {
content: "This article is very well written!",
author: "Li Si",
email: "lisi@example.com",
website: "https://lisi.blog",
article: "Article ID", // Associated with a specific article
approved: false // Pending approval
}
});
console.log("Comment created successfully:", data.id);
Query Association
// Query articles and their comments
const {
data
} = await models.article.get({
filter: {
where: {
_id: {
$eq: "Article ID"
}
}
},
select: {
$master: true,
comments: true
}
});
console.log("Article details:", data);
console.log("Comment List:", data.comments);
🎯 Best Practices
1. Table Structure Design Suggestions
- Index Design: Create indexes for commonly queried fields to improve query performance.
- Enum Type: Use enum to restrict the optional values of status fields
2. Model Design Principles
- Normalized Design: Avoid data redundancy and follow database normalization principles
- Relationships: Design clear table relationships (one-to-one, one-to-many)
- Field Naming: Use meaningful English field names and follow naming conventions
- Required Field Validation: Properly configure mandatory constraints to ensure data integrity.
3. Performance Optimization
- Index Optimization: Create appropriate indexes for commonly queried fields.
- Pagination: Use pagaged queries for large datasets to avoid performance issues.
- Field Selection: Return only the required fields during queries.
4. Security Recommendations
- Access Control: Control data access permissions through security rules
- Password Encryption: Sensitive information such as passwords must be encrypted for storage.
🚀 Next Steps
Congratulations! You have successfully created your first document-based data model. Next, you can:
- 📚 Learn more about CRUD operations
- 🔗 Master relationship query techniques
- 🎛️ Use Data Management
- 🔒 Configure data security rules
Start building your application now! 🎉