Using Databases

In this example we will show how you can use databases within Busywork.

This example will show you how you can save data from a form in your database. Next we'll have a look at how to read that data.

For this example we are creating a bug reporting tool that saves the bug to the database and sends an email to the developer team. This example assumes you've created a empty Build called e.g. Bug Report.

1. Creating a Table

First navigate to the database view from your Build.

Click the "New Table" button

Create a table named bugs.

2. Add Fields

Our bugs table will contain information about the bug that the user submits. We want to collect information such as description, who submitted the bug and when it was submitted.

The JSON object of the bug will be,

{
"id": 123,
"subject": "Form Validation not Working",
"description": "Some long description of how to recreate the bug",
"submitted_at": 1578149350,
"submitted_by": "john doe"
}

3. Setting up the Function

The fields we've defined for the database are the ones we want filled when a bug is reported. We expect the form to submit the following data,

{
"subject": "This is a subject",
"description": "This is a description",
"submitted_by": "John Doe"
}

Which defines the functions schema. The schema is an example of the request body, in JSON format. Now simply copy and paste the above into the Test Data block in your build.

4. Insert Data

5. Integrate with Postmark to send Emails

Postmark is a service for sending transactional emails. Transactional emails are triggered by the user, such as password resets, order confirmations and receipts. Postmark provides sending emails as a service. With Busywork you can integrate the Postmark services into your backend functions.

You can connect your Postmark account using the API token in your Postmark account. This allows Busywork to send emails on your behalf. Finally you have to configure the to and from email, which template to use and the data for the template.

6. Response Format

As a reponse to the bug report we simply acknowledge that we've received it and send back that we've successfully saved the report.

7. Submit a Bug Report

After deploying the function, we can now test if we are able to submit a bug report to the function. Click the Build name in the top of the window will open a window with the URL of the function and a snippet to run it using cURL.

Now you can simply copy and paste the cURL snippet into your terminal and run it, or use Reqbin to test the function.

Using Reqbin

1. Copy and paste URL and request body

2. Send request

Checking the Database

From the database view we can see that the bug report request has been proccessed correctly and the data is saved in the database.