Enda Lee 2024
This tutorial will use SvelteKit to create a client website which will connectto Supabase, get some data, and display it.
This continues from part1_supabase.md .
The client functionality is dependent on the Supabase DB from Supabase Setup. Make sure your Database is setup before continuing. You shoulalso have completed the Sveltekit Intro Tutorial.
The tutorial is based on the code example in this repository
Download the start site from this repository (either PULL or download the zip file).
Open in VS Code.
In a terminal run npm install
.
Start the application using npm run dev
.
After starting the application, open the Event Log
link and the page will load. At this point, no data is displayed but you will see a template design for what it will eventually look like. When complete:
The page is divided in a left ands a right column layout, left for computers and right for events. Currently it displays some sample data. We will be replacing this with dynamic content from the database in the steps that follow.
Computers are displayed as button links and a table is used to show the events. Both are styled uaing Bootstrap CSS. Make sure that you understand this layout (read Bootstrap documentation, etc.) before continuing.
Supabase provides a JavaScript library to its features, including querying the database. Open a VS Code terminal and Install this library using npm:
npm install @supabase/supabase-js
The next step is to configure the web application so that it can access Supabase. The required settings can be found in the dashboard.
Project URL
and the anon public
API key. Do Not use the Secret Key!.env
file, using the following variables: **Note: you will find .env.example
in the lab repo, this can be renamed to .env
. It should contain the following:
VITE_SUPABASE_URL='replace with your supabase URL'
VITE_SUPABASE_ANON_KEY='replace with the anon key from your supabase project'
Create an instance of the Supabase client
Add supabase.js
to the lib
folder. You will find this in the src
folder. This script uses createClient
from the Supabase library to open a connection to the database URL using the anon API Key.
Now that the application has been configured to use Supabase, you are ready to retrieve data and display it in the events page.
src/routes/events
. Currently it contains a single file, +page.svelte
. Add a new file name +page.js
This script will be used to load data when the page is loaded.
+page.js
Start by importing the supabase
client which was addded to src/lib
earlier ($lib
is a shortcut for src/lib
). Now supabase
can be accessed in this page.
Now the computers
and events
data can be loaded when the page is opened, using a special function named load()
{fetch, params}
are required by the framework but will not be used diectly here.select * from computers
and store the result in computers
.select * from events
and store the result in events
. events/+page.svelte
Open routes/events/+page.svelte
. In the following steps the existing page will be modified to display the computer and events data.
Add the following code to the <script></script>
block:
line 2:
prevent Type Errors - too much detail for now!
line 5:
Get access to the data
returned by the load()
function.
line 8-9:
Get the computer and event data and store in variables (for use in the next step).
At this point the computers
and events
variables should contain data for display.
Display a list of computers
a. We will replace the sample computer data with the real computers.
b. Start by finding the compter list the the page HTML.
c. Delete the sample computers, except for ALL Computers
as that will be required later.
d. Replace the sample data with the computer names from the database. The {#each}{/each}
block is used for this.
In the example below #each
is a loop iterates
through the list of computers
returned by the database.
{#each computers as computer}
works with each computer
in turn, adding a new button link to the page using {computer.name}
{#each computers as computer}
<button on:click={()=> {filterByComputer(Number(computer.id))}} class="list-group-item list-group-item-action">
{computer.name}
</button>
{/each}
The result looks like this - the actual data depends on your database. Run the application and open the page in your brower to see it in action.
This is similiar to the previous step. The sample events in the table will be replaced by events from the database.
Locate the HTML table used to display the events and delete the existing <tr></tr>
rows in the <tbody>
section, except for the first.
Use an #each
is a loop iterates
through the list of events
returned by the database. A row will be added for each event in the table body.
After making the changes reload in your browser. The events from your database should be displayed, silimiar to:
events
By computer
At this point, the events page displays a list of computers and a list of events from the database. Next we will add a function so that when a computer in the list is clicked, that computer's events will be displayed.
The All Computers
button will be used to reset the filter and reload all the events.
Curently the data loads with the page, using the load()
function in routes/events/+page.js
. The new filterByComputer()
function needs to work after the page loads, so it will be defined in +page.svelte
.
Start by importing the supabase client at the start of the <script>
section in the page.
Now think about how the function will work.
filter_id
will be used for this.select * from events where computer_id = filtered_id
. Check the Supbase API docs. for examples:We need a way to reset the filter then All Computers is selected. To do this we will set filter_id=0
.
Finally, we need to update the page.
The function, based on requirments:
An on:click
event handler can be used to call filterByComputer
when a computer button link is clicked.
First the link for All Computers
. The function parameter, filter_id
has a default value 0, so we do not need to pass that value.
The other links require a bit more as a parameter, the computer id, is required. Svelte does not allow the function to be called like this filterByComputer(2)
from on:click={}
. Instead an arrow function
is required to pass computer.id
:
on:click={()=> {filterByComputer(Number(computer.id))}
This code will create a function call for each computer based on its id.
At this point the applicaton should display a list of computers and events with the ability to filter events by computer.
Enda Lee 2024