Aiven (Enterprise Grade FREE SQL Database)
In This Example
Create and connect to an enterprise grade SQL Database hosted by Aiven.
Do you want direct access to a database that is managed but gives you full control? If yes, this is for you!
Prerequisites
You will need:
Aiven Database Setup
Log into Aiven and create a project and database of your choice. Aiven has a generous free tier for their SQL databases and upgrade to paid databases with high availability is seamless.
Get Your Connection Details
Find your connection details in Aiven:
Then set your Database connection info in the Comnoco Workspace Configuration
panel. Remember items in this screen allow you to set different values per environment eg. development and production.
To do this:
- First set your Database Password as a
Secret
- Use the secret in a new connection which you need to populate with your details from Aiven. See here for connection settings.
Use Your Connection
To use your connection, you’ll need to set up a Comnoco Function to talk to your database. We’re going to create two functions so that you can use the first one, once only, to set up some tables and data.
You can follow the instructions below and build as you go, or you can download the 📦 example and import it into your Comnoco Workspace (if you do this, make sure you name your connection in the above steps Aiven-mysql
so that it matches what is in the example).
Use Comnoco to Set Up a Database
Let’s look at how to create a function that you would run only once to create some sample tables and data to play around with.
- Create a Comnoco
Function Collection
file - Add a
Component Function
to that file - In the
logic
section of your tree add aSQL Database Connect and Validate
block, and in it’s properties select the database connection you just set up. This block opens a connection - if the connection fails, it will return an error (that you can choose to catch to control what you do if the database is unavailable). - Next, we’ll setup a variable
Data structure
inInternal Variables
to receive a response from the database and use aSet Data Structure
to fill it with aSQL Database Execute
(which we’ll setup next). - And now we’re ready to play with SQL. Let’s set up our database, on the
SQL Database Execute
block, chose your connection name and then copy the SQL below and paste it into aText
block that you place on theQuery
slot. You can now go ahead and run your function:
Sample SQL Schema (for mySQL)
Let’s create a simple database schema themed around Formula 1 drivers and their teams. The database will consist of three tables:
drivers
- This table will store information about F1 drivers.teams
- This table will store information about F1 teams.driver_standings
- This table will record the standings of drivers for different seasons.
Here is a basic schema for each of the tables followed by sample data:
- You should see a green tick next to the function that you ran which indicates it worked. You’ll now have some tables and data in your new database that you can play with.
Database Queries (CRUD examples)
Now, let’s create an example function that performs some queries on our database:
- Right click on the the first function we built and select
Duplicate
. - Rename the new function to
Example Queries
Duplicate
theInternal Variable
three times and name each of them the following in turn:Data Structure - Create
,Data Structure - Read
,Data Structure - Update
,Data Structure - Delete
. Your function will now look like this:
Now, for each of these, let’s set up the Comnoco blocks:
Create (Insert)
Add a new a person to our drivers table.
To do this, we’ll change the SQL Database Execute
block that we duplicated. Expand your tree to find it under the Set Data Structure
(Which you should rename to Create
) then:
- Delete the
Text
block that contained our SQL. In it’s place put aSQL Database Safe Operation
block. - In the
Query Template
slot, add aText
block and fill it with the below SQL:
- Now in the
Arguments
list slot, add the input parameters as Text with values you’d like to add, like this: - You can now run your
Example Queries
function and it will add this record to your drivers table. It will give you a green tick to show it has worked.
Read (Query)
Select all information about a driver and their total career points by passing in a drivers Last Name.
Let’s move onto querying (reading) from our database:
- Duplicate your
Create
Set Data Structure
block and rename it toRead
. - In the
Query Template
slot, change the content of theText
block to the below SQL:
- Delete your unneeded
Arguments
leaving justlast_name
and change it’s value toAlonso
(that will return a good result). - Change the data structure that it sets the result to from the
Create
toRead
one (you can right click and select replace). - Now you can run your
Example Queries
function and it will run both the Create and Read actions. We just set up the Read so to see the result click on yourData Structure - Read
internal variable. It should look like this:
Other Examples
Select all teams and the number of wins they’ve achieved.
Select all drivers with their respective teams for a particular season (e.g., 2004).
Update
To see how to update a record:
Follow the same steps we took for Read but with the below sql and two parameters of last_name
driver_id
(eg. setting it to 2
will allow you to over write Senna
with a last_name
you choose).
Delete
To see how to delete a record:
Follow the same steps we took for Read but with the below sql and one parameter of driver_id
(eg. setting it to 1
will allow you to delete Schumacher
).
Your final Function Collection should look like this: