खोज…


टिप्पणियों

लेन-देन

निर्मित होने के तुरंत बाद लेन-देन का उपयोग करने की आवश्यकता होती है। यदि वे वर्तमान ईवेंट लूप में उपयोग नहीं किए जाते हैं (मूल रूप से इससे पहले कि हम किसी वेब अनुरोध की तरह प्रतीक्षा करते हैं) वे एक निष्क्रिय स्थिति में चले जाएंगे जहां आप उन्हें नहीं छोड़ सकते।

डेटाबेस में केवल एक लेनदेन हो सकता है जो एक समय में किसी विशेष ऑब्जेक्ट स्टोर पर लिखता है। तो आपके पास जितने चाहें उतने हो सकते हैं जो हमारी things दुकान से पढ़ें, लेकिन किसी भी समय केवल एक ही बदलाव कर सकता है।

IndexedDB उपलब्धता के लिए परीक्षण

आप window.indexedDB प्रॉपर्टी की उपस्थिति की जाँच करके वर्तमान परिवेश में IndexedDB समर्थन के लिए परीक्षण कर सकते हैं:

if (window.indexedDB) {
    // IndexedDB is available
}

एक डेटाबेस खोलना

डेटाबेस खोलना एक अतुल्यकालिक ऑपरेशन है। हमें अपने डेटाबेस को खोलने के लिए एक अनुरोध भेजने की आवश्यकता है और फिर घटनाओं के लिए सुनें ताकि हमें पता चले कि यह कब तैयार है।

हम एक डेमोडीबी डेटाबेस खोलेंगे। यदि यह अभी तक मौजूद नहीं है, तो अनुरोध भेजने पर यह बन जाएगा।

नीचे 2 कहते हैं कि हम अपने डेटाबेस के संस्करण 2 के लिए पूछ रहे हैं। किसी भी समय केवल एक संस्करण मौजूद है, लेकिन हम पुराने डेटा को अपग्रेड करने के लिए संस्करण संख्या का उपयोग कर सकते हैं, जैसा कि आप देखेंगे।

var db = null, // We'll use this once we have our database
    request = window.indexedDB.open("DemoDB", 2);

// Listen for success. This will be called after onupgradeneeded runs, if it does at all
request.onsuccess = function() {
    db = request.result; // We have a database!

    doThingsWithDB(db);
};

// If our database didn't exist before, or it was an older version than what we requested,
// the `onupgradeneeded` event will be fired.
// 
// We can use this to setup a new database and upgrade an old one with new data stores
request.onupgradeneeded = function(event) {
    db = request.result;

    // If the oldVersion is less than 1, then the database didn't exist. Let's set it up
    if (event.oldVersion < 1) {
        // We'll create a new "things" store with `autoIncrement`ing keys
        var store = db.createObjectStore("things", { autoIncrement: true });
    }

    // In version 2 of our database, we added a new index by the name of each thing
    if (event.oldVersion < 2) {
        // Let's load the things store and create an index
        var store = request.transaction.objectStore("things");

        store.createIndex("by_name", "name");
    }
};

// Handle any errors
request.onerror = function() {
    console.error("Something went wrong when we tried to request the database!");
};

वस्तुओं को जोड़ना

IndexedDB डेटाबेस में डेटा के साथ कुछ भी होने की आवश्यकता होती है। इस पृष्ठ के निचले भाग में रिमार्क्स अनुभाग में उल्लिखित लेन-देन के बारे में कुछ बातें ध्यान देने योग्य हैं।

हम उस डेटाबेस का उपयोग करेंगे जो हमने डेटाबेस खोलने में स्थापित किया है

// Create a new readwrite (since we want to change things) transaction for the things store
var transaction = db.transaction(["things"], "readwrite");

// Transactions use events, just like database open requests. Let's listen for success
transaction.oncomplete = function() {
    console.log("All done!");
};

// And make sure we handle errors
transaction.onerror = function() {
    console.log("Something went wrong with our transaction: ", transaction.error);
};

// Now that our event handlers are set up, let's get our things store and add some objects!
var store = transaction.objectStore("things");


// Transactions can do a few things at a time. Let's start with a simple insertion
var request = store.add({
    // "things" uses auto-incrementing keys, so we don't need one, but we can set it anyway
    key: "coffee_cup",
    name: "Coffee Cup",
    contents: ["coffee", "cream"]
});

// Let's listen so we can see if everything went well
request.onsuccess = function(event) {
    // Done! Here, `request.result` will be the object's key, "coffee_cup"
};


// We can also add a bunch of things from an array. We'll use auto-generated keys
var thingsToAdd = [{ name: "Example object" }, { value: "I don't have a name" }];

// Let's use more compact code this time and ignore the results of our insertions
thingsToAdd.forEach(e => store.add(e));

डाटा लिया जा रहा है

IndexedDB डेटाबेस में डेटा के साथ कुछ भी होने की आवश्यकता होती है। इस पृष्ठ के निचले भाग में रिमार्क्स अनुभाग में उल्लिखित लेन-देन के बारे में कुछ बातें ध्यान देने योग्य हैं।

हम उस डेटाबेस का उपयोग करेंगे जो हमने डेटाबेस खोलने में स्थापित किया है।

// Create a new transaction, we'll use the default "readonly" mode and the things store
var transaction = db.transaction(["things"]);

// Transactions use events, just like database open requests. Let's listen for success
transaction.oncomplete = function() {
    console.log("All done!");
};

// And make sure we handle errors
transaction.onerror = function() {
    console.log("Something went wrong with our transaction: ", transaction.error);
};

// Now that everything is set up, let's get our things store and load some objects!
var store = transaction.objectStore("things");


// We'll load the coffee_cup object we added in Adding objects
var request = store.get("coffee_cup");

// Let's listen so we can see if everything went well
request.onsuccess = function(event) {
    // All done, let's log our object to the console
    console.log(request.result);
};


// That was pretty long for a basic retrieval. If we just want to get just
// the one object and don't care about errors, we can shorten things a lot
db.transaction("things").objectStore("things")
    .get("coffee_cup").onsuccess = e => console.log(e.target.result);


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow