class UtilDB {
    constructor(storeName,dbName,version){//name,version不需要传,在初始化对象的时候会把值传给
        this.store = storeName;//仓库名称
        this.dbName = dbName;//数据库名称
        this.version = version;//版本号名称
        // this.data = data;//存储的数据
    }
    /**
   * 打开创建数据库 返回promise
   * @param {DB名字} name
   * @param {版本号} version
   */
    openDB () {
        return new Promise((resolve, reject) =>{
            var version=this.version || 1;
            let request=window.indexedDB.open(this.dbName,this.version);
            // /当我们传入的版本号和数据库当前版本号不一致的时候onupgradeneeded就会被调用
            request.onupgradeneeded=((e)=>{//只新建一次store
                    var db=e.target.result;
                    // if(!db.objectStoreNames.contains('drawGeometrys')){//创建数据库的时候我们就为其添加了一个名为students的object store，准备一些数据以供添加
                    //     db.createObjectStore('drawGeometrys',{autoIncrement: true});//创建数据库的表格（或者叫数据库仓库）
                    //     // db.createObjectStore('drawGeometrys',{keyPath:"id"});//以id为主键
                    // }
                    if(!db.objectStoreNames.contains(this.store)){//创建数据库的时候我们就为其添加了一个名为students的object store，准备一些数据以供添加
                        // db.createObjectStore(this.store,{autoIncrement: true});//创建数据库的表格（或者叫数据库仓库）
                        db.createObjectStore(this.store,{keyPath:"id"});//以id为主键
                    }
                    console.log('DB version changed to '+version);
                    //调用数据库实例的deleteObjectStore方法可以删除一个object store，这个就得在onupgradeneeded里面调用了
                    // if(db.objectStoreNames.contains('students')){ 
                    //     db.deleteObjectStore('students'); 
                    // }
            });
            request.onerror=function(e){
                console.log('OPen Error!');
            };
            request.onsuccess=function(e){
                //在创建indexedDB request的成功回调函数中，把request获取的DB对象赋值给了myDB的db属性，这样就可以使用myDB.db来访问创建的indexedDB了
                // myDB.db=e.target.result;
                resolve(e.target.result);
            };
        })  
    };
    closeDB(db){//关闭数据库
        db.close();
    }
    deleteDB(name){//删除数据库使用indexedDB对象的deleteDatabase方法
        indexedDB.deleteDatabase(name);
    }
    addData(db,data){//添加数据
        var transaction=db.transaction(this.store,'readwrite'); 
        var store=transaction.objectStore(this.store); 
        store.add(data);
    }
    deleteDataByKey(db,storeName,value){//删除数据
        var transaction=db.transaction(storeName,'readwrite'); 
        var store=transaction.objectStore(storeName); 
        store.delete(value); 
    }
    clearObjectStore(db,storeName){//清空数据
        var transaction=db.transaction(storeName,'readwrite'); 
        var store=transaction.objectStore(storeName); 
        store.clear();
    }
    getDataByKey(db,storeName,value){//查询数据
        var transaction=db.transaction(storeName,'readwrite'); 
        var store=transaction.objectStore(storeName); 
        var request=store.get(value); 
        request.onsuccess=function(e){ 
            var student=e.target.result; 
            console.log(student.name); 
        };
    }
    updateDataByKey(db,storeName,value){//更新数据
        // let request=window.indexedDB.open(this.dbName,this.version);
        var transaction=db.transaction(storeName,'readwrite'); 
        var store=transaction.objectStore(storeName);
        var newValue = value;
        var request = store.get(value.id);
          request.onsuccess=function(e){ 
            // var student=e.target.result;
            // student.id=value;
            store.put(newValue); 
        };
    }
  }
  export default UtilDB;