Initial Setup
To get started, I have created a free tier account and a Supabase project for the article site.
I then followed this guide to setting up a local version of the Supabase stack and linking it to my project.
With the Getting Started steps complete, I have a local installed version of the Supabase environment that I can start using:
Database Schema
Another really great feature of Supabase is that the locally stack still includes the full Studio UI for managing the project. This means that I can quickly set up my initial database schema:
Supabase is designed primarily for client-only applications where the browser hosted application is interacting directly with the persistence layer. To enable this to occur securely, Supabase supports row-level access policies for its database tables. These policies define which operations and which rows uses can perform on each table. These policies often require tuning as you develop and test the solution, but it is a good idea to set up some initial policies when you define your tables.
With the initial database schema defined, we can create a migration that exports the postgres-sql that can be used to recreate or publish this schema later
>npx supabase migration new create_initial_schema
User Accounts
Creating user accounts is a little trickier. The Supabase authentication service uses its own database schema to manage user authentication. This table stores the 'master record' for each user identity, but is purely concerned with authentication.
We also want to store own own details for our user in the user_accounts table, and we want to make sure that a user_accounts record is created whenever a new user signs up.
Also, by default Supabase doesn't give you a user interface for setting or resetting a user's password. This is probably to reduce security risks.
Eventually we will manage the account creation through the user sign-up process on our site, but in the mean time we can inject some test users via SQL.
This Gist (not mine - I found it with a google search ) shows how to inject users into the Supabase auth schema.
Once we have done that, we can run a simple SQL statement to populate our user_accounts table based on the currently defined users
Once those scripts are executed, we have some test account to play with
To make this a repeatable process, we can add this SQL to the database seeding script supabase/seed.sql
This will ensure that these accounts are created whenever we reset the database, and gives us a repeatable way of setting our test database to a known state.
Conclusion
With the above steps complete, I now have a scaffolded back end that I can start integrating into the site. The next step will be to wire up our services layer to Supabase, then create the user interface for signing up and logging in users.