Remix with Database integration
How to use Remix and Supabase for CRUD operations
November 27th, 2021
November 27th, 2021
TL;DR
Link to the source code
Here's a live demo
UPDATE: After implementing authentication, the link above has only view access.
Here's the demo with authentication
This post will be the first part of the series on how I will create the entire application. So I will start with a boring introduction about the motivation for this series.
Because I recently scored very low on my IELTS exam, I plan to create an application to help me to expand my English Vocabulary while learning Remix.
I have chosen Supabase to store my data as it allows me to focus on the Frontend part due to Supabase's easy-to-use API. You can use any provider of your choice, or you can even create your custom backend.
This part of the series will focus on how to use Remix for CRUD operations.
The Vocabulary section will consist of lists of words that are publicly available and a protected admin route to perform a CRUD operation.
Here are the properties we need for each word
:
If you are not yet familiar with Remix, I suggest checking first my previous blog post about it, or refer to their documentation.
If you want to use another provider, you can skip to this part Create the Remix project
Refer to their official documentation on how to create a Supabase project.
After creating your account, go to SQL Editor tab and execute the queries below:
In the Table Editor tab, you should see the new entry.
Lastly, in Authentication/Policies
tab, should be seeing this.
root.tsx
file under app
folder.index.tsx
file under app/routes
folder.The mandatory hello world
page is now ready.
Expect the design will be terrible. I will create a separate blog for styling.
Install Supabase javascript library
Not using Supabase? Skip to Fetch All Words
The next step will allow us to create a Supabase client utility that we can use across the whole application.
.env
file to hold your Supabase credentials.Make sure to add
.env
in the.gitignore
file.
To take advantage of strong typing, I will create a type definition.
If you prefer not to use TypeScript, remove the declarations and usages of types, and change the file extensions from
.tsx
to.jsx
.
/
to /words
As I plan to create multiple mini-apps in this project, I'll redirect /
to /words
, for now.
The code below will ensure we don't need to manually navigate to /words
every time we open the root page.
If you are not using Supabase, replace the Supabase API calls with your choice.
loader
The code above will fetch the data from Supabase and display it in a list.
$id.tsx
under app/routes/words
folder.The image below shows that it still won't show even after creating the /words/[id]
route.
Outlet
We need to add an Outlet
inside our Words Index component to fix the above issue.
After clicking on a word, $id.tsx route
will render on where we put the Outlet
.
Since we're already on the /words/$id
page, let's proceed with deletion first
The image shows a message that we did not define any action to handle the submit event.
action
After we click on the delete button, the word hello
will be deleted from the database, and the page will redirect to the /words
page.
_method
with value delete
.action
handler will trigger in the server.action
handler, we check if the _method
is delete
./words
route.It just happens that this approach does not need any JavaScript to run(try it on your browser). This means our app is interactive even before we load the JavaScript from the server.
Now we don't have anything on the list; let's create the route to handle creation.
/words
route that will navigate to /words/add
We can use the useNavigate
hook here, But for this blog, I'll show you how we can perform the entire CRUD operations without using any JavaScript on the client-side.
To avoid a 404
page, let's create the /words/add
route.
The image below shows the form we created after clicking on the Add new word
button.
To avoid the missing action error after clicking on the Submit
button, let's add an action on the words/add
route.
After clicking on the Submit
button, the word will be added to the database, and the page will redirect to the /words/$id
page.
I'm not sure if you noticed, but we haven't used any JavaScript code on the Frontend, yet it could complete the intended task. We only used an HTML form as it was initially used to handle validations and perform the submission. This is what I like about Remix; we can focus on the web fundamentals to become a better Web developer instead of making some workarounds.
Now, to handle the missing operation in our CRUD app, let's add the ability to modify an existing entry.
edit.$id.tsx
under app/routes/words
When we add a .
between words, it will transform to /
in the URL.
The above example will result in words/edit/[id]
.
Since the edit form is very similar to the add form, we can reuse the same form with additional checks to determine if we are adding or editing.
That's a lot of code; however, we can reap the benefits of simplifying the code in add.tsx
and edit.$id.tsx
.
routes/words/add.tsx
routes/words/edit.$id.tsx
Now, we have a reusable form. If we have to make a style change, we can update the WordForm
component, reflecting the change on both routes.
NOTE: I'm extracting everything to
WordForm.tsx
since it is applicable in my use case.
In order for the edit form to be populated with the existing data, we need to create a loader.
/words/$id
page to edit a wordThe image below shows the pre-filled form depending on the content of id
in the URL.
To handle the form submission, we need to add an action handler.
After modifying some fields and clicking the submit button, the page will redirect to the /words/$id
page with the updated data.
By utilizing the useTransition
hook, we can add or change something on the screen depending on the route's state.
We can replace the text states below with global loading indicator, local component spinner, disabling elements, etc.
Here's proof that we can perform the CRUD operations without using any JavaScript on the client-side(as indicated by errors in the network tab). Take note that I also simulated a slower network connection, yet the performance is not that terrible.
I'm not saying we should not use JavaScript on the client-side, as JavaScript can do some cool stuff to help with user experience. For instance, you might notice that the states were stuck in
idle
.
So far, I'm having a positive experience with the framework. Of course, I'm still learning, but I'm enjoying the process. I'm starting to agree with the Remix team said that if we become better with Remix, we become better with the Web. Working with Remix allows me to refresh my HTML skills that are almost diminishing due to too much dependency on JavaScript. I'm looking forward to using more of their features in the next iteration of this app.