-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexed_db_library.js
96 lines (80 loc) · 3.4 KB
/
indexed_db_library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function insert_record(dbName, storeName, recordData) {
/**
* Open the database and insert a record into the object store.
* The `put` method is used to insert a record into the object store.
* If the record already exists, it will be updated.
*/
let request = indexedDB.open(dbName, 1);
request.onsuccess = function (event) {
let db = event.target.result;
// Perform data insertion within a transaction
let transaction = db.transaction([storeName], 'readwrite');
let store = transaction.objectStore(storeName);
transaction.oncomplete = function (event) {
console.log('Data added successfully.');
};
transaction.onerror = function (event) {
console.log('Error adding data: ' + event.target.error);
};
store.put(recordData);
};
request.onerror = function (event) {
console.log('Error opening database: ' + event.target.error);
};
}
function read_record(dbName, storeName, callback) {
/**
* Open the database and read the latest record from the object store.
*/
let request = indexedDB.open(dbName, 1);
request.onsuccess = function (event) {
let db = event.target.result;
let transaction = db.transaction([storeName], 'readonly');
let store = transaction.objectStore(storeName);
// Create a cursor to iterate through the records in reverse order
let cursorRequest = store.openCursor(null, 'prev');
cursorRequest.onsuccess = function (event) {
let cursor = event.target.result;
if (cursor) {
// Cursor points to the latest record, so you can access its value
let latestRecord = cursor.value;
console.log('Latest record:', latestRecord);
callback(latestRecord); // Pass the data to the callback function
} else {
console.log('No records found in the store.');
callback(null); // Pass null to the callback function if no records are found
}
};
cursorRequest.onerror = function (event) {
console.error('Error reading records:', event.target.error);
callback(null); // Pass null to the callback function in case of error
};
};
request.onerror = function (event) {
console.error('Error opening database:', event.target.error);
callback(null); // Pass null to the callback function in case of error
};
}
// Function to clear the object store
function clearObjectStore(dbName, storeName) {
/*
* Open the database and clear the object store using the `clear` method.
* The `clear` method deletes all records from the object store.
*/
let request = indexedDB.open(dbName, 1);
request.onsuccess = function (event) {
let db = event.target.result;
let transaction = db.transaction([storeName], 'readwrite');
let store = transaction.objectStore(storeName);
let clearRequest = store.clear();
clearRequest.onsuccess = function (event) {
console.log('Object store cleared successfully.');
};
clearRequest.onerror = function (event) {
console.error('Error clearing object store:', event.target.error);
};
};
request.onerror = function (event) {
console.error('Error opening database:', event.target.error);
};
}