<?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: Mathias Michel</title>
    <description>The latest articles on DEV Community by Mathias Michel (@m91michel).</description>
    <link>https://dev.to/m91michel</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%2F306396%2F5aac838e-a846-4681-be85-ace5ec64b512.JPG</url>
      <title>DEV Community: Mathias Michel</title>
      <link>https://dev.to/m91michel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/m91michel"/>
    <language>en</language>
    <item>
      <title>How I used Supabase and n8n for my background tasks</title>
      <dc:creator>Mathias Michel</dc:creator>
      <pubDate>Wed, 07 Dec 2022 09:22:53 +0000</pubDate>
      <link>https://dev.to/m91michel/how-i-used-supabase-and-n8n-for-my-background-tasks-42mm</link>
      <guid>https://dev.to/m91michel/how-i-used-supabase-and-n8n-for-my-background-tasks-42mm</guid>
      <description>&lt;p&gt;If you're looking for a way to run background jobs, Supabase and n8n are two great options. &lt;a href="https://supabase.com/" rel="noopener noreferrer"&gt;Supabase&lt;/a&gt; is an open source Firebase alternative, and n8n is an open source Node-based workflow automation tool. Together they are a powerful combination for background tasks. In this post, I'll show you how I use these two tools for my projects &lt;a href="https://einfachcrypto.de/" rel="noopener noreferrer"&gt;einfachcrypto.de&lt;/a&gt; and &lt;a href="https://www.illostration.com/" rel="noopener noreferrer"&gt;illostration.com&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup of Supabase and n8n
&lt;/h2&gt;

&lt;p&gt;If you have not worked with Supabase or n8n, here are some steps on how you can set these up.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a project on &lt;a href="https://supabase.com/" rel="noopener noreferrer"&gt;Supabase&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;Either use the &lt;a href="https://n8n.io/cloud/" rel="noopener noreferrer"&gt;cloud version&lt;/a&gt; of n8n or you can host it on Railway. They offer a &lt;a href="https://railway.app/new/template/zo8wVU" rel="noopener noreferrer"&gt;template&lt;/a&gt; which you can use.&lt;/li&gt;
&lt;li&gt;Connect your Supabase by creating a credentials entry in n8n. You can find them in Supabase under &lt;code&gt;Project Settings&lt;/code&gt; -&amp;gt; &lt;code&gt;API&lt;/code&gt;. You can read more &lt;a href="https://docs.n8n.io/integrations/builtin/credentials/supabase/" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vmipcngy2rs4nbtg18p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vmipcngy2rs4nbtg18p.jpg" alt="Enter Supabase credentials in N8N" width="800" height="573"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Update coin data from CoinGecko
&lt;/h2&gt;

&lt;p&gt;My passion project, einfachcrypto.de, lists the top 250 cryptocurrencies. It is a static site built with GatsbyJS. The list of cryptocurrencies is saved in a &lt;code&gt;coins&lt;/code&gt; table in Supabase, and the &lt;a href="https://www.gatsbyjs.com/plugins/gatsby-source-supabase/" rel="noopener noreferrer"&gt;gatsby-source-supabase&lt;/a&gt; plugin pulls all the data into the frontend. &lt;a href="https://www.coingecko.com/" rel="noopener noreferrer"&gt;CoinGecko&lt;/a&gt; is the main data source. As the data, especially the price, changes quite often, I needed a way to update some of the data regularly. I am using n8n for a cron-triggered job, which fetches the latest information from CoinGecko and updates the coin entry in Supabase. This is how my workflow looks like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpsdwtocqke9tnssp322d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpsdwtocqke9tnssp322d.jpg" alt="Update cryptocurrency data workflow" width="756" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation of the steps&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Cron:&lt;/strong&gt; Trigger the workflow every X minutes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supabase:&lt;/strong&gt; Get all coins which are outdated. I check the timestamp here to see when it was last updated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CoinGecko:&lt;/strong&gt; Get the latest data for each coin.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JavaScript node:&lt;/strong&gt; Normalize and prepare the data structure so that it can be easily saved to columns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supabase:&lt;/strong&gt; Update normalized data into the Supabase&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  2. Check the status of AI generation
&lt;/h2&gt;

&lt;p&gt;I am using &lt;a href="https://replicate.com/" rel="noopener noreferrer"&gt;replicate.com&lt;/a&gt; to run the Stable Diffusion AI model for illostration.com. It is an AI infrastructure service that runs AI image generation (they call them predictions) for me. The API is built so that you first send a post request which triggers &lt;a href="https://replicate.com/docs/how-does-replicate-work#how-to-run-models-with-the-api" rel="noopener noreferrer"&gt;the prediction&lt;/a&gt;. You then have to poll every second and check the status of the prediction to see if it is finished. The response also includes the generated image. In Supabase, each prediction is saved in a predictions table, so I can track the status and retrieve the image.&lt;/p&gt;

&lt;p&gt;This polling usually happens on the client side, but I have a case where I create multiple predictions in bulk. I am using a background job to update the status of the prediction and to retrieve the generated images. As I have production and staging Supabase environments, I am doing the same steps for both.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqprolesrmtk4z5t2wfo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqprolesrmtk4z5t2wfo.jpg" alt="Get prediction status workflow" width="757" height="523"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation of the steps&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Cron:&lt;/strong&gt; Trigger the workflow every X minutes. (1 minute in my case)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supabase:&lt;/strong&gt; Get all predictions with a status of &lt;code&gt;pending&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTP Node:&lt;/strong&gt; Get the latest status from replicate API. This also gets the image url of the result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supabase:&lt;/strong&gt; Update status and image url in &lt;code&gt;prediction&lt;/code&gt; table&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. Reset credit at the end of the month
&lt;/h2&gt;

&lt;p&gt;I used the &lt;a href="https://github.com/vercel/nextjs-subscription-payments" rel="noopener noreferrer"&gt;nextjs-subscription-payment&lt;/a&gt; boilerplate for illostration.com. This boilerplate already includes two tables, &lt;code&gt;subscriptions&lt;/code&gt; and &lt;code&gt;users&lt;/code&gt;. For my credit system, I decided to keep it simple and just add a column for &lt;code&gt;credits&lt;/code&gt; on the &lt;code&gt;users&lt;/code&gt; table. Now I only have to check for active subscriptions and reset the credits at the end of the month. Each subscription includes the user_id and the amount of credits included.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyrqfssy1d1w9415py0k.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyrqfssy1d1w9415py0k.jpg" alt="Reset credits workflow" width="562" height="210"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation of the steps&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Cron:&lt;/strong&gt; Trigger the workflow on the first day of each month&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supabase Get:&lt;/strong&gt; Get all active subscriptions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supabase Update:&lt;/strong&gt; Update user based on &lt;code&gt;user_id&lt;/code&gt; and reset credits to the subscribed amount.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Check out Supabase launch week 6, which starts next week. They are releasing amazing features to Supabase in the next days. Head over to their &lt;a href="https://supabase.com/launch-week" rel="noopener noreferrer"&gt;launch week page&lt;/a&gt; and register for free.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://supabase.com/docs/reference" rel="noopener noreferrer"&gt;Supabase Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.n8n.io/" rel="noopener noreferrer"&gt;N8n documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://replicate.com/docs" rel="noopener noreferrer"&gt;Replicate Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PS: This is my first blog post on dev.to. Let me know what you think. Should I share more insights like this?&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
  </channel>
</rss>
