TypeScript में डेटाबेस के साथ काम करना: PostgreSQL और MongoDB
TypeScript एक शक्तिशाली प्रोग्रामिंग भाषा है जो JavaScript में स्टैटिक टाइपिंग जोड़ती है। आधुनिक वेब एप्लिकेशन विकसित करते समय डेटाबेस के साथ काम करना एक अनिवार्य हिस्सा है। इस लेख में हम देखेंगे कि TypeScript का उपयोग करके दो लोकप्रिय डेटाबेस — रिलेशनल PostgreSQL और डॉक्यूमेंट-ओरिएंटेड MongoDB — के साथ प्रभावी ढंग से कैसे काम किया जाए।
1. वातावरण तैयार करना और ORM का चयन
शुरू करने से पहले, प्रोजेक्ट को सेट अप करना आवश्यक है। TypeScript समर्थन के साथ एक नया Node.js प्रोजेक्ट बनाएं:
mkdir ts-database-democd ts-database-demonpm init -ynpm install typescript @types/node ts-node --save-devnpx tsc --initडेटाबेस के साथ काम करने के लिए हम लोकप्रिय लाइब्रेरीज़ का उपयोग करेंगे:
- Prisma — PostgreSQL (और अन्य SQL डेटाबेस) के लिए आधुनिक ORM
- Mongoose — MongoDB के लिए ODM जिसमें TypeScript के लिए उत्कृष्ट समर्थन है
आवश्यक पैकेज इंस्टॉल करें:
npm install prisma @prisma/client mongoosenpm install @types/mongoose --save-dev2. Prisma के माध्यम से PostgreSQL के साथ काम करना
Prisma एक ORM है जो आपके डेटाबेस स्कीमा के आधार पर स्वचालित रूप से TypeScript टाइप्स जनरेट करता है। यह पूर्ण टाइप सुरक्षा सुनिश्चित करता है।
2.1. Prisma सेट अप करना
प्रोजेक्ट में Prisma इनिशियलाइज़ करें:
npx prisma initprisma/schema.prisma फ़ाइल में डेटा मॉडल परिभाषित करें:
generator client { provider = "prisma-client-js"}
datasource db { provider = "postgresql" url = env("DATABASE_URL")}
model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt}
model Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) author User @relation(fields: [authorId], references: [id]) authorId Int createdAt DateTime @default(now()) updatedAt DateTime @updatedAt}कनेक्शन स्ट्रिंग के साथ .env फ़ाइल बनाएं:
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"माइग्रेशन चलाएं:
npx prisma migrate dev --name init2.2. TypeScript के साथ CRUD ऑपरेशन
अब डेटाबेस के साथ काम करने के लिए टाइप किया हुआ कोड लिखें। src/postgres.ts फ़ाइल बनाएं:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
interface CreateUserInput { email: string; name?: string;}
interface CreatePostInput { title: string; content?: string; authorId: number;}
// उपयोगकर्ता बनानाasync function createUser(data: CreateUserInput) { return await prisma.user.create({ data: { email: data.email, name: data.name, }, });}
// पोस्ट के साथ सभी उपयोगकर्ता प्राप्त करनाasync function getUsersWithPosts() { return await prisma.user.findMany({ include: { posts: true, }, });}
// पोस्ट बनानाasync function createPost(data: CreatePostInput) { return await prisma.post.create({ data: { title: data.title, content: data.content, authorId: data.authorId, }, });}
// उपयोगकर्ता अपडेट करनाasync function updateUser(id: number, data: Partial<CreateUserInput>) { return await prisma.user.update({ where: { id }, data, });}
// उपयोगकर्ता हटानाasync function deleteUser(id: number) { return await prisma.user.delete({ where: { id }, });}
// उपयोग का उदाहरण(async () => { try { const user = await createUser({ email: 'ivan@example.com', n