storage.js 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
let db = null
const defaultTableName = 'history'
export default {
	init(tableName = defaultTableName) {
		let request = window.indexedDB.open('ai-draw', '1')
		return new Promise((resolve, reject) => {

			// 数据库操作过程中出错,则错误回调被触发
			request.onerror = (event) => {
				console.log("db init报错" + event)
				reject()
			}
			
			request.onsuccess = (event) => {
				db = event.target.result
				console.log("db init成功")
				resolve()
			}
			
			request.onupgradeneeded = (event) => {
                
			   db = event.target.result
			   let objectStore = db.createObjectStore(tableName, { keyPath: 'id' })
			   objectStore.createIndex('key', 'key', { unique: true })
			   console.log("db onupgradeneeded 成功")
			   resolve()
			}
		})
	},
	isConnected(tableName = defaultTableName){
		return db != null
	},
	set(tableName = defaultTableName, key, val, id = 100){
		let request = db.transaction(tableName, 'readwrite')
		          .objectStore(tableName)
		          .add({
					  id: id,
					  key: key,
					  data: val
				  })
		return new Promise((resolve, reject) => {
			request.onsuccess = (event) => {
				console.info('添加成功', event)
				resolve(event)
			}
			request.onerror = (event) => {
				console.info('添加失败')
				reject(event)
			}
		})
	},
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	put(tableName = defaultTableName, key, val, id = 100){
		let request = db.transaction(tableName, 'readwrite')
		          .objectStore(tableName)
		          .put({
					  id: id,
					  key: key,
					  data: val
				  })
		return new Promise((resolve, reject) => {
			request.onsuccess = (event) => {
				console.info('添加成功', event)
				resolve(event)
			}
			request.onerror = (event) => {
				console.info('添加失败')
				reject(event)
			}
		})
	},
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	getAll(tableName = defaultTableName){
		let request = db.transaction(tableName, 'readwrite')
		          .objectStore(tableName)
		          .index('key')
				  .getAll()
				  
		console.info("开水获取全部")
		
		return new Promise((resolve, reject) => {
			request.onsuccess = (event) => {
				console.info('获取成功')
				resolve(request.result)
			}
			request.onerror = (event) => {
				console.info('获取失败')
				reject(event)
			}
		})
	},
	get(tableName = defaultTableName, key){
		let request = db.transaction(tableName, 'readwrite')
		          .objectStore(tableName)
		          .index('key')
				  .get(key)
				  
		console.info("获取")
		
		return new Promise((resolve, reject) => {
			request.onsuccess = (event) => {
				console.info('获取成功')
				resolve(request.result)
			}
			request.onerror = (event) => {
				console.info('获取失败')
				reject(event)
			}
		})
	},
	del(tableName = defaultTableName, id = 100){
		
		let request = db.transaction(tableName, 'readwrite')
		          .objectStore(tableName)
				  .delete(id)
		
				  
		console.info("开始删除")
		
		return new Promise((resolve, reject) => {
			request.onsuccess = (event) => {
				console.info('删除成功')
				resolve(request.result)
			}
			request.onerror = (event) => {
				console.info('删除失败')
				reject(event)
			}
		})
	}
		
}