Skip to content

Instantly share code, notes, and snippets.

@jdrew1303
Forked from erichiller/sql_js_in_typescript.ts
Created March 12, 2017 05:08
Show Gist options
  • Save jdrew1303/21c7c31e511d690ca2c6f1f612deb5be to your computer and use it in GitHub Desktop.
Save jdrew1303/21c7c31e511d690ca2c6f1f612deb5be 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