𝗕𝘂𝗶𝗹𝗱𝗶𝗻𝗴 𝗮 𝗣𝗿𝗼𝗷𝗲𝗰𝘁 𝗠𝗮𝗻𝗮𝗴𝗲𝗺𝗲𝗻𝘁 𝗕𝗮𝗰𝗸𝗲𝗻𝗱

I am building a collaborative project management tool for my internship at CodeAlpha.

It works like a simplified Trello or Asana. Users create projects, invite members, and move tasks across boards. I use Express.js, Prisma, and Socket.io to power it.

Here are the technical lessons I learned while building the backend.

𝗠𝗮𝗻𝗮𝗴𝗶𝗻𝗴 𝗗𝗮𝘁𝗮 𝗥𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝘀𝗵𝗶𝗽𝘀

Users and projects have a many-to-many relationship. I use a ProjectMember table to connect them. This table stores extra data like the user role.

I added a unique constraint to this table. This ensures a user appears only once per project. It also creates a composite key for fast lookups.

For the Kanban board, I kept things simple. A task status is defined by the board it belongs to. Moving a task from "To Do" to "Done" is just a matter of updating its board ID.

𝗔𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗲 𝗠𝗶𝘀𝘁𝗮𝗸𝗲𝘀

I learned a hard lesson about database connections. Do not create a new PrismaClient in every route file. This creates too many connections and crashes your app.

Instead, create one shared instance in a single file. Import that same instance into every route. This keeps your connection pool stable.

𝗔𝘂𝘁𝗵𝗼𝗿𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝗮𝗻𝗱 𝗦𝗲𝗰𝘂𝗿𝗶𝘁𝘆

I use a protect middleware to guard routes. It checks the JWT in the Authorization header.

I use optional chaining when reading headers. This prevents the app from crashing if a header is missing.

For this project, I use a 7-day access token. While production apps use short-lived tokens and refresh tokens, a long-lived token works well for this specific scope.

𝗗𝗮𝘁𝗮 𝗜𝗻𝘁𝗲𝗴𝗿𝗶𝘁𝘆 𝘄𝗶𝘁𝗵 𝗧𝗿𝗮𝗻𝘀𝗮𝗰𝘁𝗶𝗼𝗻𝘀

Creating a project requires three steps:

  • Create the project record
  • Assign the owner
  • Create the default boards

If one step fails, your data becomes corrupt. I use prisma.$transaction to wrap these steps. If any part fails, the whole process rolls back. This keeps your database clean.

Common Logic Errors

I ran into two common bugs:

  1. Relation Arrays: When you include relations in a query, Prisma returns an array. You cannot access a property directly on the relation. You must pick the first item in the array first.

  2. ルートの順序: Expressはルートを上から下へと順番に照合します。パラメータを含むルートを特定のルートよりも前に記述すると、Expressは誤ったルートに一致してしまいます。特定のルートは常にパラメータ付きのルートよりも上に記述するようにしてください。

これらのバックエンドのパターンは、今や私のワークフローの一部となっています。

出典: https://dev.to/chinwuba_jeffrey/building-the-backend-for-a-project-management-tool-express-prisma-and-the-mistakes-i-made-along-5gia