-
Notifications
You must be signed in to change notification settings - Fork 9
/
localdb.js
367 lines (305 loc) · 8.47 KB
/
localdb.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
localDB © 2012 Michael Donaldson
Simple localStorage Database
Version: 0.2.1
License: MIT (http://opensource.org/licenses/MIT)
*/
function localdb(db)
{
//Check if database already exists, if it doesn't create it
if(localStorage[db] === undefined){
localStorage[db] = JSON.stringify({});
}
//Load database
var database = JSON.parse(localStorage[db]);
//Define a save function that will save changes made to the database
var save = function(database){
localStorage[db] = JSON.stringify(database);
return true;
};
var localdb = {
/*
Delete Database
*/
deleteDatabase: function(dbname){
if(localStorage[dbname] !== undefined){
delete localStorage[dbname];
return true;
} else {
console.error('A database with the name "'+dbname+'" could not be found');
return false;
}
},
/*
Create Table
*/
createTable: function(table){
if(database[table] === undefined){
database[table] = {
'totalrows': 0,
'autoinc': 1,
'rows': {}
};
save(database);
} else {
console.error('A table with the name "'+table+'" already exists');
return false;
}
},
/*
Delete Table
*/
dropTable: function(table){
if(database[table] !== undefined){
delete database[table];
save(database);
} else {
console.error('A table with the name "'+table+'" could not be found');
return false;
}
},
/*
Get Table Meta
*/
tableMeta: function(table){
if(database[table] !== undefined){
var meta = {
totalrows: database[table].totalrows,
autoinc: database[table].autoinc
};
return meta;
} else {
console.error('A table with the name "'+table+'" could not be found');
return false;
}
},
/*
Insert Data
*/
insert: function(table, data){
if(database[table] === undefined){
localdb.createTable(table);
}
autoinc = database[table].autoinc;
data.ID = autoinc;
database[table].rows[autoinc] = data;
database[table].autoinc += 1;
database[table].totalrows += 1;
save(database);
},
/*
Update Data
*/
update: function(table, data, where){
if(database[table] !== undefined){
var rows = database[table].rows;
for(row in rows){
if(rows[row] !== null){
//Default value for stop change
var stopchange = 0;
//Runs through where criteria to see if the current row is a match
//if it isn't it changes stopchange to 1 to prevent the update
//running on this row
for(i in where){
if(database[table].rows[row][i] != where[i]){
stopchange = 1;
}
}
//If stopchange is still 0, update the row
if(stopchange == 0){
for(i in data){
database[table].rows[row][i] = data[i];
}
//Updates are finished, save changes
save(database);
}
}
}
} else {
console.error('A table with the name "'+table+'" could not be found');
return false;
}
},
/*
Update Data By ID
*/
updateById: function(table, data, id){
if(database[table] !== undefined){
for(i in data){
database[table].rows[id][i] = data[i];
}
save(database);
} else {
console.error('A table with the name "'+table+'" could not be found');
return false;
}
},
/*
Delete Data
*/
remove: function(table, where){
if(database[table] !== undefined){
var rows = database[table].rows;
for(row in rows){
if(rows[row] != null){
//Default value for stop delete
var stopdelete = 0;
//Runs through where criteria to see if the current row is a match
//if it isn't it changes stopdelete to 1 to prevent the row from
//being deleted
for(i in where){
if(database[table].rows[row][i] != where[i]){
stopdelete = 1;
}
}
//If stopdelete is still 0, delete the row
if(stopdelete == 0){
delete database[table].rows[row];
save(database);
}
}
}
} else {
console.error('A table with the name "'+table+'" could not be found');
return false;
}
},
/*
Delete Data By ID
*/
removeById: function(table, id){
if(database[table] !== undefined){
if(database[table].rows[id] !== null){
delete database[table].rows[id];
save(database);
} else {
console.error('The specified row could not be found');
return false;
}
} else {
console.error('A table with the name "'+table+'" could not be found');
return false;
}
},
/*
Find Data
*/
find: function(table, where, limit, offset, type){
if(!type){
var type = 'AND';
}
if(database[table] !== undefined){
var rows = database[table].rows;
var results = [];
//Check if there are any where parameters
var noparams = 1;
for(p in where){
noparams = 0;
break;
}
//Decide what to do based on if there are any parameters or not
if(noparams == 0)
{
for(row in rows){
//Is a match?
var ismatch = 0;
//Decide which method is used, AND or OR
if(type.toUpperCase() == 'AND'){
for(i in where){
if(database[table].rows[row][i] == where[i]){
ismatch = 1;
} else {
ismatch = 0;
break;
}
}
} else if(type.toUpperCase() == 'OR') {
for(i in where){
if(database[table].rows[row][i] == where[i]){
ismatch = 1;
break;
}
}
}
if(ismatch == 1){
results.push(database[table].rows[row]);
}
}
} else {
for(row in rows){
results.push(database[table].rows[row]);
}
}
if(!offset){
var offset = 0;
} else if(typeof(offset) != 'number'){
offset = 0;
}
if(typeof(limit) == 'number'){
if(limit > parseInt(0)){
return results.slice(offset, limit+offset);
} else {
return results.slice(offset);
}
} else {
return results;
}
} else {
console.error('A table with the name "'+table+'" could not be found');
return false;
}
},
/*
Find Data By ID
*/
findById: function(table, id){
if(database[table] !== undefined){
if(database[table].rows[id] !== undefined){
return database[table].rows[id];
} else {
console.error('The requested record could not be found');
return false;
}
} else {
console.error('A table with the name "'+table+'" could not be found');
return false;
}
},
/*
Database Exists
*/
dbExists: function(thedb){
if(localStorage[thedb] !== undefined){
return true;
} else {
return false;
}
},
/*
Table Exists
*/
tableExists: function(table){
if(database[table] !== undefined){
return true;
} else {
return false;
}
},
/*
Export JSON
*/
exportData: function(table){
if(table){
if(database[table] !== undefined){
return JSON.stringify(database[table]);
} else {
console.error('Table not found');
return false;
}
} else {
return JSON.stringify(database);
}
}
};
return localdb;
}