-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (131 loc) · 3.15 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const fs = require('fs');
const Timer = require('./lib/timer');
const ERR = require('./lib/errors');
/**
* 本地文件数据库-json格式
* @param {Object} options - 必需的一些配置
* @param {String} options.filepath - 数据库文件路径
* @param {Number} options.maxsize - 存储的最大个数
* @param {Number} options.sleep - 持久化时间间隔
*
* id:{}
*/
var DB = function (options) {
if (!options.filepath) {
throw ERR.noFilePath;
}
if (!options.maxsize) {
options.maxsize = 0;
}
if (typeof options.maxsize !== 'number') {
throw ERR.maxsizeNotNumber;
}
this.options = options;
this.ids = [];
this.rows = {};
this.changes = 0;
init(this);
};
function init(db) {
let fn = db.options.filepath;
if (!fs.existsSync(fn)) {
fs.writeFileSync(fn, '', {
encoding: 'utf8'
});
} else {
let content = fs.readFileSync(fn, {
encoding: 'utf8'
});
content.split(/[\r\n]+/).forEach(function (line) {
let idx = line.indexOf(':');
if (idx > 0) {
let id = line.substr(0, idx) - 0;
db.ids.push(id);
db.rows[id] = line.substr(idx + 1);
}
});
}
new Timer(db);
}
DB.prototype.getById = function (id) {
let value = this.rows[id];
// 返回出去的都是json格式
if (value && typeof value === 'string') {
value = JSON.parse(value);
this.rows[id] = value;
}
return value;
};
DB.prototype.getByFilter = function (filter) {
if (typeof filter !== 'function') {
throw ERR.filterNotFunction;
}
let results = [];
for (let id in this.rows) {
let value = this.getById(id);
if (filter(value)) {
results.push(value);
}
}
return results;
};
DB.prototype.getAll = function () {
return this.getByFilter(function (value) {
return true;
});
};
DB.prototype.count = function () {
return this.ids.length;
};
DB.prototype.add = function (data) {
if (typeof data !== 'object') {
throw ERR.dataNotJson;
}
if (this.ids.length) {
data.id = this.ids[this.ids.length - 1] + 1;
} else {
data.id = 1;
}
this.ids.push(data.id);
this.rows[data.id] = data;
this.changes += 1;
return 1;
};
DB.prototype.update = function (id, data) {
let value = this.getById(id);
if (!value) {
return 0;
}
data.id = id;
Object.assign(value, data);
this.rows[id] = value;
this.changes += 1;
return 1;
};
DB.prototype.updates = function(rows){
let count = 0;
rows.forEach(function(data){
count += this.update(data.id, data);
});
return count;
};
DB.prototype.delete = function (id) {
if (!this.rows[id]) {
return 0;
}
delete this.rows[id];
let idx = this.ids.indexOf(id);
if (idx > -1) {
this.ids.splice(idx, 1);
}
this.changes += 1;
return 1;
};
DB.prototype.clear = function () {
let count = this.count();
this.ids = [];
this.rows = {};
this.changes += count;
return count;
};
module.exports = DB;