-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
431 lines (405 loc) · 11.1 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link rel="stylesheet" href="./example/index.css" />
</head>
<body>
<h1>IndexedDB Demo</h1>
<div class="ctrlbar">
<div class="ctrl-add active" data-add>添加 add</div>
<div class="ctrl-put" data-put>修改 put</div>
<div class="ctrl-delete" data-delete>删除 delete</div>
<div class="ctrl-query" data-query>查找 query</div>
</div>
<div class="test">
<div class="ctrl add show">
<ul>
<!-- <li class="use-id">
<label for="">ID</label>
<input type="text" class="id" placeholder="请输入id" />
</li> -->
<li class="use-name">
<label for="">姓名</label>
<input type="text" class="name" placeholder="请输入姓名" />
</li>
<li class="use-school">
<label for="">学校</label>
<input type="text" class="school" placeholder="请输入学校" />
</li>
<li>
<button id="addData">添加</button>
</li>
</ul>
</div>
<div class="ctrl put hide">
<ul>
<li class="use-id">
<label for="">ID</label>
<input type="text" class="id" placeholder="请输入id" />
</li>
<li class="use-name">
<label for="">姓名</label>
<input type="text" class="name" placeholder="请输入姓名" />
</li>
<li class="use-school">
<label for="">学校</label>
<input type="text" class="school" placeholder="请输入学校" />
</li>
<li>
<button id="putData">修改</button>
</li>
</ul>
</div>
<div class="ctrl delete hide">
<ul>
<li class="use-id">
<label for="">ID</label>
<input type="text" class="id" placeholder="请输入id" />
</li>
<li class="use-name">
<label for="">姓名</label>
<input type="text" class="name" placeholder="请输入姓名" />
</li>
<li class="use-school">
<label for="">学校</label>
<input type="text" class="school" placeholder="请输入学校" />
</li>
<li>
<button id="deleteData">删除</button>
</li>
</ul>
</div>
<div class="ctrl query hide">
<ul>
<li class="use-id">
<label for="">ID</label>
<input type="text" class="id" placeholder="请输入id" />
</li>
<li class="use-name">
<label for="">姓名</label>
<input type="text" class="name" placeholder="请输入姓名" />
</li>
<li class="use-school">
<label for="">学校</label>
<input type="text" class="school" placeholder="请输入学校" />
</li>
<li>
<button id="queryData">查找</button>
</li>
</ul>
</div>
</div>
<h2>预览 previews</h2>
<div class="tip"></div>
<div class="show-data">
<div class="show-head">
<ul>
<li>id</li>
<li>姓名</li>
<li>学校</li>
</ul>
</div>
<div id="showData"></div>
</div>
</body>
<script src="./example/tinydb.js"></script>
<script>
const ctrlbar = document.querySelector('.ctrlbar');
const testNode = document.querySelector('.test');
const add = document.querySelector("#addData");
const put = document.querySelector("#putData");
const _delete = document.querySelector("#deleteData");
const query = document.querySelector("#queryData");
const tip = document.querySelector('div.tip')
let wrap = document.querySelector("#showData");
let frame = document.createDocumentFragment();
const cache = {
ctrlbar: [ctrlbar.children[0]],
test: [testNode.children[0]]
}
ctrlbar.addEventListener('click', function (e) {
const allInput = document.querySelectorAll('input');
if (Reflect.ownKeys(e.target.dataset).length === 0) {
return false
}
if (cache.ctrlbar.length > 0) {
cache.ctrlbar[0].classList.remove('active')
cache.ctrlbar.length = 0
}
if (cache.test.length > 0) {
cache.test[0].classList.remove('show')
cache.test.length = 0
}
tip.innerHTML = ``;
cache.ctrlbar.push(e.target)
for (const key in e.target.dataset) {
const showNode = testNode.querySelector(`.${key}`)
tip.innerHTML = (key !== 'add' ? `use id ${key}`: '');
showNode.classList.add('show')
cache.test.push(showNode)
}
e.target.classList.add('active')
allInput.forEach(input => {
input.value = ''
})
})
const tables = [{
name: "table_student",
primaryKey: "id",
autoIncrement: true,
indexs: [{
index: "id",
relativeIndex: "id",
unique: true
},
{
index: "name",
relativeIndex: "name",
unique: false
},
{
index: "school",
relativeIndex: "school",
unique: false
}
]
}, {
name: "table_delete",
primaryKey: "id",
},
{
name: "table_delete1",
primaryKey: "id",
}
]
const test = TinyDB.of()
test.setup({
dbName: 'test',
version: 1
})
async function init() {
const result = await test.createTable(tables)
console.log(result)
}
init()
async function addTable() {
const result = await test.setVersion(2).createTable([{
name: "add_table",
keyPath: "id",
}])
console.log(result)
}
// addTable()
async function test_insert() {
await test.insert('table_student', {
name: 'lee1',
school: 'Github1',
})
await test.insert('table_student', {
name: 'lee2',
school: 'Github2',
})
await test.insert('table_student', {
name: 'lee3',
school: 'Github3'
})
await test.insert('table_student', {
name: 'lee4',
school: 'Github4'
})
await test.insert('table_student', {
name: 'lee5',
school: 'Github5'
})
}
// test_insert()
async function test_getAll() {
const result = await test.getAll('table_student')
console.log(result)
}
// test_getAll()
async function test_some() {
const result = await test.some('table_student', {
index: 'id',
lower: 1,
upper: 3
})
console.log(result)
}
// test_some()
async function test_update() {
const newData = {
name: 'lee11'
}
const result = await test.updateRecord('table_student', {
index: 'id',
value: 1
}, newData)
console.log(result)
}
// test_update()
async function test_getByPrimaryKey() {
const result = await test.getByPrimaryKey('table_student', 3)
console.log(result)
}
// test_getByPrimaryKey()
async function test_getByIndex() {
const result = await test.getByIndex('table_student', {
index: 'id',
value: 2
})
console.log(result)
}
// test_getByIndex()
async function test_deleteRecord() {
// used index
const result = await test.deleteRecord('table_student', {
index: 'id',
value: 6
})
// used primaryKey to delete
// const result = await test.deleteRecord('table_student', 4)
console.log(result)
}
// test_deleteRecord()
async function test_deleteDatabase() {
const result = await test.deleteDatabase('test')
console.log(result)
}
// test_deleteDatabase()
async function test_clearTableRecord() {
const result = await test.clearTableRecord('table_student')
console.log(result)
}
// test_clearTableRecord()
async function test_deleteTable() {
const result = await test.setVersion(3).deleteTable('table_delete')
console.log(result)
}
// test_deleteTable()
async function test_getDataWithLimits() {
const result = await test.getDataWithLimits('table_student', {
length: 1
})
console.log(result)
}
// test_getDataWithLimits()
showData();
add.addEventListener('click', async () => {
let inputs = document.querySelectorAll(".add input");
let data = {
name: inputs[0].value,
school: inputs[1].value
};
await test.insert("table_student", data);
requestAnimationFrame(() => showData())
})
put.addEventListener('click', async () => {
let inputs = document.querySelectorAll(".put input");
let data = {
name: inputs[1].value,
school: inputs[2].value
};
if (inputs[0].value !== "") data.id = +inputs[0].value;
await test.updateRecord("table_student", {
index: 'id',
value: data.id
}, data)
requestAnimationFrame(() => showData())
})
_delete.addEventListener('click', async () => {
let inputs = document.querySelectorAll(".delete input");
let data = {};
await test.deleteRecord("table_student", {
index: 'id',
value: +inputs[0].value
});
requestAnimationFrame(() => showData())
})
query.addEventListener('click', async () => {
let inputs = document.querySelectorAll(".query input");
let data = {};
console.log(inputs[0].value)
const result = await test.getByIndex('table_student', {
index: 'id',
value: +inputs[0].value
})
for (const item of result) {
const ul = new Element("ul", {
id: "db-data"
}, [
new Element("li", {
class: "id"
}, [`${item.id}`]),
new Element("li", {
class: "name"
}, [`${item.name}`]),
new Element("li", {
class: "school"
}, [`${item.school}`])
]);
frame.append(ul.render());
wrap.innerHTML = ``;
wrap.append(frame);
}
frame.innerHTML = ``;
})
const Element = function (tagName, props, children) {
this.tagName = tagName;
this.props = props;
this.children = children;
};
Element.prototype.render = function () {
const ele = document.createElement(this.tagName);
const props = this.props;
for (var propsName in props) {
var propsValue = props[propsName];
ele.setAttribute(propsName, propsValue);
}
const children = this.children || [];
children.forEach(child => {
const childEl =
child instanceof Element ?
child.render() :
document.createTextNode(child);
ele.appendChild(childEl);
});
return ele;
};
const createElement = function (tagName, props, children) {
return new Element(tagName, props, children);
};
async function showData() {
const result = await test.getAll("table_student")
if(!result.length) {
wrap.innerHTML = ``
return
}
for (const item of result) {
const ul = new Element("ul", {
id: "db-data"
}, [
new Element("li", {
class: "id"
}, [`${item.id}`]),
new Element("li", {
class: "name"
}, [`${item.name}`]),
new Element("li", {
class: "school"
}, [`${item.school}`])
]);
frame.append(ul.render());
wrap.innerHTML = ``;
requestAnimationFrame(() => {
wrap.append(frame);
})
}
frame.innerHTML = ``;
}
</script>
</html>