API with NestJS #165. Time intervals with the Drizzle ORM and PostgreSQL

NestJS SQL

This entry is part 165 of 168 in the API with NestJS

Sometimes, in our application, we would like to define a specific duration of time. We could represent it as a number of seconds, for example. However, we might want to be more flexible and be able to use various units, such as minutes, hours, days, or weeks. In this article, we learn how to achieve that with PostgreSQL and the Drizzle ORM using intervals.

Defining an interval

Fortunately, the Drizzle ORM supports the column type built into PostgreSQL. Let’s use it.

database-schema.ts

Validating input data

We must ensure users use the correct data format before letting them add rows to our new table. ISO 8601 is a popular format for dates and time-related data. An interval defined with ISO 8601 starts with the letter followed by the interval value. The letter separates the date from the time.

An example of a valid ISO interval is .

YearsY
Months / MinutesM
WeeksW
DaysD
HoursH
SecondsS

We can use the letter either to indicate minutes or months depending on whether we use it before or after .

The table above shows that means 2 years, 3 months, 4 days, 5 hours, 6 minutes, and 7 seconds.

Let’s validate the data users send through our API before putting it into our database. Unfortunately, JavaScript does not support ISO intervals out of the box. We can use a date library such as Luxon to create a custom validator with the library to address this.

IsIsoInterval.ts

We can now use the custom validator in our DTO.

CreateFood.dto.ts

This approach allows us to respond with the 400 Bad Request status if the user provides an invalid interval.

Interval formats built into PostgreSQL

Let’s make an HTTP request and create a row in our table.

postgres

The database returned the interval in a format different than ISO. By default, PostgreSQL represents the intervals using a format called . We can check that out by looking at the value.

The format explicitly specifies years, months, and days followed by the time in the format.

We use to convert a string to an interval.

We can go a step further and specify microseconds, milliseconds, weeks, decades, centuries, or even millennia. Moreover, we can use the sign to negate a part of the value.

Alternatively, we can use the keyword to negate all parts of the date to achieve a negative interval.

postgres_verbose

Another format is , where is replaced with hours, minutes, and seconds stated explicitly. To change to in a particular session, we need to run the command in our PostgreSQL database.

In , the same rules apply when dealing with negative intervals as with the format.

sql_standard

Alternatively, PostgreSQL also supports the interval output format. When we use it, PostgreSQL outputs the intervals using the SQL standard.

It starts with the years and months separated by a dash, followed by the number of days and the time separated by spaces.

To create a negative interval, we must use the  sign instead of next to every section we want to negate.

iso_8601

Fortunately, we can also use the ISO 8601 format, which we explained at the beginning of our article. To change the interval format in our database permanently for all database connections, we need to run the following command

where is the name of our database. Once we do that, PostgreSQL starts using the ISO format for intervals.

To define a negative interval, we must use the sign before every part of our interval that we want to negate.

Working with intervals

Thanks to changing the interval format in our database to permanently, it also affects our Drizzle ORM queries.

While Drizzle ORM queries the interval as a simple string, we can use Luxon to parse it and interact with it.

food.service.ts

For example, in the method, we decrease the expiration interval by one day. To do that, we used the method built into Luxon. If you want to know more about how you can interact with intervals with Luxon, check out the official documentation.

Functions and operators in PostgreSQL

PostgreSQL allows us to perform various operations with intervals. For example, we can subtract them from dates.

We can also add and subtract intervals from each other.

Additionally, we can use math to multiply and divide the intervals.

Summary

In this article, we explored the interval data type in PostgreSQL and learned how to use it with Drizzle ORM. To do that, we had to get familiar with various interval formats built into PostgreSQL. Additionally, we used the Luxon library to implement validation in our API and to interact with the intervals in our TypeScript code. Thanks to combining the intervals functionality built into PostgreSQL with Luxon’s date and time handling, we were able to manage the interval data effectively.

Series Navigation<< API with NestJS #164. Improving the performance with indexes using Drizzle ORMAPI with NestJS #166. Logging with the Drizzle ORM >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments