<?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>DEV Community: Henry</title>
    <description>The latest articles on DEV Community by Henry (@graeyy).</description>
    <link>https://dev.to/graeyy</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%2F393417%2F4942d610-acbf-4036-98c4-579fd49f21dc.jpeg</url>
      <title>DEV Community: Henry</title>
      <link>https://dev.to/graeyy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/graeyy"/>
    <language>en</language>
    <item>
      <title>Mastering TypeORM Relationships in NestJS: A Complete Guide</title>
      <dc:creator>Henry</dc:creator>
      <pubDate>Fri, 25 Apr 2025 08:40:44 +0000</pubDate>
      <link>https://dev.to/graeyy/mastering-typeorm-relationships-in-nestjs-a-complete-guide-5ggb</link>
      <guid>https://dev.to/graeyy/mastering-typeorm-relationships-in-nestjs-a-complete-guide-5ggb</guid>
      <description>&lt;p&gt;One of the superpowers of building with &lt;strong&gt;NestJS&lt;/strong&gt; is how seamlessly it integrates with &lt;strong&gt;TypeORM&lt;/strong&gt;, giving developers a clean, structured way to model database relationships using decorators and entities.&lt;/p&gt;

&lt;p&gt;But while getting started with basic models is easy, understanding &lt;strong&gt;relationships&lt;/strong&gt;—how tables/entities connect—is where things get more interesting, and sometimes confusing.&lt;/p&gt;

&lt;p&gt;In this post, we’ll break down how to model and use relationships in &lt;strong&gt;TypeORM&lt;/strong&gt; with &lt;strong&gt;NestJS&lt;/strong&gt;, covering the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One-to-One
&lt;/li&gt;
&lt;li&gt;One-to-Many / Many-to-One
&lt;/li&gt;
&lt;li&gt;Many-to-Many
&lt;/li&gt;
&lt;li&gt;How to use relations in services
&lt;/li&gt;
&lt;li&gt;How to load related data
&lt;/li&gt;
&lt;li&gt;Tips and pitfalls
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s dive in.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is a Relationship in TypeORM?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In database design, a relationship is how tables (or entities in ORM) connect. Think:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;user&lt;/strong&gt; has one &lt;strong&gt;profile&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;post&lt;/strong&gt; has many &lt;strong&gt;comments&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;student&lt;/strong&gt; can be enrolled in many &lt;strong&gt;courses&lt;/strong&gt;, and vice versa&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These connections are defined using relation decorators like &lt;code&gt;@OneToOne&lt;/code&gt;, &lt;code&gt;@ManyToOne&lt;/code&gt;, &lt;code&gt;@OneToMany&lt;/code&gt;, and &lt;code&gt;@ManyToMany&lt;/code&gt; in TypeORM.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Setting Up: Example Models&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We’ll use the example of a blogging system, with &lt;code&gt;User&lt;/code&gt;, &lt;code&gt;Post&lt;/code&gt;, and &lt;code&gt;Comment&lt;/code&gt; entities.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; @nestjs/typeorm typeorm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;1. One-to-One: User &amp;amp; Profile&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Scenario:
&lt;/h3&gt;

&lt;p&gt;Each user has &lt;strong&gt;one profile&lt;/strong&gt;. Each profile belongs to &lt;strong&gt;one user&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  User Entity
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;PrimaryGeneratedColumn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;OneToOne&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Profile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cascade&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;JoinColumn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Profile&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Profile Entity
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;PrimaryGeneratedColumn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;bio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;OneToOne&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;@JoinColumn()&lt;/code&gt; is used on the side that owns the relationship.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;2. One-to-Many / Many-to-One: Post &amp;amp; Comment&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Scenario:
&lt;/h3&gt;

&lt;p&gt;One &lt;strong&gt;post&lt;/strong&gt; can have many &lt;strong&gt;comments&lt;/strong&gt;. Each &lt;strong&gt;comment&lt;/strong&gt; belongs to one &lt;strong&gt;post&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Post Entity
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;PrimaryGeneratedColumn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;OneToMany&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Comment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cascade&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Comment&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Comment Entity
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Comment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;PrimaryGeneratedColumn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;ManyToOne&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is probably the most commonly used relationship in apps: a user has many posts, an order has many items, etc.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;3. Many-to-Many: Students &amp;amp; Courses&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Scenario:
&lt;/h3&gt;

&lt;p&gt;A student can be enrolled in many courses. A course can have many students.&lt;/p&gt;

&lt;h3&gt;
  
  
  Student Entity
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;PrimaryGeneratedColumn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;ManyToMany&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Course&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;course&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;course&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;students&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;JoinTable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;courses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Course&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Course Entity
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Course&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;PrimaryGeneratedColumn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;ManyToMany&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;courses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;students&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;@JoinTable()&lt;/code&gt; on &lt;strong&gt;one side only&lt;/strong&gt; to define the owning side of the relationship.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Loading Relationships in Services&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In your services, you can load related entities using the &lt;code&gt;relations&lt;/code&gt; option in &lt;code&gt;find&lt;/code&gt; or &lt;code&gt;findOne&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// post.service.ts&lt;/span&gt;
&lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;relations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;comments&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Nested Relations:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;relations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;posts.comments&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is powerful for loading deeply nested related data in one go.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Creating and Saving Relationships&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You can also save entities with their relations using cascade options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john_doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;bio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fullstack dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Make sure you set &lt;code&gt;cascade: true&lt;/code&gt;&lt;/strong&gt; on the relation if you're doing this.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Pitfalls &amp;amp; Best Practices&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Don’t overfetch:&lt;/strong&gt; Only include &lt;code&gt;relations&lt;/code&gt; you need. Too many joins = slow queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use DTOs:&lt;/strong&gt; Don’t expose raw entities in your controllers. Transform them into DTOs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoid circular relations in JSON:&lt;/strong&gt; Be careful when returning nested entities—you can easily create circular JSON references.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use transactions:&lt;/strong&gt; When saving deeply nested relationships that depend on each other, wrap in a transaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Always index foreign keys:&lt;/strong&gt; Speeds up joins significantly.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;In Summary&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;TypeORM relationships + NestJS provide a declarative and powerful way to work with relational databases.&lt;/p&gt;

&lt;p&gt;To recap:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;@OneToOne&lt;/code&gt;, &lt;code&gt;@OneToMany&lt;/code&gt;, &lt;code&gt;@ManyToOne&lt;/code&gt;, and &lt;code&gt;@ManyToMany&lt;/code&gt; to model relationships.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;relations&lt;/code&gt; in &lt;code&gt;.find()&lt;/code&gt; queries to pull in related data.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;cascade&lt;/code&gt; when saving nested entities.&lt;/li&gt;
&lt;li&gt;Always structure your data flow using services and DTOs, not raw entities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading!.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>typeorm</category>
    </item>
    <item>
      <title>Why Your Company Needs a Blog (Yes, Even Yours)</title>
      <dc:creator>Henry</dc:creator>
      <pubDate>Fri, 25 Apr 2025 08:31:09 +0000</pubDate>
      <link>https://dev.to/graeyy/why-your-company-needs-a-blog-yes-even-yours-3in</link>
      <guid>https://dev.to/graeyy/why-your-company-needs-a-blog-yes-even-yours-3in</guid>
      <description>&lt;p&gt;There’s something special about scrolling through a company’s blog and realizing… &lt;em&gt;these people care&lt;/em&gt;. They’re not just building a product. They’re thinking deeply. They’re learning in public. They’re transparent when things break. They’re proud of their process.&lt;/p&gt;

&lt;p&gt;That’s the kind of feeling I get every time I read Monzo’s engineering blog. It’s not just techy. It’s honest. Thoughtful. Real. And it made me wonder—&lt;strong&gt;why don’t more companies do this?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So here’s the case, plain and simple: &lt;strong&gt;your company—no matter how big or small—needs a blog.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And not just because “content marketing is good for SEO” (though it is). But because your blog can be the soul of your business, showing the world &lt;em&gt;who you are, how you think, and what you care about&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let me explain.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;1. A Blog Builds Trust&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;People don’t just buy products. They buy into people. They follow stories. They root for progress.&lt;/p&gt;

&lt;p&gt;When you write about the hard stuff—how your team fixed a production bug at 2am, or why you chose one approach over another—you let customers &lt;em&gt;in&lt;/em&gt;. You’re not just a faceless company anymore. You’re human.&lt;/p&gt;

&lt;p&gt;That trust? It compounds.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;2. A Blog Helps People Find You&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s talk visibility.&lt;/p&gt;

&lt;p&gt;Blog posts—real, useful, authentic ones—live forever. They show up in Google searches. They get shared. They start conversations. One post can bring in traffic, leads, and talent months or even &lt;em&gt;years&lt;/em&gt; after you hit publish.&lt;/p&gt;

&lt;p&gt;You don’t need to write clickbait or chase trends. Just write what you know. Share what you’re learning. The right people will find you.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;3. A Blog Attracts the Right Talent&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You know what the best engineers, designers, and marketers all have in common? They care about &lt;em&gt;how&lt;/em&gt; things are built, not just &lt;em&gt;what&lt;/em&gt; is built.&lt;/p&gt;

&lt;p&gt;Your blog is a window into your company’s brain. It shows potential hires what kind of problems you solve, how your team communicates, and what kind of place they’d be joining.&lt;/p&gt;

&lt;p&gt;It's like passive recruiting—but way more authentic.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;4. Writing Makes You Smarter&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This one might be my favorite.&lt;/p&gt;

&lt;p&gt;When someone on your team writes about a technical challenge or a product decision, they’re forced to slow down. Think clearly. Get to the essence of the idea.&lt;/p&gt;

&lt;p&gt;Writing is a forcing function for clarity. And it often sparks better internal conversations, too. Your blog becomes a reflection of your culture of thinking.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;5. It’s Yours Forever&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Social media platforms rise and fall. Algorithms change. But your blog is your own space. Your words. Your domain.&lt;/p&gt;

&lt;p&gt;You can write about outages, product decisions, hiring philosophies, lessons learned, even team rituals. It becomes an archive of your journey—one that grows more valuable with time.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;"But What Would We Even Write About?"&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Glad you asked. Here’s a quick brainstorm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How we fixed that one bug that broke everything&lt;/li&gt;
&lt;li&gt;Why we rewrote our backend in Go&lt;/li&gt;
&lt;li&gt;What we learned from launching too early (or too late)&lt;/li&gt;
&lt;li&gt;How we do support as a small team&lt;/li&gt;
&lt;li&gt;Our favorite tools (and why we use them)&lt;/li&gt;
&lt;li&gt;What we’re reading as a team this month&lt;/li&gt;
&lt;li&gt;The day everything went wrong—and what it taught us&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Start small. Be honest. Keep it conversational. The best posts feel like a story told over coffee.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;In Closing…&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A blog isn’t a marketing tactic. It’s a &lt;strong&gt;mirror&lt;/strong&gt;—a reflection of your thinking, your values, your progress.&lt;/p&gt;

&lt;p&gt;It builds trust. It makes you discoverable. It attracts the people you want to work with. And most importantly, it reminds you—and your team—why you’re doing this in the first place.&lt;/p&gt;

&lt;p&gt;So start writing, curious kids like I could get lot of help reading them.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Starting over as a backend Engineer</title>
      <dc:creator>Henry</dc:creator>
      <pubDate>Mon, 14 Apr 2025 22:15:20 +0000</pubDate>
      <link>https://dev.to/graeyy/starting-over-as-a-backend-engineer-6oi</link>
      <guid>https://dev.to/graeyy/starting-over-as-a-backend-engineer-6oi</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The best way to write is to start writing - Anonymous&lt;br&gt;
It’s been 5 years I wrote my first program in C, then hop into PHP, HTML, CSS and JavaScript. Unfortunately I stoked to JavaScript, and evolved to Typescript following my love for something typed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The journey got tense with ReactJs, Nestjs seriously there are a lot to learn in the JavaScript workspace. Unfortunately I chose to stick with the backend side of things focusing on building secure API delving into REST API and GraphQL, looking to checkout Grpc, Kafka, Redis e.t.c&lt;/p&gt;

&lt;p&gt;Learned a lot about scaling a backend service, knowing the why for a distributed system in scaling backend services.&lt;/p&gt;

&lt;p&gt;Frankly speaking backend is FUN.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>backend</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
