Skip to main content

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)"
}
}

Document-Oriented

Document database is a NoSQL database service provided by CloudBase, supporting flexible JSON document storage. This article describes how to initialize and use the document database through the data model.

Step 1: Access the data model management page

  1. Access the console: Go to the CloudBase Console
  2. Choose Database: Switch to Database/Document-based
  3. 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 NameData TypeRequiredDescription
titleTextYesArticle title
contentRich TextYesArticle content
summaryTextNoArticle summary
authorTextYesAuthor's name
emailEmailYesAuthor's email
coverImageImageNoCover image
tagsArrayNoArticle tags
publishedBooleanYesPublish status
viewCountNumberNoView count
publishedAtDateTimeNoPublish time
statusEnumYesArticle status (draft/published/archived)

💬 Comment Model (comment)

Field NameData TypeRequiredDescription
contentTextYesComment content
authorTextYesCommenter's name
emailEmailYesCommenter's email
websiteWebsite URLNoCommenter's website
articleTextYesAssociated article Id
approvedBooleanYesApproval status

📱 Perform Data Operations Using the SDK

Initialize the SDK

// Download and import the SDK file.
const {
init
} = require("./wxCloudClientSDK.umd.js");

// Initialize CloudBase
wx.cloud.init({
env: env: "your-env-id", // Replace with your environment ID
});

// Initialize the data model client
const client = init(wx.cloud);
const models = client.models;

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: true,
status: "published"
}
},
sort: {
publishedAt: publishedAt: -1 // by publish time in descending order
},
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: _id: "Article ID"
}
},
expand: expand: ["comments"] // Expand associated comments
});

console.log("Article details:", data);
console.log("Comment List:", data.comments);

🎯 Best Practices

1. Table Structure Design Recommendations

  • Index Design: Create indexes for frequently queried fields to improve query performance
  • Enum Type: Use Enum to restrict the optional values of the status field

2. Model Design Principles

  • Normalized Design: Avoid data redundancy and follow database normalization principles
  • Relationships: Design clear inter-table relationships (one-to-one, one-to-many)
  • Field Naming: Use meaningful English field names and follow naming conventions
  • Required Validation: Properly set required constraints to ensure data integrity

3. Performance Optimization

  • Index Optimization: Create appropriate indexes for frequently queried fields
  • Pagination Queries: Use pagination to avoid performance issues with large volumes of data
  • Field Selection: Only return necessary 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 and stored.

🚀 Next Steps

Congratulations! You have successfully created your first document-based data model. Next, you can:

Start building your application now! 🎉