← Back to Node.js Course | Chapter 9: Database Integration | Lesson 5 of 7

ORM के Basics

एक ORM database tables को JavaScript objects में map करता है ताकि आप कम raw SQL लिखें।

In this page:

  1. ORM Basics
Syntax
javascript
const Model = sequelize.define('ModelName', {
  field: DataTypes.TYPE
});
const records = await Model.findAll();

ORM Basics

Sequelize, Prisma, और TypeORM जैसे Object-relational mappers code में models define करते हैं और queries generate करते हैं। ये development तेज़ करते हैं, migrations और validation offer करते हैं, लेकिन costly queries hide कर सकते हैं। performance work के लिए ये जो SQL generate करते हैं वह सीखें।

Note: N+1 queries catch करने के लिए development में generated SQL log करें।

उदाहरण: ORM basics

bash
const { Sequelize, DataTypes } = require("sequelize");
const db = new Sequelize("sqlite::memory:");
const User = db.define("User", { name: DataTypes.STRING, age: DataTypes.INTEGER });
await db.sync();
await User.create({ name: "Ada", age: 36 });
console.log((await User.findAll({ where: { age: 36 } })).length);

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. generated SQL ignore करना
  2. N+1 query problems होना
  3. एक ORM को SQL समझने के replacement की तरह treat करना
चैप्टर सारांश
  • ORMs tables को objects में map करते हैं
  • Popular ORMs: Prisma, Sequelize, TypeORM हैं
  • ये SQL generate करते हैं
  • N+1 queries के लिए watch करें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.