Posts

Showing posts with the label zod

Guide to using Pothos GraphQL

Image
Introduction Pothos GraphQL is a GraphQL Schema Builder , which is a library that helps you write/define a GraphQL Schema using TypeScript code (a Code-First API approach). Functions Helps define each Query , Mutation and GraphQL Type using strongly typed TypeScript code, automatically catching errors to ensure 100% Type Safety . Pothos has a built-in plugin to connect with Drizzle (@pothos/plugin-drizzle) . You only need to declare tables in Drizzle corresponding to Objects in GraphQL and Pothos will automatically understand relations, pagination , helping you avoid writing complex SELECT/JOIN statements. Detail bun add @pothos/core @pothos/plugin-drizzle @pothos/plugin-zod Create file src/drizzle-orm/schema/schema.ts import {sql} from 'drizzle-orm' import { check, decimal, integer, pgTable, serial, text, timestamp, unique, varchar, } from 'drizzle-orm/pg-core' export const baseColumns = { id : serial ( 'id' ). primaryKe...

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...