Posts

Showing posts with the label development

READ-THROUGH

Image
Introduction This article will guide you on implementing READ-THROUGH in NestJS with Redis and Postgres I will also use @nestjs/cache-manager to create an Abstraction layer That is a Cache Interceptor acting as the single source of truth where the app only needs to retrieve data from it. If the cache is unavailable, it will query the Database to fetch data and save it into the cache Regarding the theoretical content, you can review the previous article where I covered it Detail Please install the following packages bun add cache-manager cache-manager-redis-yet @nestjs/cache-manager Create file module/cache-manager.module.ts to create CacheManagerModule connecting to Redis . The app will view this as the data source and only fetch data from here import { CacheModule } from '@nestjs/cache-manager' import { Module } from '@nestjs/common' import { ConfigModule , ConfigService } from '@nestjs/config' import {redisStore} from 'cach...

Implement GraphQL with Drizzle

Image
Introduction In the previous article, I guided you on how to use Drizzle to connect with Postgres . Now, we will continue using it to implement GraphQL . Next, we need to combine it with the following tools: drizzle-graphql This is the official library developed by the Drizzle ORM team itself to automatically generate a GraphQL Schema (including full Queries and Mutations ) from your Drizzle Schema . GraphQL Yoga Serves as an extremely lightweight and modern GraphQL Server Framework (replacing the older/heavier Apollo Server ). Functions: Listens for HTTP connections from the client (for example at port 4000/graphql ). Receives GraphQL (Query/Mutation) queries sent via POST requests. Executes handler functions ( Resolvers ) and returns JSON data to the client. Provides a built-in GraphiQL interface on the browser for you to test queries. Detail In this article, I will guide you on how to implement a simple CRUD GraphQL system. You only need to define the Schema and connect ...

Resolve N+1 Query Problem

Image
Introduction N+1 Query Problem is a performance issue that occurs when an application executes 1 initial query to fetch a list of N records and then executes N additional sub-queries to fetch related data for each record. The total number of queries sent to the Database will be N + 1 . As N increases (for example N = 1000), the application must execute 1001 SQL statements, causing an I/O bottleneck , increasing latency and overloading the Database . Example Suppose you need to display 10 products (N = 10) along with the list of variants for each product: First Query (1): Get the list of 10 products: SELECT * FROM products LIMIT 10 Next N Queries (N = 10): Iterate through each product to get variants. SELECT * FROM product_variants WHERE product_id = 1 ; SELECT * FROM product_variants WHERE product_id = 2 ; ... SELECT * FROM product_variants WHERE product_id = 10 ; Total: 10+1=11 SQL queries. If fetching 1,000 products, the number of queries spikes to 1,001 SQL ...