-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.js
169 lines (143 loc) · 3.58 KB
/
storage.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
/*******************************************************
* generic file/record storage module (server)
* May 2015
* Mike Amundsen (@mamund)
* Soundtrack : Complete Collection : B.B. King (2008)
*******************************************************/
/*
- simple storage component writes files to disk
- FOLDER is the collection (tasks, users, notes, etc.)
- FILE is the record (stored as JSON object, w/ ID as filename)
- CRUD style interface (list, item, add, update, remove)
- "contains"-type filtering is supported, no sort or join
- NOTE: all actions are *synchronous*
*/
var fs = require('fs');
var folder = process.cwd() + '/data/';
var utils = require('./connectors/utils.js');
module.exports = main;
function main(object, action, arg1, arg2, arg3) {
var rtn;
switch (action) {
case 'list':
rtn = getList(object);
break;
case 'filter':
rtn = getList(object, arg1);
break;
case 'item':
rtn = getItem(object, arg1);
break;
case 'add':
rtn = addItem(object, arg1, arg2);
break;
case 'update':
rtn = updateItem(object, arg1, arg2, arg3);
break;
case 'remove':
rtn = removeItem(object, arg1);
break;
default:
rtn = null;
break;
}
return rtn;
}
function getList(object, filter) {
var coll, item, list, i, x, t, name;
coll = [];
try {
list = fs.readdirSync(folder + object + '/');
for (i = 0, x = list.length; i < x; i++) {
item = JSON.parse(fs.readFileSync(folder + object + '/' + list[i]));
if (filter) {
t = null;
for (var name in filter) {
try {
if (item[name].toString().toLowerCase().indexOf(filter[name].toString().toLowerCase()) !== -1) {
t = list[i];
} else {
t = null;
}
} catch (err) {
t = null;
}
}
if (t !== null) {
coll.push(item);
}
} else {
coll.push(item);
}
}
} catch (ex) {
coll = [];
}
return coll;
}
function getItem(object, id) {
var rtn;
try {
rtn = JSON.parse(fs.readFileSync(folder + object + '/' + id));
} catch (ex) {
rtn = null;
}
return rtn;
}
function addItem(object, item, id) {
var rtn;
if (id) {
item.id = id;
} else {
item.id = makeId();
}
item.dateCreated = new Date();
item.dateUpdated = item.dateCreated;
if (fs.existsSync(folder + object + '/' + item.id)) {
rtn = utils.exception("User", "Record already exists");
} else {
try {
fs.writeFileSync(folder + object + '/' + item.id, JSON.stringify(item));
rtn = getItem(object, item.id);
} catch (ex) {
rtn = null;
}
}
return rtn;
}
function updateItem(object, id, item) {
var current, rtn;
current = getItem(object, id);
if (!current) {
rtn = utils.exception("Task", "Invalid [id]", 400);
return rtn;
}
current = item;
current.dateUpdated = new Date();
rtn = null;
try {
fs.writeFileSync(folder + object + '/' + id, JSON.stringify(current));
rtn = getItem(object, id);
} catch (ex) {
rtn = null;
}
return rtn;
}
function removeItem(object, id) {
var rtn;
try {
fs.unlinkSync(folder + object + '/' + id);
rtn = getList(object);
} catch (ex) {
rtn = getList(object);
}
return rtn;
}
function makeId() {
var rtn;
rtn = String(Math.random());
rtn = rtn.substring(2);
rtn = parseInt(rtn).toString(36);
return rtn;
}
// EOF