<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Flávio Ribeiro</title>
    <description>The latest articles on Forem by Flávio Ribeiro (@flavioribeirojr).</description>
    <link>https://forem.com/flavioribeirojr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1611148%2F139afb51-812c-45e4-b2e3-3e556c8fa9e7.png</url>
      <title>Forem: Flávio Ribeiro</title>
      <link>https://forem.com/flavioribeirojr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/flavioribeirojr"/>
    <language>en</language>
    <item>
      <title>Trying out a new stack: my experience working with tRPC and Drizzle on my Next.JS project</title>
      <dc:creator>Flávio Ribeiro</dc:creator>
      <pubDate>Tue, 02 Jul 2024 02:26:07 +0000</pubDate>
      <link>https://forem.com/flavioribeirojr/trying-out-a-new-stack-my-experience-working-with-trpc-and-drizzle-on-my-nextjs-project-3mho</link>
      <guid>https://forem.com/flavioribeirojr/trying-out-a-new-stack-my-experience-working-with-trpc-and-drizzle-on-my-nextjs-project-3mho</guid>
      <description>&lt;p&gt;Recently I started working on a side project and I wanted to make it a fullstack typescript application. I decided to use Next.JS to take advantage of the tools already included in the framework(e.g. router, rsc, caching, etc.). On top of that I added two amazing libraries that make an excellent usage of &lt;strong&gt;Typescript&lt;/strong&gt;: &lt;code&gt;tRPC&lt;/code&gt; and &lt;code&gt;drizzle&lt;/code&gt;, I want to talk a little bit about my experience working with them.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://orm.drizzle.team/"&gt;Drizzle&lt;/a&gt;: a typescript ORM
&lt;/h3&gt;

&lt;p&gt;There are a lot of &lt;em&gt;ORM&lt;/em&gt; and query builders available for Node.JS, you might have used or heard of packages such as Prisma, Knex, Sequelize or Typeorm. All of those will help you connect to a database, create your tables and run your queries, some of them will even let you define the entity types for your schema. What drizzle does different is the way it takes a big advantage of the type system Typescript offers.&lt;/p&gt;

&lt;p&gt;The first thing I noticed when I started working with drizzle is how easy it was to define the schema for my tables. I didn't had to setup annotations or manually define the type for each column. Another thing that I found super helpful is the ability to reference the columns using a &lt;code&gt;camelCase&lt;/code&gt; style while still defining the column name as &lt;code&gt;snake_case&lt;/code&gt;, making easier to make manual queries. See the example below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmont3ywa38e1isi42fi1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmont3ywa38e1isi42fi1.png" alt="Code example of drizzle schema" width="364" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am defining an users table with 3 columns with different types, notice how the object keys are defined using the &lt;code&gt;camelCase&lt;/code&gt; style and in the first parameter of the drizzle constructors(&lt;code&gt;varchar&lt;/code&gt;, &lt;code&gt;date&lt;/code&gt;) I'm defining the name that will be used internally. Using drizzle defining the schema like this will automatically give you types, for example when you define a &lt;code&gt;varchar&lt;/code&gt; column the type inferred will be &lt;code&gt;string&lt;/code&gt;. See below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hkvfb3pfanm2s7eisif.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hkvfb3pfanm2s7eisif.png" alt="Code example of the inferred types on drizzle" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see all of my types are already defined by drizzle.&lt;/p&gt;

&lt;p&gt;Drizzle have a big set of functions that facilitate building typesafe queries, they offer a full solution for making unions, joins and other common sql operations. It is also worth mentioning the magic &lt;code&gt;sql&lt;/code&gt; operator that let you write your own query in case you need to.&lt;/p&gt;

&lt;p&gt;If you want to learn more take a look at their &lt;a href="https://orm.drizzle.team/docs/overview"&gt;docs&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://trpc.io/"&gt;tRPC&lt;/a&gt;: you might not need swagger 🤷🏽‍♂️
&lt;/h3&gt;

&lt;p&gt;One of the difficult parts of working with frontend is when you need to define what comes back from the server. One of the solutions available is to use swagger to document your api and choose between a lot of libraries that can turn the swagger document into a client that handles the calls to your server. A problem that might arise from doing this is how accurate your documentation is as changes are made to the implementation on your server.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tRPC&lt;/code&gt; is a solution for fullstack applications that use Typescript. It make typesafe easy. The types of the inputs and outputs of your &lt;code&gt;procedures&lt;/code&gt; are defined in just one place and you don't even need to explicitly create them, they will be inferred. By having all the server types defined in the actual code that is running in the server it is impossible to create inaccurate type descriptions. Below is an example of a call from a react component to a server procedure, notice how the types are already defined:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vhsaxvv3sbb753vv3s4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vhsaxvv3sbb753vv3s4.png" alt="Code example of using tRPC mutation on a react component" width="800" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Is worth mentioning other two libraries that work really well together with tRPC: &lt;code&gt;zod&lt;/code&gt; and &lt;code&gt;react-query&lt;/code&gt;. With &lt;code&gt;zod&lt;/code&gt; you can at the same time define a schema type and validate it against the actual value, making &lt;code&gt;mutations&lt;/code&gt;, for example, much easier to validate. &lt;code&gt;react-query&lt;/code&gt; is the missing piece for working with promises in React, and tRPC has a really good wrapper around it so that you can easily use your queries and mutations on your components.&lt;/p&gt;

&lt;p&gt;Please note that this only work if you are working with a fullstack Typescript application, if you are working with different languages there is nothing tRPC can do for you. Keep swagging 🕺🏽&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Working with typescript nowadays days feels mandatory when working with javascript, as it improve the developer experience and avoid silly typing bugs, and working with tools that have Typescript at their core is the ultimate way to make the best out of it.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>typescript</category>
      <category>postgres</category>
      <category>trpc</category>
    </item>
  </channel>
</rss>
