| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- // const indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
- const indexedDB = window.indexedDB;
- class IndexedDB {
- private _db: IDBDatabase | null; // 数据库
- private _transaction: IDBTransaction | null; // 事务
- private _request: IDBOpenDBRequest | null;
- private _dbName: string; // 数据库名
- private _cacheTableName: string; // 表名
- private _dbversion: number; // 数据库版本
- constructor() {
- this._db = null;
- this._transaction = null;
- this._request = null;
- this._dbName = "userWritingDB";
- this._cacheTableName = "userWritingTable";
- this._dbversion = 1;
- }
- async initDB(val: string): Promise<Event> {
- return new Promise<Event>((resolve, reject) => {
- this._request = indexedDB.open(this._dbName, this._dbversion);
- // 数据库初始化成功
- this._request!.onsuccess = (event) => {
- this._db = this._request!.result;
- resolve(event);
- };
- // 数据库初始化失败
- this._request!.onerror = (event) => {
- reject(event);
- };
- // 数据库初次创建或更新时会触发
- this._request!.onupgradeneeded = (event) => {
- const db = this._request!.result;
- if (!db.objectStoreNames.contains(this._cacheTableName)) {
- db.createObjectStore(this._cacheTableName, {
- keyPath: val,
- });
- }
- resolve(event);
- };
- });
- }
- closeDB() {
- if (this._db) {
- this._db.close();
- console.log(`关闭数据库`);
- }
- }
- async addData(params: any): Promise<Event> {
- return new Promise<Event>((resolve, reject) => {
- const transaction = this._db!.transaction(this._cacheTableName, "readwrite");
- const store = transaction.objectStore(this._cacheTableName);
- const response = store.add(params);
- // 操作成功
- response.onsuccess = (event) => {
- console.log("操作成功");
- resolve(event);
- };
- // 操作失败
- response.onerror = (event) => {
- console.log("操作失败");
- reject(event);
- };
- });
- }
- async getDataByKey(key: any): Promise<any> {
- return new Promise<any>((resolve, reject) => {
- const transaction = this._db!.transaction(this._cacheTableName);
- const objectStore = transaction.objectStore(this._cacheTableName);
- // 通过主键读取数据
- const request = objectStore.get(key);
- // 操作成功
- request.onsuccess = () => {
- resolve(request.result);
- };
- // 操作失败
- request.onerror = (event) => {
- reject(event);
- };
- });
- }
- async clearDB(): Promise<Event> {
- return new Promise<Event>((resolve, reject) => {
- const transaction = this._db && this._db.transaction(this._cacheTableName, "readwrite");
- const store = transaction && transaction.objectStore(this._cacheTableName);
- const response = store && store.clear();
- // 操作成功
- response.onsuccess = (event) => {
- console.log("清空数据库数据");
- resolve(event);
- };
- // 操作失败
- response.onerror = (event) => {
- reject(event);
- };
- });
- }
- async deleteDBAll(): Promise<void> {
- const deleteRequest = indexedDB.deleteDatabase(this._dbName);
- return new Promise<void>((resolve, reject) => {
- deleteRequest.onerror = function (event) {
- console.log("删除失败");
- };
- deleteRequest.onsuccess = function (event) {
- console.log("删除数据库成功");
- };
- });
- }
- }
- export default IndexedDB;
|