-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (98 loc) · 2.76 KB
/
index.js
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
(function(){
'use strict';
angular
.module('angular-indexedDB', [])
.service('IndexedDB',[
'$q',
Service
])
function Service($q){
var self = this;
var db;
// Default messages
var messages = {
'initSuccessMessage': {
status: 200,
message: 'Successfully intialized indexedDB'
},
'initErrorMessage': {
status: 422,
message: 'Your browser doesn\'t support indexDB'
},
'addSucess':{
status: 200,
message: 'Successfully added'
},
'addError':{
status: 422,
message: 'Already added in database'
},
'removeSuccess':{
status: 200,
message: 'Successfully removed'
},
'removeError': {
status: 422,
message: 'The data with this key doesn\'t exist'
},
'updateSuccess':{
status: 200,
message: 'Successfully updated'
},
}
// Initalize the indexedDB
self.init = init;
// add data
self.add = add;
// remove data
self.remove = remove;
// update data
self.update = update;
function init(dbName, Scheme, key){
return $q(function(resolve, reject){
if (!window.indexedDB){
return reject(messages.initErrorMessage);
}
var request = window.indexedDB.open(dbName, 3);
request.onerror = function(event){
return reject(event.target)
}
request.onsuccess = function(event) {
db = request.result;
};
request.onupgradeneeded = function(event){
db = event.target.result;
var objectStore = db.createObjectStore(Scheme, {keyPath: key});
}
})
}
function add(Scheme, data){
return $q(function(resolve, reject){
var request = db.transaction([Scheme], "readwrite")
.objectStore(Scheme)
.add(data);
request.onsuccess = function(event) {
resolve(messages.addSucess)
};
request.onerror = function(event) {
reject(messages.addError);
}
})
}
function remove(scheme, key) {
var $q(function(resolve, reject){
var request = db.transaction([scheme], "readwrite")
.objectStore(scheme)
.delete(key);
request.onsuccess = function(event) {
resolve(messages.removeSuccess);
};
request.onerror = function(event) {
reject(messages.removeError);
}
})
}
function update(){
}
}
})();