forked from quandz24-ui/OriginOS_web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
82 lines (67 loc) · 2.35 KB
/
data.js
File metadata and controls
82 lines (67 loc) · 2.35 KB
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
let db = null;
// ✅ Ghi dữ liệu
function setData(id, content) {
const tx = db.transaction("user_data", "readwrite");
const store = tx.objectStore("user_data");
store.put({ id, content });
}
function getData(id, callback) {
if (!db) return callback(null);
const tx = db.transaction("user_data", "readonly");
const store = tx.objectStore("user_data");
const req = store.get(id);
req.onsuccess = () => callback(req.result ? req.result.content : null);
req.onerror = () => callback(null);
}
// ✅ Xóa một mục
function removeData(id) {
const tx = db.transaction("user_data", "readwrite");
const store = tx.objectStore("user_data");
store.delete(id);
tx.oncomplete = () => console.log("🗑️ Đã xóa:", id);
}
// ✅ Xóa toàn bộ
function removeAllData() {
const tx = db.transaction("user_data", "readwrite");
const store = tx.objectStore("user_data");
const clearReq = store.clear();
clearReq.onsuccess = () => console.log("🧹 Đã xóa toàn bộ dữ liệu");
clearReq.onerror = () => console.error("❌ Lỗi khi xóa toàn bộ");
}
// Hàm khởi tạo và mở DB
// Lưu dữ liệu: setData(key, value)
// Đọc dữ liệu: getData(key, callback)
// Xóa một mục: removeData(key)
// Xóa tất cả: removeAllData()
function initOriginDB(callbackWhenReady) {
const request = indexedDB.open("OriginDB", 1);
request.onupgradeneeded = function (event) {
db = event.target.result;
if (!db.objectStoreNames.contains("user_data")) {
db.createObjectStore("user_data", { keyPath: "id" });
}
};
request.onsuccess = function (event) {
db = event.target.result;
console.log("✅ OriginDB sẵn sàng");
if (typeof callbackWhenReady === "function") callbackWhenReady();
};
request.onerror = function (event) {
console.error("❌ Lỗi IndexedDB:", event.target.error);
};
}
function getData(id, callback) {
if (!db) {
console.warn("⚠️ DB chưa sẵn sàng.");
callback(null);
return;
}
const tx = db.transaction("user_data", "readonly");
const store = tx.objectStore("user_data");
const req = store.get(id);
req.onsuccess = () => callback(req.result ? req.result.content : null);
req.onerror = () => {
console.error("❌ Lỗi khi đọc:", id);
callback(null);
};
}