Introducci贸n a Node.js y Express
Node.js y Express
Entorno de trabajo
Variables de entorno
Rutas din谩micas
Body parser
Postman
CRUD
驴Que es una API Restful?
Solicitudes GET
Solicitudes POST
CRUD
Soluci贸n del reto de validaci贸n
Solicitudes DELETE
MIddlewares
Middlewares - logger
Middlewares - ErrorHandler
DB
Instalar Postgresql
Instalar Prisma y PostgreSQL Client
Prisma Models
JWT
Autenticaci贸n utilizando JSON Web Tokens (JWT)
Auth - Register
Auth - Login
Expres.js
Arquitectura
Arquitectura parte 2
Creaci贸n y Migraci贸n de Modelos con Prisma para Citas M茅dicas
Admin controllers / services
Admin controllers / services parte 2
Reservations
Reservations parte 2
Appointments
Deploy
PostgreSQL
Deploy
You don't have access to this class
Keep learning! Join and start boosting your career
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.
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:
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.
To implement authentication in our medical appointment application, we need to update the user model with additional fields:
Our user model must include:
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}
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.
With our user model updated, we are ready to implement authentication in our application. The next steps will include:
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
Want to see more contributions, questions and answers from the community?