Vue Query Builder With Cube.js

Leonid Yakovlev
Jul 16, 2020 · 12 min read
Image for post
Image for post

Quite commonly in our applications, we need to create interactive report builders to let users build custom reports and dashboards. This usually involves selecting the metrics, groupings, date ranges, filters, and chart types. To help developers build such interactive components, we’ve created a query builder component in Cube.js client libraries.

We’ve already covered how to use it in the React client library, and in this blog post, we’ll talk about using it with Vue.js. The query builder component uses the scoped slots technique and lets developers implement their own render logic. This way it gives maximum flexibility for building a custom-tailored UI with minimal API. Below you can see the demo of the query builder component with Vuetify.

You can find the live demo of the example here and its source code is available on Github.

Image for post

Setup a Demo Backend

if you already have Cube.js backend up and running you can skip this step.

Let’s start by setting up a database with some sample data. We’ll use PostgreSQL and our example e-commerce dataset for this tutorial. You can download and import it by running the following commands.

Next, install the Cube.js CLI if you don’t have it already, and create a new project.

Cube.js uses environment variables for configuration, which starts with . To configure the connection to our database, we need to specify the DB type and name. In the Cube.js project folder replace the contents of file with the following:

Now that we have everything configured, let’s start the Cube.js development server with the following command.

Navigate to http://localhost:4000 in your browser to access the Cube.js Playground. It is a development environment that generates the Cube.js schema, creates scaffolding for charts, and lets you test your queries and debug them. It also has its own query builder, which lets you generate charts with different charting libraries.

Image for post
Image for post

Now let’s move on to building our own query builder with Vue.js! 💻

Building a Query Builder

We’re going to use Vue CLI to generate a new project. Run the following command to install Vue CLI if you don’t have it already.

To a create a new project with Vue CLI run the following command inside the Cube.js project folder.

To render the UI for query builder we are going to use Vuetify, a Vue UI Library with material components. Let’s add it to our project. Run the following command inside the folder.

To create our color scheme you can open and add this code:

Now we can start our frontend application.

You can check your newly created Vue app at http://localhost:8080. Next, let’s install dependencies we’ll need for building our Query Builder: Cube.js Vue client, Vue Chartkick, and Chart.js.

Let’s create a first simple Query Builder to allow users to select the metric from the dropdown list and then render it as a line chart over time.

Cube.js Query Builder component can load the list of available measures based on the data schema from the backend. We can access these measures as an slot prop. We'll render that list with the component from Vuetify. Then when the user selects the measure, we're going to use a slot prop to update the measures in our query and finally render the query result as a line chart with Vue Chartkick and Chart.js.

You can learn more about other slot props in the Query Builder component in the docs.

Replace the content of the file with the following.

Now we can use updated component in our component. Replace the content of the with the following.

The last small change we need to make is to register the plugin. Update the file.

We’ve just built our first simple query builder 🎉. Navigate to http://localhost:8080/ in your browser and you should be able to test it out.

Image for post
Image for post

We can already plot the count of orders over time. But what if we want to see the breakdown of orders by the status? To do so, we’d need to introduce the dimensions dropdown to let users select the grouping option.

We’ll use more slot props for this: , and . They work they same as slot props for measures allowing us to list the available dimensions and update the list of selected ones.

Update file.

Refresh your browser and now you should be able to select the dimensions for grouping as well!

Image for post
Image for post
Vue Query Builder with Cube.js

#vue#javascript#tutorial#webdev

Quite commonly in our applications, we need to create interactive report builders to let users build custom reports and dashboards. This usually involves selecting the metrics, groupings, date ranges, filters, and chart types. To help developers build such interactive components, we’ve created a query builder component in Cube.js client libraries.

We’ve already covered how to use it in the React client library, and in this blog post, we’ll talk about using it with Vue.js. The query builder component uses the scoped slots technique and lets developers implement their own render logic. This way it gives maximum flexibility for building a custom-tailored UI with minimal API. Below you can see the demo of the query builder component with Vuetify.

You can find the live demo of the example here and its source code is available on Github.

Image for post
Image for post

Setup a Demo Backend

if you already have Cube.js backend up and running you can skip this step.

Let’s start by setting up a database with some sample data. We’ll use PostgresQL and our example e-commerce dataset for this tutorial. You can download and import it by running the following commands.

Next, install the Cube.js CLI if you don’t have it already, and create a new project.

Cube.js uses environment variables for configuration, which starts with . To configure the connection to our database, we need to specify the DB type and name. In the Cube.js project folder replace the contents of file with the following:

Now that we have everything configured, let’s start the Cube.js development server with the following command.

Navigate to http://localhost:4000 in your browser to access the Cube.js Playground. It is a development environment that generates the Cube.js schema, creates scaffolding for charts, and lets you test your queries and debug them. It also has its own query builder, which lets you generate charts with different charting libraries.

Image for post
Image for post

Now let’s move on to building our own query builder with Vue.js! 💻

Building a Query Builder

We’re going to use Vue CLI to generate a new project. Run the following command to install Vue CLI if you don’t have it already.

To a create a new project with Vue CLI run the following command inside the Cube.js project folder.

To render the UI for query builder we are going to use Vuetify, a Vue UI Library with material components. Let’s add it to our project. Run the following command inside the folder.

To create our color scheme you can open and add this code:

Now we can start our frontend application.

You can check your newly created Vue app at http://localhost:8080. Next, let’s install dependencies we’ll need for building our Query Builder: Cube.js Vue client, Vue Chartkick, and Chart.js.

Let’s create a first simple Query Buider to allow users to select the metric from the dropdown list and then render it as a line chart over time.

Cube.js Query Builder component can load the list of available measures based on the data schema from the backend. We can access these measures as an slot prop. We'll render that list with the component from Vuetify. Then when the user selects the measure, we're going to use a slot prop to update the measures in our query and finally render the query result as a line chart with Vue Chartkick and Chart.js.

You can learn more about other slot props in the Query Builder component in the docs.

Replace the content of the file with the following.

Now we can use updated component in our component. Replace the content of the with the following.

The last small change we need to make is to register the plugin. Update the file.

We’ve just built our first simple query builder 🎉. Navigate to http://localhost:8080/ in your browser and you should be able to test it out.

Image for post
Image for post

We can already plot the count of orders over time. But what if we want to see the breakdown of orders by the status? To do so, we’d need to introduce the dimensions dropdown to let users select the grouping option.

We’ll use more slot props for this: , and . They work they same as slot props for measures allowing us to list the available dimensions and update the list of selected ones.

Update file.

Refresh your browser and now you should be able to select the dimensions for grouping as well!

Image for post
Image for post

That’s all for this tutorial. Congratulations on completing it! 🎉

There are other controls you can add to your query builder like filters, date range, and granularity, as well as chart type selector. You can find a more complicated example with all these controls here and its source code here on Github.

Please send any comments or feedback you might have to this Slack Community. Thank you and I hope you found this tutorial helpful!

That’s all for this tutorial. Congratulations on completing it! 🎉

There are other controls you can add to your query builder like filters, date range, and granularity, as well as chart type selector. You can find a more complicated example with all these controls here and its source code here on Github.

Please send any comments or feedback you might have to this Slack Community. Thank you and I hope you found this tutorial helpful!

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store