🟢 Node.js Intermediate

What is Prisma and how is it used in Node.js?

Answer

Prisma is a next-generation ORM (Object-Relational Mapper) for Node.js and TypeScript that supports PostgreSQL, MySQL, SQLite, SQL Server, MongoDB, and CockroachDB. It consists of three tools: (1) Prisma Schema — a declarative data model file (schema.prisma) that defines your database schema, relations, and client generator; (2) Prisma Client — an auto-generated, type-safe query builder: const users = await prisma.user.findMany({ where: { active: true }, include: { posts: true } });; (3) Prisma Migrate — schema migration management. Advantages over alternatives (Sequelize, TypeORM): full TypeScript type safety (auto-generated types from schema), intuitive API, powerful filtering/sorting/pagination, and an excellent developer experience. Database changes flow through migrations: (1) Modify schema.prisma; (2) Run npx prisma migrate dev to generate SQL migration and apply it; (3) Prisma Client is automatically updated. Prisma Studio provides a visual database browser.