Your cart is currently empty!
Category: indexeddb
-
Khóa học miễn phí IndexedDB – Indexes nhận dự án làm có lương
IndexedDB – Indexes
Indexes are a kind of object store used to retrieve data from the reference object stored by a specified property. Even though an index is inside the reference object store and contains the same data, instead of the reference store”s primary key it uses the specified property as its key path.
Indexes are used to define a unique constraint on your data and they are made when the object stores are created. To create an index, call the createIndex method on an object store instance −
Syntax
var myIDBIndex = objectStore.createIndex(indexName, keyPath); var myIDBIndex = objectStore.createIndex(indexName, keyPath, objectParameters);
This method creates and returns an index object. The method creates an index that takes the following arguments −
-
Index name − The name of the index.
-
Keypath − We mention the primary key here.
-
Object Parameters − There are two object parameters.
-
Unique − Duplicate values cannot be added.
-
Multi entry − If true, the index will add an entry in the index for each array element when the keyPath resolves to an Array. If false, it will add one single entry containing the Array.
Example
The following example shows the implementation of indexes in an object store −
<!DOCTYPE html> <html lang="en"> <head> <title>Document</title> </head> <body> <script> const request = indexedDB.open("botdatabase",1); request.onupgradeneeded = function(){ const db = request.result; const store = db.createObjectStore("bots",{ keyPath: "id"}); store.createIndex("branch_db",["branch"],{unique: false}); } request.onsuccess = function(){ document.write("database opened successfully"); const db = request.result; const transaction=db.transaction("bots","readwrite"); const store = transaction.objectStore("bots"); const branchIndex = store.index("branch_db"); store.add({id: 1, name: "jason",branch: "IT"}); store.add({id: 2, name: "praneeth",branch: "CSE"}); store.add({id: 3, name: "palli",branch: "EEE"}); store.add({id: 4, name: "abdul",branch: "IT"}); store.put({id: 4, name: "deevana",branch: "CSE"}); transaction.oncomplete = function(){ db.close; } } </script> </body> </html>
Output
branchIndex: 1 [''CSE''] 0: "CSE" length: 1 4 {id: 4, name: ''deevana'', branch: ''CSE''} branch: "CSE" id: 4 name: "deevana" 2 [''EEE''] 0: "EEE" length: 1 3 {id: 3, name: ''palli'', branch: ''EEE''} branch: "EEE" id: 3 name: "palli" 3 [''IT''] 0: "IT" length: 1 1 {id: 1, name: ''jason'', branch: ''IT''} branch: "IT" id: 1 name: "jason"
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí IndexedDB – Deleting Data nhận dự án làm có lương
IndexedDB – Deleting Data
There are many situations where we need to delete data from the database; be it for storage purposes or just removing unwanted data to free up space. If we want to delete this unnecessary data from a database we can use the .delete() function
Syntax
const request = objectStore.delete(data);
We use the delete() function to delete the fields of the database which are not required.
Example
Let us look at an example script to delete data −
<!DOCTYPE html> <html lang="en"> <head> <title>Document</title> </head> <body> <script> const request = indexedDB.open("botdatabase",1); request.onupgradeneeded = function(){ const db = request.result; const store = db.createObjectStore("bots",{ keyPath: "id"}); } request.onsuccess = function(){ document.write("database opened successfully"); const db = request.result; const transaction=db.transaction("bots","readwrite"); const store = transaction.objectStore("bots"); store.add({id: 1, name: "jason",branch: "IT"}); store.add({id: 2, name: "praneeth",branch: "CSE"}); store.add({id: 3, name: "palli",branch: "EEE"}); store.add({id: 4, name: "abdul",branch: "IT"}); store.put({id: 4, name: "deevana",branch: "CSE const deletename = store.delete(1); deletename.onsuccess = function(){ document.write("id : 1 has been deleted"); } transaction.oncomplete = function(){ db.close; } } </script> </body> </html>
Output
database opened successfully id : 1 has been deleted
The database after deletion of id:1 =
0 2 {id: 2, name: ''praneeth'', branch: ''CSE''} 1 3 {id: 3, name: ''palli'', branch: ''EEE''} 2 4 {id: 4, name: ''deevana'', branch: ''CSE''}
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc