-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
113 lines (101 loc) · 4.65 KB
/
index.html
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>IndexedDB Demo</title>
<script type="module">
import { faker } from 'https://esm.sh/@faker-js/faker';
import { openDB } from 'https://cdn.jsdelivr.net/npm/idb@8/+esm';
const DATABASE_NAME = "indexeddb-demo";
const OBJECT_STORE_NAME = "person";
async function createIndexedDB() {
document.getElementById('action-label').innerHTML = " ";
console.log("Try to create DB...")
const start = performance.now();
await openDB(DATABASE_NAME, 1, {
upgrade(database) {
if (!database.objectStoreNames.contains(OBJECT_STORE_NAME)) {
database.createObjectStore(OBJECT_STORE_NAME, {keyPath: 'id'});
console.log('Person object store created successfully')
}
}
});
const end = performance.now();
document.getElementById('action-label').innerText = `DB created successfully took ${Math.trunc(end - start)} ms`;
console.log("DB created successfully");
}
async function saveDataToIndexedDB(size) {
document.getElementById('action-label').innerHTML = " ";
const data = generateFakePersonsData(size);
console.log(`Saving ${size} to person object store...`);
const start = performance.now();
const db = await openDB(DATABASE_NAME)
db.clear(OBJECT_STORE_NAME) // truncate person object store before inserting new data
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
const store = tx.store
data.forEach(item => store.put(item));
await tx.done;
const end = performance.now();
document.getElementById('action-label').innerText = `Data saved successfully. took ${Math.trunc(end - start)} ms`;
console.log("Data saved successfully");
}
function generateFakePersonsData(size) {
const persons = [];
for (let i = 0; i < size; i++) {
persons.push({
id: i,
name: faker.person.fullName(),
birthDate: faker.date.birthdate({min: 18, max: 80, mode: "age", refDate: 'string'}),
email: faker.internet.email(),
address: faker.location.streetAddress(),
city: faker.location.city(),
country: faker.location.country()
});
}
return persons;
}
async function getAll() {
document.getElementById('action-label').innerHTML = " ";
const start = performance.now();
const database = await openDB(DATABASE_NAME);
const data = await database.getAll(OBJECT_STORE_NAME);
const end = performance.now();
document.getElementById('action-label').innerText = `Total items fetched: ${data.length}. took ${Math.trunc(end - start)} ms`;
console.log(`Get all finished successfully (size: ${data.length})`);
}
async function getById() {
document.getElementById('action-label').innerHTML = " ";
const id = document.getElementById('search-id').value;
console.log(`Searching for id: ${id}`);
const start = performance.now();
const database = await openDB(DATABASE_NAME);
const item = await database.get(OBJECT_STORE_NAME, +id);
const end = performance.now();
document.getElementById('action-label').innerText = `Get item took ${Math.trunc(end - start)} ms`;
document.getElementById('search-item').innerText = JSON.stringify(item);
console.log(`Get by id finished successfully`);
}
window.createIndexedDB = createIndexedDB;
window.saveDataToIndexedDB = saveDataToIndexedDB;
window.getAll = getAll;
window.getById = getById;
</script>
</head>
<body>
<h1>Hello IndexedDB</h1>
<h3>Open browser console to see the magic!</h3>
<button onclick="createIndexedDB()">Create IndexedDB</button>
<button onclick="saveDataToIndexedDB(25000)">Save data</button>
<button onclick="getAll()">Get all</button>
<div>
<span id="action-label"> </span>
</div>
<div style="margin-top: 10px">
<label for="search-id">Search by ID: </label><input id="search-id" type="text">
<button onclick="getById()">Submit</button>
</div>
<div style="margin-top: 10px">
<span id="search-item"></span>
</div>
</body>
</html>