Get started

Adding a new data model

To add a new data model to your API, all you need to do is add a new table (or view) to your database.

Open the file db/include/schema.sql.

Add the following table definition to the file:

drop table if exists comments;

create table if not exists comments (
    id serial primary key,
    todo_id int not null references todos(id) on delete cascade,
    user_id uuid not null default (auth.jwt() ->> 'sub')::uuid,
    text varchar(255) not null
);

There are two interesting aspects here:

  • The todo_id column is a foreign key to the todos table, by leveraging this information, subZero REST library will automatically detect all the relationships between the tables (this also works for views)
  • The user_id column is used to store the id of the user that created the comment. This will later be used to control access to the data. Although we do not define it as such, this column is a foreign key to the auth.users table, that is an underlying table used by the subZero Auth library to store user information.

All done, now save the file, drum roll .... and nothing happens?

That's right, just because a table is added to the database (or was there all along), it doesn't mean it's automatically exposed through the API. You need to explicitly configure the permissions for each table/view if they need to be available through the API.

Previous
Create a new project