1

Use database for keyword query function

This commit is contained in:
Alex Yatskov 2014-09-16 18:35:04 +09:00
parent 6a57a825f1
commit 28d419f8be
2 changed files with 35 additions and 3 deletions

View File

@ -3,6 +3,8 @@
var _ = require('underscore'); var _ = require('underscore');
var db_keywords = require('./keywords.json'); var db_keywords = require('./keywords.json');
var db_data = require('./data.json'); var db_data = require('./data.json');
var mysql = require('mysql');
var connection = null;
function innerProduct(values1, values2) { function innerProduct(values1, values2) {
@ -121,8 +123,30 @@ function searchBuildHints(searchParams, minScore, keyword, range, steps) {
return hints; return hints;
} }
module.exports.getKeywords = function() { module.exports.loadDb = function(params) {
return _.keys(db_keywords).sort(); module.exports.freeDb();
connection = mysql.createConnection(params);
}
module.exports.freeDb = function() {
if (connection) {
connection.end();
connection = null;
}
}
module.exports.getKeywords = function(callback) {
connection.query('SELECT name FROM keywords ORDER BY name ASC', function(err, rows) {
if (err) {
throw err;
}
var keywords = _.map(rows, function(row) {
return row['name']
});
callback(keywords);
});
} }
module.exports.execQuery = function(query) { module.exports.execQuery = function(query) {

View File

@ -9,9 +9,17 @@ var search = require('./db/search.js');
function main() { function main() {
var app = express(); var app = express();
search.loadDb({
'host': 'localhost',
'user': 'hscd',
'database': 'hscd'
});
app.use('/keywords', function(req, res) { app.use('/keywords', function(req, res) {
console.log('Requesting keywords'); console.log('Requesting keywords');
res.json(search.getKeywords()); search.getKeywords(function(keywords) {
res.json(keywords);
});
}); });
app.use('/search', function(req, res) { app.use('/search', function(req, res) {