Skip to content

Instantly share code, notes, and snippets.

@erichiller
Last active September 3, 2019 05:27
Show Gist options
  • Save erichiller/fe0ca29c946cd46c7bccdab56ceaff0e to your computer and use it in GitHub Desktop.
Save erichiller/fe0ca29c946cd46c7bccdab56ceaff0e to your computer and use it in GitHub Desktop.
import fs = require('fs');
import sql = require('sql.js');
export class Security {
private db;
private history: dayRecord[];
constructor(
private symbol: string,
private dbname: string = "f.db"
) {
console.log("Security constructor for " + symbol);
this.db = new sql.Database(fs.readFileSync(path.join(__dirname, this.dbname)));
// date stored as ISO 8601
this.writeSql("CREATE TABLE IF NOT EXISTS " + this.symbol + " ("+
" `date` TEXT NOT NULL UNIQUE, " +
" `open` REAL NOT NULL, " +
" `high` REAL NOT NULL, " +
" `low` REAL NOT NULL, " +
" `close` REAL NOT NULL, " +
" `volume` NUMERIC, " +
" `adj_close` REAL, " +
" PRIMARY KEY(date) " +
"); ");
console.log("table created");
}
writeSql(sqlstr: string) {
console.log("writeSql:"+sqlstr)
this.db.run(sqlstr);
fs.writeFileSync(this.dbname, new Buffer(this.db.export()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment