Librería que permite trabajar con bases de datos usando objetos en lugar de SQL directo.
ORM (Object-Relational Mapping) es una técnica que permite interactuar con bases de datos relacionales usando objetos de programación en lugar de SQL directo. Ventajas: - Abstracción de la base de datos - Type safety en lenguajes tipados - Migraciones automáticas - Prevención de SQL injection - Productividad (menos código) Desventajas: - Curva de aprendizaje - Posible overhead en rendimiento - Queries complejas pueden ser difíciles ORMs populares: Prisma, TypeORM, Sequelize (JS), SQLAlchemy (Python), Eloquent (PHP).
// Comparación: SQL vs ORM (Prisma) // SQL directo (con riesgo de SQL injection) const sql = `SELECT * FROM users WHERE email = '${email}'`; // ORM - Prisma const user = await prisma.user.findUnique({ where: { email }, include: { posts: { where: { published: true }, orderBy: { createdAt: 'desc' } } } }); // Transacciones con ORM await prisma.$transaction(async (tx) => { const user = await tx.user.create({ data: userData }); await tx.profile.create({ data: { ...profileData, userId: user.id } }); await tx.auditLog.create({ data: { action: 'USER_CREATED', entityId: user.id } }); }); // TypeORM con decoradores @Entity() class User { @PrimaryGeneratedColumn('uuid') id: string; @Column({ unique: true }) email: string; @OneToMany(() => Post, post => post.author) posts: Post[]; }