<?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: André</title>
    <description>The latest articles on Forem by André (@andreligne).</description>
    <link>https://forem.com/andreligne</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%2F70976%2F488920e4-8563-4eae-a5ea-201819ddbc6e.jpeg</url>
      <title>Forem: André</title>
      <link>https://forem.com/andreligne</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/andreligne"/>
    <language>en</language>
    <item>
      <title>Remount /etc/fstab without rebooting</title>
      <dc:creator>André</dc:creator>
      <pubDate>Mon, 03 May 2021 06:32:53 +0000</pubDate>
      <link>https://forem.com/andreligne/remount-etc-fstab-without-rebooting-4o9m</link>
      <guid>https://forem.com/andreligne/remount-etc-fstab-without-rebooting-4o9m</guid>
      <description>&lt;p&gt;Ever worked on a thing where you need to mount a network share on boot for a Linux machine and find yourself rebooting it over and over to try new configs and see if it works?&lt;/p&gt;

&lt;p&gt;I just did that for 20 minutes, and it turns out there’s a command for this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mount -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it.&lt;/p&gt;

&lt;p&gt;Here’s an excerpt from the &lt;code&gt;man&lt;/code&gt; page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-a, --all
       Mount all filesystems (of the given types) mentioned in fstab (except for those whose line contains the noauto keyword). The filesystems are mounted following
       their order in fstab.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’re welcome!&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>linux</category>
    </item>
    <item>
      <title>Making sure all commits pass the tests</title>
      <dc:creator>André</dc:creator>
      <pubDate>Sat, 01 May 2021 12:29:12 +0000</pubDate>
      <link>https://forem.com/andreligne/making-sure-all-commits-pass-the-tests-1699</link>
      <guid>https://forem.com/andreligne/making-sure-all-commits-pass-the-tests-1699</guid>
      <description>&lt;p&gt;If you’re a bit too pedantic about your &lt;code&gt;git&lt;/code&gt; history, like I am, you probably want to make sure that all the commits are passing whatever checks the project has set up before merging it into the &lt;code&gt;main&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;It turns out there’s a way to automatically run a command for each commit when you’re doing a &lt;code&gt;git-rebase&lt;/code&gt; — you can pass the &lt;code&gt;--exec&lt;/code&gt; flag (or the shorthand &lt;code&gt;-x&lt;/code&gt; flag).&lt;/p&gt;

&lt;p&gt;With this flag, &lt;code&gt;git&lt;/code&gt; will run the given command after applying each commit onto the new &lt;code&gt;HEAD&lt;/code&gt;. If the command returns a non-zero exit code, the rebase will stop on that commit and allow you to update the commit so that it passes.&lt;/p&gt;

&lt;p&gt;For example, if you have a Ruby project (using RSpec) and want to check that all commits after the &lt;code&gt;main&lt;/code&gt; branch are passing the tests, you can run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git rebase -x “bundle exec rspec” main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why bother?
&lt;/h2&gt;

&lt;p&gt;There are a few good reasons, other than the warm fuzzy feeling it brings you inside, to make sure that the commits are valid.&lt;/p&gt;

&lt;p&gt;My main reason for caring about this is that it helps you keep track that all commits are working. If you have commits that aren’t passing the tests or have incomplete/broken features, it will be more challenging to track down a separate issue using &lt;code&gt;git-bisect&lt;/code&gt;. Since you might land on a commit that is not in a working state, it is harder to tell if a commit is good or bad.&lt;/p&gt;

&lt;p&gt;It also forces you to think about commits as an atomic piece of &lt;em&gt;completed&lt;/em&gt; work, which makes it easier to write a great commit message which summarizes the unit of work.&lt;/p&gt;

</description>
      <category>git</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>Include FactoryBot in RSpec</title>
      <dc:creator>André</dc:creator>
      <pubDate>Sat, 10 Oct 2020 14:43:34 +0000</pubDate>
      <link>https://forem.com/andreligne/include-factorybot-in-rspec-5901</link>
      <guid>https://forem.com/andreligne/include-factorybot-in-rspec-5901</guid>
      <description>&lt;p&gt;When using &lt;code&gt;FactoryBot&lt;/code&gt; for generating random data in my &lt;code&gt;RSpec&lt;/code&gt; tests for my Rails app, I had to keep prefixing my call to &lt;code&gt;create&lt;/code&gt; with &lt;code&gt;FactoryBot&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This caused my specs to look a bit cluttered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;trace = FactoryBot.create :trace
parent = FactoryBot.create :trace_field, trace: trace
child = FactoryBot.create :trace_field, trace: trace
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is possible to include the &lt;code&gt;FactoryBot&lt;/code&gt; methods into RSpec so it will automatically find the methods from there.&lt;/p&gt;

&lt;p&gt;To get that working, you could properly set up RSpec, &lt;a href="https://github.com/thoughtbot/factory_bot/blob/5c071d42fda1682282461978923a2bd89f2ad27d/GETTING_STARTED.md#rspec"&gt;as per the documentation&lt;/a&gt;, but if you didn’t you can add this to a file named &lt;code&gt;spec/support/factory_bot.rb&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# spec/support/factory_bot.rb
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also, make sure that you also uncomment this line in &lt;code&gt;spec/rails_helper.rb&lt;/code&gt; so that RSpec includes any files you put in &lt;code&gt;spec/support/&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that we’ve fixed that we can remove the &lt;code&gt;FactoryBot&lt;/code&gt;  prefix from the &lt;code&gt;create&lt;/code&gt; calls to clean the whole thing up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;trace = create :trace
parent = create :trace_field, trace: trace
child = create :trace_field, trace: trace

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>todayilearned</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Sprinkle some Elm in your React</title>
      <dc:creator>André</dc:creator>
      <pubDate>Sun, 04 Oct 2020 11:01:58 +0000</pubDate>
      <link>https://forem.com/andreligne/sprinkle-some-elm-in-your-react-4d6i</link>
      <guid>https://forem.com/andreligne/sprinkle-some-elm-in-your-react-4d6i</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1506224772180-d75b3efbe9be%3Fixlib%3Drb-1.2.1%26q%3D80%26fm%3Djpg%26crop%3Dentropy%26cs%3Dtinysrgb%26w%3D2000%26fit%3Dmax%26ixid%3DeyJhcHBfaWQiOjExNzczfQ" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1506224772180-d75b3efbe9be%3Fixlib%3Drb-1.2.1%26q%3D80%26fm%3Djpg%26crop%3Dentropy%26cs%3Dtinysrgb%26w%3D2000%26fit%3Dmax%26ixid%3DeyJhcHBfaWQiOjExNzczfQ" alt="Sprinkle some Elm in your React"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After being out of the &lt;a href="https://elm-lang.org" rel="noopener noreferrer"&gt;Elm&lt;/a&gt; world for about a year, I decided this weekend to try it out again. I created a new application (using &lt;a href="https://github.com/halfzebra/create-elm-app" rel="noopener noreferrer"&gt;create-elm-app&lt;/a&gt;) and got going. After a few minutes, the joy of writing Elm was back. The helpful error messages, the flexibility of describing data in the type system - it was fantastic!&lt;/p&gt;

&lt;p&gt;In another side-project I've been working on for a few months, we are using React. Even though React is an excellent framework for building web applications, my fingers started to itch for writing Elm again.&lt;/p&gt;

&lt;p&gt;Then it occurred to me - why can't we write an Elm module for a small set of a screen and embed that in a React component?&lt;/p&gt;

&lt;p&gt;So that's what this post is about!&lt;/p&gt;




&lt;p&gt;To get started, we need a project set up using &lt;code&gt;webpack&lt;/code&gt; capable of compiling both React and Elm files. I'll leave this as an exercise for the reader.&lt;/p&gt;

&lt;p&gt;This is the contents of the &lt;code&gt;Main.elm&lt;/code&gt; file that we'll use to try and figure this one out.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module Main exposing (..)

import Browser
import Html exposing (Html, text)

main : Program () Model Msg
main =
    Browser.element
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }

type alias Model =
    {}

init : () -&amp;gt; ( Model, Cmd Msg )
init flags =
    ( Model
    , Cmd.none
    )

type Msg
    = Noop

update : Msg -&amp;gt; Model -&amp;gt; ( Model, Cmd Msg )
update _ model =
    ( model, Cmd.none )

view : Model -&amp;gt; Html Msg
view model =
    Html.div [] [text "Hello from Elm!"]

subscriptions : Model -&amp;gt; Sub Msg
subscriptions _ =
    Sub.none
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This doesn't do anything interesting since it only prints out the string &lt;code&gt;"Hello from Elm!"&lt;/code&gt;, but it's good enough to try and get it running inside of a React component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fandreligne.io%2Fcontent%2Fimages%2F2020%2F09%2FScreenshot-2020-09-30-at-21.53.56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fandreligne.io%2Fcontent%2Fimages%2F2020%2F09%2FScreenshot-2020-09-30-at-21.53.56.png" alt="Sprinkle some Elm in your React"&gt;&lt;/a&gt;Screenshot of the Elm application we'll wrap&lt;/p&gt;

&lt;h2&gt;
  
  
  How does an Elm application initialize?
&lt;/h2&gt;

&lt;p&gt;When you create any Single Page Application (SPA), you usually have some entry point Javascript that imports the SPA's main module and mounts it on a specific DOM node on the page.&lt;/p&gt;

&lt;p&gt;To take a closer look at this, we can open the generated &lt;code&gt;index.html&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Elm } from '../Main';

document.addEventListener('DOMContentLoaded', () =&amp;gt; {
  const target = document.createElement('div');
  document.body.appendChild(target);

  Elm.Main.init({ node: target });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
Example &lt;code&gt;hello_elm.js&lt;/code&gt; from a new project





&lt;ul&gt;
&lt;li&gt;First, we import the Elm application from the &lt;code&gt;Main.elm&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;On the &lt;code&gt;DOMContentLoaded&lt;/code&gt; event, we create a new DOM element for a &lt;code&gt;div&lt;/code&gt; and append it to the body&lt;/li&gt;
&lt;li&gt;We then initialize the Elm application to run inside the new  &lt;code&gt;div&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main takeaway here is that we will need a DOM node to run our Elm application inside.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the React component
&lt;/h2&gt;

&lt;p&gt;Now that we understand how the Elm application gets initialized, we can start thinking about designing our React component to host it.&lt;/p&gt;

&lt;p&gt;Since the only thing we need to initialize the Elm application is a DOM node, let's create a component that renders a &lt;code&gt;div&lt;/code&gt; we can use as our target.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';

export default function ElmComponent() {
  return &amp;lt;div /&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get the reference of that &lt;code&gt;div&lt;/code&gt;, we can use the &lt;code&gt;useRef&lt;/code&gt; hook from &lt;code&gt;react&lt;/code&gt; to get a reference to the DOM node for the entire lifecycle of the component. The &lt;code&gt;useRef&lt;/code&gt; function takes an initial value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useRef } from 'react';

export default function ElmComponent() {
  const target = useRef();

  return &amp;lt;div ref={target} /&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we know where to put the application, we can import our Elm module and use the &lt;code&gt;useEffect&lt;/code&gt; hook to initialize it when the component first mounts to the DOM.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useRef } from 'react';
import { Elm } from '../Main';

export default function ElmComponent() {
  const target = useRef();

  useEffect(() =&amp;gt; Elm.Main.init({ node: target.current });

  return (
    &amp;lt;&amp;gt;
      &amp;lt;h1&amp;gt;Hello from React!&amp;lt;/h1&amp;gt;
      &amp;lt;div ref={target} /&amp;gt;
    &amp;lt;&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we render our &lt;code&gt;ElmComponent&lt;/code&gt;, our Elm application will run &lt;em&gt;inside&lt;/em&gt; our React application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fandreligne.io%2Fcontent%2Fimages%2F2020%2F09%2FScreenshot-2020-09-30-at-21.53.20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fandreligne.io%2Fcontent%2Fimages%2F2020%2F09%2FScreenshot-2020-09-30-at-21.53.20.png" alt="Sprinkle some Elm in your React"&gt;&lt;/a&gt;The final result&lt;/p&gt;

&lt;h2&gt;
  
  
  How can we pass data from React?
&lt;/h2&gt;

&lt;p&gt;While embedding the Elm application in an already existing React application, there's a good chance you want to send along some data from the React app to the Elm app. This could be anything from authentication tokens for making HTTP requests to a server to the current user.&lt;/p&gt;

&lt;p&gt;To do this, we can change the type signature of our &lt;code&gt;main&lt;/code&gt; function in the Elm program to signal that we're expecting the program to receive initial data when it starts up. We also then to add to the data we want to hold in the program to our &lt;code&gt;Model&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;For passing an authentication token from React into the Elm program, so that we can use it to make HTTP requests, we could change our module to something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Browser
import Html exposing (Html, text)

main : Program String Model Msg
main =
    Browser.element
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }

type alias Model =
    { authToken : String }

init : String -&amp;gt; ( Model, Cmd Msg )
init authToken =
    ( Model authToken
    , Cmd.none
    )

type Msg
    = Noop

update : Msg -&amp;gt; Model -&amp;gt; ( Model, Cmd Msg )
update _ model =
    ( model, Cmd.none )

view : Model -&amp;gt; Html Msg
view model =
    Html.div [] [text &amp;lt;| "Token: " ++ model.authToken]

subscriptions : Model -&amp;gt; Sub Msg
subscriptions _ =
    Sub.none

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The differences in our new Elm program is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Model&lt;/code&gt; is constructed with an  &lt;code&gt;authToken : String&lt;/code&gt; value&lt;/li&gt;
&lt;li&gt;The type signature of &lt;code&gt;main&lt;/code&gt; now specifies the type of the &lt;code&gt;flags&lt;/code&gt; passed to it&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;init&lt;/code&gt; also has an updated type signature an build the initial &lt;code&gt;Model&lt;/code&gt; with the value from the flags&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we now render the &lt;code&gt;ElmComponent&lt;/code&gt; and pass along a string as the &lt;code&gt;flags&lt;/code&gt; prop, the Elm program will store that inside the initial model so that we can use it later. Let's run the new program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useRef } from 'react';
import { Elm } from '../Main';

export default function ElmComponent() {
  const target = useRef();

  useEffect(() =&amp;gt; Elm.Main.init({
    node: target.current,
    flags: "my-very-secret-token"
  });

  return &amp;lt;div ref={target} /&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fandreligne.io%2Fcontent%2Fimages%2F2020%2F10%2FScreenshot-2020-10-02-at-11.30.27.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fandreligne.io%2Fcontent%2Fimages%2F2020%2F10%2FScreenshot-2020-10-02-at-11.30.27.png" alt="Sprinkle some Elm in your React"&gt;&lt;/a&gt;Our rendered Elm program with arguments passed from React.&lt;/p&gt;

&lt;p&gt;You can also pass along more complex data to your Elm program using &lt;code&gt;flags&lt;/code&gt;, such as objects, tuples, arrays and so on. The Elm guide has great documentation if that is something you want to read more about!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://guide.elm-lang.org/interop/flags.html" rel="noopener noreferrer"&gt;Flags · An Introduction to Elm&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  In summary
&lt;/h2&gt;

&lt;p&gt;By using this little building block in your React application, you can start introducing Elm into your codebase without rewriting the application.&lt;/p&gt;

&lt;p&gt;This can help you try if you like it enough in a low-risk situation and later decide if you want to expand your usage of it throughout the application. Otherwise, you only have a small portion of Elm running and can easily convert it back to React.&lt;/p&gt;

&lt;p&gt;But since Elm is fantastic, that'll probably never happen. 😁&lt;/p&gt;

</description>
      <category>elm</category>
      <category>react</category>
    </item>
    <item>
      <title>Counting large tables in PostgreSQL</title>
      <dc:creator>André</dc:creator>
      <pubDate>Thu, 01 Oct 2020 08:39:05 +0000</pubDate>
      <link>https://forem.com/andreligne/counting-large-tables-in-postgresql-2ae2</link>
      <guid>https://forem.com/andreligne/counting-large-tables-in-postgresql-2ae2</guid>
      <description>&lt;p&gt;Imagine you have a &lt;em&gt;really&lt;/em&gt; large table (&amp;gt; 100m rows). How do you figure out how many rows there are in that table within a reasonable time?&lt;/p&gt;

&lt;p&gt;If you try and do a &lt;code&gt;SELECT COUNT(id) FROM my_large_table&lt;/code&gt;, it will end up taking a lot of time since the database will have to scan throw all the rows to count them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku pg:ps
 pid | state | source | running_for | transaction_start | waiting | query
------+--------+------------------+-----------------+-------------------------------+---------+--------------------------------------------------
 6926 | active | psql interactive | 00:10:12.549096 | 2020-10-01 08:22:18.090989+00 | t | SELECT COUNT(id) FROM pageviews;
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
I gave up after waiting for 10 minutes...





&lt;p&gt;A faster way is to get an approximate value by looking at statistics from the catalog table &lt;code&gt;pg_catalog.pg_class&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE=&amp;gt; SELECT reltuples::bigint FROM pg_catalog.pg_class WHERE relname = 'pageviews';
 reltuples
-----------
 136032896
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This took less than a millisecond, compared to the 9 minutes that we waited for the &lt;code&gt;COUNT&lt;/code&gt; query to finish. It may not be as accurate, but it's enough useful to set expectations for adding new indices to the table or doing any large operations on it.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>GraphQL field names in Ruby</title>
      <dc:creator>André</dc:creator>
      <pubDate>Thu, 24 Sep 2020 05:27:17 +0000</pubDate>
      <link>https://forem.com/andreligne/graphql-field-names-in-ruby-2op9</link>
      <guid>https://forem.com/andreligne/graphql-field-names-in-ruby-2op9</guid>
      <description>&lt;p&gt;While building a GraphQL API, I noticed that many times the name of the field on the type didn’t match the name of the method on the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Types::UserType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;BaseObject&lt;/span&gt;
  &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:is_active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;
  &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:is_banned&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_active&lt;/span&gt;
    &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active?&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_banned&lt;/span&gt;
    &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;banned?&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This caused me to have to write custom methods in the Object type that acts as a proxy for the calling the method.&lt;/p&gt;

&lt;p&gt;It turns out there’s a &lt;a href="https://graphql-ruby.org/fields/introduction.html#field-resolution"&gt;documented way&lt;/a&gt; in the &lt;code&gt;graphql-ruby&lt;/code&gt; gem that basically does all of this for you - the &lt;code&gt;method&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;And by using the &lt;code&gt;method:&lt;/code&gt; attribute, we can rewrite our previous example like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Types::UserType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;BaseObject&lt;/span&gt;
  &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:is_active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;method: :active?&lt;/span&gt;
  &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:is_banned&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;method: :banned?&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can now keep writing methods using the usual Ruby conventions but expose them in the schema using names that make sense for the API consumer.&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>ruby</category>
      <category>graphql</category>
    </item>
  </channel>
</rss>
