You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
18 Hrs
18 Min
13 Seg
Curso de Backend con ExpressJS

Curso de Backend con ExpressJS

Oscar Barajas Tavares

Oscar Barajas Tavares

Prisma Models

17/30
Resources

Creating a robust API requires not only a connection to the database, but also the ability to manage and display information efficiently. In this phase of development, we will learn how to populate our database, update models and prepare our application to implement user authentication, fundamental elements for any modern application.

How to populate our database with initial information?

To start working with real data in our API, we need to create initial or "seed" data that will allow us to test the functionality. There are two main approaches to generate this data:

  1. Consult the Prisma documentation to create scripts manually.
  2. Use artificial intelligence to generate the necessary script.

In this case, we opt for the second option, creating a file called seed.js that contains the logic to populate our database:

// We use the Prisma client to interact with the databaseconst { PrismaClient } = require('@prisma/client');const prisma = new PrismaClient();
async function main() { // Creating demo users const users = [ { name: 'User 1', email: '[email protected]' }, { name: 'User 2', email: '[email protected]' }, { name: 'User 3', email: '[email protected]' } ];
 for (const user of users) { await pr isma.user.create({ data: user }); }
 console.log('Demo users created successfully');}
main() .catch(e => console.error(e)) .finally(async () => await prisma.$disconnect());

This script uses the Prisma client to create sample users in our database. To run it, we simply use the command:

node prisma/seed.js

Once executed, we can verify that the data has been created correctly by accessing the db-user endpoint of our API, where we should now see the information of the created users.

How to update our user model to support authentication?

To implement authentication in our medical appointment application, we need to update the user model with additional fields:

Adding required fields.

Our user model must include:

  1. Password: To store the user's password.
  2. Role: To define the user permissions (admin or user).
  3. Appointments: To relate the user to his medical appointments.

The updated structure in our schema.prisma file would look like this:

model User { id Int @id @default(autoincrement()) email String @unique name String password String role Role appointments Appointment[]}
enum Role { ADMIN USER}

Applying changes to the database

Before applying these changes, it is advisable to remove the existing users to avoid conflicts. We can do this with a simple script:

await prisma.user.deleteMany();

Once the users are deleted, we apply the migrations to update the database structure:

npx prisma migrate dev --name update_user_model

And we generate the updated Prisma client:

npx prisma generate

These commands will synchronize our database with the new model and update the Prisma client so we can use the new fields in our code.

What are the steps to implement authentication?

With our user model updated, we are ready to implement authentication in our application. The next steps will include:

  1. Create endpoints for user registration: Allow users to create accounts with email, password and assign them a role.
  2. Implement authentication: Verify credentials and generate access tokens.
  3. Protect routes: Ensure that only authenticated users can access certain functionalities.
  4. Manage role-based permissions: Differentiate between permitted actions for administrators and regular users.

It is important to maintain good security practices, such as password hashing and input data validation, to protect our users' information.

The framework we are building will allow us to not only authenticate users, but also implement business logic specific to our medical appointment application, such as listing available hours and creating new appointments.

Developing a robust API requires attention to these fundamental details that we are addressing step by step. Have you worked with seed scripts before? Share your experiences or doubts in the comments section to enrich the learning of the whole community.

Contributions 0

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?