-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChromeStorageDB.js
271 lines (260 loc) · 11.4 KB
/
ChromeStorageDB.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
Hello, I am Hasan Mahmud Rimon
I developed this class to interact chrome storage API as Model Based Database
You can create a Data Model and can apply GET(), POST(), PUT(), DELETE() methods on them.
demo model and application:
let User = new ChromeStorageDB({
name:'users',
attributes:{
id:{
autoIncrement: true,
primaryKey: true,
},
username:{
type: 'string',
primaryKey: true,
},
followed:{
type: 'boolean',
}
}
});
User.GET({});
User.POST({
username:'hasan__mahmud',
followed:true
});
User.PUT({
set: {followed:false},
where:{username:[{operation:'equal',value:'hasan__mahmud'}]}
});
User.DELETE({
username:[{operation:'equal',value:'hasan__mahmud}]
});
*/
class StorageDB{
constructor({name,attributes}){
let storageSetup = this.#constructionCheck({name,attributes});
/*db name (a string)*/
this.name = storageSetup.name;
/*
attributes has 5 parameter
type: ('string','number','boolean') -defalut 'number'
autoIncrement: (true,false) - default false
primaryKey: (true,false) - default false
allowNull: (true,false) - default false
default: ('string','number','boolean',null) - default null
*/
this.attributes = storageSetup.attributes;
}
#constructionCheck({name,attributes}){
if(!name) throw new Error ('storage must have a name value(string)');
if(!attributes) throw new Error ('storage must have some attributes(object)');
let attributeKeys = Object.keys(attributes);
if(attributeKeys.length == 0) throw new Error ('attributes must have 1 or more keys');
/* if(attributeKeys.length != [...new Set(attributeKeys)].length) throw new Error ('each attribute must be unique')*/
for(let i=0;i<attributeKeys.length;i++){
let attribute = attributes[attributeKeys[i]];
if(typeof attribute.type == 'undefined') attribute.type = 'number';
if(typeof attribute.autoIncrement == 'undefined') attribute.autoIncrement = false;
if(typeof attribute.primaryKey == 'undefined') attribute.primaryKey = false;
if(typeof attribute.allowNull == 'undefined') attribute.allowNull = false;
if(typeof attribute.default == 'undefined') attribute.default = null;
if(!(attribute.type=='number' || attribute.type=='boolean' || attribute.type=='string' /*|| attribute.type=='object' || attribute.type=='array'*/)) throw new Error ('unknown attribute type')
if(!(attribute.autoIncrement === true || attribute.autoIncrement === false)) throw new Error ('unknown value for autoIncrement');
if(!(attribute.primaryKey === true || attribute.primaryKey === false)) throw new Error ('unknown value for primaryKey');
if(!(attribute.allowNull === true || attribute.allowNull === false)) throw new Error ('unknown value for allowNull');
if(attribute.type!='number' && attribute.autoIncrement) throw new Error('autoIncrement is only for number data type');
if(attribute.primaryKey && attribute.default!=null) throw new Error('primaryKey with default value is not possible');
if(attribute.default!=null && typeof attribute.default != attribute.type) throw new Error('default value should be equal to key type')
}
return {name,attributes};
}
async #build(){
let thisDB = await this.#getDB();
if(typeof thisDB!='object' || !Array.isArray(thisDB)) {
await this.#setDB([]);
thisDB = await this.#getDB();
}
}
async #getDB() {
return new Promise((resolve, reject) => {
chrome.storage.local.get(this.name, (result) => { resolve(result[this.name]); });
});
}
async #setDB(db) {
return new Promise((resolve, reject) => {
let obj = {};
obj[this.name]=db;
chrome.storage.local.set(obj, function() {resolve(true)});
});
}
#POSTCheck = (row) => {
if(typeof row !='object' && Array.isArray(row)) throw new Error ('New row is not enough to get entried');
if(!row) throw new Error('Row is just empty');
let rowKeys = Object.keys(row);
if(!rowKeys.length) throw new Error('Row is empty');
let attributes = this.attributes;
let attributesKeys = Object.keys(attributes);
for(let i=0;i<attributesKeys.length;i++){
let attribute = attributes[attributesKeys[i]];
if(!attribute.autoIncrement){
if(typeof(row[attributesKeys[i]])!=attribute.type || typeof(row[attributesKeys[i]])=='undefined'){
if(typeof(row[attributesKeys[i]])!='undefined') throw new Error (`wrong data type for '${attributesKeys[i]}' key`);
if(attribute.primaryKey) throw new Error (`Primary key '${attributesKeys[i]}' must be a valid data`);
if(attribute.default==null && !attribute.allowNull) throw new Error (`No valid data provided for '${attributesKeys[i]}' key`);
row[attributesKeys[i]] = attribute.default;
}
}
}
return row;
}
async POST(row){
await this.#build();
let db = await this.#getDB();
row = this.#POSTCheck(row);
let attributes = this.attributes;
let attributesKeys = Object.keys(attributes);
for(let i=0;i<attributesKeys.length;i++){
let attribute = attributes[attributesKeys[i]];
if(attribute.autoIncrement){
row[attributesKeys[i]] = 1;
if(db.length!=0){
row[attributesKeys[i]] = (db[db.length-1].id)+1;
}
}
if(attribute.primaryKey){
if(db.find( data => {return data[attributesKeys[i]] === row[attributesKeys[i]]} ) != undefined)
throw new Error (`primaryKey '${attributesKeys[i]}' value is not unique`);
// console.log(`primaryKey '${attributesKeys[i]}' value is not unique`);
}
}
db.push(row);
await this.#setDB(db);
return db;
}
async GET(where){
await this.#build();
where = this.#WHERECheck(where);
const db = await this.#getDB();
if(where==null) return db;
return this.#whereReturn({db,where,logic:true})
}
async DELETE(where){
await this.#build();
where = this.#WHERECheck(where);
if(where==null) return 0;
const db = await this.#getDB();
const results = this.#whereReturn({db,where,logic:false});
await this.#setDB(results);
return results;
}
#WHERECheck(where){
if(where===undefined) return null;
if(where!=null)
if(typeof where!='object' || Array.isArray(where))
throw new Error('where key got unexpected value');
let whereKeys = Object.keys(where);
if(whereKeys.length==0) return null;
let attributes = this.attributes;
let attributeKeys = Object.keys(attributes);
for(let i=0;i<whereKeys.length;i++){
let whereKeyValues = where[whereKeys[i]];
if(!attributeKeys.includes(whereKeys[i]))
throw new Error('where key got keys that are not availabale on attribute keys.');
if(!whereKeyValues)
throw new Error('you must be valid object as where key value');
if(!Array.isArray(whereKeyValues))
throw new Error('where key value got unexpected value');
if(whereKeyValues.length==0)
throw new Error('where key value atleast need one item');
whereKeyValues.forEach(whereKeyValue => {
let whereKeyValueKeys = Object.keys(whereKeyValue);
if(whereKeyValueKeys.length!=2)
throw new Error('where key value keys must have 2 key');
if(typeof whereKeyValue.operation != 'string')
throw new Error('opeartion value must be string');
if(typeof whereKeyValue.value != attributes[whereKeys[i]].type)
throw new Error('value type must match');
});
}
return where;
}
#SETCheck(set){
if(set===undefined || set===null) return null;
if(set!=null)
if(typeof set!='object' || Array.isArray(set))
throw new Error('set key got invalid value');
const setKeys = Object.keys(set);
if(setKeys.length==0) throw new Error('must have 1 or more keys in set');
const attributes = this.attributes;
const attributeKeys = Object.keys(attributes);
for(let i=0;i<setKeys.length;i++){
const setKey = setKeys[i];
if(!attributeKeys.includes(setKeys) && typeof set[setKey] != attributes[setKey].type)
throw new Error('set keys value is not defined as key value');
if(attributes[setKey].autoIncrement) throw new Error('autoIncrement should not be changed');
if(attributes[setKey].primaryKey) throw new Error('primaryKey should not be changed');
}
return set;
}
#whereReturn({db,where,logic,hook}){
if(typeof logic !='boolean') throw new Error('there must be a boolean logic value');
if(hook == undefined) hook = null;
const dbLength = db.length;
let result = [];
for(let j=0;j<dbLength;j++){
let row = db[j];
const whereKeys = Object.keys(where);
let interKey = true;
for(let i=0; i<whereKeys.length;i++){
const whereKey = whereKeys[i];
const whereKeyValues = where[whereKey];
let intraKey = false;
for(let k=0;k<whereKeyValues.length;k++){
const operation = whereKeyValues[k].operation;
const value = whereKeyValues[k].value;
switch(operation){
case 'equal':
intraKey = intraKey || row[whereKey]===value;
break;
case 'notEqual':
intraKey = intraKey || row[whereKey]!==value;
break;
default:
throw new Error('invalid operation')
break;
}
}
interKey = interKey && intraKey;
}
if((interKey && logic) || (!interKey && !logic)){
if(hook===null){
result.push(row);
}else{
row = hook(row);
}
}
}
if(hook===null) return result; else return db;
}
async PUT({set,where}){
await this.#build();
where = this.#WHERECheck(where);
set = this.#SETCheck(set);
if(where==null) return 0;
let db = await this.#getDB();
const hook = (row)=>{
const setKeys = Object.keys(set);
const setValues = Object.values(set);
for(let i=0;i<setKeys.length;i++){
row[setKeys[i]] = setValues[i];
}
return row;
}
db = this.#whereReturn({db,where,logic:true,hook});
await this.#setDB(db);
return db;
}
}
export default StorageDB;