स्विफ्ट में डेटाबेस के साथ कार्य: PostgreSQL और MongoDB

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

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

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

स्विफ्ट में डेटाबेस के साथ काम करने का परिचय

स्विफ्ट लंबे समय से केवल मोबाइल डेवलपमेंट की भाषा नहीं रह गई है। Vapor और Kitura जैसे सर्वर फ्रेमवर्क के आगमन के साथ, स्विफ्ट बैकएंड समाधान बनाने के लिए एक पूर्ण उपकरण बन गया है। किसी भी सर्वर सिस्टम का मुख्य पहलू डेटाबेस के साथ काम करना है। इस लेख में, हम देखेंगे कि दो लोकप्रिय DBMS: रिलेशनल PostgreSQL और डॉक्यूमेंट-ओरिएंटेड MongoDB से कैसे कनेक्ट और इंटरैक्ट किया जाए। आप जानेंगे कि किन लाइब्रेरी का उपयोग करना है, कनेक्शन कैसे सेट करना है और शुद्ध स्विफ्ट पर बुनियादी CRUD ऑपरेशन कैसे करना है।



स्विफ्ट में PostgreSQL: सेटअप और बुनियादी ऑपरेशन

PostgreSQL सबसे विश्वसनीय रिलेशनल डेटाबेस में से एक है। स्विफ्ट में इसके साथ काम करने के लिए कई लाइब्रेरी हैं, लेकिन सबसे लोकप्रिय है PostgresNIO - एक एसिंक्रोनस ड्राइवर जो SwiftNIO पर आधारित है। यदि आप Vapor फ्रेमवर्क का उपयोग कर रहे हैं, तो PostgreSQL के साथ काम करना Fluent ORM के माध्यम से सरल हो जाता है।



PostgresNIO के माध्यम से PostgreSQL से कनेक्शन

निर्भरता स्थापित करने से शुरू करें। Package.swift में जोड़ें:

// swift-tools-version:5.7import PackageDescription

let package = Package( name: "MyApp", dependencies: [ .package(url: "https://github.com/vapor/postgres-nio.git", from: "1.18.0") ], targets: [ .target(name: "MyApp", dependencies: [ .product(name: "PostgresNIO", package: "postgres-nio") ]) ])


कनेक्शन और एक सरल SELECT क्वेरी निष्पादित करने का उदाहरण:

import PostgresNIOimport NIOCoreimport NIOPosix

let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)

let config = PostgresConnection.Configuration( host: "localhost", port: 5432, username: "myuser", password: "mypassword", database: "mydb", tls: .disable)

let connection = try await PostgresConnection.connect( on: eventLoopGroup.next(), configuration: config)

let rows = try await connection.query("SELECT id, name FROM users LIMIT 5;", logger: nil)

for try await row in rows { let id: Int = try row["id"].decode(Int.self) let name: String = try row["name"].decode(String.self) print("User: \\(id) - \\(name)")}


Vapor Fluent (ORM) के साथ कार्य

Fluent PostgreSQL के साथ काम करने के लिए एक उच्च-स्तरीय API प्रदान करता है। उपयोगकर्ता मॉडल इस प्रकार दिखेगा:

import Fluentimport Vapor

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 }}

// रिकॉर्ड बनानाlet user = User(name: "इवान", email: "ivan@example.com")try await user.save(on: req.db)

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


स्विफ्ट में MongoDB: दस्तावेज़ों के साथ कार्य

MongoDB एक NoSQL डेटाबेस है जो डेटा को BSON (बाइनरी JSON) प्रारूप में संग्रहीत करता है। स्विफ्ट के लिए MongoDB Inc. द्वारा विकसित एक आधिकारिक ड्राइवर MongoSwift है। यह एसिंक्रोनस ऑपरेशन और सिंक्रोनस मोड दोनों का समर्थन करता है।



MongoSwift की स्थापना और कनेक्शन

Package.swift में निर्भरता जोड़ें:

dependencies: [    .package(url: "https://github.com/mongodb/mongo-swift-driver.git", from: "1.3.0")],targets: [    .target(name: "MyApp", dependencies: [        .product(name: "MongoSwift", package: "mongo-swift-driver")    ])]


स्थानीय MongoDB से कनेक्शन और दस्तावेज़ सम्मिलित करने का उदाहरण:

import MongoSwift

let client = try MongoClient("mongodb://localhost:27017")let db = client.db("mydatabase")let collection = db.collection("u

ब्लॉग

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