Posts

Showing posts with the label drizzle

Using Drizzle ORM

Image
Introduction This is a next-generation TypeScript-first ORM (Object-Relational Mapping) library designed to interact with relational databases ( PostgreSQL, MySQL, SQLite ) in a lightweight, Type-safe and high-performance manner. Unlike traditional ORMs (such as Prisma, TypeORM, Hibernate ) that hide SQL behind complex abstraction layers, Drizzle follows the philosophy If you know SQL, you know Drizzle . It serves as both an ORM and a powerful Query Builder . Advantages Absolute TypeScript & Type-safe standard (100% Type-Safe): Schema is defined directly using TypeScript functions. When you write queries, Drizzle automatically infers the return type without needing cumbersome code generation steps. Extremely high performance & super lightweight ( Zero Overhead ): Drizzle has no heavy dependencies and does not use background engines (such as Rust binaries in Prisma ). Its ultra-small package size helps optimize cold start times, making it ideal for Serverless and Edge En...

Using Zod to validate data in NestJS

Image
Introduction This article will guide you on using Zod combined with Drizzle ORM to validate data such as payload body, url param, object and more. If you are using Drizzle ORM , Zod is almost the overwhelming choice thanks to its ability to automatically generate Schema from the Database , completely eliminating duplicate code typing and making the application type-safe. Besides using Zod , we also need to combine additional packages as follows drizzle-zod to automatically generate Zod schema from Database Tables . Helps automatically generate Zod Schemas directly from tables defined in Drizzle ORM. Avoids defining data twice (once in the database table in Drizzle, once creating Zod Schema to validate request body). Main features: Automatic mapping: Converts Drizzle data types (string, number, boolean, date, enum) into corresponding Zod schema. Flexible schema creation: Supports generating schema for inserted data ( createInsertSchema ), queried data ( createSelectSchema ) or upd...