AuraID Database: Shared Package & Core Schema Setup
Hey guys! Let's dive into setting up the @aura/database package for our AuraID project. This is a super important step because it'll act as the single source of truth for all our data-related stuff. Think of it as the brain of our operation, especially when it comes to managing user identities and application access. We're going to create a shared package within our monorepo that houses our Prisma schema, the generated client, and all the necessary configurations. This shared setup is crucial for ensuring consistency and efficiency across the entire project.
🛠️ Tasks Breakdown
Commit 1: chore(database): initialize package structure
Okay, so first things first, we need to lay the groundwork. This initial commit is all about getting the basic structure of the @aura/database package up and running. We'll be creating the directory, setting up the package.json file with the correct workspace naming, and configuring the tsconfig.json to extend our base configuration. We'll also be installing prisma and all its related dependencies. Finally, we'll move the Prisma files from the apps/server to packages/database. This move is crucial because it centralizes our database schema and client generation logic into a single, shared location. This makes sure that everyone is using the same definition of the data and the same way of interacting with it. It also makes it easier to maintain and update the database schema in the future, as any changes only need to be made in one place.
- Creating the Package Directory: We'll start by making the
packages/databasedirectory. This is where all the magic will happen. - Initializing package.json: Next, we will initialize a
package.jsonfile within thepackages/databasedirectory, ensuring proper workspace naming with@aura/database. This will tell the package manager that this directory contains a package that can be used by other parts of the monorepo. - Setting up tsconfig.json: Then, we will set up
tsconfig.jsonto extend the base configuration. This will make sure that the TypeScript compiler knows how to handle the code in the database package. - Installing Prisma Dependencies: After that, we'll install
prismaand all its related dependencies. These dependencies are essential for defining the database schema, generating the Prisma client, and managing database migrations. - Moving Prisma Files: Lastly, the Prisma files from the
apps/serverwill be moved to thepackages/databasedirectory, centralizing the database schema and client generation logic.
Commit 2: feat(database): define core identity schema
Now for the core part: defining our database schema. This commit is all about setting up the models that will represent our data. We're talking about the User model for global identity, the Application model for security boundaries, the ApplicationMembership model for access control, and the VerificationToken model for handling single-use intents. This step will also include adding deletedAt fields for soft delete support, which is important for data management and compliance.
- Implement User Model: We will create the
Usermodel, which will represent the global identity. This model will likely include fields for user details, like username, email, and a hashed password using Argon2 for secure storage. - Implement Application Model: Then, we will create the
Applicationmodel, which will define security boundaries. This model will represent each application or service that uses our AuraID system. - Implement ApplicationMembership Model: After that, we will implement the
ApplicationMembershipmodel, which acts as a bridge, or "visa", linking users to specific applications and defining their access rights. - Implement VerificationToken Model: Furthermore, we will create the
VerificationTokenmodel for handling single-use intents, such as email verification or password reset requests. - Add deletedAt Fields: Finally, we will add
deletedAtfields for mandatory Soft Delete support across all relevant models. This allows us to mark records as deleted without actually removing them from the database, which is often crucial for data recovery and auditability. The models are the blueprints for how our data is stored and organized. Getting this right is absolutely critical for the long-term success of the project.
Commit 3: feat(database): export shared prisma client
Alright, in this final step, we are going to create a shared Prisma client. This commit focuses on making the Prisma client accessible across the monorepo, allowing other services like @aura/server and @aura/shared to interact with the database. We will also add scripts for easy database management, like generating the client and running migrations.
- Create index.ts: We'll create an
index.tsfile in thepackages/databaseto export a singletonPrismaClientinstance. This means that we'll create a single instance of the client and make it available to other parts of the project, which will help to optimize performance and prevent inconsistencies. - Export Prisma Types: We will export Prisma types for use in
@aura/serverand@aura/shared. This will provide type safety and code completion when working with the database in those packages. - Add db:generate and db:migrate scripts: Lastly, we will add
db:generateanddb:migratescripts to the package'spackage.json. These scripts will streamline the process of generating the Prisma client and applying database migrations. Thedb:generatescript will generate the Prisma client based on the current schema. Thedb:migratescript will apply any pending database migrations to update the database schema. This step is super important for making sure that our database is always up-to-date and in sync with the current code.
✅ Definition of Done (DoD)
To ensure everything works as expected, we have a clear "Definition of Done". Here's what needs to be checked off before we can call this whole thing a success.
- pnpm build runs successfully: We need to make sure that the project builds without any errors. This means that all the TypeScript code compiles correctly, and all the dependencies are installed.
- Prisma schema passes prisma validate: We will validate the Prisma schema. This ensures that the schema is valid and that there are no syntax errors or other issues.
- Soft Delete logic is reflected in model fields: We will ensure that the soft delete logic (ADR-004) is correctly reflected in the model fields.
- Package is successfully linked to apps/server via package.json: We need to ensure that the database package is correctly linked to the
apps/serverpackage viapackage.json. This will ensure that the server can access and use the database package's functionality. - JOURNAL.md updated: The
JOURNAL.mdfile needs to be updated with any friction regarding monorepo hoisting or Prisma generation. It will contain any issues encountered during development and any decisions made to resolve them. This will help with debugging and future reference.
That's the plan, guys! We're laying the foundation for our AuraID data layer. By following these steps, we're building a solid and reliable database structure that will support our project's growth for a long time. Let's get started!