Develop a RESTful API using Node.js, Express, TypeScript, and an SQL database (preferrably PostgreSQL).
1. Database Design & Setup:
- Design a database with three tables:
Users
,Posts
, andComments
. Users can have multiple posts, and each post can have multiple comments. - Implement necessary indexing for anticipated query performance.
2. API Development:
- Endpoints to:
- Create and retrieve users (
/users
). - Create a post for a user and retrieve all posts of a user (
/users/:id/posts
). - Add a comment to a post (
/posts/:postId/comments
). - Performance Challenge: Fetch the top 3 users with the most posts and, for each of those users, the latest comment they made. This should be achieved with efficient querying.
- Create and retrieve users (
3. Query Optimization Task:
-
Optimize the following SQL query related to the designed schema:
SELECT users.id, users.name, posts.title, comments.content FROM users LEFT JOIN posts ON users.id = posts.userId LEFT JOIN comments ON posts.id = comments.postId WHERE comments.createdAt = (SELECT MAX(createdAt) FROM comments WHERE postId = posts.id) ORDER BY (SELECT COUNT(posts.id) FROM posts WHERE posts.userId = users.id) DESC LIMIT 3;
The query attempts to fetch the top 3 users with the most posts and, for each of those users, the latest comment they made. However, it's inefficient and needs optimization.
4. Middleware & Error Handling:
- Implement a simple token-based authentication middleware.
- Add basic validation for input data.
- Implement error handling for the main API routes.
Evaluation Criteria:
- Database Design: Structure, relationships, and indexing strategy.
- API Design & Implementation: RESTful practices, efficiency of the code, and especially the performance of the "Performance Challenge" endpoint.
- Query Efficacy: Ability to optimize the provided SQL query.
- Code Quality: Readability, maintainability, and use of TypeScript features.
- Error Handling: How robust the application is against unexpected situations or invalid input.
- NodeJs (TypeScript & Express)
- Postgres for pure data
- Redis
- Docker
- Postman
Unit tests are a must, submissions without tests will be ignored.
3 days
- Your API endpoints should be well documented in POSTMAN.
- Code should be hosted on a git repository, Github preferably.
- The API should be hosted on a live server (e.g. https://heroku.com)
- Your app should be
containerized
usingdocker
. - Share with us through email the:
- Repository
- Hosted API URL
- Postman Collection Public URL