परिचय: स्विफ्ट और डेटाबेस क्यों?
\nस्विफ्ट केवल मोबाइल एप्लिकेशन बनाने की भाषा नहीं है। Vapor और SwiftNIO फ्रेमवर्क की बदौलत, डेवलपर्स पूर्ण सर्वर एप्लिकेशन बना सकते हैं जो रिलेशनल और डॉक्यूमेंट-ओरिएंटेड डेटाबेस के साथ इंटरैक्ट करते हैं। इस लेख में, हम समझेंगे कि स्विफ्ट से PostgreSQL और MongoDB से कैसे जुड़ें, और शुरुआती लोगों के लिए सरल कोड उदाहरण दिखाएंगे।
\nयह लेख उन लोगों के लिए है जो स्विफ्ट की मूल बातों से पहले से परिचित हैं, लेकिन डेटाबेस के साथ काम करना शुरू कर रहे हैं। हम दो लोकप्रिय दृष्टिकोणों पर विचार करेंगे: क्लासिक SQL (PostgreSQL) और लचीला NoSQL (MongoDB)।
\n\n1. स्विफ्ट में PostgreSQL: संरचित डेटा
\nPostgreSQL एक शक्तिशाली ओपन-सोर्स रिलेशनल डेटाबेस है। स्विफ्ट में इसके साथ काम करने के लिए, हम PostgresKit पैकेज का उपयोग करेंगे, जो Vapor इकोसिस्टम का हिस्सा है।
1.1 स्थापना और कॉन्फ़िगरेशन
\nPackage.swift फ़ाइल में निर्भरता जोड़ें:
// swift-tools-version:5.7\nimport PackageDescription\n\nlet package = Package(\n name: "MyApp",\n dependencies: [\n .package(url: "https://github.com/vapor/postgres-kit.git", from: "2.0.0")\n ],\n targets: [\n .executableTarget(\n name: "MyApp",\n dependencies: [\n .product(name: "PostgresKit", package: "postgres-kit")\n ]\n )\n ]\n)\n\n1.2 डेटाबेस से कनेक्शन
\nकॉन्फ़िगरेशन का एक इंस्टेंस बनाएं और PostgreSQL सर्वर से कनेक्ट करें:
\nimport PostgresKit\n\nlet eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)\ndefer { try! eventLoopGroup.syncShutdown() }\n\nlet configuration = PostgresConfiguration(\n hostname: "localhost",\n port: 5432,\n username: "swift_user",\n password: "secret",\n database: "swift_db"\n)\n\nlet pool = EventLoopGroupConnectionPool(\n source: PostgresConnectionSource(configuration: configuration),\n on: eventLoopGroup\n)\ndefer { try! pool.syncShutdown() }\n\nlet connection = try pool.requestConnection().wait()\nprint("PostgreSQL से कनेक्शन स्थापित!")\n\n1.3 SQL क्वेरी निष्पादित करना
\nअब एक सरल क्वेरी करें तालिका बनाने और डेटा सम्मिलित करने के लिए:
\n// उपयोगकर्ताओं की तालिका बनाएं\ntry connection.query("""\n CREATE TABLE IF NOT EXISTS users (\n id SERIAL PRIMARY KEY,\n name TEXT NOT NULL,\n email TEXT UNIQUE NOT NULL\n )\n""").wait()\n\n// डेटा सम्मिलित करें\ntry connection.query("""\n INSERT INTO users (name, email) \n VALUES ('अलेक्से', 'alex@example.com')\n""").wait()\n\n// डेटा पढ़ें\nlet rows = try connection.query("SELECT * FROM users").wait()\nfor row in rows {\n let id = try row.decode(Int.self, column: "id")\n let name = try row.decode(String.self, column: "name")\n let email = try row.decode(String.self, column: "email")\n print("उपयोगकर्ता: \\(name) (\\(email)), ID: \\(id)")\n}\n\n2. स्विफ्ट में MongoDB: लचीले दस्तावेज़
\nMongoDB एक NoSQL डेटाबेस है जो डेटा को JSON-जैसे दस्तावेज़ों के रूप में संग्रहीत करता है। स्विफ्ट के लिए कई ड्राइवर हैं, लेकिन हम आधिकारिक MongoDB Swift Driver का उपयोग करेंगे।
\n\n2.1 ड्राइवर की स्थापना
\nPackage.swift में पैकेज जोड़ें:
dependencies: [\n .package(url: "https://github.com/mongodb/mongo-swift-driver.git", from: "1.0.0")\n],\ntargets: [\n .executableTarget(\n name: "MyApp",\n dependencies: [\n .product(name: "MongoSwift", package: "mongo-swift-driver")\n ]\n )\n]\n\n2.2 MongoDB से कनेक्शन
\nएक क्लाइंट बनाएं और डेटाबेस चुनें:
\nimport MongoSwift\n\nlet client = try MongoClient("mongodb://localhost:27017")\ndefer { try? client.syncClose() }\n\nlet database = client.db("swift_m