1.angularJs中 用于 操作indexedDB数据库
import { Injectable } from '@angular/core';
import { HttpService } from '../../shared/service/http.service';
@Injectable({
providedIn: 'root',
})
export class IndexedDBService {
private isdbSupported;
private db;
private defaultStore = "chat";
private userInfo = "user";
private unreadchat = "unreadchat";
private unreadNum = "unreadNum";
// private readyToSend = "readyToSend";
private dbName = "xw_im";
public request;
constructor(private http: HttpService) {
this.onPageLoad();
}
onPageLoad() {
try {
const indexedDB = window.indexedDB;
if (indexedDB) {
this.isdbSupported = indexedDB;
}
const openReq = indexedDB.open(this.dbName, 1);
openReq.onupgradeneeded = (e) => {
// tslint:disable-next-line:no-string-literal
const thisDb = e.target['result'];
if (!thisDb.objectStoreNames.contains(this.defaultStore)) {
thisDb.createObjectStore(this.defaultStore);
}
if (!thisDb.objectStoreNames.contains(this.userInfo)) {
thisDb.createObjectStore(this.userInfo);
}
if (!thisDb.objectStoreNames.contains(this.unreadchat)) {
thisDb.createObjectStore(this.unreadchat);
}
if (!thisDb.objectStoreNames.contains(this.unreadNum)) {
thisDb.createObjectStore(this.unreadNum);
}
// if (!thisDb.objectStoreNames.contains(this.readyToSend)) {
// thisDb.createObjectStore(this.readyToSend);
// }
};
openReq.onsuccess = (e) => {
console.log(" msg -----> indexedDBService.onPageLoad Success");
// tslint:disable-next-line:no-string-literal
this.db = e.target['result'];
// this.savePerson();
// this.getByKey(1);
// this.deleteByKey(1);
};
openReq.onerror = (e) => {
console.log("Error");
console.dir(e);
};
} catch (error) {
console.log('msg-----> error:' + JSON.stringify(error));
}
}
// tslint:disable-next-line:ban-types
getByKey(key, storeName = this.defaultStore, callbackFunc: Function) {
try {
const transaction = this.db.transaction([storeName], "readonly");
const store = transaction.objectStore(storeName);
const obj = store.get(key);
callbackFunc(obj);
} catch (error) {
console.log('msg-----> error:' + JSON.stringify(error));
}
}
// tslint:disable-next-line:ban-types
getAllValues(storeName = this.defaultStore, callbackFunc: Function) {
try {
const transaction = this.db.transaction([storeName], "readonly");
const store = transaction.objectStore(storeName);
const obj = store.openCursor();
callbackFunc(obj);
} catch (error) {
console.log('msg-----> error:' + JSON.stringify(error));
}
}
// tslint:disable-next-line:ban-types
addCallbak(content, key, storeName = this.defaultStore, callbackFunc: Function) {
try {
const transaction = this.db.transaction([storeName], "readwrite");
const store = transaction.objectStore(storeName);
const req = store.add(content, key);
req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
req.onsuccess = (e) => { callbackFunc(); console.log("save complete callbak 添加成功 " + storeName); };
} catch (error) {
console.log('msg-----> error:' + JSON.stringify(error));
}
}
// tslint:disable-next-line:ban-types
putByKeyCallbak(content, key, storeName = this.defaultStore, callbackFunc: Function) {
try {
const transaction = this.db.transaction([storeName], "readwrite");
const store = transaction.objectStore(storeName);
const req = store.put(content, key);
req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
req.onsuccess = (e) => { callbackFunc(); console.log("update complete callbak"); };
} catch (error) {
console.log('msg-----> error:' + JSON.stringify(error));
}
}
// tslint:disable-next-line:ban-types
deleteByKeyCallbak(key, storeName = this.defaultStore, callbackFunc: Function) {
try {
const transaction = this.db.transaction([storeName], "readwrite");
const store = transaction.objectStore(storeName);
const req = store.delete(key);
req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
req.onsuccess = (e) => { callbackFunc(); console.log("delete complete"); };
} catch (error) {
console.log('msg-----> error:' + JSON.stringify(error));
}
}
// tslint:disable-next-line:ban-types
add(content, key, storeName = this.defaultStore) {
try {
const transaction = this.db.transaction([storeName], "readwrite");
const store = transaction.objectStore(storeName);
const req = store.add(content, key);
req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
req.onsuccess = (e) => { console.log("save complete " + storeName + " " + content); };
} catch (error) {
console.log('msg-----> error:' + JSON.stringify(error));
}
}
// tslint:disable-next-line:ban-types
putByKey(content, key, storeName = this.defaultStore) {
try {
const transaction = this.db.transaction([storeName], "readwrite");
const store = transaction.objectStore(storeName);
const req = store.put(content, key);
req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
req.onsuccess = (e) => { console.log("update complete"); };
} catch (error) {
console.log('msg-----> error:' + JSON.stringify(error));
}
}
deleteByKey(key, storeName = this.defaultStore) {
try {
const transaction = this.db.transaction([storeName], "readwrite");
const store = transaction.objectStore(storeName);
const req = store.delete(key);
req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
req.onsuccess = (e) => console.log("delete complete");
} catch (error) {
console.log('msg-----> error:' + JSON.stringify(error));
}
}
}