let databaseName = "localIndexDB";
let objectT = "OBJECT";
let StringT = "STRING";
let JSONT = "JSON";
let testTag = indexedDB || webkitIndexedDB || mozIndexedDB || null;
let request;
if(testTag){
request = window.indexedDB.open(databaseName);
}
/**
* db
*/
let db;
/**
* ======================functions===================
*/
/**
*
* @param db
*/
function initTable(db) {
debugger;
let objectStore;
if (!db.objectStoreNames.contains(objectT)) {
objectStore = db.createObjectStore(
objectT,
{ autoIncrement: true }
);
//objectStore.createIndex('value', 'value', { unique: false });
}
let stringStore;
if (!db.objectStoreNames.contains(StringT)) {
stringStore = db.createObjectStore(
StringT,
{ autoIncrement: true }
);
stringStore.createIndex('value', 'value', { unique: false });//字符串打开索引
}
let jsonStore;
if (!db.objectStoreNames.contains(JSONT)) {
jsonStore = db.createObjectStore(
JSONT,
{ autoIncrement: true }
);
//JSONT.createIndex('value', 'value', { unique: false });
}
}
/**
* test
*/
function test() {
}
/**
* 處理方法
* @constructor
*/
/**
* 判断是否为空
*/
function validatenull(val) {
if (typeof val == 'boolean') {
return false;
}
if (typeof val == 'number') {
return false;
}
if (val instanceof Array) {
if (val.length == 0) return true;
} else if (val instanceof Object) {
if (JSON.stringify(val) === '{}') return true;
} else {
if (val == 'null' || val == null || val == 'undefined' || val == undefined || val == '') {
return true;
}
return false;
}
return false;
}
/**
*
* @returns {string}
*/
function generateUUID() {
let d = new Date().getTime();
if (window.performance && typeof window.performance.now === "function") {
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
//Object
/**
*
* @param key
* @param value
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function ObjectAdd(key,value,callbackSuccess,callbackError){
let id ;
if (!validatenull(key)){
id = key
}else{
id = generateUUID();
}
const request = db.transaction([objectT], 'readwrite')
.objectStore(objectT)
.add({ id: id, value: value});
request.onsuccess = function (event) {
if (callbackSuccess instanceof Function){
callbackSuccess(id,value);
}
};
request.onerror = function (event) {
if (callbackError instanceof Function){
callbackError(id);
}
console.log("新增失敗!");
}
}
/**
*
* @param key
* @param value
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function ObjectUpdate(key,value,callbackSuccess,callbackError){
const request = db.transaction([objectT], 'readwrite')
.objectStore(objectT)
.put({id: key, value:value});
request.onsuccess = function (event) {
if (callbackSuccess instanceof Function){
callbackSuccess(key,value);
}
};
request.onerror = function (event) {
if (callbackError instanceof Function){
callbackError(id);
}
console.log('数据更新失败');
}
}
/**
*
* @param key
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function ObjectDelete(key,callbackSuccess,callbackError){
const request = db.transaction([objectT], 'readwrite')
.objectStore(objectT)
.delete(key);
request.onsuccess = function (event) {
if (callbackSuccess instanceof Function){
callbackSuccess();
}
};
request.onerror = function (event) {
console.log('数据删除失败');
if (callbackError instanceof Function){
callbackError();
}
}
}
/**
*
* @param key
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function ObjectRead(key,callbackSuccess,callbackError){
const transaction = db.transaction([objectT]);
const objectStore = transaction.objectStore(objectT);
const request = objectStore.get(key);
request.onerror = function(event) {
console.log('事务失败');
if (callbackError instanceof Function){
callbackError();
}
};
request.onsuccess = function( event) {
if (callbackSuccess instanceof Function){
callbackSuccess(request.result.value);
}
};
}
/**
*
* @param callbackSuccess
* @param callbackError
*/
export function getAllObject(callbackSuccess,callbackError){
const objectStore = db.transaction(objectT).objectStore(objectT);
objectStore.openCursor().onsuccess = function (event) {
const cursor = event.target.result;
let arr = [];
if (cursor) {
const obj = {key:cursor.key,value:cursor.value.value};
arr.push(obj);
cursor.continue();
} else {
console.log('没有更多数据了!');
}
if (callbackSuccess instanceof Function){
callbackSuccess(arr);
}
};
objectStore.openCursor().onerror = function (event) {
if (callbackError instanceof Function){
callbackError();
}
console.log("读取数据失败!");
}
}
//String
/**
*
* @param key
* @param value
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function StringAdd(key,value,callbackSuccess,callbackError){
let id ;
if (!validatenull(key)){
id = key
}else{
id = generateUUID();
}
const request = db.transaction([StringT], 'readwrite')
.objectStore(StringT)
.add({ id: id, value: value});
request.onsuccess = function (event) {
if (callbackSuccess instanceof Function){
callbackSuccess(id,value);
}
};
request.onerror = function (event) {
if (callbackError instanceof Function){
callbackError(id);
}
console.log("新增失敗!");
}
}
/**
*
* @param key
* @param value
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function StringUpdate(key,value,callbackSuccess,callbackError){
const request = db.transaction([StringT], 'readwrite')
.objectStore(StringT)
.put({id: key, value:value});
request.onsuccess = function (event) {
if (callbackSuccess instanceof Function){
callbackSuccess(key,value);
}
};
request.onerror = function (event) {
if (callbackError instanceof Function){
callbackError(id);
}
console.log('数据更新失败');
}
}
/**
*
* @param key
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function StringDelete(key,callbackSuccess,callbackError){
const request = db.transaction([StringT], 'readwrite')
.objectStore(StringT)
.delete(key);
request.onsuccess = function (event) {
if (callbackSuccess instanceof Function){
callbackSuccess();
}
};
request.onerror = function (event) {
console.log('数据删除失败');
if (callbackError instanceof Function){
callbackError();
}
}
}
/**
*
* @param key
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function StringRead(key,callbackSuccess,callbackError){
const transaction = db.transaction([StringT]);
const objectStore = transaction.objectStore(StringT);
const request = objectStore.get(key);
request.onerror = function(event) {
console.log('事务失败');
if (callbackError instanceof Function){
callbackError();
}
};
request.onsuccess = function( event) {
if (callbackSuccess instanceof Function){
callbackSuccess(request.result.value);
}
};
}
/**
*
* @param likeStr
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function StringFuzzySearch(likeStr,callbackSuccess,callbackError) {
const transaction = db.transaction(StringT, "readonly");
const objectStore = transaction.objectStore(StringT);
const request = objectStore.openCursor();
let most = 0;
let arr = [];
request.onsuccess = function(event) {
const cursor = event.target.result;
if (cursor) {
if (cursor.value.value.indexOf(likeStr) !== -1&&most<=10) {
const obj = {key:cursor.key,value:cursor.value.value};
arr.push(obj);
}
cursor.continue();
}
if (callbackSuccess instanceof Function){
callbackSuccess(arr);
}
};
request.onerror = function (event) {
if (callbackError instanceof Function){
callbackError();
}
}
}
/**
*
* @param callbackSuccess
* @param callbackError
*/
export function getAllString(callbackSuccess,callbackError){
const objectStore = db.transaction(StringT).objectStore(StringT);
objectStore.openCursor().onsuccess = function (event) {
const cursor = event.target.result;
let arr = [];
if (cursor) {
const obj = {key:cursor.key,value:cursor.value.value};
arr.push(obj);
cursor.continue();
} else {
console.log('没有更多数据了!');
}
if (callbackSuccess instanceof Function){
callbackSuccess(arr);
}
};
objectStore.openCursor().onerror = function (event) {
if (callbackError instanceof Function){
callbackError();
}
console.log("读取数据失败!");
}
}
//JSON
/**
*
* @param key
* @param value
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function JsonAdd(key,value,callbackSuccess,callbackError){
let id ;
if (!validatenull(key)){
id = key
}else{
id = generateUUID();
}
const request = db.transaction([JSONT], 'readwrite')
.objectStore(JSONT)
.add({ id: id, value: value});
request.onsuccess = function (event) {
if (callbackSuccess instanceof Function){
callbackSuccess(id,value);
}
};
request.onerror = function (event) {
if (callbackError instanceof Function){
callbackError(id);
}
console.log("新增失敗!");
}
}
/**
*
* @param key
* @param value
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function JsonUpdate(key,value,callbackSuccess,callbackError){
const request = db.transaction([JSONT], 'readwrite')
.objectStore(JSONT)
.put({id: key, value:value});
request.onsuccess = function (event) {
if (callbackSuccess instanceof Function){
callbackSuccess(key,value);
}
};
request.onerror = function (event) {
if (callbackError instanceof Function){
callbackError(id);
}
console.log('数据更新失败');
}
}
/**
*
* @param key
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function JsonDelete(key,callbackSuccess,callbackError){
const request = db.transaction([JSONT], 'readwrite')
.objectStore(JSONT)
.delete(key);
request.onsuccess = function (event) {
if (callbackSuccess instanceof Function){
callbackSuccess();
}
};
request.onerror = function (event) {
console.log('数据删除失败');
if (callbackError instanceof Function){
callbackError();
}
}
}
/**
*
* @param key
* @param callbackSuccess
* @param callbackError
* @constructor
*/
export function JsonRead(key,callbackSuccess,callbackError){
const transaction = db.transaction([JSONT]);
const objectStore = transaction.objectStore(JSONT);
const request = objectStore.get(key);
request.onerror = function(event) {
console.log('事务失败');
if (callbackError instanceof Function){
callbackError();
}
};
request.onsuccess = function( event) {
if (callbackSuccess instanceof Function){
callbackSuccess(request.result.value);
}
};
}
/**
*
* @param callbackSuccess
* @param callbackError
*/
export function getAllJson(callbackSuccess,callbackError){
const objectStore = db.transaction(JSONT).objectStore(JSONT);
objectStore.openCursor().onsuccess = function (event) {
const cursor = event.target.result;
let arr = [];
if (cursor) {
const obj = {key:cursor.key,value:cursor.value.value};
arr.push(obj);
cursor.continue();
} else {
console.log('没有更多数据了!');
}
if (callbackSuccess instanceof Function){
callbackSuccess(arr);
}
};
objectStore.openCursor().onerror = function (event) {
if (callbackError instanceof Function){
callbackError();
}
console.log("读取数据失败!");
}
}
/**
* ======================functions===================
*/
request.onsuccess = function (event) {
db = request.result;
initTable(db);
test(event);
console.log('数据库打开成功');
};
request.onerror = function (event) {
console.log('数据库打开报错');
};
request.onupgradeneeded = function (event) {
db = event.target.result;
initTable(db)
}