API with NestJS #155. Offset and keyset pagination with the Drizzle ORM
Wanago » TypeScript
by Marcin Wanago
2w ago
As our database grows, maintaining good performance becomes more important. Returning large amounts of data at once through our API can negatively affect efficiency. A common solution is to divide data into smaller chunks, presenting it to the user as infinite scrolling or multiple pages. In this article, we implement this approach using PostgreSQL and the Drizzle ORM. We also compare different pagination methods and their impact on performance. Offset and limit Let’s start by looking at a simple select query that returns all entries from a particular table. articles.service.ts import { Injec ..read more
Visit website
API with NestJS #152. SQL constraints with the Drizzle ORM
Wanago » TypeScript
by Marcin Wanago
1M ago
When working with SQL databases, we can configure constraints to ensure our data does not get corrupted. In this article, we explore and implement different SQL constraints using PostgreSQL, NestJS, and the Drizzle ORM. Not-null constraint By default, SQL columns can hold nulls, representing an absence of value. We need the not-null constraint using the notNull() function to prevent that. database-schema.ts export const articles = pgTable('articles', { id: serial('id').primaryKey(), title: text('title').notNull(), content: text('content').notNull(), authorId: integer('author_id ..read more
Visit website
API with NestJS #151. Implementing many-to-one relationships with Drizzle ORM
Wanago » TypeScript
by Marcin Wanago
1M ago
Managing relationships between tables is a significant part of dealing with SQL databases. In this article, we continue learning to use Drizzle ORM with NestJS and implement many-to-one relationships. Check out this repository if you want to see the full code from this article. The many-to-one relationship With many-to-one relationships, a row from the first table can be connected to multiple rows in the second table. What’s crucial is that the row from the second table can relate to just one row from the first table. A great example is an article that can have a single author. On the other ..read more
Visit website
API with NestJS #150. One-to-one relationships with the Drizzle ORM
Wanago » TypeScript
by Marcin Wanago
1M ago
When building a database, the tables we set up often connect to each other. Managing these relationships is one of the crucial parts of working with databases. In the previous article, we learned how to use NestJS with Drizzle to set up a simple project with PostgreSQL. This time, we go further and write more complex code that involves the one-to-one relationship. You can find the code from this article in this repository. What is a one-to-one relationship? When designing a database with users and their addresses, we could add the address_country, address_city, and address_street to the ..read more
Visit website
API with NestJS #149. Introduction to the Drizzle ORM with PostgreSQL
Wanago » TypeScript
by Marcin Wanago
2M ago
Drizzle is a lightweight TypeScript ORM that lets us manage our database schema. Interestingly, it allows us to manage our data through a relational API or an SQL query builder. In this article, we learn how to set up NestJS with Drizzle to implement create, read, update, and delete operations. We also learn how to use Drizzle to manage migrations. Check out this repository if you want to see the full code from this article. Connecting to the database Let’s use Docker Compose to create a PostgreSQL database for us. docker-compose.yml version: "3" services: postgres: container_name: po ..read more
Visit website
API with NestJS #148. Understanding the injection scopes
Wanago » TypeScript
by Marcin Wanago
3M ago
When a NestJS application starts, it creates instances of various classes, such as controllers and services. By default, NestJS treats those classes as singletons, where a particular class has only one instance. NestJS then shares the single instance of each provider across the entire application’s lifetime, creating a singleton provider scope. If you want to know more about the Singleton pattern, check out JavaScript design patterns #1. Singleton and the Module The singleton scope fits most use cases. Thanks to creating just one instance of each provider and sharing it with all consumers, N ..read more
Visit website
API with NestJS #147. The data types to store money with PostgreSQL and Prisma
Wanago » TypeScript
by Marcin Wanago
4M ago
Managing money is a part of developing web applications that we can’t cut corners on. This article explores various data types built into PostgreSQL and Prisma that we could use to handle finances. We also discuss which ones to avoid and why. Why we should avoid floating point numbers We are used to using fractions when dealing with money – for example, $15.20 means fifteen dollars and twenty cents. Therefore, we might think of using floating-point types to store the monetary values. Using floating-point numbers is a very popular way to represent numbers that include a fraction. schema.pr ..read more
Visit website
API with NestJS #146. Polymorphic associations with PostgreSQL and Prisma
Wanago » TypeScript
by Marcin Wanago
5M ago
Often, we might have a situation where a single entity, such as a comment, needs to be associated with more than one type of table. For example, the user might be able to comment on articles, photos, and more. One solution would be to create a separate table for each type of comment, such as ArticleComment and PhotoComment. This could lead to duplicating a lot of logic since a comment for an article or a photo would contain the same columns, such as the author and the content. An alternative would be to implement polymorphic association. It is a design pattern where a table can be associa ..read more
Visit website
API with NestJS #145. Securing applications with Helmet
Wanago » TypeScript
by Marcin Wanago
5M ago
Ensuring that our application is secure is one of the most important things we must do as developers. One of the ways to protect our application from well-known vulnerabilities is to set appropriate response headers. There are quite a lot of different security-related response headers to consider. Fortunately, the helmet library can set them for us. Helmet maintains a set of various response headers that aim to make applications more secure. When we look at their changelog, we can see that the maintainers keep the list of headers up to date by adding new ones and deprecating headers that are n ..read more
Visit website
API with NestJS #144. Creating CLI applications with the Nest Commander
Wanago » TypeScript
by Marcin Wanago
5M ago
Command Line Applications are very useful for developers. We can interact with them by executing specific commands in the terminal, giving us much control and flexibility. You probably already interact with various CLI applications, such as Git or NPM. Interestingly, we can also use NestJS to create a Command Line Interface (CLI) application. In this article, we learn what we can do with CLI apps and how to implement one with NestJS and the Nest Commander. Command Line Interface applications A straightforward example of a CLI tool is cp, which is used to copy files. cp file.txt copiedFil ..read more
Visit website

Follow Wanago » TypeScript on FeedSpot

Continue with Google
Continue with Apple
OR