· 7 years ago · Dec 09, 2018, 03:48 PM
1/**
2 * Stores candle data
3 * @param {string} table - table name
4 * @param {array{}} candles - array of candle objects
5 */
6 storeCandles: function(table, candles) {
7 try {
8 if (!this.dbFile) {
9 throw "Database file unspecified";
10 }
11
12 const dbConn = this.getDb();
13 dbConn
14 .prepare(
15 `CREATE TABLE IF NOT EXISTS [${table}] (id INTEGER PRIMARY KEY AUTOINCREMENT, open REAL, close REAL, high REAL, low REAL, volume REAL, tradeId INTEGER, startTime INTEGER, endTime INTEGER)`
16 )
17 .run();
18
19 const insertStmt = dbConn.prepare(
20 `INSERT INTO [${table}] (open, close, high, low, volume, tradeId, startTime, endTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
21 );
22 dbConn.transaction(() => {
23 candles.forEach(
24 ({ open, close, high, low, volume, tradeId, startTime, endTime }) =>
25 insertStmt.run(
26 open,
27 close,
28 high,
29 low,
30 volume,
31 tradeId,
32 startTime,
33 endTime
34 )
35 );
36 })();
37 Logger.debug(`${candles.length} added to ${table}`);
38 } catch (e) {
39 Logger.error(e.message);
40 }
41 },