This task involves building a server with REST API using Express.js and TypeScript. The task also requires implementing basic database CRUD operations, as outlined in the provided task file.
The dependencies for this project are as follows:
[email protected] \SeeChen\Self-Learning\REST_Using_Express_JS
+-- @prisma/[email protected]
+-- @types/[email protected]
+-- @types/[email protected]
+-- @types/[email protected]
+-- @types/[email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
`-- [email protected]
Additionally, you can view the package.json or package-lock.json for more details.
I am use SQLite for development. The schema is defined in schema.prisma.
The database structure is as follows:
model User {
id Int @id @default(autoincrement())
name String
email String @unique
password String
address String
userCompany UserCompany?
}
model Company {
id Int @id @default(autoincrement())
name String @unique
address String
users UserCompany[]
}
model UserCompany {
userId Int @unique
companyId Int
role Int // 0: Admin, 1: User
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
company Company @relation(fields: [companyId], references: [id], onDelete: Cascade)
@@id([userId, companyId])
}
This task utilizes a simple layered architecture, as shown below:
src
├───auth
├───controllers
├───dao
├───middleware
├───routes
└───Services
Currently, the architecture consists of six layers: auth, controllers, dao, middleware, routes, and Services.
ROLE
:
The auth layer abstracts all authentication-related operations, encapsulating them into reusable functions. By consolidating these operations, it simplifies the handling of authentication and authorization across the application.
FUNCTION
:
The auth layer provides methods like verifyToken, verifyPassword, and genToken, which are commonly used in various parts of the app. This layer ensures consistency and security in authentication workflows by centralizing similar code into one file.
ROLE
:
The controller layer acts as an intermediary between the routes and the business logic of the application. It processes incoming requests, interacts with the model (or database), and prepares the response.
FUNCTION
:
Controllers handle the core functionality of the application. They receive data from the routes, perform operations (such as fetching or saving data), and return the appropriate response to the client.
ROLE
:
The routes layer defines the endpoints of the application and maps incoming requests to the appropriate controller functions.
FUNCTION
:
Routes specify how the application responds to client requests. They determine what happens when a particular URL is accessed and specify which controller should handle the request.
ROLE
:
Middleware functions are functions that have access to the request object, response object, and the next middleware function in the application’s request-response cycle.
Function
:
Middleware can modify the request or response, end the request-response cycle, or call the next middleware in the stack. They are commonly used for tasks such as authentication, logging, and error handling.
ROLE
:
The DAO layer provides an abstraction over direct database operations, making it easier to manage data access and manipulation consistently across the application.
Function
:
DAOs handle all database queries and operations (such as creating, reading, updating, and deleting records) by providing a standardized interface. This keeps data logic separated from business logic and supports more maintainable and scalable database interactions.
ROLE
:
The services layer encapsulates the core business logic and workflows of the application. It serves as an intermediary between controllers and DAOs or external data sources, handling the essential rules and processes.
Function
:
Services define how the application processes data and implement reusable functions that encapsulate specific workflows and rules. This helps maintain consistency in business logic across different parts of the app and supports a clear separation of concerns.
You can run the server with the command npx tsx server.ts
.
To test my API and understand its functionality, you can use Postman. Download the collection file REST.postman_collection.json or click here to access the Postman website.