Blog

How to Create a Table Schema in One Query

by on May 21, 2019

Create Table Schema in One Query

Today, we are going to look at a useful and interesting, but hidden, feature of Backendless. This function will give us the ability to create a multi-column table schema with a single query.

Some features like these are not covered in the documentation, so they are not recommended for use in production because they are not supported and there is no guarantee that their implementation will not change. Still, these features can be very helpful during initial development. Using these features is not for the faint of heart!

Now, daredevils, let’s get started!

For this example, we have the task of creating a table, assigning a name to it (for example, TableWithColumns), creating a column called StringColumn with column type STRING, creating a column called DoubleColumn with column type DOUBLE, and creating a column called  NotNullStringColumn with column type STRING  and constraints NOTNULL. You can still create columns with different constraints, separate ones, etc., but for the sake of demonstration, these items will be enough for now.

Using Backendless Console

Let’s first look at the step by step process if you were to use Backendless Console.

Creating a table:

Create a Table in Backendless

Adding a StringColumn Column:

Add a String Column to Table Schema

And we’d do the same for two more columns.

Using Console Routes

Alternately, the same can be done using console routes:

curl --header "auth-key: <auth-key>" -H Content-Type:application/json -X POST -d '{"name":"TableWithColumns"}' -v -S https://develop.backendless.com/<application-id>/console/data/tables
curl --header "auth-key: <auth-key>" -H Content-Type:application/json -X POST -d '{"name":"StringColumn","dataType":"STRING"}' -v -S https://develop.backendless.com/<application-id>/console/data/tables/TableWithColumns/columns
curl --header "auth-key: <auth-key>" -H Content-Type:application/json -X POST -d '{"name":"DoubleColumn","dataType":"DOUBLE"}' -v -S https://develop.backendless.com/<application-id>/console/data/tables/TableWithColumns/columns
curl --header "auth-key: <auth-key>" -H Content-Type:application/json -X POST -d '{"name":"NotNullStringColumn","dataType":"STRING","required":true}' -v -S https://develop.backendless.com/<application-id>/console/data/tables/TableWithColumns/columns

<application-id>  – the ID of your application generated upon its creation. You can obtain the value in the Manage > App Settings section of Backendless Console.

<auth-key>  – You can find this by checking the Inspect Element in your browser:

Inspect Element to find auth-key

And now the magic begins: all of the above steps can be combined into a single request!

curl --header "auth-key: <auth-key>" -H Content-Type:application/json -X POST -d '{"name":"TableWithColumns","columns": [{"name":"StringColumn","dataType":"STRING"},"name":"DoubleColumn","dataType":"DOUBLE"},{"name":"NotNullStringColumn","dataType":"STRING","required":true}]}' -v -S http://<domain>/<Application ID>/console/data/tables

There you have it. This action gives you the ability to create a table schema with multiple columns in a single query rather than constructing the table in the Backendless user interface. Again, we don’t suggest incorporating this functionality into your finished product, but it can be a useful function during the development of your app.

Thanks for reading, and happy coding!

Leave a Reply