Using Pipes Inside Your App
Transcript Summary
Introduction to Pipes
Hey everybody, it's Tim from Database here, and welcome back to the next section of our Introduction to Pipes. In the last video, we covered the general pipes interface, including how to browse for pipes, understanding available pipes, the difference between configurable and non-configurable pipes, and the detailed configuration setup for pipes like Database Date Utilities, Weather Pipe, and the Database REST API.
Understanding the Interface
We explored various tabs available in the pipes configuration, such as API Calls, Global Parameters, and Global Headers. Within the API Call section, we reviewed tabs like Overview, where you set methods and endpoints, as well as tabs for authorization headers, request, test, and response.
Now that we’ve covered how to install and configure pipes, we’ll take a look at practical applications. Pipes can be triggered in different locations within the Database Builder, such as Data Builder, Page Builder, and the Automation Tab. Once you understand the basics of setting up a pipe, this knowledge can be applied across these different areas. However, there are unique use cases where pipes are especially useful.
Practical Examples of Pipes
For simplicity, we’ll use the three pipes we previously installed: Date Utilities Pipe, Weather Pipe, and REST API Pipe. We’ll explore examples ranging in complexity, starting with simple utility pipes, then moving on to external API communication with the Weather Pipe, and finally, the REST API Pipe which offers the ultimate level of flexibility.
Example 1: Using Pipes in Data Builder
The Data Builder is typically where you start building out your application. In this example, we’re working with Event Records. When creating event records, we want to gather data like the event date. Our goal is to use the Date Utilities Pipe to return the day of the week based on the event date.
We’ll use a Table Rule to trigger the pipe whenever a record is created or edited. Once you have the pipe installed, you’ll see an option to add the pipe within the rule interface. We’ll select the Date Utilities Pipe and choose the Get Day Name action, feeding dynamic data into the parameters (in this case, the event date). The Response from the pipe can then be saved to a field called Day of the Week.
Example 2: Using Pipes in Form Record Rules
We can trigger pipes within Form Record Rules just like in the table rules example. In the Page Builder, when adding a new event record, we configure the form to trigger the Date Utilities Pipe. The pipe runs based on the event date and stores the day of the week in a designated field. This setup ensures the same functionality as the table rule but is triggered within a form.
Example 3: Using Pipes with Action Links
You can also trigger pipes via Action Links. For example, if you add an action link to update records, you can trigger the Get Day Name pipe, feed in the event date, and update the day of the week field in the record.
Example 4: Using Pipes in Scheduled Tasks
When working with large data sets, you can use pipes in Scheduled Tasks to automate updates. For example, if you have hundreds of event records, you can configure a scheduled task to trigger the Get Day Name pipe for each record and update the relevant fields.
Using External APIs with Pipes
Now, let’s explore examples of getting real-time data from external APIs using the Weather Pipe. This pipe communicates with the Open Weather Map API to fetch current temperature data based on a zip code.
Example 1: Weather Pipe in Data Source
In the Page Builder, we set up a table to display event data and include a Location field. The zip code from this field is passed to the Weather Pipe, which fetches the current temperature. The Pipe Fields (temperature and feels-like temperature) are added as columns to the table, displaying real-time weather data for each event location.
Example 2: Using Weather Pipe in Forms
Let’s say you have field service workers who need to record measurements along with the current temperature. In a form, you can add a button that triggers the Weather Pipe when clicked. The technician can input the zip code, retrieve the current temperature, and verify the data before submitting the form.
Advanced Example: Pipes in Validation Rules
For a more complex example, we can use pipes in Validation Rules. Imagine users purchasing Credit Packs to post jobs on a job board. Each job post requires a certain number of credits, and users are only allowed to post if they have enough credits.
To validate this, we use the Database REST API Pipe to look into the User Table, find the logged-in user by email, and retrieve the number of available credits. If the user has enough credits, the form submission proceeds. If not, an error message is shown.
Configuring the Pipe for Validation
We configure the pipe to filter the User Table by the user’s email address and return the number of available credits. The Response from the pipe is mapped and used in the validation rule to ensure the user has sufficient credits to post a job.
Setting Up Validation Rule with API Integration
We'll make sure we return this as a number since we want to make a number comparison against the value that we're returning back into our page builder. Inside this form, we can then continue to set up our validation rule.
We're going to add that new validation rule, and we're going to trigger a pipe as part of this validation rule. We'll run our database REST API pipe, choose the "filter by a single field" API call, and set the filter parameter to the logged-in user's field of email. Then, we'll select what we want to validate against.
We’re going to have our form inputs available, and we’ll also have the pipe responses available. We can choose the "available credits" response and check whether those available credits are lower than the form field value of "how many weeks would you like this post to be active for?" In this case, one credit equals one week.
For example, when a user has five credits and they select six weeks for their post, their credits are lower than the number of weeks they'd like it to be active for, and they will receive a message saying they don't have enough credits or they fail the validation.
Testing the Validation
Let’s log in and take a look at what that validation looks like. You can see we have five credits. I can put in our job post, and if we want this to be active for 10 weeks, when I click save, it triggers the validation based on the API response, and we get our message. If I lower it to an acceptable value, like five weeks, the validation passes through the API response, and we're good to go.
Using Page Rules for Conditional Access
In a similar vein, let’s say we want to stop a user from making a job post based on some extracted value. We did that through validation rules, but we could also use page rules. For this example, we’ll set up another pipe using the Stripe pipe.
We'll install the Stripe pipe, configure it, and store our API key in global parameters. In this case, we want to allow someone to make a job post or access a page only if their subscription is active, and that subscription is stored in Stripe.
We’ll choose to get the subscription details in our user's table where we're storing the subscription ID from Stripe. We’ll pass that to Stripe’s API and get a response to check whether the subscription is active. If it’s not, we’ll show a message that the user doesn’t have access to the page because their subscription is not active.
Adding the Page Rule
We’ll add a new page rule to show this message based on the Stripe pipe response. We’ll add a condition to check if the subscription status returned by Stripe is "not active." If so, the user will be blocked from accessing the page, and a message will be shown.
To test, we can visit the page. If the subscription is active, we have access. If the subscription is canceled and we try to access the page again, it communicates with Stripe’s API and blocks access.
Triggering Pipes via JavaScript
There’s one more advanced example I want to show: triggering pipes via JavaScript. Inside any page layout, you have access to a JavaScript tab where you can write custom JavaScript to communicate with external APIs. However, doing this exposes your API credentials, but with "Pipes Script," you can trigger pipes securely from JavaScript without exposing credentials.
For any configurable pipe, such as the Stripe pipe, we can copy a JavaScript snippet that contains a specific and unique ID for the pipe. When this code is placed inside the JavaScript tab of your page, the pipe is triggered securely behind the scenes.
Debugging Pipe Responses
Using Pipes Script, we can log responses directly into the developer console. This is helpful for debugging, especially when an error occurs. You can enable or disable debug mode in your pipe to log the responses and see what’s happening when the pipe is triggered. This is especially useful when something isn't working, as it allows you to see the full response from the pipe in real time.
Recap and Conclusion
Throughout this lesson, we learned how to install and configure pipes, explored basic REST API terminology, and used API documentation to create pipes. We’ve also triggered pipes through various rules, including table rules, form rules, validation rules, and page rules. Additionally, we learned about advanced usage, like triggering pipes via JavaScript using Pipes Script.
Pipes are a powerful tool that bridges the gap between your database application and external data. Thanks for watching this lesson on how to use pipes inside of Tadabase. We’ll see you next time!
Conclusion
Pipes offer powerful integration capabilities in your Database application, from simple date utilities to complex API communications. By triggering pipes in various rules, forms, action links, and scheduled tasks, you can leverage external data sources, validate records, and automate tasks efficiently. Thanks for watching, and I’ll see you in the next video!
We'd love to hear your feedback.