|
| 1 | +import IndexDBP from '../dist/indexDBP.js'; |
| 2 | + |
| 3 | +(async () => { |
| 4 | + const mydb = new IndexDBP({ |
| 5 | + name: 'testDB', |
| 6 | + }); |
| 7 | + await mydb.init(); |
| 8 | + // deleteIndex |
| 9 | + // if (mydb.containObjectStore('indexObjectStore') && mydb.$db.indexObjectStore.containIndex('time')) { |
| 10 | + // await mydb.$db.indexObjectStore.deleteIndex('time') |
| 11 | + // } |
| 12 | + |
| 13 | + // create objectStore |
| 14 | + if (!mydb.containObjectStore('testObjectStore')) { |
| 15 | + await mydb.createObjectStore('testObjectStore'); |
| 16 | + } |
| 17 | + if (!mydb.containObjectStore('indexObjectStore')) { |
| 18 | + await mydb.createObjectStore('indexObjectStore', {keyPath: 'randomId'}); |
| 19 | + await mydb.$db.indexObjectStore.createIndex('time', 'time', {unique: false, multiEntry: false}); |
| 20 | + } |
| 21 | + |
| 22 | + // delete ObjectStore |
| 23 | + // if (mydb.containObjectStore('testObjectStore2')) { |
| 24 | + // mydb.deleteObjectStore('testObjectStore2'); |
| 25 | + // } |
| 26 | + |
| 27 | + // insert Data |
| 28 | + await mydb.$db.testObjectStore.insert({key1: 'hello', key2: 123, key3: true, key4: new Date()}); |
| 29 | + const rand1: number = Math.random(); |
| 30 | + const rand2: number = Math.random(); |
| 31 | + await mydb.$db.indexObjectStore.insert({randomId: rand1, time: new Date('2019/04/03')}); |
| 32 | + await mydb.$db.indexObjectStore.insert({randomId: rand2, time: new Date('2019/04/05')}); |
| 33 | + // find Data |
| 34 | + const res0 = await mydb.$db.testObjectStore.find(1); |
| 35 | + const res1 = await mydb.$db.indexObjectStore.find(new Date('2019/04/03'), 'time'); |
| 36 | + const res2 = await mydb.$db.indexObjectStore.find({$lte: rand2 , count: 5}); |
| 37 | + console.log(res0, res1); |
| 38 | + console.log(res2, rand2); |
| 39 | + // remove data |
| 40 | + // let resRemove = await mydb.$db.testObjectStore.remove({$lte: 5}); |
| 41 | + // console.log(resRemove); |
| 42 | + // update data |
| 43 | + // let resUpdate = await mydb.$db.testObjectStore.update({$lte: 10}, {test: 222}, {multi: true}); |
| 44 | + const resUpdate = await mydb.$db.testObjectStore.update(1, {test: 222}, {upsert: true}); |
| 45 | + console.log(resUpdate); |
| 46 | +})(); |
0 commit comments