浏览器IndexedDB数据库封装

export default class IndexedDBWrapper {
  constructor(databaseName, storeName, keyPath) {
    this.databaseName = databaseName;
    this.storeName = storeName;
    this.keyPath = keyPath;
    this.db = null;
  }

  // 打开 IndexedDB 数据库
  async open() {
    const request = window.indexedDB.open(this.databaseName, 1);

    request.onerror = (event) => {
      console.error('IndexedDB error:', event.target.error);
    };

    request.onupgradeneeded = (event) => {
      const db = event.target.result;
      const objectStore = db.createObjectStore(this.storeName, { keyPath: this.keyPath });
      //console.log(`${this.storeName} object store created.`);
    };

    return new Promise((resolve, reject) => {
      request.onsuccess = (event) => {
        this.db = event.target.result;
        //console.log(`${this.databaseName} database opened.`);
        resolve();
      };

      request.onerror = (event) => {
        console.error(`${this.databaseName} database error:`, event.target.error);
        reject(event.target.error);
      };
    });
  }

  // 关闭 IndexedDB 数据库
  close() {
    this.db.close();
    //console.log(`${this.databaseName} database closed.`);
  }

  // 写入数据
  async put(data) {
    const transaction = this.db.transaction(this.storeName, 'readwrite');
    const objectStore = transaction.objectStore(this.storeName);
    const request = objectStore.put(data);

    return new Promise((resolve, reject) => {
      request.onsuccess = (event) => {
        console.log(`Data written to ${this.storeName} object store.`);
        resolve();
      };

      request.onerror = (event) => {
        console.error(`Error writing data to ${this.storeName} object store:`, event.target.error);
        reject(event.target.error);
      };
    });
  }

  // 读取数据
  async get(key) {
    const transaction = this.db.transaction(this.storeName, 'readonly');
    const objectStore = transaction.objectStore(this.storeName);
    const request = objectStore.get(key);

    return new Promise((resolve, reject) => {
      request.onsuccess = (event) => {
        const data = event.target.result;
        //console.log(`Data read from ${this.storeName} object store.`);
        resolve(data);
      };

      request.onerror = (event) => {
        console.error(`Error reading data from ${this.storeName} object store:`, event.target.error);
        reject(event.target.error);
      };
    });
  }

  // 删除数据
  async delete(key) {
    const transaction = this.db.transaction(this.storeName, 'readwrite');
    const objectStore = transaction.objectStore(this.storeName);
    const request = objectStore.delete(key);

    return new Promise((resolve, reject) => {
      request.onsuccess = (event) => {
        //console.log(`Data deleted from ${this.storeName} object store.`);
        resolve();
      };

      request.onerror = (event) => {
        console.error(`Error deleting data from ${this.storeName} object store:`, event.target.error);
        reject(event.target.error);
      };
    });
  }

  // 获取所有数据
  async getAll() {
    const transaction = this.db.transaction(this.storeName, 'readonly');
    const objectStore = transaction.objectStore(this.storeName);
    const request = objectStore.getAll();

    return new Promise((resolve, reject) => {
      request.onsuccess = (event) => {
        const data = event.target.result;
        //console.log(`All data read from ${this.storeName} object store.`);
        resolve(data);
      };

      request.onerror = (event) => {
        console.error(`Error reading all data from ${this.storeName} object store:`, event.target.error);
        reject(event.target.error);
      };
    });
  }
}

你可能感兴趣的:(javascript,数据库,前端,javascript)