<?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: Lorran Rodrigues</title>
    <description>The latest articles on DEV Community by Lorran Rodrigues (@lorransr).</description>
    <link>https://dev.to/lorransr</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%2F230076%2F9775a3b2-323c-4fb5-b9f9-0ad45c1fa071.jpeg</url>
      <title>DEV Community: Lorran Rodrigues</title>
      <link>https://dev.to/lorransr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lorransr"/>
    <language>en</language>
    <item>
      <title>From GUI to CLI: Transforming my query workflow with usql and jq</title>
      <dc:creator>Lorran Rodrigues</dc:creator>
      <pubDate>Wed, 15 May 2024 16:57:51 +0000</pubDate>
      <link>https://dev.to/lorransr/from-gui-to-cli-transforming-my-query-workflow-with-usql-and-jq-175i</link>
      <guid>https://dev.to/lorransr/from-gui-to-cli-transforming-my-query-workflow-with-usql-and-jq-175i</guid>
      <description>&lt;p&gt;A couple of months ago I decided to climb the steepest ladder that is using &lt;a href="https://kev.town/2010/12/15/this-is-your-brain-on-vim/"&gt;vim as my code editor&lt;/a&gt;. It took me some time, but I was able to get that amazing feeling of coding in the speed of thought and making my daily routine feel so more productive. That feeling was kind of addictive, and soon I became really obsessed on how to make other parts of my job, as a data engineer, feel more "vimy".&lt;/p&gt;

&lt;p&gt;I usually deal with data spread across multiple databases and my tool for the job of inspecting resources and test some simple queries was &lt;a href="https://dbeaver.io/"&gt;dbeaver&lt;/a&gt;, which is great, but it can be overwhelming in terms of visual information (specially on my use case). I needed speed, I needed something in the terminal, I needed vim.&lt;/p&gt;

&lt;p&gt;Since some of the databases I was interacting were postgres, I started to play around with &lt;a href="https://www.postgresql.org/docs/current/app-psql.html"&gt;psql&lt;/a&gt;. It felt amazing, it had a great integration with the terminal, no visual pollution, I could also use vim as my query editor, and it was blazing fast. The problem was that only &lt;strong&gt;some&lt;/strong&gt; of the databases I was interacting were postgres. I needed something that could connect to multiple databases, something universal, so I don't have to invest time in new tooling all the time. So I searched on github "universal sql" and I felt in love with &lt;a href="https://github.com/xo/usql"&gt;usql&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For those who are not familiar, usql is a command-line interface for multiple databases heavily inspired on psql written in go. &lt;/p&gt;

&lt;p&gt;From its documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Database administrators and developers that would prefer to work with a tool like psql with non-PostgreSQL databases, will find usql intuitive, easy-to-use, and a great replacement for the command-line clients/tools for other databases.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why is it so great?
&lt;/h2&gt;

&lt;p&gt;Configuring usql is quite easy. The tools use two files for configuration, &lt;code&gt;.usqlrc&lt;/code&gt; and &lt;code&gt;.usqpass&lt;/code&gt;. The first one allows  us for example to choose the table display format and the preferrer editor&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- my actual .usqlrc
\set EDITOR nvim
\pset format json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the &lt;code&gt;.usqlpass&lt;/code&gt; allow us to configure a pattern matching syntax that will assign a specific user/password depending on the matching database. For example, if I have the following configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysql:*:*:*:username:mypassword
pg:*:*:*:another_account:mypassword
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;whenever I connect to a mysql database like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;usql mysql://my-database-url.com:3306
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;usql will try to use the user/password defined on the &lt;code&gt;.usqlpass&lt;/code&gt;. This can become super useful, for handling multiple database connections&lt;/p&gt;

&lt;p&gt;Another great feature is allowing me to use vim as the query editor. So whenever I need to edit the recent submitted query I can command &lt;code&gt;\e&lt;/code&gt; and it will launch and vim buffer, so I can delightfully edit the SQL on vim.&lt;/p&gt;

&lt;p&gt;But the real game changer was its piping capabilities. You've noticed that I've set json as my table format, and that's not deliberate. It's because of another tool called &lt;a href="https://jqlang.github.io/jq/"&gt;jq&lt;/a&gt;, which became my favorite way to interact with json data. Mostly because of its elegant syntax and its natural integration with the terminal and also vim. &lt;/p&gt;

&lt;p&gt;By configuring the table format as json I can easily pipe the results to jq and do all sort of witchcraft with the query results on the fly. &lt;/p&gt;

&lt;p&gt;Want to use the result of a query as input to some python script?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select id from orders \g | jq "[.[].id] | join(", ")" &amp;gt; input.txt | python main.py input.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That way I can naturally query and also do a bunch of data wrangling with the results in the process. It takes some time to really get the hang of &lt;code&gt;jq&lt;/code&gt; syntax, but once you get it, it's absolute gold. &lt;/p&gt;

&lt;p&gt;I can also switch back to the default result format configuration by doing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\pset format aligned
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Switching to usql has revolutionized my workflow, providing a faster, more efficient way to query databases. Combined with jq, it’s transformed how I interact with and process data. I highly recommend giving usql a try if you're looking to streamline your database management. I'd love to hear about your experiences and any tips you might have!&lt;/p&gt;

</description>
      <category>sql</category>
      <category>productivity</category>
      <category>vim</category>
    </item>
  </channel>
</rss>
