I want to read a big JSON file (this to be precise), iterate over it, check which are new entries and save or update as necessary.
Doing this with sails, and sails mysql I found out that the whole process slows down the first time and if I try to run it a second time it dies at some point. My mysql server seems to handle that for around 24k requests and dies.
Here is the code with which I made it worked, but it completely ignores sails, or sails-mysql and I use mysql directly to achieve this.
This is the working code:
var path = require('path')
var Promise = require('bluebird')
var fs = Promise.promisifyAll(require('fs'))
var rootPath = process.cwd()
var filePath = path.join(rootPath, 'assets/systems.json')
var mysql = require("mysql");
Promise.promisifyAll(mysql);
Promise.promisifyAll(require("mysql/lib/Connection").prototype);
Promise.promisifyAll(require("mysql/lib/Pool").prototype);
var pool = mysql.createPool({
connectionLimit: 10,
host: 'localhost',
user: 'user',
password: 'pass',
database: 'db'
});
function getSqlConnection() {
return pool.getConnectionAsync().disposer(function(connection) {
connection.release();
});
}
fs.readFileAsync(filePath, 'utf8').then(JSON.parse).then(function(systems) {
total = systems.length
return systems
})
.map(function(item, index, value) {
Promise.using(getSqlConnection(), function (conn) {
return conn.queryAsync('SELECT * FROM system WHERE name = "' + item.name + '"')
})
.then(function(have_system) {
// do something with the info
return the_result_of_doing_something
})
.caught(function(err) {
console.log(err)
})
}, {concurency: 5})
.caught(SyntaxError, function(e) {
console.log("Invalid JSON in file " + e.fileName + ": " + e.message);
})
.lastly(function() {
broadcast(["All DONE!!!", updated_systems, new_systems, current])
})
How can I achieve this without requiring mysql again and creating my own connection? So this:
function getSqlConnection() {
return pool.getConnectionAsync().disposer(function(connection) {
connection.release();
});
}
Promise.using(getSqlConnection(), function (conn) {
return conn.queryAsync('SELECT * FROM system WHERE name = "' + item.name + '"')
})
Would become something like:
function getSailsConnection() {
return sails.pool.getConnectionAsync().disposer(function(connection) {
connection.release();
});
}
Promise.using(getSailsConnection(), function (conn) {
return conn.System.findOne({name: item.name})
})
If I can properly release I don't even have to use Promise.using() since the waterline model can use promises.
System.findOne({name: item.name}).then(function(have_system) {
// do something
System.releaseConection() // or some other API call to achieve this
})
.caught(function(err) {
System.releaseConection() // or some other API call to achieve this
})
PS: I tried to add a sails-mysql tag too but I'm not allowed.