आधुनिक वेब एप्लिकेशन, विशेष रूप से प्रगतिशील वेब ऐप्स (पीडब्ल्यूए) बनाते समय, डेटा को ऑफ़लाइन संग्रहीत करने का एक तरीका होना महत्वपूर्ण है। IndexedDB एक शक्तिशाली क्लाइंट-साइड डेटाबेस है जो वेब ऐप्स को उपयोगकर्ता के ऑफ़लाइन होने पर भी डेटा संग्रहीत और पुनर्प्राप्त करने की अनुमति देता है। यह मार्गदर्शिका आपको IndexedDB की मूल बातें बताएगी, जो आपको दिखाएगी कि अपने वेब ऐप के भीतर डेटा (CRUD संचालन) कैसे बनाएं, पढ़ें, अपडेट करें और हटाएं।
IndexedDB फ़ाइलों और ब्लॉब्स सहित बड़ी मात्रा में संरचित डेटा के क्लाइंट-साइड भंडारण के लिए एक निम्न-स्तरीय एपीआई है। लोकलस्टोरेज के विपरीत, IndexedDB आपको केवल स्ट्रिंग्स ही नहीं, बल्कि जटिल डेटा प्रकारों को संग्रहीत करने की अनुमति देता है। यह एक एसिंक्रोनस, ट्रांसेक्शनल डेटाबेस मॉडल का उपयोग करता है, जो इसे बड़े डेटासेट या ऑफ़लाइन डेटा सिंकिंग को संभालने वाले अनुप्रयोगों के लिए शक्तिशाली बनाता है।
आइए IndexedDB के साथ काम करने के मुख्य चरणों के बारे में जानें। हम कवर करेंगे:
IndexedDB के साथ इंटरैक्ट करने के लिए, आपको सबसे पहले डेटाबेस से एक कनेक्शन खोलना होगा। यदि डेटाबेस मौजूद नहीं है, तो इसे बनाया जाएगा।
const request = indexedDB.open('MyCustomersDatabase', 1); request.onerror = (event) => { console.error('Database error:', event.target.errorCode); }; request.onsuccess = (event) => { const db = event.target.result; console.log('Database opened successfully', db); }; request.onupgradeneeded = (event) => { const db = event.target.result; if (!db.objectStoreNames.contains('customers')) { const objectStore = db.createObjectStore('customers', { keyPath: 'id' }); objectStore.createIndex('name', 'name', { unique: false }); objectStore.createIndex('email', 'email', { unique: true }); console.log('Object store created.'); } };
यहां क्या हो रहा है:
अब जब हमारे पास अपना डेटाबेस और ऑब्जेक्ट स्टोर सेट अप हो गया है, तो आइए इसमें कुछ डेटा जोड़ें।
const addCustomer = (db, customer) => { const transaction = db.transaction(['customers'], 'readwrite'); const objectStore = transaction.objectStore('customers'); const request = objectStore.add(customer); request.onsuccess = () => { console.log('Customer added:', customer); }; request.onerror = (event) => { console.error('Error adding customer:', event.target.errorCode); }; } const customer = { id: 1, name: 'John Doe', email: '[email protected]' }; request.onsuccess = (event) => { const db = event.target.result; addCustomer(db, customer); };
यहां क्या हो रहा है:
IndexedDB से डेटा पढ़ना भी आसान है। आइए get() विधि का उपयोग करके उस ग्राहक को पुनः प्राप्त करें जिसे हमने अभी जोड़ा है।
const getCustomer = (db, id) => { const transaction = db.transaction(['customers'], 'readonly'); const objectStore = transaction.objectStore('customers'); const request = objectStore.get(id); request.onsuccess = (event) => { const customer = event.target.result; if (customer) { console.log('Customer found:', customer); } else { console.log('Customer not found.'); } }; request.onerror = (event) => { console.error('Error fetching customer:', event.target.errorCode); }; } request.onsuccess = (event) => { const db = event.target.result; getCustomer(db, 1); // Fetch customer with ID 1 };
मौजूदा रिकॉर्ड को अपडेट करने के लिए, हम पुट() विधि का उपयोग कर सकते हैं, जो ऐड() के समान काम करता है लेकिन यदि कुंजी पहले से मौजूद है तो रिकॉर्ड को बदल देता है।
const updateCustomer = (db, customer) => { const transaction = db.transaction(['customers'], 'readwrite'); const objectStore = transaction.objectStore('customers'); const request = objectStore.put(customer); request.onsuccess = () => { console.log('Customer updated:', customer); }; request.onerror = (event) => { console.error('Error updating customer:', event.target.errorCode); }; } const updatedCustomer = { id: 1, name: 'Jane Doe', email: '[email protected]' }; request.onsuccess = (event) => { const db = event.target.result; updateCustomer(db, updatedCustomer); };
अंत में, किसी रिकॉर्ड को हटाने के लिए, डिलीट() विधि का उपयोग करें।
const deleteCustomer = (db, id) => { const transaction = db.transaction(['customers'], 'readwrite'); const objectStore = transaction.objectStore('customers'); const request = objectStore.delete(id); request.onsuccess = () => { console.log('Customer deleted.'); }; request.onerror = (event) => { console.error('Error deleting customer:', event.target.errorCode); }; } request.onsuccess = (event) => { const db = event.target.result; deleteCustomer(db, 1); // Delete customer with ID 1 };
IndexedDB क्लाइंट-साइड डेटा स्टोरेज को संभालने के लिए एक मजबूत समाधान है, खासकर ऑफलाइन-फर्स्ट वेब ऐप्स में। इस गाइड का पालन करके, आपने सीखा कि कैसे:
IndexedDB के साथ, आप अधिक लचीले वेब एप्लिकेशन बना सकते हैं जो स्थानीय रूप से डेटा संग्रहीत करते हैं और इंटरनेट कनेक्शन के बिना भी काम करते हैं।
एमडीएन वेब डॉक्स - IndexedDB एपीआई
IndexedDB कैसे काम करता है, इसके API तरीके और उपयोग के मामलों पर एक व्यापक मार्गदर्शिका।
एमडीएन इंडेक्सेडडीबी गाइड
Google डेवलपर्स - IndexedDB
ऑफ़लाइन-सक्षम वेब ऐप्स बनाने के लिए IndexedDB की सर्वोत्तम प्रथाओं और उपयोग को कवर करने वाला एक विस्तृत लेख।
Google डेवलपर्स - IndexedDB
W3C अनुक्रमित डेटाबेस एपीआई
W3C का आधिकारिक विनिर्देश IndexedDB के तकनीकी कार्यान्वयन और संरचना को रेखांकित करता है।
W3C IndexedDB स्पेक
यदि आप इस ट्यूटोरियल के अलावा IndexedDB के बारे में और अधिक जानना चाहते हैं तो ये संसाधन अतिरिक्त गहराई और संदर्भ प्रदान करेंगे!
हैप्पी कोडिंग!
अस्वीकरण: उपलब्ध कराए गए सभी संसाधन आंशिक रूप से इंटरनेट से हैं। यदि आपके कॉपीराइट या अन्य अधिकारों और हितों का कोई उल्लंघन होता है, तो कृपया विस्तृत कारण बताएं और कॉपीराइट या अधिकारों और हितों का प्रमाण प्रदान करें और फिर इसे ईमेल पर भेजें: [email protected] हम इसे आपके लिए यथाशीघ्र संभालेंगे।
Copyright© 2022 湘ICP备2022001581号-3