Using Nested Queries with the Database Query Builder

Intro

You can access the nested data created with the help of sub tables using a single database query in your API Action. This way you don’t need complicated workflows such as using repeats and nesting database queries inside them to access nested data.

Nested Queries

First, create a new API Action:

And add a name to it:

Then add a new step inside it:

Select Database Query:

Open the Query Builder:

And in the Tables dropdown you will see main tables and their sub tables. The sub tables appear under the main tables and start with /:

We select our blog_posts table:

You can see the table columns:

As well as its nested sub table:

Select the columns you want to output as well as the sub table and add them to the selected columns list:

Double clicking the Sub Table name opens a separate query builder for it:

Here you can choose which fields to output:

Or do some filtering as well:

Click OK, when you are done adjusting the sub table options:

Click OK:

And save your server action:

You can preview the results in your browser:

And see that the nested data structure is being returned by our database query. You can se the nested query results (comments per each blog article):

You can use this data on the front end to create nested repeats with the available data.

Using Nested Data On Your Page

Now after we have the nested data returned by our query, let’s add in on our page.
We created a basic layout using a row with column for the blog posts and another row with column nested inside it, for the comments. Let’s add the dynamic data to this layout:

Add a new component in App Structure:

Under Data select Server Connect:

And click the Select Server Action button:

We select the server action returning our blog articles and their comments:

Main Repeat

Then select the row that contains our blog posts and open the Convert To menu:

Select Repeat Children to make this row a Repeat Children region:

And select a dynamic expression for the repeat:

This should be the main database query, returning our blog posts:

Turn off App Connect mode from the top toolbar (the thunder icon) and you can then see the content inside the repeat region and bind the data. We double click the post title and click the dynamic data picker icon:

And select the title returned by our database query:

Do the same for the rest of the dynamic data for the blog post - date, author and text:

Nested Repeat

Then we select the row, nested in the blog column and open the Convert To menu:

We select Repeat Children:

And let’s select the dynamic expression for it:

Here, we select the nested database query, called comments - available in the data picker, inside the repeat region which we created for the blog articles:

Then let’s bind the dynamic data inside this repeat children region:

You can pick the data from the data picker, inside the nested repeat which we just created:

Do the same for all the dynamic data inside the nested repeat. Save your page when you are done:

And let’s preview the results in the browser. You can see the comments, nested under the specific blog article they belong to:

That’s how easy it is to create and use nested database queries with Wappler.

5 Likes

Looks really helpful. :+1:

Can you guys confirm if this is just creating a subquery here or merging JSONs after running two separate queries… or something else? And how efficient is this?

Also, is this sub table relation based on primary & foriegn keys? What would happen to table which have foreign key references of each other?

Yes sub queries work fully with database relations defined with primary and foreign keys. Those are actually auto created when you add a sub table innige database manager.

Query builder creates a single json output where all subqueries are available as nested data. So you have all your data in a single query!

You can fully define any sub query by calling a nested query builder dialog when double clicking on the grid nested query column in the query builder.

1 Like

Thanks for the details George.
What I was asking is if the nested query logic you guys have implemented is actually just subqueries behind the scenes or are you runnig the queries separately and then merging the JSONs?

We have pretty intelligent sub query running that actually executed just two database queries instead of sub query for each main row and mergers them automatically.

We are even also looking for more optional ways to run just a single query for modern databases that support json data and generate all the nested output at once.

3 Likes

So it would be more efficient in general use cases where we use nested repeats or complex custom queries. Glad to know that.

I am not sure how well this conncept will work with existing devs like us who maintain control over DB directly.
But I really like this idea of connected table for no-code development & makes query builder really powerful even without having to use Wappler’s DB manager.

Yes the new nested queries will perform much better, specially when you have many nested records.

It should also work just fine with custom databases as long as you have your relations well defined with keys and sub tables prefixed with the main table name.

The database manager will auto import such schema’s on tables schema refresh.

It runs the main query and a query for each sub table, it is more efficient then doing a query for each record in a repeater. The results of the queries are afterwards merged together to create the nested structure.

2 Likes

Is the efficiency significantly better in the long term for scaling? If it is, then I’ll change my current repeat workflows to using this.

3 posts were split to a new topic: The new database sub tables on existing databases schemas

A post was split to a new topic: Problem querying nested sub-tables

Thanks for this super cool feature! (without reading this tutorial) I actually struggled to find the option to specificy my subtable query. Now in your tutorial I see you should double click to go to the subtable query. I think the UI could support this a little better, so it becomes clearer that you can just double click :grin:

Amazing feature!

1 Like