This video is available to students only

Completing the Select Date Page

Completing the Select Date Page

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

Ok, at this point we are starting to see end-to-end, most of the hard infrastructure pieces have been put together. So let's try to actually make your app do something. I will increase the pace a bit since most of what we're doing now is just building with the blocks we have already established.

You have the schema for bookings in place, but you are not yet using it. Figuring out whether or not a time slot is available involves a bit of set theory. That is something we can choose to do in a couple of places: in the database, using a stored procedure/function or view, on the backend or on the frontend. I personally like to make the database give me basic data that matches basic invariants. Complex logic is hard to debug for the simple reason that debugging is harder in a database than in a general purpose environment like Node or the browser.

The backend, as I see it, should filter and handle data that is potentially privileged, and it should seek to minimize the amount of data that is sent to the frontend. Once that is done, it's perfectly fine to let the frontend apply logic to data. For this reason, I often work with normalized data all the way through the frontend and only combine things there.

The problem we are facing first is showing the customer which dates and times they can make a booking. Times where every provider in the database are booked should appear as unavailable, but any slot where at least one provider is free should appear as bookable. If the customer chooses said slot, the system should figure out which provider to make the booking with.

getAvailableSlots endpoint#

So, let's create an endpoint that gets available slots within a time frame. We'll make a query on the backend that gets us availability for each provider. Then we'll combine it on the frontend to figure out which days and time slots are book-able.

We'll create a new file for this query as it's not quite as trivial as what we've been doing so far. Create /services/backend/src/trpc/getBookedSlots.ts:

This is a rather complex Knex query, which I would definitely consider turning into a view or stored procedure. Not least because we're pushing Knex itself to its limits and need to fall back to raw here and there. Kanel does not currently support generating types out of functions or stored procedures but I intend to introduce that so it would be as streamlined as everything else. For now though, we are doing this operation on the backend and so we make an explicit type of the output, called slot. The query divides every day into time slots of three hours and returns an array for each provider, telling us whether or not that specific slot is available or booked.

Let's expose this function in an endpoint. Update /services/backend/src/trpc/appRouter.ts:

This lesson preview is part of the Fullstack Typescript with TailwindCSS and tRPC Using Modern Features of PostgreSQL course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

Unlock This Course

Get unlimited access to Fullstack Typescript with TailwindCSS and tRPC Using Modern Features of PostgreSQL, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Fullstack Typescript with TailwindCSS and tRPC Using Modern Features of PostgreSQL