jeefo-mysql
Advanced tools
Comparing version 0.0.3 to 0.0.4
263
index.js
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. | ||
* File Name : index.js | ||
* Created at : 2016-09-27 | ||
* Updated at : 2016-09-27 | ||
* Updated at : 2016-09-30 | ||
* Author : jeefo | ||
@@ -18,53 +18,232 @@ * Purpose : | ||
let path = require("path"), | ||
async = require("async"), | ||
config_path = path.resolve(process.cwd(), "config"), | ||
config = require(config_path).database, | ||
mysql_connection = require("./src/mysql_connection"); | ||
let path = require("path"), | ||
config_path = path.resolve(process.cwd(), "config"), | ||
config = require(config_path).database, | ||
let mysql_connections_container = {}; | ||
call = require("./src/call"), | ||
jeefo = require("jeefo"), | ||
filter = require("./src/filter"), | ||
is_object = jeefo.is_object, | ||
is_function = jeefo.is_function, | ||
prepare_select = require("./src/prepare_select"), | ||
prepare_update = require("./src/prepare_update"), | ||
prepare_insert = require("./src/prepare_insert"), | ||
prepare_delete = require("./src/prepare_delete"), | ||
let filter = (conn, database, table, data, callback) => { | ||
let column_names = Object.keys(data); | ||
async.waterfall( | ||
[ | ||
cb => { | ||
let query = ` | ||
SELECT \`COLUMN_NAME\` FROM \`INFORMATION_SCHEMA\`.\`COLUMNS\` | ||
WHERE | ||
\`TABLE_SCHEMA\` = ? AND | ||
\`TABLE_NAME\` = ? AND | ||
\`COLUMN_NAME\` IN(?) | ||
`; | ||
mysql_connection = require("./src/mysql_connection"), | ||
mysql_connections_container = {}; | ||
conn(query, [database, table, column_names], cb); | ||
}, | ||
(columns, f, cb) => { | ||
columns = columns.map(c => c.COLUMN_NAME); | ||
let noop = () => {}; | ||
let filtered_data = columns.reduce((payload, key) => { | ||
payload[key] = data[key]; | ||
return payload; | ||
}, {}); | ||
cb(null, filtered_data); | ||
// curry functions | ||
let filter_curry = (table, func) => { | ||
return (table_name, data, callback) => { | ||
if (is_function(data)) { | ||
callback = data; | ||
} | ||
if (is_object(table_name)) { | ||
data = table_name; | ||
table_name = table; | ||
} | ||
return func(table_name, data, callback); | ||
}; | ||
}; | ||
let select_curry = (table, func) => { | ||
return (table_name, definition, callback) => { | ||
// no definition, no table_name | ||
if (is_function(table_name)) { | ||
callback = table_name; | ||
table_name = table; | ||
definition = {}; | ||
} else { | ||
if (is_function(definition)) { | ||
callback = definition; | ||
} | ||
], | ||
callback | ||
); | ||
// no table_name | ||
if (is_object(table_name)) { | ||
definition = table_name; | ||
table_name = table; | ||
} | ||
} | ||
return func(table_name, definition, callback); | ||
}; | ||
}; | ||
module.exports = table => { | ||
let connection = mysql_connections_container[table]; | ||
if (! connection) { | ||
connection = mysql_connections_container[table] = mysql_connection(config); | ||
connection.filter = (data, callback) => { | ||
filter(connection, config.database, table, data, callback); | ||
}; | ||
let update_curry = (table, require_callback, func) => { | ||
if (is_function(require_callback)) { | ||
func = require_callback; | ||
} | ||
return (table_name, data, where, callback) => { | ||
// No table passed, which means table_name is data object. | ||
if (is_object(table_name)) { | ||
// no where object | ||
if (is_function(data)) { | ||
callback = data; | ||
where = {}; | ||
// has where object | ||
} else if (is_function(where)) { | ||
callback = where; | ||
where = data; | ||
} else if (is_object(data)) { | ||
where = data; | ||
} else { | ||
where = {}; | ||
} | ||
data = table_name; | ||
table_name = table; | ||
// has table name passed, now where is optional | ||
} else if (is_function(where)) { | ||
callback = where; | ||
where = {}; | ||
} | ||
if (! require_callback) { | ||
callback = callback || noop; | ||
} | ||
return func(table_name, data, where, callback); | ||
}; | ||
}; | ||
let insert_curry = (table, require_callback, func) => { | ||
return (table_name, data, callback) => { | ||
if (is_object(table_name)) { | ||
callback = data; | ||
data = table_name; | ||
table_name = table; | ||
} | ||
if (! require_callback) { | ||
callback = callback || noop; | ||
} | ||
return func(table_name, data, callback); | ||
}; | ||
}; | ||
let delete_curry = (table, func) => { | ||
return (table_name, where, callback) => { | ||
if (is_function(table_name)) { | ||
callback = table_name; | ||
table_name = table; | ||
where = {}; | ||
} else if (is_object(table_name)) { | ||
callback = where; | ||
where = table_name; | ||
table_name = table; | ||
} | ||
return func(table_name, where, callback); | ||
}; | ||
}; | ||
let make_new_connection = table => { | ||
let connection = mysql_connections_container[table] = mysql_connection(config, table); | ||
let exec = (prepared, callback) => { | ||
connection(prepared.query, prepared.values, callback); | ||
}; | ||
// Filter | ||
connection.filter = filter_curry(table, (table_name, data, callback) => { | ||
filter(connection, config.database, table_name, data, callback); | ||
}); | ||
// Procedure function | ||
// TODO: call_curry | ||
connection.call = (procedure, values, callback) => { | ||
if (is_function(values)) { | ||
callback = values; | ||
values = null; | ||
} | ||
exec(call(procedure, values), (err, results, last_query) => { | ||
if (callback) { | ||
callback(err, results && results[0], last_query); | ||
} | ||
}); | ||
}; | ||
// Select statement | ||
connection.prepare_select = select_curry(table, prepare_select); | ||
connection.find = select_curry(table, (table_name, definition, callback) => { | ||
exec(prepare_select(table_name, definition), callback); | ||
}); | ||
connection.first = select_curry(table, (table_name, definition, callback) => { | ||
definition.$limit = 1; | ||
exec(prepare_select(table_name, definition), (err, results, last_query) => { | ||
callback(err, results && results[0], last_query); | ||
}); | ||
}); | ||
connection.all = callback => { | ||
connection.find(callback); | ||
}; | ||
// Select with total | ||
connection.prepare_select_with_total = select_curry(table, (table_name, definition) => prepare_select(table_name, definition, true)); | ||
connection.find_with_total = select_curry(table, (table_name, definition, callback) => { | ||
exec(prepare_select(table_name, definition, true), (err, results, last_query) => { | ||
callback(err, { | ||
records : (results && results[0]) || [], | ||
total : (results && results[1] && results[1][0] && results[1][0].total) || 0 | ||
}, last_query); | ||
}); | ||
}); | ||
connection.first_with_total = select_curry(table, (table_name, definition, callback) => { | ||
definition.$limit = 1; | ||
exec(prepare_select(table_name, definition, true), (err, results, last_query) => { | ||
callback(err, { | ||
record : (results && results[0]) || [], | ||
total : (results && results[1] && results[1][0] && results[1][0].total) || 0 | ||
}, last_query); | ||
}); | ||
}); | ||
// Update statement | ||
connection.prepare_update = update_curry(table, prepare_update); | ||
connection.update = update_curry(table, (table_name, data, where, callback) => { | ||
exec(prepare_update(table_name, data, where), callback); | ||
}); | ||
connection.update_first = update_curry(table, (table_name, data, where, callback) => { | ||
where.$limit = 1; | ||
exec(prepare_update(table_name, data, where), callback); | ||
}); | ||
connection.update_and_back = update_curry(table, true, (table_name, data, where, callback) => { | ||
exec(prepare_update(table_name, data, where, true), (err, results, last_query) => { | ||
callback(err, (results && results[2]), last_query); | ||
}); | ||
}); | ||
connection.update_first_and_back = update_curry(table, true, (table_name, data, where, callback) => { | ||
where.$limit = 1; | ||
exec(prepare_update(table_name, data, where, true), (err, results, last_query) => { | ||
callback(err, (results && results[2] && results[2][0]), last_query); | ||
}); | ||
}); | ||
// Insert statement | ||
connection.prepare_insert = insert_curry(table, prepare_insert); | ||
connection.insert = insert_curry(table, (table_name, data, callback) => { | ||
exec(prepare_insert(table_name, data), callback); | ||
}); | ||
connection.insert_and_back = insert_curry(table, true, (table_name, data, callback) => { | ||
exec(prepare_insert(table_name, data, true), (err, results, last_query) => { | ||
callback(err, results && results[2] && results[2][0], last_query); | ||
}); | ||
}); | ||
// Delete statement | ||
connection.prepare_delete = delete_curry(table, prepare_delete); | ||
connection.delete = delete_curry(table, (table_name, where, callback) => { | ||
exec(prepare_delete(table_name, where), callback); | ||
}); | ||
connection.delete_first = delete_curry(table, (table_name, where, callback) => { | ||
where.$limit = 1; | ||
exec(prepare_delete(table_name, where), callback); | ||
}); | ||
return connection; | ||
}; | ||
module.exports = table => { | ||
return mysql_connections_container[table] || make_new_connection(table); | ||
}; |
{ | ||
"name": "jeefo-mysql", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Very simple MySQL connection instances for each table. I had annoyed enough for MySQL Deadlocks.", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git://github.com:je3f0o/node-jeefo-mysql.git" | ||
"url": "git://github.com/%3Aje3f0o/node-jeefo-mysql.git" | ||
}, | ||
@@ -15,3 +15,5 @@ "keywords": [ | ||
], | ||
"author": "je3f0o", | ||
"author": { | ||
"name": "je3f0o" | ||
}, | ||
"license": "MIT", | ||
@@ -21,3 +23,13 @@ "dependencies": { | ||
"mysql": "^2.11.1" | ||
} | ||
}, | ||
"gitHead": "af1ac5be8efc56b0bb67c83a2942e6dba7afc302", | ||
"bugs": { | ||
"url": "https://github.com/%3Aje3f0o/node-jeefo-mysql/issues" | ||
}, | ||
"readme": "ERROR: No README data found!", | ||
"homepage": "https://github.com/%3Aje3f0o/node-jeefo-mysql#readme", | ||
"_id": "jeefo-mysql@0.0.3", | ||
"scripts": {}, | ||
"_shasum": "2f5d3e9a5458df0a7471f27ed09580798ff44a36", | ||
"_from": "jeefo-mysql@*" | ||
} |
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. | ||
* File Name : mysql_connection.js | ||
* Created at : 2016-09-27 | ||
* Updated at : 2016-09-27 | ||
* Updated at : 2016-09-28 | ||
* Author : jeefo | ||
@@ -18,8 +18,9 @@ * Purpose : | ||
let async = require("async"), | ||
mysql = require("mysql"); | ||
let async = require("async"), | ||
mysql = require("mysql"), | ||
jeefo = require("jeefo"), | ||
is_function = jeefo.is_function, | ||
escape_identifier = mysql.escapeId, | ||
let is_undefined = function (value) { | ||
return value === void 0; | ||
}; | ||
mysql_connection_instances = jeefo.map(); | ||
@@ -41,17 +42,40 @@ let mysql_connect = (config, callback) => { | ||
module.exports = (config) => { | ||
let timeout, connection_instance; | ||
module.exports = (config, table) => { | ||
let connection = mysql_connection_instances[table] = {}, timeout; | ||
let close = () => { | ||
connection.instance.destroy(); | ||
connection.instance = null; | ||
connection.is_connecting = false; | ||
}; | ||
return (query, values, callback) => { | ||
if (is_undefined(callback)) { | ||
if (is_function(values)) { | ||
callback = values; | ||
values = null; | ||
} | ||
query = query.replace(/\$\$table/g, table); | ||
query = query.replace(/\$table/g, escape_identifier(table)); | ||
clearTimeout(timeout); | ||
async.waterfall( | ||
[ | ||
cb => { | ||
if (connection_instance) { | ||
cb(null, connection_instance); | ||
if (connection.instance) { | ||
cb(null, connection.instance); | ||
} else if (connection.is_connecting) { | ||
let i = 0; | ||
(function retry () { | ||
setTimeout(() => { | ||
if (connection.instance) { | ||
cb(null, connection.instance); | ||
} else if (i < 100) { | ||
i += 1; | ||
retry(); | ||
} | ||
}, 20); | ||
}()); | ||
} else { | ||
connection.is_connecting = true; | ||
mysql_connect(config, cb); | ||
@@ -61,17 +85,13 @@ } | ||
(conn, cb) => { | ||
connection_instance = conn; | ||
connection_instance.query(query, values, cb); | ||
connection.instance = conn; | ||
conn.query(query, values, cb); | ||
} | ||
], | ||
function () { | ||
(err, data) => { | ||
if (callback) { | ||
callback.apply(null, arguments); | ||
callback(err, data, { query, values }); | ||
} | ||
clearTimeout(timeout); | ||
timeout = setTimeout(() => { | ||
connection_instance.destroy(); | ||
connection_instance = null; | ||
}, 5000); | ||
timeout = setTimeout(close, 10000); | ||
} | ||
@@ -78,0 +98,0 @@ ); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
20564
12
682
1
1