Socket
Socket
Sign inDemoInstall

sync-mysql

Package Overview
Dependencies
23
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.0 to 3.0.0

123

lib/index.js
'use strict';
var _stringify = require('babel-runtime/core-js/json/stringify');
var _stringify2 = _interopRequireDefault(_stringify);
var _typeof2 = require('babel-runtime/helpers/typeof');

@@ -19,12 +15,6 @@

var _fs = require('fs');
var _child_process = require('child_process');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var WORKER_PATH = require.resolve('./worker');
var rpc = require('sync-rpc');
Function('', (0, _fs.readFileSync)(WORKER_PATH, 'utf8'));
var Connection = function () {

@@ -34,6 +24,16 @@ function Connection(config) {

this._config = config;
this._client = rpc(__dirname + '/worker.js', config);
}
(0, _createClass3.default)(Connection, [{
key: '_end',
value: function _end(id) {
return this._client({ type: 'end', id: id });
}
}, {
key: 'finishAll',
value: function finishAll() {
this._client({ type: 'end-all' });
}
}, {
key: 'call',

@@ -52,14 +52,58 @@ value: function call(name, args) {

}
return request({
config: this._config,
method: 'call',
args: [name, args]
});
return this._client({ type: 'call', name: name, args: args });
}
}, {
key: 'query',
value: function query(q, args) {
if (typeof q !== 'string') {
throw new TypeError('Expected q to be a string');
value: function query(str) {
var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
if (typeof str !== 'string') {
throw new TypeError('Expected query to be a string');
}
if (!Array.isArray(values)) {
throw new TypeError('Expected args to be strings');
}
if (!values.every(function (arg) {
return typeof arg === 'string' || typeof arg === 'number' || typeof arg === 'boolean' || arg === null;
})) {
throw new TypeError('Expected every arg to be a string, number, boolean or null');
}
return this._client({ type: 'query', str: str, values: values });
}
}, {
key: 'dispose',
value: function dispose() {
return this._client({ type: 'dispose' });
}
}, {
key: 'queueQuery',
value: function queueQuery(str) {
var _this = this;
var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
if (typeof str !== 'string') {
throw new TypeError('Expected query to be a string');
}
if (!Array.isArray(values)) {
throw new TypeError('Expected args to be strings');
}
if (!values.every(function (arg) {
return typeof arg === 'string' || typeof arg === 'number' || typeof arg === 'boolean' || arg === null;
})) {
throw new TypeError('Expected every arg to be a string, number, boolean or null');
}
var id = this._client({ type: 'queue-query', str: str, values: values });
return function () {
return _this._end(id);
};
}
}, {
key: 'queueCall',
value: function queueCall(name, args) {
var _this2 = this;
if (typeof name !== 'string') {
throw new TypeError('Expected name to be a string');
}
if (!Array.isArray(args)) {

@@ -73,8 +117,10 @@ throw new TypeError('Expected args to be strings');

}
return request({
config: this._config,
method: 'query',
args: [q, args]
});
var id = this._client({ type: 'queue-call', name: name, args: args });
return function () {
return _this2._end(id);
};
}
// shorthands
}, {

@@ -86,2 +132,7 @@ key: 'update',

}, {
key: 'queueUpdate',
value: function queueUpdate() {
return this.queueQuery.apply(this, arguments);
}
}, {
key: 'getRecord',

@@ -101,24 +152,2 @@ value: function getRecord(table, id) {

function request(input) {
if (!_child_process.spawnSync) {
throw new Error('Sync-request requires node version 0.12 or later. If you need to use it with an older version of node\n' + 'you can `npm install sync-request@2.2.0`, which was the last version to support older versions of node.');
}
var req = (0, _stringify2.default)(input);
var res = (0, _child_process.spawnSync)(process.execPath, [WORKER_PATH], { input: req });
if (res.status !== 0) {
throw new Error(res.stderr.toString());
}
if (res.error) {
if (typeof res.error === 'string') res.error = new Error(res.error);
throw res.error;
}
var response = JSON.parse(res.stdout);
if (response.success) {
return response.result;
} else {
throw new Error(response.error.message || response.error || response);
}
}
module.exports = Connection;
'use strict';
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _extends2 = require('babel-runtime/helpers/extends');

@@ -11,10 +7,2 @@

var _stringify = require('babel-runtime/core-js/json/stringify');
var _stringify2 = _interopRequireDefault(_stringify);
var _concatStream = require('concat-stream');
var _concatStream2 = _interopRequireDefault(_concatStream);
var _thenMysql = require('then-mysql');

@@ -30,24 +18,44 @@

function respond(data) {
process.stdout.write((0, _stringify2.default)(data), function () {
process.exit(0);
});
}
process.stdin.pipe((0, _concatStream2.default)(function (stdin) {
var req = JSON.parse(stdin.toString());
var db = new _thenMysql2.default((0, _extends3.default)({}, req.config, {
function init(config) {
var db = new _thenMysql2.default((0, _extends3.default)({}, config, {
connectionLimit: 1
}));
_promise2.default.resolve(null).then(function () {
return db[req.method].apply(db, (0, _toConsumableArray3.default)(req.args));
}).finally(function (result) {
return db.dispose();
}).done(function (result) {
respond({ success: true, result: result });
}, function (err) {
respond({ success: false, error: err.message });
});
}));
var results = [];
return function (message) {
switch (message.type) {
case 'dispose':
return db.dispose();
case 'query':
return db.query(message.str, message.values);
case 'call':
return db.call(message.name, message.args);
case 'queue-query':
{
var index = results.length;
results.push(db.query(message.str, message.values));
return index;
}
case 'queue-call':
{
var _index = results.length;
results.push(db.query(message.str, message.values));
return _index;
}
case 'end':
{
var result = results[message.id];
results[message.id] = null;
return result;
}
case 'end-all':
{
return _promise2.default.all(results).forEach(function (value, i) {
results[i] = null;
}).then(function () {
return null;
});
}
}
};
}
module.exports = init;
{
"name": "sync-mysql",
"version": "2.0.0",
"version": "3.0.0",
"main": "lib/index.js",

@@ -13,2 +13,3 @@ "description": "Make synchronous queries to a mysql database",

"concat-stream": "^1.6.0",
"sync-rpc": "^1.1.0",
"then-mysql": "^1.1.1"

@@ -15,0 +16,0 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc