This repository contains the design and implementation of a bookstore database using Supabase, along with a RESTful API for querying book information. The database includes two tables: Books and Authors. A set of advanced SQL queries is also provided. Additionally, a NestJS-based RESTful API has been developed for querying and filtering book data.
The database consists of two relational tables:
- Books: Contains information about books, including the title, price, publication date, and author details.
- Authors: Contains information about authors, including their name, country, and a foreign key relation to the books table.
-
Books table has columns for:
book_id: Primary key (auto-incremented).title: Name of the book.author_id: Foreign key referencing theAuthorstable.price: Price of the book.publish_date: Publication date of the book.
-
Authors table has columns for:
author_id: Primary key (auto-incremented).name: Name of the author.country: Country of the author.user_id: Foreign key referencing the Supabaseauth.users(id)table for user authentication.
A RESTful API endpoint GET /books is implemented using NestJS and Supabase Edge Functions. This endpoint supports the following features:
- Filtering: Books can be filtered by
author_id. - Sorting: Books can be sorted by
publish_date. - Pagination: Books can be retrieved in paginated results.
- GET /books:
- Supports filtering books by author using an optional query parameter:
author_id. - Supports sorting books by publish date using an optional query parameter:
sort. - Supports pagination with parameters
pageandlimit. - Secure access using Supabase Auth for authenticated users.
- Supports filtering books by author using an optional query parameter:
Postman Collection: You can test the API using this Postman link.
Here are the SQL queries for the advanced tasks:
SELECT
author_id
FROM
books
GROUP BY
author_id
HAVING
COUNT(*) > 5;Explanation: This query groups the books by author_id and selects only those authors who have published more than 5 books.
SELECT
country,
AVG(price) AS average_price
FROM
books
JOIN authors ON books.author_id = authors.author_id
GROUP BY
country;Explanation: This query joins the books table with the authors table to calculate the average price of books per country.
SELECT
b.title,
a.name AS author_name,
b.price,
b.publish_date
FROM books b
JOIN authors a ON b.author_id = a.author_id
ORDER BY b.price DESC;Explanation: This query retrieves the list of books, including author names, and sorts the books by price in descending order.
The database tables were created using the following SQL scripts:
-- Create authors table with user_id for authentication
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY,
user_id UUID REFERENCES auth.users(id),
name VARCHAR(255) NOT NULL,
country VARCHAR(100)
);-- Create books table with user_id and author_id for relationships
CREATE TABLE books (
book_id SERIAL PRIMARY KEY,
user_id UUID REFERENCES auth.users(id),
title VARCHAR(255) NOT NULL,
author_id INTEGER REFERENCES authors(author_id),
price FLOAT(4) NOT NULL,
publish_date DATE NOT NULL
);-
Clone the Repository:
git clone https://github.com/behrouzsedighi/task-supabase cd bookstore-api -
Install Dependencies: Ensure you have Node.js installed. Then, install the required dependencies:
npm install
-
Set Up Supabase:
- Create a Supabase project and configure it with the required tables.
- Use the provided SQL scripts to create the
authorsandbookstables in your Supabase database.
-
Environment Configuration: Set up the environment variables in a
.envfile:SUPABASE_URL=your_supabase_url SUPABASE_API_KEY=your_supabase_api_key
-
Run the Application: Start the API server:
npm run start
-
Testing:
- You can test the API using the provided Postman collection link: Postman Collection.
-
Database Design:
- The
authorstable is linked to thebookstable using a foreign keyauthor_id. Each book record references an author and contains necessary details like title, price, and publish date. - The
user_idin both tables ensures that the data is associated with a specific authenticated user in Supabase.
- The
-
API Security:
- The API is secured using Supabase Auth, ensuring that only authenticated users can access the data. Tokens are verified for every request.
-
Pagination and Query Parameters:
- The
GET /booksendpoint supports pagination withpageandlimitparameters, making it efficient for large datasets.
- The
Feel free to fork this repository and contribute. Please ensure that you follow best practices for writing clean, maintainable code.