Quick Start
Practical Example: 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 it has been 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", // Publication time
"status": "published", // Article status
"articleNo": "A000001", // Article number
"location": { // Release location
"type": "Point",
"coordinates": [116.4074, 39.9042] // Latitude and longitude coordinates (Beijing)"
}
}
Step 1: Access the data model management page
- Access the console: Go to the Tencent Cloud Development Platform
- Select Database: Switch to Tencent Cloud Development Platform/Database/Document-based
- Create Model: Click New Model
Step 2: Design the model structure
Taking the blog system as an example, we need to create two related data models:
Article Model (article)
| Field Name | Data Type | Required | Description |
|---|---|---|---|
title | Text | Yes | Article title |
content | Rich Text | Yes | Article content |
summary | Text | No | Article summary |
author | Text | Yes | Author's name |
email | Yes | Author's email | |
coverImage | Image | No | Cover image |
tags | Array | No | Article tags |
published | Boolean | Yes | Publish status |
viewCount | Number | No | View count |
publishedAt | DateTime | No | Publish time |
status | Enum | Yes | Article status (draft/published/archived) |
Comment Model (comment)
| Field Name | Data Type | Required | Description |
|---|---|---|---|
content | Text | Yes | Comment content |
author | Text | Yes | Commenter's name |
email | Yes | Commenter's email | |
website | Website URL | No | Commenter's website |
article | Text | Yes | Associated article Id |
approved | Boolean | Yes | Approval status |
Data Operations Using the SDK
Initialize the SDK
- Mini Program
- Web
- Cloud Function
// Download and import the SDK file.
const cloudbase = require("@cloudbase/wx-cloud-client-sdk");
// Initialize CloudBase
wx.cloud.init({
env: 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 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()
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()
Basic Data Operations
Creating Articles
// Create a new article
const {
data
} = await models.article.create({
data: {
title: title: "Vue 3 Getting Started Tutorial",
content: content: "<h1>Vue 3 Introduction</h1><p>Vue 3 is a modern front-end framework...</p>",
summary: Obtain the database instance (for collection operations)
author: author: "Zhang San",
email: "zhangsan@example.com",
tags: summary: "This article introduces the basic concepts and usage of Vue 3",
published: true,
status: "published",
publishedAt: new Date().toISOString()
}
});
console.log("Article created successfully:", data.id);
Querying Article List
// Obtain the database instance (for collection operations)
const {
data
} = await models.article.list({
filter: {
where: {
published: {
$eq: true
},
status: {
$eq: "published"
}
}
},
sort: {
publishedAt: publishedAt: 'desc' // In descending order of publish time
},
pageSize: 10,
pageNumber: 1
});
// Query published articles
console.log("Total:", data.total);
Creating Comments
// Create a comment for the article
const {
data
} = await models.comment.create({
data: {
content: content: "This article is very well-written!",
author: author: "Li Si",
email: "lisi@example.com",
website: "https://lisi.blog",
article: article: "Article ID", // Associated with a specific article
approved: approved: false // pending review
}
});
console.log("Comment created successfully:", data.id);
Association Query
// 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);
Next Steps
Congratulations! You have successfully created your first document-based data model. Next, you can:
- Learn more CRUD operations
- Master Association Query Techniques
- Data Management
- Configure Data Security Rules
Start building your application now!