<?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: Nanpipat Klinpratoom</title>
    <description>The latest articles on DEV Community by Nanpipat Klinpratoom (@nanpipatdev).</description>
    <link>https://dev.to/nanpipatdev</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%2F743164%2F60362d18-228c-4d1b-927e-b6689220e564.png</url>
      <title>DEV Community: Nanpipat Klinpratoom</title>
      <link>https://dev.to/nanpipatdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nanpipatdev"/>
    <language>en</language>
    <item>
      <title>GORM repository by Go Generics 💖</title>
      <dc:creator>Nanpipat Klinpratoom</dc:creator>
      <pubDate>Sat, 14 Jan 2023 09:40:19 +0000</pubDate>
      <link>https://dev.to/nanpipatdev/gorm-repository-by-go-generics-1dc6</link>
      <guid>https://dev.to/nanpipatdev/gorm-repository-by-go-generics-1dc6</guid>
      <description>&lt;p&gt;Normally, when we do a service for a certain entity, we will find that many tables have similar query commands. For example, doing CRUD service will be in the form of Create, Find, Update, and Delete. This is done in the level of the repository in our project. If there are many tables, we may have to make a repository multiple times, but overall the commands inside will be similar.&lt;/p&gt;

&lt;p&gt;Today, we will try to make a single repository file that can be used for multiple models or tables by using gorm and go generic. &lt;/p&gt;

&lt;p&gt;Let's take a look at the code for the repository.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The code explains a little bit about the &lt;code&gt;NewRepository&lt;/code&gt; function, which is used to call this repository by passing in any models (go generic). After that, I will create other functions such as &lt;code&gt;Create&lt;/code&gt;, &lt;code&gt;FindAll&lt;/code&gt;. These are examples of overriding functions from gorm. You don't have to name them like gorm, but I prefer to do it this way because it's easier to read the gorm document and apply it.&lt;/p&gt;

&lt;p&gt;The next part is the various conditions such as &lt;code&gt;Where&lt;/code&gt;, &lt;code&gt;Order&lt;/code&gt; and others. They are similar, just the return is different. Because we can do multiple conditions and it will stack up more and more.&lt;/p&gt;

&lt;p&gt;By creating these functions, you can customize the parameters that are received and sent out. You can do many things. I may not have given examples that are too strange because I want to show how to use go generic in receiving and returning any type.&lt;/p&gt;

&lt;p&gt;Great, now take a look at the code when implementing it, it will look like this&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I will not explain about injecting the db or structuring the project, because I think each person has a different format. This will be a general example. The db value I sent will be of type gorm.DB, as required by our repository. Another value is the context that we may need to use if we use it with a framework or third-party that we want to send the context to.&lt;/p&gt;

&lt;p&gt;All of this will just be a concept in doing go generic when using gorm repository, you can customize it all as you want.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nanpipat-dev/gorm-generic-repository" rel="noopener noreferrer"&gt;GITHUB PROJECT ✨ nanpipat-dev/gorm-generic-repository&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the git link that I have as an example for this article. If you have any questions or comments, please let me know.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>มาลองทำ gorm repository โดยใช้ Go Generics กันครับ</title>
      <dc:creator>Nanpipat Klinpratoom</dc:creator>
      <pubDate>Thu, 12 Jan 2023 10:42:42 +0000</pubDate>
      <link>https://dev.to/nanpipatdev/maalngtham-gorm-repository-odyaich-go-generics-kankhrab-1h5h</link>
      <guid>https://dev.to/nanpipatdev/maalngtham-gorm-repository-odyaich-go-generics-kankhrab-1h5h</guid>
      <description>&lt;p&gt;ปกติแล้วเวลาเราจะทำ service ซักตัว เราจะพบว่าในหลาย ๆ table นั้น จะมีการใช้คำสั่ง query ที่ค่อนข้างจะเป็นรูปแบบที่ซ้ำ ๆ กัน เช่น การทำ CRUD service ก็จะเป็นการ Create, Find, Update, Delete อะไรทำนองนี้ ซึ่งใน level ของการทำ repository ในโปรเจคเรานั้น หากมีหลาย table ก็อาจจะต้องมีการทำ repository ในหลายรอบที่ต่างออกไป แต่รวม ๆ แล้วคำสั่งข้างในก็จะคล้าย ๆ กัน&lt;/p&gt;

&lt;p&gt;วันนี้เราเลยจะมาลองทำให้มี repository แค่หนึ่งไฟล์ แต่ใช้ได้กับหลาย ๆ models หรือหลาย ๆ table กันครับ โดยใช้ gorm กับ go generic กันครับ&lt;/p&gt;

&lt;p&gt;มาดูโค้ดในส่วนของ repository กันครับ&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;อธิบายโค้ดเพิ่มซักนิดนึงครับ ตรง function &lt;code&gt;NewRepository&lt;/code&gt; คือการเรียกใช้ repository นี้ โดยการโยน models ใดก็ได้เข้ามา (go generic)&lt;/p&gt;

&lt;p&gt;และหลังจากนั้นผมจะสร้าง function อื่น ๆ เพิ่ม เช่นการ &lt;code&gt;Create&lt;/code&gt; , &lt;code&gt;FindAll&lt;/code&gt; พวกนี้คือการ override function ของ gorm เข้าไป โดยคุณอาจจะไม่ต้องตั้งชื่อตาม gorm ก็ได้ แต่ผมค่อนข้างจะชินในการตั้งแบบนี้ เพราะเวลาอ่าน document ของ gorm จะได้เอามาปรับใช้ได้เลยง่าย ๆ&lt;/p&gt;

&lt;p&gt;ส่วนถัดมาก็คือส่วนของ condition ต่าง ๆ เช่น &lt;code&gt;Where&lt;/code&gt; , &lt;code&gt;Order&lt;/code&gt; และอื่น ๆ ก็จะทำคล้าย ๆ กัน เพียงแค่การ return จะไม่เหมือนกัน เพราะเราสามารถทำได้หลาย ๆ condition มันจึงจะ stack ไปเรื่อย ๆ มากกว่า&lt;/p&gt;

&lt;p&gt;โดยการทำ function เหล่านี้ คุณสามารถ custom parameter ที่รับเข้ามาและส่งออกไปได้เลย ทำได้หลากหลายมากมาก ผมอาจจะไม่ได้ยกตัวอย่างที่แปลก ๆ ไว้ เพราะอยากให้เห็นภาพการใช้ go generic ในการรับ type อะไรเข้ามาก็ return สิ่งนั้นออกไปครับ&lt;/p&gt;

&lt;p&gt;โอเคครับ ทีนี้มาถึงโค้ดตอนนำไปใช้ จะหน้าตาเป็นแบบนี้&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;โดยผมจะไม่อธิบายถึงการ inject db เข้ามา หรือการวาง stucture ของโปรเจคนะครับ เพราะคิดว่าแต่ละคนคงมีรูปแบบที่ไม่เหมือนกัน อันนี้ก็จะเป็นตัวอย่างคร่าว ๆ โดยค่า db ที่ผมส่งไปก็จะเป็น type gorm.DB ตามที่ repository ของเราต้องการ และก็อีกค่าเป็นค่า context ที่เราอาจจะต้องใช้ถ้าเราใช้กับ framework หรือ third-party ที่เราอยากจะส่ง context ไปใช้นะครับ&lt;/p&gt;

&lt;p&gt;ซึ่งทั้งหมดจะเป็นแค่ concept คร่าว ๆ ในการทำ go generic มาใช้กับการทำ gorm repository คุณสามารถ custom มันเองได้ทั้งหมดตามแต่ที่คุณต้องการครับ&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nanpipat-dev/gorm-generic-repository" rel="noopener noreferrer"&gt;https://github.com/nanpipat-dev/gorm-generic-repository&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;อันนี้จะเป็นลิ้ง git ที่ผมทำไว้เป็นตัวอย่างสำหรับบทความนี้ครับ หากมีข้อสงสัยหรือมีข้อติติงตรงไหนคอมเม้นบอกได้เลยครับ ขอบคุณที่อ่านมาจนจบครับ 🙏🏻&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
