Skip to content

Commit 9639fe9

Browse files
author
AryanSinghalGit
committed
add indexedDB
1 parent 4c6ab34 commit 9639fe9

File tree

3 files changed

+83
-23
lines changed

3 files changed

+83
-23
lines changed

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@material-ui/core": "^4.11.0",
6+
"@material-ui/core": "^4.10.2",
77
"@testing-library/jest-dom": "^4.2.4",
88
"@testing-library/react": "^9.5.0",
99
"@testing-library/user-event": "^7.2.1",
10+
"dexie": "^3.0.2",
1011
"faker": "^4.1.0",
12+
"idb": "^5.0.4",
1113
"material-table": "^1.68.1",
1214
"react": "^16.13.1",
1315
"react-dom": "^16.13.1",

src/todo/todo.jsx

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,91 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import {
3-
Paper, Button, Grid, TextField, MenuItem,
3+
Button, Grid, TextField, MenuItem,
44
} from '@material-ui/core';
55
import faker from 'faker';
66
import MaterialTable from 'material-table';
7+
// import { openDB } from 'idb';
8+
import Dexie from 'dexie';
79

810
const Todo = () => {
911
const [dataStructure, setDataStructure] = React.useState('array');
1012
const [array, setArray] = React.useState([]);
13+
const [arrayFromDB, setArrayFromDB] = React.useState([]);
1114
const [dataCount, setDataCount] = React.useState(1);
1215
const [error, setError] = React.useState('');
16+
const [store, setStore] = React.useState();
17+
const [addDelete, setAddDelete] = React.useState(false);
1318

14-
const generateData = () => {
19+
useEffect(() => {
20+
try {
21+
if (!('indexedDB' in window)) {
22+
console.log('This browser doesn\'t support IndexedDB');
23+
return;
24+
}
25+
const db = new Dexie('listOfObjects');
26+
db.version(1).stores({
27+
listOfObjects: 'id,jobTitle,jobType',
28+
});
29+
db.listOfObjects.clear();
30+
setStore(db.listOfObjects);
31+
} catch (err) {
32+
console.log(err);
33+
}
34+
}, []);
35+
36+
const generateData = async () => {
37+
setAddDelete(true);
38+
const initialTime = new Date();
1539
if (dataStructure === 'array') {
1640
for (let i = 0; i < dataCount; i += 1) {
1741
array.push({
1842
jobTitle: faker.name.jobTitle(),
1943
jobType: faker.name.jobType(),
2044
});
2145
}
22-
setArray([...array]);
46+
setAddDelete(false);
47+
console.log('time taken by Array in inserting is:', (new Date() - initialTime));
48+
return setArray([...array]);
49+
}
50+
try {
51+
const arrayFromDBLength = arrayFromDB.length;
52+
for (let i = 1; i <= dataCount; i += 1) {
53+
store.add({
54+
id: arrayFromDBLength + i,
55+
jobTitle: faker.name.jobTitle(),
56+
jobType: faker.name.jobType(),
57+
});
58+
}
59+
const dbArray = await store.where('id').above(0).toArray();
60+
console.log('time taken by DB in inserting is:', (new Date() - initialTime));
61+
setAddDelete(false);
62+
return setArrayFromDB(dbArray);
63+
} catch (e) {
64+
setAddDelete(false);
65+
return alert(`Error: + ${e.stack || e}`);
2366
}
2467
};
2568

26-
const deleteData = () => {
69+
const deleteData = async () => {
70+
const initialTime = new Date();
2771
if (dataStructure === 'array') {
28-
for (let i = 0; i < dataCount; i += 1) {
72+
const arrayLength = array.length;
73+
for (let i = 0; i < dataCount && arrayLength; i += 1) {
2974
array.pop();
3075
}
31-
setArray([...array]);
76+
console.log('time taken by Array in deleting is:', (new Date() - initialTime));
77+
return setArray([...array]);
78+
}
79+
try {
80+
const arrayLength = arrayFromDB.length;
81+
for (let i = 0; i < dataCount && arrayLength; i += 1) {
82+
store.delete(arrayLength - i);
83+
}
84+
const dbArray = await store.where('id').above(0).toArray();
85+
console.log('time taken by DB in deleting is:', (new Date() - initialTime));
86+
return setArrayFromDB(dbArray);
87+
} catch (e) {
88+
return alert(`Error: + ${e.stack || e}`);
3289
}
3390
};
3491

@@ -59,6 +116,7 @@ const Todo = () => {
59116
label: 'IndexedDB',
60117
},
61118
];
119+
62120
const columns = [
63121
{ title: 'Job Title', field: 'jobTitle' },
64122
{ title: 'Job Type', field: 'jobType' },
@@ -75,7 +133,6 @@ const Todo = () => {
75133
label="Select Data Structure"
76134
value={dataStructure}
77135
onChange={handleScrollDownChange}
78-
helperText="Please select your currency"
79136
variant="outlined"
80137
>
81138
{options.map((option) => (
@@ -123,20 +180,11 @@ const Todo = () => {
123180
</Button>
124181
</Grid>
125182
</Grid>
126-
<div align="center">
127-
{console.log(array)}
128-
{
129-
(array && array.length)
130-
? (
131-
<MaterialTable
132-
title="TODO"
133-
columns={columns}
134-
data={array}
135-
/>
136-
)
137-
: null
138-
}
139-
</div>
183+
<MaterialTable
184+
title="TODO"
185+
columns={columns}
186+
data={(dataStructure === 'array') ? [...array] : [...arrayFromDB]}
187+
/>
140188
</div>
141189
);
142190
};

0 commit comments

Comments
 (0)