-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmicrodb.js
176 lines (153 loc) · 4.51 KB
/
microdb.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
// TINY Database file - *very* simple storage.
"use strict";
var fs = require('fs');
var MicroData = function(opts) {
var self = this;
var defaults = {
'file': '',
'savetime': 10,
'maxrec': 0,
'datatype': 1,
'flushonexit': true,
'defaultClean': false
};
this.options = defaults;
if ( typeof opts === 'object' ) {
for ( var idx in opts ) { this.options[idx] = opts[idx]; }
}
if ( this.options.datatype === 0 ) {
this.data = [];
} else {
this.data = {};
}
if ( this.options.flushonexit === true && this.options.file !== '' ) {
process.on('exit', function () {
self.flush();
});
}
this.findAll = function(key, value) {
if ( self.options.datatype === 0 ) { return false; }
var retArray = [];
for ( var idx in self.data ) {
if ( key in self.data[idx] && self.data[idx][key] === value ) {
retArray.push(idx);
}
}
return retArray;
}
this.findAllWithKey = function(key) {
if ( self.options.datatype === 0 ) { return false; }
var retArray = [];
for ( var idx in self.data ) {
if ( key in self.data[idx] ) {
retArray.push(idx);
}
}
return retArray;
}
this.find = function(key, value) {
if ( self.options.datatype === 0 ) { return false; }
for ( var idx in self.data ) {
if ( key in self.data[idx] && self.data[idx][key] === value ) {
return idx;
}
}
return false;
}
this.sortByKeys = function(sorts,cleanBad) {
if ( self.options.datatype === 0 ) { return false; }
if ( typeof cleanBad === 'undefined' ) { cleanBad = self.options.defaultClean; }
var sorter = [];
for ( var ident in self.data ) {
var tempArray = [ident]
for ( var sort in sorts ) {
tempArray.push(self.data[ident][sorts[sort][0]]);
}
sorter.push(tempArray);
}
if ( sorter.length > 0 ) {
for ( var srtIdx = sorts.length; srtIdx > 0; srtIdx-- ) {
if ( typeof sorts[srtIdx-1][2] !== 'undefined' && sorts[srtIdx-1][2] === true ) {
sorter = sorter.sort(function(a,b){
var multi = ( sorts[srtIdx-1][1] != 'desc' ) ? 1 : -1;
if ( a[srtIdx] < b[srtIdx] ) return -1*multi;
if ( a[srtIdx] > b[srtIdx] ) return 1*multi;
return 0;
});
} else {
sorter = sorter.sort(function(a,b) {
if ( sorts[srtIdx-1][1] != 'desc' ) {
return a[srtIdx] - b[srtIdx];
} else {
return b[srtIdx] - a[srtIdx];
}
});
}
}
}
if ( cleanBad === false ) { return sorter; }
var retSort = [];
for ( var idx = 0; idx < sorter.length; idx++ ) {
var keepMe = true;
for ( var check = 1; check < sorter[idx].length; check++ ) {
if ( typeof sorter[idx][check] === 'undefined' ) { keepMe = false; }
}
if ( keepMe === true ) { retSort.push(sorter[idx]); }
}
return retSort;
}
this.sortByKey = function(key, direction, alpha, cleanBad) {
return this.sortByKeys([[key,direction,alpha]], cleanBad);
}
this.startTime = function() {
if ( this.options.savetime > 0 ) {
this.time = setInterval( function() { self.save(); }, this.options.savetime * 1000*60 );
}
}
this.add = function(text, num) {
if ( typeof num === 'undefined' && this.options.datatype == 1 ) {
num = new Date().getTime();
}
if ( this.options.datatype === 0 ) {
this.data.push(text);
if ( this.options.maxrec > 0 ) {
while ( this.data.length > this.options.maxrec ) {
this.data.shift();
}
}
return this.data.length;
} else {
this.data[num] = text;
return num;
}
}
this.del = function(num) {
if ( this.options.datatype == 0 ) {
this.data.splice(num,1);
} else {
delete this.data[num];
}
return this.data.length;
}
this.save = function() {
if ( this.options.file === '' ) { return true; }
fs.writeFile(this.options.file, JSON.stringify(this.data), function() { return true; });
}
this.flush = function() {
if ( this.options.file === '' ) { return true; }
fs.writeFileSync(self.options.file, JSON.stringify(self.data));
return true;
}
this.load = function() {
this.data = JSON.parse(fs.readFileSync(this.options.file).toString());
}
if ( fs.existsSync(self.options.file) ) {
// Load if the file is there
this.load();
} else {
// Otherwise, create it.
this.flush();
}
this.startTime();
}
module.exports = MicroData;