storage.js 3.0 KB
Newer Older
1 2 3 4
let db = null
const defaultTableName = 'history'
export default {
	init(tableName = defaultTableName) {
5
		let request = window.indexedDB.open('ai-draw', 1)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
		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' })
24
			//    objectStore.createIndex('key', 'key', { unique: true })
25 26 27 28 29 30 31 32
			   console.log("db onupgradeneeded 成功")
			   resolve()
			}
		})
	},
	isConnected(tableName = defaultTableName){
		return db != null
	},
33
	set(tableName = defaultTableName, val, id = 100){
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
		let request = db.transaction(tableName, 'readwrite')
		          .objectStore(tableName)
		          .add({
					  id: id,
					  data: val
				  })
		return new Promise((resolve, reject) => {
			request.onsuccess = (event) => {
				console.info('添加成功', event)
				resolve(event)
			}
			request.onerror = (event) => {
				console.info('添加失败')
				reject(event)
			}
		})
	},
51
	put(tableName = defaultTableName, val, id = 100){
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		let request = db.transaction(tableName, 'readwrite')
		          .objectStore(tableName)
		          .put({
					  id: id,
					  data: val
				  })
		return new Promise((resolve, reject) => {
			request.onsuccess = (event) => {
				console.info('添加成功', event)
				resolve(event)
			}
			request.onerror = (event) => {
				console.info('添加失败')
				reject(event)
			}
		})
	},
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	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)
			}
		})
	},
88
	get(tableName = defaultTableName, id = 100){
89
		let request = db.transaction(tableName, 'readwrite')
90
		          .objectStore(tableName).get(id)
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
				  
		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)
			}
		})
	}
		
}