-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastore.js
170 lines (134 loc) · 3.68 KB
/
datastore.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
"use strict";
let StockVO = require("./stock_vo");
let StatusVO = require("./status_vo");
let fs = require("fs");
let config = {
LOCATION: "/tmp/stock/data",
STATFILE: "status",
STOCKPREFIX: "sto_"
};
let NULL_STAT = {
};
function check(){
let exists = false;
let dirStat;
try {
dirStat = fs.statSync(config.LOCATION);
}
catch(errDir){
console.log("No stat directory, will need to create");
}
if (!!dirStat && !dirStat.isDirectory) throw new Error("Needs to be a directory");
let cfgStat;
try {
cfgStat = fs.statSync(config.LOCATION+"/"+config.STATFILE);
exists = true;
}
catch(errFile){
console.log("No stat file, will need to create");
}
if (!!cfgStat && !cfgStat.isFile) throw new Error("Need config file");
return exists;
}
function setup(){
let pathItems = config.LOCATION.split("/");
var buildPath = "";
while (pathItems.length){
buildPath += "/";
buildPath += pathItems.shift();
var testStat;
try {
testStat = fs.statSync(buildPath);
if (!testStat) throw new Error("no dir");
}
catch (err){
fs.mkdirSync(buildPath);
testStat = fs.statSync(buildPath);
}
if (!testStat && !testStat.isDirectory()) throw new Error("Path not doable");
}
writeStatus(NULL_STAT);
}
function writeStatus(status){
let statFile;
try {
statFile = fs.openSync(config.LOCATION+"/"+config.STATFILE, "w");
if ("object" === typeof status){
//TODO validate keys
status = JSON.stringify(status);
}
else {
throw new Error("cannot serialize that!");
}
fs.writeSync(statFile, status);
}
catch(err){
if (statFile) fs.closeSync(statFile);
throw err;
}
fs.closeSync(statFile);
return true;
}
function writeStock(info){
if (!info || !info.metadata || !info.metadata.ticker) throw new Error("Invalid stock symbol");
let filename = config.LOCATION+"/"+config.STOCKPREFIX+info.metadata.ticker.toLowerCase();
let stockFile;
try {
stockFile = fs.openSync(filename, "w");
if ("object" === typeof info) {
if ("function" === info.toJSON) {
info = info.toJSON();
}
else {
//TODO validate keys
info = JSON.stringify(info, null, 4);
}
}
else {
throw new Error("cannot serialize that!");
}
fs.writeSync(stockFile, info);
}
catch(err){
if (stockFile) fs.closeSync(stockFile);
throw err;
}
fs.closeSync(stockFile);
return true;
}
function readStock(id){
if (!id || ("string" !== typeof id)) throw new Error("Invalid stock symbol");
let filename = config.LOCATION+"/"+config.STOCKPREFIX+id.toLowerCase();
let stockvo = fs.readFileSync(filename, "utf8");
if ("string" === typeof stockvo){
stockvo = StockVO.fromJSON(stockvo);
}
else {
throw new Error("unexpected object format");
}
return stockvo;
}
function readStatus(){
let filename = config.LOCATION+"/"+config.STATFILE;
let statusvo = fs.readFileSync(filename, "utf8");
if ("string" === typeof statusvo){
statusvo = StatusVO.fromJSON(statusvo);
}
else {
throw new Error("Unexpected object format");
}
return statusvo;
}
/*
function config(param){
config = params;
}
*/
module.exports = {
check: check,
setup: setup,
writeStatus: writeStatus,
writeStock: writeStock,
readStock: readStock,
readStatus: readStatus
};