tencent cloud

CloudBase

Product Introduction
Product Overview
Features and Strengths
Use Cases
System Limits
Purchase Guide
Product Pricing
Description Of Billing Capability Items
Yearly/Monthly Subscription Package Description
Alarm and Notification
Overdue Payment Instructions
Development Guide
Cloud Storage
Database
Identity Verification
Cloud function
Static website management
SDK Documentation
Client SDK
Server SDKs
Management-side SDK
Product Agreement
Cloud Development Service Level Agreement

Data Model Overview

PDF
フォーカスモード
フォントサイズ
最終更新日: 2025-12-31 11:48:17

Data Model Introduction

A data model is a tool for organizing and managing data, operating on top of CloudBase databases. It helps us better understand and operate data, improving development efficiency and data quality.
A CloudBase data model provides developers with more advanced querying, verification, and other features. It offers multi-platform SDKs for calls and allows one-click generation of editable management applications. It also has a built-in CMS management console and AI-based intelligent analysis capabilities, enabling users with various roles to manage and analyze data.
The following are some core features of a data model:
1. Data verification: automatically checks whether data is correct to avoid errors.
2. Association relationship handling: automatically handles relationships between data to simplify operations.
3. Automatically code generation: quickly generates code based on the data model to improve development speed.
4. CMS management console: provides simple and easy-to-use data management interfaces for non-technical users to operate.
5. Automatic application code generation: quickly generates editable management applications from models.
6. AI-based intelligent analysis: uses AI to analyze data and identify valuable information.
7. Advanced query support: supports complex queries to meet various needs.

Relationship Between Data Models and Databases

A data model is an abstraction of a database, defining the structure, attributes, constraints, and relationships of the data model. It provides a unified way to describe and operate data, simplifying data operations and queries. The data model capability is the Object-Relational Mapping (ORM) commonly used in development.
The data model and database have an association relationship. A data model corresponds to a collection (in a non-structured database) or a table (in a structured database) in the database.
Using data models does not mean being restricted to using data models only. While using data models, if you need more complex operations or operations that data models cannot handle well, you can also use the database's native methods to directly read and write data.
Compared to ORM in traditional development frameworks, CloudBase data models not only include SDK capabilities for data operations related to ORM but also integrate additional features such as data management interfaces, users and permissions system, content management, and further capabilities like application generation and data analysis based on data models.

Enabling Data Models

Access CloudBase Cloud Backend > Data Model, select New Model, and choose from 3 methods:
Connect to an existing database table and generate a data model: Connect to an existing database table or collection and generate a data model. This method allows connecting to collections from existing document cloud databases or tables in self-owned MySQL databases to generate collections. This does not impact the existing database collections or tables, nor impact the existing data. It merely generates models to provide model-usable methods for further development.
Create a database and configure a data model: Choose to create a database table or collection and configure the model structure. In this case, a new collection or table will be created in the selected database (based on the chosen cloud database type or self-owned database), and a data model will be generated. Once the data model is created, you can read and write data using data model methods or database methods.
Import existing CMS: Import models from the existing CloudBase CMS, mainly used for migrating existing CMS. By using the new version of content management, you can obtain continuously updated and iteratively upgraded CMS capacity.
After creating a model, you can immediately use the various capabilities provided by the data model.

Advantages of Using Data Models

Using data models can improve development efficiency, reduce maintenance costs, enhance data security, and provide powerful data analysis and management capabilities.

Data Verification and Type Checking

Data models can automatically handle data verification and type checking to ensure data accuracy and consistency. This helps avoid errors during data input, updates, and queries, improving data quality.
First, we define a simple post data model post in the cloud backend.
We define two attributes: title (string type), body (string type).
Next, we attempt to insert an object containing incorrectly typed data:
Use the create() method to create a post:
try {
const { data } = await models.post.create({
data: {
title: "Hello, World",
body: 123456, // This attribute is intentionally set as a string type, instead of an object ID type.
},
});

// Return the created post ID.
console.log(data);
// { id: "7d8ff72c665eb6c30243b6313aa8539e"}
} catch (error) {
console.error("Error:", error);
}
In this example, we intentionally set the value of the body attribute to number type 123456 rather than the correct text type. When we attempt to insert this data, the data model detects a type mismatch and throws an error. The output is as follows:
Error: WxCloudSDKError: [Error] Data format verification failed. Root cause: [#/body: expected type: String, found: Integer], Reason: attribute [Body], flag [body], type mismatch: Expected type: string. Solution steps: Modify the data type according to the reason. errRecord:{"title":"Hello, World","body":123456} [Operation] Call post.create
This error prompt indicates that the data model successfully detected the type error and prevented the insertion of erroneous data. This ensures data accuracy and consistency.

Automatic Handling of Association Relationships

Data models can automatically handle association relationships between entities, such as one-to-one, one-to-many, and many-to-many relationships. This makes it more convenient to manage these relationships during database design and operations, improving data processing efficiency.
We can use nested queries to read related data from multiple tables in the database, such as posts and their comments.
For example, use select to specify the associated attribute and its internal specific attribute in the query result.
const { data } = await models.post.list({
// Query only necessary attributes.
select: {
_id: true,
title: true,
updatedAt: true,
comments: {
_id: true,
createdAt: true,
comment: true,
},
},
filter: {
where: {},
},
getCount: true, // Enable to get total count.
});

// Return the queried data list `records` and the total count `total`.
// The returned content is obviously filtered by the database, only returning _id, title, and updatedAt attributes.
console.log(data);
// {
// records: [
// {
// _id: '9FSAHWM9VV',
// comments: [
// {
// createdAt: 1718096509916,
// comment: '11',
// _id: '9FSAJF3GLG',
// },
// ],
// title: 'Bonjour le Monde',
// updatedAt: 1718096503886,
// },
// {
// _id: '9FSAHWM9VU',
// comments: [],
// title: 'Hello, World',
// updatedAt: 1718096503886,
// },
// {
// _id: '9FSAHWM9VT',
// comments: [],
// title: 'Hola Mundo ',
// updatedAt: 1718096503886,
// },
// {
// _id: '9FSAHWM9VS',
// comments: [],
// title: 'Hello World',
// updatedAt: 1718096503886,
// },
// ],
// }

Automatic CRUD Code Generation

The data model can automatically generate CRUD code for multiple platforms, such as Mini Programs, web, and cloud functions. This significantly reduces developers' workload and improves development efficiency.
For example, use the upsert() method to create or update the post content:
const post = {
title: "Hello World",
body: "Hello World",
_id: "foo",
};
const { data } = await models.post.upsert({
create: post,
update: post,
filter: {
where: {
_id: {
$eq: post._id,
},
},
},
});
console.log(data);
// Return when creating.
// {
// "count": 0,
// "id": "foo"
// }

// Return when updating.
// {
// "count": 1,
// "id": ""
// }

Built-in CMS Management Console

The data model provides a built-in content management system (CMS), allowing non-technical users to easily manage and maintain data, reducing operational costs.

Generating Editable Management Console Code with One Click

Data models support the one-click generation of an editable management console, using low-code technology to visually modify and customize development. This eliminates the need for developers to manually write management interfaces, further improving development efficiency.

One-Click AI-Based Intelligent Data Analysis

Data models can use AI to perform intelligent analysis of data. This helps extract valuable information from massive datasets and provides strong support for decision-making.

Support for MySQL Advanced Queries

Data models support advanced query features for relational databases such as MySQL, including complex conditional filtering, sorting, and grouping. Users can query and analyze data more flexibly to meet various business needs.
For details, see the database native query documentation.
For example, query records where the author's contact number starts with 1858.
const result = await models.$runSQL(
"SELECT * FROM `lcap-wzcs_iuujo7p` WHERE author_tel LIKE '{{tel}}';",
{
tel: "1858%",
}
);

console.log(result);
// {"data":{"total":1,"executeResultList":[{"owner":"1739272568342245378","is_published":true,"author_web":"https://qq.com","banner":"cloud://lowcode-0gr8x3i8cd1c6771.6c6f-lowcode-0gr8x3i8cd1c6771-1307578329/weda-uploader/ec687de371d4ad064efd0a424a69e969-logo (1).png","auto_no":"1000","body":"<p>hello world</p>","title":"hello","type":"[\\"test\\",\\"test\\"]","author_tel":"18588881111","createdAt":1719475245475,"createBy":"1739272568342245378","read_num":997,"updateBy":"1739272568342245378","_openid":"1739272568342245378","extra":"{}","markdown":"# aa\\n\\n\\n\\n","author_email":"a@qq.com","json":"{\\"a\\":\\"1\\"}","_id":"9JXU7BWFZJ","region":"Beijing","updatedAt":1719475245475}],"backendExecute":"28"},"requestId":"0d4c98c3-a3ff-4c55-93cc-d0f5c835f82c"}


ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック