/ src / common / data / database.js
database.js
 1  import { openDB } from 'idb'
 2  import DappModel from './dapp'
 3  
 4  const DB_NAME = 'status_discover'
 5  const DB_STORE_DAPPS = 'store_dapps'
 6  
 7  function open() {
 8    return openDB(DB_NAME, 1, {
 9      upgrade(db) {
10        db.createObjectStore(DB_STORE_DAPPS, {
11          keyPath: 'id',
12        })
13      },
14    })
15  }
16  
17  export default class Database {
18    static async fetchAllDapps() {
19      const result = []
20      const db = await open()
21      let cursor = await db.transaction(DB_STORE_DAPPS).store.openCursor()
22  
23      while (cursor) {
24        result.push(Object.assign(new DappModel(), cursor.value))
25        cursor = await cursor.continue()
26      }
27      return result
28    }
29  
30    static async creditDapp(dapp) {
31      const db = await open()
32      await db.put(DB_STORE_DAPPS, dapp)
33    }
34  }