Lesson 1 - Bring in Data
This tutorial takes you through the process of connecting a datasource and querying data on Appsmith.
Connect datasource
In your application, go to the sidebar and click the Data button.
Click the + icon next to Datasources in your workspace to add a new datasource.
Select PostgreSQL under the Databases section. This opens the page where you can configure the fields to connect to your PostgreSQL database.
Rename the default database name from Untitled datasource 1 to
usersTutorialDB
. You may have to click the pencil icon next to the database name if it is not already selected.Enter the following details in the connection parameter fields:
Host Address:mockdb.internal.appsmith.com
Port:5432
Database Name:users
Username:users
Password:new-users-db-pass
Click the Test Configuration button to test the connection and ensure the database is valid.
Click Save to create and save the database connection. You'll see the
usersTutorialDB
database page.
Query Data
Click the + New Query button to the right of the screen. You will see the query editor with a default fetch query to pull ten records from the
usersTutorialDB
database table.Rename the query from Query1 to
getUsers
. You may have to click the pencil icon if it is not already selected.For this tutorial, modify the query as shown below to fetch the records in the ascdending order of the
id
field.SELECT * FROM public."users" ORDER BY id LIMIT 10;
Click the Run button on the top right of the screen to execute the query and confirm that it returns data.
Click the Settings tab. Switch on the Run query on page load option.
You've created your first query to fetch the list of records in the database.
Display data in Table
Click the Widgets tab on the Entity Explorer to the left of the screen.
Drag a Table widget and drop it to the left of the canvas.
A Property Pane appears to the right of the screen, which contains all the properties of the widget. On the top of the property pane, click on the default name Table1 and rename it to
usersTable
.In the Table Data property, click the JS button next to it.
To display the data from the getUsers query, type in
getUsers.data
between the curly braces. This JavaScript expression connects the data from the getUsers query to the Table widget.
The mustache template {{}}
is used to write JS code inside widgets and queries on Appsmith.
You've displayed the results from the getUsers query on the Table widget.