2020-03-08 22:40:44 -05:00
|
|
|
const micro = require('micro');
|
2020-03-09 10:49:19 -05:00
|
|
|
const queryString = require('querystring');
|
2020-03-08 22:40:44 -05:00
|
|
|
const Database = require('better-sqlite3');
|
|
|
|
|
|
|
|
|
|
module.exports = (req, res) => {
|
|
|
|
|
console.log(req.method, req.url);
|
2020-03-09 10:49:19 -05:00
|
|
|
res.setHeader("Access-Control-Allow-Origin","*");
|
|
|
|
|
//console.log(res);
|
2020-03-08 22:40:44 -05:00
|
|
|
if (req.method === "GET") {
|
|
|
|
|
console.log("Get Request coming in");
|
2020-03-09 10:49:19 -05:00
|
|
|
let qsAttr;
|
|
|
|
|
let func=req.url;
|
|
|
|
|
if (req.url.indexOf("?") >=0){
|
|
|
|
|
func=req.url.substring(0,req.url.indexOf("?"))
|
|
|
|
|
qsAttr=queryString.parse(req.url.substring(req.url.indexOf("?")+1));
|
|
|
|
|
}
|
|
|
|
|
console.log(func,qsAttr);
|
|
|
|
|
if (func === "/getCombinations") {
|
|
|
|
|
console.log("Request for combinations");
|
2020-03-08 22:40:44 -05:00
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
|
|
|
|
const stmt = db.prepare("SELECT distinct Symbol,date(TimeStamp,'localtime') Date ,time(TimeStamp,'localtime') Time FROM OptionQuotes");
|
|
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
|
|
|
|
}
|
2020-03-09 10:49:19 -05:00
|
|
|
if (func === "/getSymbols") {
|
|
|
|
|
console.log("Request for Symbols");
|
|
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
|
|
|
|
const stmt = db.prepare("SELECT distinct Symbol FROM OptionQuotes order by 1");
|
|
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
|
|
|
|
}
|
|
|
|
|
if (func === "/getDates") {
|
|
|
|
|
console.log("Request for Dates");
|
|
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
|
|
|
|
const stmt = db.prepare("SELECT distinct date(TimeStamp,'localtime') Date FROM OptionQuotes order by 1");
|
|
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
|
|
|
|
}
|
|
|
|
|
if (func === "/getTimes") {
|
|
|
|
|
console.log("Request for Times");
|
|
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
|
|
|
|
let sql = "SELECT distinct time(TimeStamp,'localtime') Time FROM OptionQuotes where 1=1"
|
|
|
|
|
if (qsAttr.Symbol) {
|
|
|
|
|
sql +=" and Symbol='" + qsAttr.Symbol+ "'";
|
|
|
|
|
}
|
|
|
|
|
if (qsAttr.Date){
|
|
|
|
|
sql +=" and date(TimeStamp,'localtime')='" + qsAttr.Date+ "'";
|
|
|
|
|
}
|
|
|
|
|
sql+=" order by 1";
|
|
|
|
|
const stmt = db.prepare(sql);
|
|
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
|
|
|
|
}
|
|
|
|
|
if (func === "/getOptionChain") {
|
|
|
|
|
console.log("Request for OptionChain");
|
|
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
|
|
|
|
let sql = "SELECT Data FROM OptionQuotes where 1=1"
|
|
|
|
|
if (qsAttr.Symbol) {
|
|
|
|
|
sql +=" and Symbol='" + qsAttr.Symbol+ "'"
|
|
|
|
|
}
|
|
|
|
|
if (qsAttr.Date){
|
|
|
|
|
sql +=" and date(TimeStamp,'localtime')='" + qsAttr.Date+ "'";
|
|
|
|
|
}
|
|
|
|
|
if (qsAttr.Time){
|
|
|
|
|
sql +=" and time(TimeStamp,'localtime')='" + qsAttr.Time+ "'";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stmt = db.prepare(sql);
|
|
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 22:40:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|