The backend is built with TypeScript and Express, and it is deployed to Netlify functions in the same instance as idol frontend.
The backend has a layered design and can be divided into three layers:
-
Gateway Layer This layer exists mostly in
api.ts
.It uses express to parse request, handle basic authentication, and process handler result.
It gives the business logic layer parsed request information and authenticated user object, and expect the business logic layer to do work based on these information and return some information to be sent back to frontend. The business logic layer is also allowed to throw some pre-defined errors, which will be handled by this layer.
-
Business Logic Layer This layer exists in files like
xxxAPI.ts
.The business logic layer is where all the interesting logic happens. This layer may perform some more granular permission checks, perform some data consistency checks, do some manipulation of the request data and do some database operations.
However, this layer doesn't do raw database operations directly. This is delegated to database layer.
-
Database Layer
This layer is just a wrapper around the firestore. It provides an convenient interface more specific to IDOL logic.
Example: An endpoint to update member information.
-
In the gateway layer, we authenticated the user and parse the request body. Then we call the business logic layer function
updateMember
. -
In business logic layer, we check the user permission in two more places: (1), (2). We also perform some data format check here. If nothing is wrong, we call the database layer function to write the data.
-
In the database logic layer, we perform some raw database operation. The code here is usually quite simple and not directly related to any business logic. Example.
The IDOL backend has a Firebase instance for development and a Firebase instance for production. To use the production Firebase instance locally, set the USE_PROD_DB
environment variable to true
. Otherwise, set USE_PROD_DB
to false
to use the development Firebase instance.