Raw SQL Queries in Go LIKE a 10x Engineer! - Repository Pattern Example

Поделиться
HTML-код
  • Опубликовано: 4 ноя 2024

Комментарии • 34

  • @ArcticPrimal
    @ArcticPrimal 9 месяцев назад +4

    You can get type-safe raw sql if you're using jetbrains IDEs with autocomplete and suggestions because all the ides are well-integrated with databases & its own dbms. They can compared your created tables in the db with your raw sql and highlight any errors in the raw sql.

    • @massy-3961
      @massy-3961 9 месяцев назад +1

      Gotta love Jetbrains IDEs

    • @paologaleotti8478
      @paologaleotti8478 3 месяца назад

      is there something similar in vscode?

  • @katungiyassin9947
    @katungiyassin9947 3 месяца назад

    Your teaching style is good keep up the good work.

  • @newpersxn
    @newpersxn 9 месяцев назад +1

    Nice vid. I need to know your color theme tho, looks so fitting for golang (might sound strange, but i really think that)

    • @samvcodes
      @samvcodes  8 месяцев назад

      Hi, thank you! The colour scheme is 'poimandres' :)

    • @katungiyassin9947
      @katungiyassin9947 3 месяца назад

      ​@@samvcodes I really need a neovim setup like yours. How can I achieve that?

  • @jpcairesf
    @jpcairesf 8 месяцев назад +1

    Using testcontainers (or similar tool) is a game changer. Please don't make integration tests with in-memory dbs if you are not using one

  • @vedantmatanhelia1016
    @vedantmatanhelia1016 Месяц назад

    Why would we be using txn if we follow this pattern ?

  • @mha-k1k
    @mha-k1k 9 месяцев назад +1

    How do you setup the database in the tests? and how do you handle migrations?

    • @samvcodes
      @samvcodes  9 месяцев назад +2

      I wrote my own package for migrations that’s in early development- it’s open source and on my GH. As for the tests, I linked a video in the description which showcases how I was approaching tests, I’ve since changed it a bit, so I’ll make an updated one soon 😊

  • @anonymouse1344
    @anonymouse1344 9 месяцев назад +1

    Awesome video but volume of sound is low. I had to put volume to max on my laptop to hear it clearly.

    • @samvcodes
      @samvcodes  9 месяцев назад

      Noted! I’ll fix that for the next video, I have a new microphone so I’m still trying to get it right. Thanks for letting me know! 😊

  • @Cyber_Lanka
    @Cyber_Lanka 9 месяцев назад

    Thanks for the amazing explanation. I'm currently learning Go. One question though. Isn't using a transaction for everything a bit expensive?

    • @ArcticPrimal
      @ArcticPrimal 9 месяцев назад +2

      No databases are optimized for tx and usage of tx is critical since transactions enable data integrity and consistency during insert, update and delete. For instance if you inserting for two related tables and you get an error on the second table, data will still be created for the first table without any related data of the second table resulting in useless inconsistent data. Also If im not mistaken, some if not all databases run sql statements in transactions by default and i think you can rollback in sql server without even starting a transaction(BEGIN TX) as long as you didnt commit the tx. Transactions are not necessary for a reading data (SELECT). Many professionals in big tech and startups have deleted their data costing them millions when it could've been easily prevented by a simple "BEGIN TRANS".

    • @lehoangngo8201
      @lehoangngo8201 3 месяца назад +1

      @@ArcticPrimal why a select statement in 1:24 use transaction

    • @ArcticPrimal
      @ArcticPrimal 3 месяца назад +1

      @@lehoangngo8201 weird I don't know, he shouldn't have, it's pointless. A select statement is read-only, and on its own, it 100% doesn't need a transaction. Transactions should only be used with a select if you're also using statements such as insert, update & delete to make changes to the database.

    • @lehoangngo8201
      @lehoangngo8201 3 месяца назад

      @@ArcticPrimal thanks for clarify, I find myself very struggle when working with sqlx despite people say that it was easy. Have you ever considered sqlx + sqlc, surfed reddit and found that combination but don't know does it worth a try

  • @ПожилойСкарабей
    @ПожилойСкарабей 9 месяцев назад +1

    Why not use sqlc?

    • @advertslaxxor
      @advertslaxxor 9 месяцев назад

      Yeah. You can use sqlc, then sprinkle your own custom methods in. It's a bit of pain to get the generated files tagged properly, but it's just a bunch of stuff in their config. (the suffix option is broken and does not apply to models.go etc)

    • @samvcodes
      @samvcodes  9 месяцев назад

      For sure! It’s an option, and I’ve used it on projects before. This video is more aimed at those managing legacy or just projects using std lib related code and a nice pattern to manage that 😊

    • @dandogamer
      @dandogamer 9 месяцев назад +1

      You cant do dynamic queries with SQLC

    • @advertslaxxor
      @advertslaxxor 9 месяцев назад +1

      @@dandogamer You can. SQLC only provides some boilerplate for you. You are free to query the database as you desire outside of that, or augment the boilerplate with your own functions.

    • @viniciusmachadorodrigues1724
      @viniciusmachadorodrigues1724 9 месяцев назад

      Nah, if for some reason your schema change you'll have to redo it all again.​@@advertslaxxor

  • @YvanFlorian
    @YvanFlorian 9 месяцев назад

    Why do you prefer using raw SQL as opposed to using an ORM like GORM? People have various opinions about this, just want to hear you on this one…

    • @ArcticPrimal
      @ArcticPrimal 9 месяцев назад +3

      In my opinion orm seems simple and fun at first when its basic queries however as your project grows and the queries get a lil bit complex plus with associations/relations, its no longer simple to do these complex queries in orm compared to raw sql. ORM can be too abstract

    • @paologaleotti8478
      @paologaleotti8478 9 месяцев назад +1

      I completeley agree, used several ORMs in production and they all scale horribly. @@ArcticPrimal

    • @mohamedimranechehabi5735
      @mohamedimranechehabi5735 9 месяцев назад +3

      On top of the points the other commenters made, SQL is really easy, and writing SQL queries yourself is just so much simpler and better, and using something like SQLC makes it even better.

    • @viniciusmachadorodrigues1724
      @viniciusmachadorodrigues1724 9 месяцев назад +1

      Performance is also a concern here, GORM sucks on this aspect. If performance is not important to you, then there are better languages with frameworks that will do everything to you.

  • @geoblk3000
    @geoblk3000 8 месяцев назад

    For the integration tests what I'm doing is to spin up a new test schema that is a clone of the public schema and execute my migration files.
    Then while testing I temporarily set the search_path of the database to the test schema.
    Finally, if the test passed I remove the test schema, this has the added benefit that if a test fails I can keep the test schema and inspect it manually to see on which table structure or data the test failed on.

  • @TheMrlewismedia
    @TheMrlewismedia 9 месяцев назад

    👏👍👏