I want to save tick data from a cryptocurrency exchange directly into my mysql database.
My schema looks like the following:
CREATE TABLE `cryptocurrencies` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`last` decimal(19,4) NOT NULL,
`lowestAsk` decimal(19,4) NOT NULL,
`highestBid` decimal(19,4) NOT NULL,
`percentChange` decimal(19,4) NOT NULL,
`baseVolume` decimal(19,4) NOT NULL,
`quoteVolume` decimal(19,4) NOT NULL,
`isFrozen` decimal(19,4) NOT NULL,
`24hrHigh` decimal(19,4) NOT NULL,
`24hrLow` decimal(19,4) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I get the data the following way:
var db = require('./node_exchanges/db/db.js')
var autobahn = require('autobahn');
var wsuri = "wss://api.poloniex.com";
var connection = new autobahn.Connection({
url: wsuri,
realm: "realm1"
});
connection.onopen = function(session) {
function marketEvent(args, kwargs) {
console.log(args);
}
function tickerEvent(args, kwargs) {
console.log(args);
}
var data = session.subscribe('ticker', tickerEvent);
db.connect(db.MODE_PRODUCTION, function() {
db.fixtures(data, function(err) {
if (err) return console.log(err)
console.log('Data has been loaded...')
})
});
}
connection.open();
My db.js
file looks like the following:
var mysql = require('mysql')
, async = require('async')
var PRODUCTION_DB = 'c9'
, TEST_DB = 'c9'
exports.MODE_TEST = 'mode_test'
//exports.MODE_PRODUCTION = 'mode_production'
var state = {
pool: null,
mode: null,
}
exports.connect = function(mode, done) {
state.pool = mysql.createPool({
host: 'localhost',
user: 'test',
password: '',
database: mode === exports.MODE_PRODUCTION ? PRODUCTION_DB : TEST_DB
})
state.mode = mode
done()
}
exports.get = function() {
return state.pool
}
exports.fixtures = function(data) {
var pool = state.pool
if (!pool) return done(new Error('Missing database connection.'))
var names = Object.keys(data.tables)
async.each(names, function(name, cb) {
async.each(data.tables[name], function(row, cb) {
var keys = Object.keys(row)
, values = keys.map(function(key) { return "'" + row[key] + "'" })
pool.query('INSERT INTO ' + name + ' (' + keys.join(',') + ') VALUES (' + values.join(',') + ')', cb)
}, cb)
}, done)
}
exports.drop = function(tables, done) {
var pool = state.pool
if (!pool) return done(new Error('Missing database connection.'))
async.each(tables, function(name, cb) {
pool.query('DELETE * FROM ' + name, cb)
}, done)
}
The data is coming in from the api, but nothing is save within the database. Any suggestions what I am doing wrong?
I appreciate your reply!
via mrquad
No comments:
Post a Comment