db.js
5.31 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const DB_STORE_NAME = 'luckydraw.GuaranteeTransaction';
let objectStore;
const createObjectStore = db => {
objectStore = db.createObjectStore(DB_STORE_NAME, {
keyPath: 'id',
autoIncrement: true
});
objectStore.createIndex('id', 'id', {
unique: true
});
objectStore.createIndex('name', 'name');
};
const createObjectStoreOrder = db => {
if (!db.objectStoreNames.contains(DB_STORE_NAME)) {
createObjectStore(db);
} else {
db.deleteObjectStore(DB_STORE_NAME);
createObjectStore(db);
}
};
const DBVERSION = 1;
const DBNAME = 'luckydraw';
// 数据库:IDBDatabase 对象
// 对象仓库:IDBObjectStore 对象
// 索引: IDBIndex 对象
// 事务: IDBTransaction 对象
// 操作请求:IDBRequest 对象
// 指针: IDBCursor 对象
// 主键集合:IDBKeyRange 对象
let db;
class LuckydrawIndecDB {
constructor() {
this.InitIndexedDB();
}
add = (TableName, newItem) => {
const addInfo = {
createdTime: Date.now(),
updateTime: Date.now()
};
return new Promise((resolve, reject) => {
const transaction = db.transaction([TableName], 'readwrite');
const objectStore = transaction.objectStore(TableName);
const objectStoreRequest = objectStore.add(
Object.assign({}, addInfo, newItem)
);
objectStoreRequest.onsuccess = () => {
resolve(true);
};
objectStoreRequest.onerror = error => {
reject(error.target.error);
};
});
};
edit = (TableName, id, data) => {
const editInfo = {
updateTime: Date.now()
};
return new Promise((resolve, reject) => {
const transaction = db.transaction([TableName], 'readwrite');
const objectStore = transaction.objectStore(TableName);
const objectStoreRequest = objectStore.get(id);
objectStoreRequest.onsuccess = () => {
const myRecord = objectStoreRequest.result;
for (const key in data) {
if (typeof myRecord[key] !== 'undefined') {
myRecord[key] = data[key];
}
}
const objectStoreRequestGetRes = objectStore.put(
Object.assign({}, myRecord, editInfo)
);
objectStoreRequestGetRes.onsuccess = () => {
resolve(true);
};
objectStoreRequestGetRes.onerror = error => {
reject(error);
};
};
});
};
del = (TableName, id) => {
return new Promise((resolve, reject) => {
const objectStore = db
.transaction([TableName], 'readwrite')
.objectStore(TableName);
const objectStoreRequest = objectStore.delete(id);
objectStoreRequest.onsuccess = () => {
resolve(true);
};
objectStoreRequest.onerror = error => {
reject(error);
};
});
};
clear = TableName => {
return new Promise((resolve, reject) => {
const objectStore = db
.transaction([TableName], 'readwrite')
.objectStore(TableName);
const objectStoreRequest = objectStore.clear();
objectStoreRequest.onsuccess = () => {
resolve(true);
};
objectStoreRequest.onerror = error => {
reject(error);
};
});
};
count = TableName => {
return new Promise((resolve, reject) => {
const objectStore = db
.transaction([TableName], 'readwrite')
.objectStore(TableName);
const objectStoreRequest = objectStore.count();
objectStoreRequest.onsuccess = () => {
resolve(objectStoreRequest.result);
};
objectStoreRequest.onerror = error => {
reject(error);
};
});
};
get = (TableName, id) => {
return new Promise((resolve, reject) => {
const objectStore = db.transaction(TableName).objectStore(TableName);
const objectStoreRequest = objectStore.get(id);
objectStoreRequest.onsuccess = () => {
resolve(objectStoreRequest.result);
};
objectStoreRequest.onerror = error => {
reject(error);
};
});
};
getKey = (TableName, key) => {
return new Promise((resolve, reject) => {
const objectStore = db.transaction(TableName).objectStore(TableName);
const objectStoreRequest = objectStore.getKey(key);
objectStoreRequest.onsuccess = () => {
resolve(objectStoreRequest.result);
};
objectStoreRequest.onerror = error => {
reject(error);
};
});
};
getAll = TableName => {
return new Promise((resolve, reject) => {
const objectStore = db.transaction(TableName).objectStore(TableName);
const objectStoreRequest = objectStore.getAll();
objectStoreRequest.onsuccess = () => {
resolve(objectStoreRequest.result);
};
objectStoreRequest.onerror = error => {
reject(error);
};
});
};
onerror = event => {
console.log('db-connection-fail', event);
};
InitIndexedDB = () => {
const DBOpenRequest = window.indexedDB.open(DBNAME, DBVERSION);
// 数据库打开失败
DBOpenRequest.onerror = event => {
this.onerror(event);
};
DBOpenRequest.onsuccess = () => {
// 存储数据结果
db = DBOpenRequest.result;
console.log('db-connection-success');
};
DBOpenRequest.onupgradeneeded = () => {
db = event.target.result;
createObjectStoreOrder(db);
};
};
}
const database = new LuckydrawIndecDB();
export { LuckydrawIndecDB, database, DB_STORE_NAME };