Friday, October 4, 2024
HomeJavaScriptAPI with NestJS #165. Time intervals with the Drizzle ORM and PostgreSQL

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


Typically, in our utility, we want to outline a selected period of time. We may symbolize it as various seconds, for instance. Nevertheless, we would wish to be extra versatile and be capable of use numerous models, reminiscent of minutes, hours, days, or weeks. On this article, we discover ways to obtain that with PostgreSQL and the Drizzle ORM utilizing intervals.

Defining an interval

Luckily, the Drizzle ORM helps the
column kind constructed into PostgreSQL. Let’s use it.

database-schema.ts

Validating enter information

We should guarantee customers use the proper information format earlier than letting them add rows to our new desk. ISO 8601 is a well-liked format for dates and time-related information. An interval outlined with ISO 8601 begins with the letter
adopted by the interval worth. The letter
separates the date from the time.

An instance of a legitimate ISO interval is
.

Years Y
Months / Minutes M
Weeks W
Days D
Hours H
Seconds S

We are able to use the
letter both to point minutes or months relying on whether or not we use it earlier than or after
.

The desk above exhibits that
means 2 years, 3 months, 4 days, 5 hours, 6 minutes, and seven seconds.

Let’s validate the information customers ship by our API earlier than placing it into our database. Sadly, JavaScript doesn’t help ISO intervals out of the field. We are able to use a date library reminiscent of Luxon to create a customized validator with the
library to deal with this.

IsIsoInterval.ts

We are able to now use the customized validator in our DTO.

CreateFood.dto.ts

This method permits us to reply with the 400 Dangerous Request standing if the consumer gives an invalid interval.

Interval codecs constructed into PostgreSQL

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

postgres

The database returned the interval in a format totally different than ISO. By default, PostgreSQL represents the intervals utilizing a format known as
. We are able to test that out by wanting on the
worth.

The
format explicitly specifies years, months, and days adopted by the point within the
format.

We use
to transform a string to an interval.

We are able to go a step additional and specify microseconds, milliseconds, weeks, a long time, centuries, and even millennia. Furthermore, we are able to use the
signal to negate part of the worth.

Alternatively, we are able to use the
key phrase to negate all components of the date to realize a detrimental interval.

postgres_verbose

One other format is
, the place
is changed with hours, minutes, and seconds acknowledged explicitly. To alter
to
in a selected session, we have to run the
command in our PostgreSQL database.

In
, the identical guidelines apply when coping with detrimental intervals as with the
format.

sql_standard

Alternatively, PostgreSQL additionally helps the
interval output format. Once we use it, PostgreSQL outputs the intervals utilizing the SQL normal.

It begins with the years and months separated by a touch, adopted by the variety of days and the time separated by areas.

To create a detrimental interval, we should use the
 signal as an alternative of
subsequent to each part we wish to negate.

iso_8601

Luckily, we are able to additionally use the ISO 8601 format, which we defined firstly of our article. To alter the interval format in our database completely for all database connections, we have to run the next command

the place
is the identify of our database. As soon as we try this, PostgreSQL begins utilizing the ISO format for intervals.

To outline a detrimental interval, we should use the
signal earlier than each a part of our interval that we wish to negate.

Working with intervals

Due to altering the interval format in our database to
completely, it additionally impacts our Drizzle ORM queries.

Whereas Drizzle ORM queries the interval as a easy string, we are able to use Luxon to parse it and work together with it.

meals.service.ts

For instance, within the
technique, we lower the expiration interval by someday. To do this, we used the
technique constructed into Luxon. If you wish to know extra about how one can work together with intervals with Luxon, take a look at the official documentation.

Capabilities and operators in PostgreSQL

PostgreSQL permits us to carry out numerous operations with intervals. For instance, we are able to subtract them from dates.

We are able to additionally add and subtract intervals from one another.

Moreover, we are able to use math to multiply and divide the intervals.

Abstract

On this article, we explored the interval information kind in PostgreSQL and realized tips on how to use it with Drizzle ORM. To do this, we needed to get acquainted with numerous interval codecs constructed into PostgreSQL. Moreover, we used the Luxon library to implement validation in our API and to work together with the intervals in our TypeScript code. Due to combining the intervals performance constructed into PostgreSQL with Luxon’s date and time dealing with, we had been capable of handle the interval information successfully.

Sequence Navigation

<< API with NestJS #164. Enhancing the efficiency with indexes utilizing Drizzle ORM

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments