Data Binding
Once you have configured your database or API, you can retrieve data through data sources and utilize it on pages or components.
- List-type components can directly configure data sources
- Pages and other components need to declare data sources first before they can be used
Usage Workflow
Retrieving Data Through Data Sources
You can configure data sources on pages or list-type components (List, Select View, Data Selector) to fetch data:
- List-type components: Select data tables or APIs in the right sidebar - Data section
- Pages: Declare a data source on page in the right sidebar - Data section
When retrieving database data, the following configurations are required:
- Filter: Define one or more conditions to filter the required data
- Sort: Specify the display order of data. Note: The order of sorting fields must be consistent with distinct fields. If the field has vector storage enabled, advanced semantic relevance sorting can also be implemented
- Distinct: When non-duplicate data is required, specify one or more fields to ensure uniqueness of returned results
- Limit: Control the number of returned records.
1
returns a single record, values greater than1
return an array
When comparing any value with null in WHERE filter conditions, the result is always true.
To achieve a false result when comparing with null values, you can add conditional data: when the value is null, set a value that makes the condition impossible to be true.
Example: For a filter like id = ${inputValue}
, you can add conditional data that returns -1 when inputValue is null. Since all IDs are positive numbers, the condition id = -1
will always be false.
Data Binding
After retrieving data through data sources, you can bind data within pages or components for display or use. The scope of use includes component content, component actions, and page actions.
-
Data source on page: Can be referenced by all components within the page. Reference path: Page data → Data source
-
** Data source on list component**: Can be referenced by components within list items. Reference path: Component data → List.
- You can access the array item index and item data.
- Through item data, you can access various fields of that item.
- In certain scenarios (such as in conditions), you can also select the complete data of the array item through “Self option”.
Examples
This article will use a classic “blog website” as an example to implement features such as publishing articles, displaying article lists, viewing article details, liking, and sending notifications, demonstrating data query, creation, and modification operations. This process will involve various aspects of data:
- Using database as data source
- Using parameters for data transmission
- Using request action for data query and modification
Data Model Design
To implement blog website functionality, the following data models need to be designed:
- Blog table: Used to save blog articles, including fields like
title
,content
,author
,published_status
,show_at
,like_count
, etc. One account can publish multiple articles, so the account table and post table have a one-to-many relationship, connected through theauthor
field. - notification table: Used to save notifications, including fields like
sender_account
,receiver_account
,message
, etc. The account table establishes two one-to-many relationships with the notification table through thesender_account
andreceiver_account
fields, representing sender and receiver respectively. Overall, the account table and notification table have a many-to-many relationship. For more information about relationships, please refer to Data Model - Relationships.
Creating Data - Publishing a New Article
When users want to create a new article, we need to store the user input content in the database.
- Page Design: Create a “Writing” page that includes components like title input and rich text editor for the main content.
- Action Configuration: Configure an “Insert Blog” action on the “Add” button.
- Data Mapping: Map the values of input components one by one to corresponding fields in the
Blog
data table (title
,content
, etc.). Theauthor
field is bound to the Logged in user’sid
.
Reading Data - Displaying Article List
- Bind Data Source: Add a “List” component to the homepage and set its data source to the
post
table. - Configure Query: Add filter condition
published_status = true
to get only published articles; set sorting byshow_at
in descending order to put latest articles first. - Bind Data: Bind list item components like title and cover image to the current item data (
item
) fields such astitle
andcover_image
.
Data Reading - Viewing Single Article Details
Demonstrates the collaborative work of data transmission and data usage.
- Declare Parameter: Create a “Detail Page” and declare a path parameter:
blog_id
. - Set Parameter: Configure “Navigation” action on each list item in the article list. During navigation, assign the current item data’s
id
toblog_id
, passing it to the “Detail Page”. - Use Parameter: Add a data source in the “Detail Page” and set a key filter condition:
id
equals the path parameterpost_id
. Also set data limit to1
. - Display Content: Through this precise query, the detail page can display all content of the specific article.
Updating Data - Liking and Sending Notifications
This step demonstrates how to modify existing data and execute a series of automated operations.
- Configure Like Action: Configure an “Update Blog” action on the “Like” button.
- Specify Target: Set filter condition to
id
equals current item data’sid
, this is a crucial step to ensure only the correct record is updated. - Execute Update: Update the
like_count
field value tolike_count + 1
.
- Specify Target: Set filter condition to
- Chain Operation: Send Notification: After the like action succeeds, immediately add a second action - “Insert notification”, writing a new data entry to the
notification
table. - Use Context Data: The notification content can be dynamically generated, for example, sender
sender_account
is the logged in user’s ID, receiverreceiver_account
is the current item data’sauthor
, and message contentmessage
is generated by concatenating content from the previous step’s result data.
Notes
- Data Request Types: In addition to regular “Query”, Momen also supports “Subscription”, which can automatically push data to the frontend when changes occur, very suitable for developing real-time chat and other features.
- Security Warning: When executing “Update” or “Delete” operations, be sure to carefully check whether filter conditions are precise. A careless mistake could result in incorrect modification or clearing of entire table data.
- Data Processing: For returned array data, you can use formulas like
GET_ITEM
for processing and extraction.