Swift में डेटाबेस के साथ काम करना: PostgreSQL और MongoDB

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें
```html

परिचय: Swift पारिस्थितिकी तंत्र में डेटाबेस



Swift लंबे समय से केवल मोबाइल ऐप डेवलपमेंट तक सीमित नहीं रहा है। Vapor और Perfect जैसे सर्वर फ्रेमवर्क के आगमन के साथ, यह भाषा बैकएंड बनाने के लिए एक पूर्ण उपकरण बन गई है। हालांकि, Swift पर डेटाबेस के साथ काम करने की अपनी विशिष्टताएँ हैं। Python या JavaScript के विपरीत, यहाँ कोई अंतर्निहित ड्राइवर नहीं हैं — सब कुछ तृतीय-पक्ष पैकेजों के माध्यम से हल किया जाता है।



इस लेख में, हम दो लोकप्रिय DBMS से कनेक्ट करने का तरीका समझेंगे: रिलेशनल PostgreSQL और डॉक्यूमेंट-ओरिएंटेड MongoDB। आप जानेंगे कि किन लाइब्रेरीज़ का उपयोग करना है, कनेक्शन कैसे सेट करना है और बुनियादी CRUD ऑपरेशन कैसे करना है। उदाहरण iOS/macOS पर केंद्रित हैं, लेकिन कोड सर्वर वातावरण में भी काम करेगा।



Swift में PostgreSQL: Vapor और Fluent के माध्यम से क्लासिक



लाइब्रेरी का चयन

Swift में PostgreSQL के साथ काम करने के लिए डी-फैक्टो मानक Fluent है — Vapor फ्रेमवर्क से ORM। Fluent PostgreSQL, MySQL, SQLite और MongoDB (एक अलग ड्राइवर के माध्यम से) को सपोर्ट करता है। यदि आपको Vapor के बिना काम करना है, तो PostgresClientKit का उपयोग करें — एक निम्न-स्तरीय ड्राइवर।



कनेक्शन सेटअप

Package.swift में डिपेंडेंसी जोड़ें:

.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0")


configure.swift में बेसिक कॉन्फ़िगरेशन:

import Fluentimport FluentPostgresDriverimport Vapor

public func configure(_ app: Application) throws { app.databases.use(.postgres( hostname: "localhost", port: 5432, username: "swift_user", password: "secret", database: "swift_db" ), as: .psql) // माइग्रेशन जोड़ें app.migrations.add(CreateUser())}


User मॉडल के उदाहरण पर CRUD ऑपरेशन

एक मॉडल और माइग्रेशन बनाएँ:

final class User: Model, Content {    static let schema = "users"        @ID(key: .id)    var id: UUID?        @Field(key: "name")    var name: String        @Field(key: "email")    var email: String        init() { }        init(id: UUID? = nil, name: String, email: String) {        self.id = id        self.name = name        self.email = email    }}

struct CreateUser: Migration { func prepare(on database: Database) -> EventLoopFuture<Void> { database.schema("users") .id() .field("name", .string, .required) .field("email", .string, .required) .create() } func revert(on database: Database) -> EventLoopFuture<Void> { database.schema("users").delete() }}


कंट्रोलर में क्वेरी का उदाहरण:

// उपयोगकर्ता बनानाlet user = User(name: "Алексей", email: "alex@example.com")try await user.save(on: req.db)

// सभी उपयोगकर्ताओं को खोजनाlet users = try await User.query(on: req.db).all()

// ईमेल द्वारा फ़िल्टर करनाlet filtered = try await User.query(on: req.db) .filter(\\.$email == "alex@example.com") .first()


महत्वपूर्ण: Fluent EventLoopFuture या async/await (Vapor 4.60+ से) के माध्यम से एसिंक्रोनसी का उपयोग करता है। सुनिश्चित करें कि आपका वातावरण Swift 5.5+ को सपोर्ट करता है।



Swift में MongoDB: MongoKitten के साथ डॉक्यूमेंट की लचीलापन



ड्राइवर का चयन

MongoDB के लिए मुख्य लाइब्रेरी MongoKitten है। यह MongoDB के सभी आधुनिक संस्करणों को सपोर्ट करता है, जिसमें Atlas (क्लाउड संस्करण) शामिल है। विकल्प MongoDB Inc. का MongoSwift है, लेकिन MongoKitten को शैली में अधिक "स्विफ्टी" माना जाता है।



डेटाबेस से कनेक्शन

Package.swift में जोड़ें:

.package(url: "https://github.com/OpenKitten/MongoKitten.git", from: "7.0.0")


कनेक्शन का उदाहरण (iOS/macOS के लिए):

```                

ब्लॉग

पुस्तक अनुशंसाएँ