diff --git a/lib/adapter.js b/lib/adapter.js index 05ac584..5881965 100644 --- a/lib/adapter.js +++ b/lib/adapter.js @@ -19,9 +19,7 @@ module.exports = function createMySQLAdapter(client, settings) { disconnect: cb => client.end(cb), closeConnection: () => { return new Promise(resolve => { - client.end(function() { - resolve(); - }); + client.end(() => resolve()); }); }, define: spec => informationSchema.registerModel(spec.model.modelName, spec), diff --git a/lib/enum-factory.js b/lib/enum-factory.js index e9609a7..063e68f 100644 --- a/lib/enum-factory.js +++ b/lib/enum-factory.js @@ -1,7 +1,7 @@ const EnumFactory = function() { if (arguments.length > 0) { const Enum = function Enum(arg) { - if (typeof arg === 'number' && arg % 1 == 0) { + if (typeof arg === 'number' && arg % 1 === 0) { return Enum._values[arg]; } else if (Enum[arg]) { return Enum[arg]; @@ -9,31 +9,29 @@ const EnumFactory = function() { return arg; } else if (arg === null) { return null; - } else { - return ''; } + return ''; }; const dxList = []; - dxList.push(''); // Want empty value to be at index 0 to match MySQL Enum values and MySQL non-strict behavior. - for (let arg in arguments) { - arg = String(arguments[arg]); + // Want empty value to be at index 0 to match MySQL Enum values and MySQL non-strict behavior. + dxList.push(''); + [].slice.call(arguments).forEach(arg => { Object.defineProperty(Enum, arg.toUpperCase(), { configurable: false, enumerable: true, value: arg, writable: false }); dxList.push(arg); - } + }); Object.defineProperty(Enum, '_values', { configurable: false, enumerable: false, value: dxList, writable: false }); Object.defineProperty(Enum, '_members', { configurable: false, enumerable: false, value: dxList.slice(1), writable: false }); Object.defineProperty(Enum, '_string', { configurable: false, enumerable: false, value: stringified(Enum), writable: false }); Object.freeze(Enum); return Enum; - } else { - throw 'No arguments - could not create Enum.'; } + throw 'No arguments - could not create Enum.'; }; function stringified(anEnum) { const s = []; for (const i in anEnum._values) { - if (anEnum._values[i] != '') { + if (anEnum._values[i] !== '') { s.push('\'' + anEnum._values[i] + '\''); } } diff --git a/lib/information-schema.js b/lib/information-schema.js index 9253af5..bd31dda 100644 --- a/lib/information-schema.js +++ b/lib/information-schema.js @@ -73,18 +73,33 @@ module.exports = function() { throw new Error(`Unexpected ("object") type for ${ model }.${ key }`); } - if (!prop || 'undefined' === typeof val) return val; - if (prop.type.name === 'Number') return val; - if (val === null) return 'NULL'; + if (!prop || 'undefined' === typeof val) { + return val; + } + + if (prop.type.name === 'Number') { + return val; + } + + if (val === null) { + return 'NULL'; + } + if (prop.type.name === 'Date') { - if (!val) return 'NULL'; + if (!val) { + return 'NULL'; + } + if (!val.toUTCString) { val = new Date(val); } + return val; } - if (prop.type.name === 'Boolean') return val ? 1 : 0; + if (prop.type.name === 'Boolean') { + return val ? 1 : 0; + } return val.toString(); } @@ -132,13 +147,15 @@ module.exports = function() { sql = [ '`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY' ]; } - Object.keys(models[model].properties).forEach(function(prop) { - if (prop === 'id') return; + Object.keys(models[model].properties).forEach(prop => { + if (prop === 'id') { + return; + } sql.push('`' + prop + '` ' + propertySettingsSQL(model, prop)); }); // Declared in model index property indexes. - Object.keys(models[model].properties).forEach(function(prop) { + Object.keys(models[model].properties).forEach(prop => { const p = models[model].properties[prop]; const i = p.index; if (i && !(p.type instanceof Array)) { @@ -149,7 +166,7 @@ module.exports = function() { // Settings might not have an indexes property. const dxs = models[model].settings.indexes; if (dxs) { - Object.keys(dxs).forEach(function(prop) { + Object.keys(dxs).forEach(prop => { const ix = dxs[prop]; sql.push(formatIndexDefinition(prop, ix.kind, ix.type, ix.keys, ix.columns)); }); @@ -194,7 +211,8 @@ module.exports = function() { dt = numericOptionsByType(p, dt); break; case 'Date': - dt = columnType(p, 'DATETIME'); // Currently doesn't need options. + // Currently doesn't need options. + dt = columnType(p, 'DATETIME'); break; case 'Boolean': dt = 'TINYINT(1)'; @@ -204,7 +222,8 @@ module.exports = function() { break; case 'Enum': dt = 'ENUM(' + p.type._string + ')'; - dt = stringOptions(p, dt); // Enum columns can have charset/collation. + // Enum columns can have charset/collation. + dt = stringOptions(p, dt); break; } return dt; diff --git a/lib/lookup-by-query.js b/lib/lookup-by-query.js index 35308e2..6471220 100644 --- a/lib/lookup-by-query.js +++ b/lib/lookup-by-query.js @@ -71,8 +71,10 @@ module.exports = function(informationSchema, db) { }); function buildOrderBy(order) { - if (typeof order === 'string') order = [ order ]; - return 'ORDER BY ' + order.map(function(o) { + if (typeof order === 'string') { + order = [ order ]; + } + return 'ORDER BY ' + order.map(o => { const t = o.split(/\s+/); if (t.length === 1) { return '`' + o + '`'; @@ -82,10 +84,15 @@ module.exports = function(informationSchema, db) { } function buildGroupBy(group) { - if (typeof group === 'string') group = [ group ]; - return 'GROUP BY ' + group.map(function(o) { + if (typeof group === 'string') { + group = [ group ]; + } + + return 'GROUP BY ' + group.map(o => { const t = o.split(/\s+/); - if (t.length === 1) return '`' + o + '`'; + if (t.length === 1) { + return '`' + o + '`'; + } return '`' + t[0] + '` ' + t[1]; }).join(', '); } diff --git a/lib/mysql.js b/lib/mysql.js index a9b7759..db8953d 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -24,7 +24,7 @@ exports.initialize = function initializeSchema(schema, callback) { function initializeConnection(connection, schema, callback) { // Attach listeners first - connection.on('error', function(err) { + connection.on('error', err => { schema.log('connection error', err); schema.connected = false; }); @@ -32,7 +32,7 @@ function initializeConnection(connection, schema, callback) { if (schema.settings.pool) { callback(); } else { - connection.connect(function(err) { + connection.connect(err => { if (err) { console.log('connection.connect err', err); console.log('will reconnect in 60 secs'); diff --git a/lib/schema-operations.js b/lib/schema-operations.js index 63e399d..dc66003 100644 --- a/lib/schema-operations.js +++ b/lib/schema-operations.js @@ -199,8 +199,10 @@ module.exports = function(informationSchema, db, settings) { .map(f => `DROP COLUMN ${ escapeKey(f.Field) }`)); // remove indexes - aiNames.forEach(function(indexName) { - if (indexName === 'id' || indexName === 'PRIMARY') return; + aiNames.forEach(indexName => { + if (indexName === 'id' || indexName === 'PRIMARY') { + return; + } if (indexNames.indexOf(indexName) === -1 && !m.properties[indexName] || m.properties[indexName] && (!m.properties[indexName].index || m.properties[indexName].type instanceof Array || m.properties[indexName].type.name === 'JSON')) { sql.push('DROP INDEX `' + indexName + '`'); } else { @@ -215,8 +217,10 @@ module.exports = function(informationSchema, db, settings) { if (m.settings.indexes[indexName].keys) { m.settings.indexes[indexName].columns = m.settings.indexes[indexName].keys.join(','); } - m.settings.indexes[indexName].columns.split(/,\s*/).forEach(function(columnName, i) { - if (ai[indexName].columns[i] !== columnName) orderMatched = false; + m.settings.indexes[indexName].columns.split(/,\s*/).forEach((columnName, i) => { + if (ai[indexName].columns[i] !== columnName) { + orderMatched = false; + } }); } if (!orderMatched) { @@ -263,17 +267,32 @@ module.exports = function(informationSchema, db, settings) { } function changed(newSettings, oldSettings) { - if (oldSettings.Null === 'YES') { // Used to allow null and does not now. - if (newSettings.allowNull === false) return true; - if (newSettings.null === false) return true; + // Used to allow null and does not now. + if (oldSettings.Null === 'YES') { + if (newSettings.allowNull === false) { + return true; + } + if (newSettings.null === false) { + return true; + } + } + // Did not allow null and now does. + if (oldSettings.Null === 'NO') { + if (newSettings.allowNull === true) { + return true; + } + if (newSettings.null === true) { + return true; + } + if (typeof newSettings.null === 'undefined' && typeof newSettings.allowNull === 'undefined') { + return true; + } } - if (oldSettings.Null === 'NO') { // Did not allow null and now does. - if (newSettings.allowNull === true) return true; - if (newSettings.null === true) return true; - if (typeof newSettings.null === 'undefined' && typeof newSettings.allowNull === 'undefined') return true; + + if (oldSettings.Type.toUpperCase() !== dataType(newSettings).toUpperCase()) { + return true; } - if (oldSettings.Type.toUpperCase() !== dataType(newSettings).toUpperCase()) return true; return false; } }