Skip to content

Update library to mysql2 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

A MySQL client for Node.js that makes defining tables easy and automatically migrates table schemas.

This module extends the popular [`mysql`](https://www.npmjs.com/package/mysql) module, so it is recommended that you read the [`mysql` documentation](https://github.com/mysqljs/mysql#introduction), especially the sections on [connection options](https://github.com/mysqljs/mysql#connection-options), [performing queries](https://github.com/mysqljs/mysql#performing-queries), [escaping query values](https://github.com/mysqljs/mysql#escaping-query-values), and [escaping query identifiers](https://github.com/mysqljs/mysql#escaping-query-identifiers).
This module extends the popular [`mysql2`](https://www.npmjs.com/package/mysql2) module, so it is recommended that you read the [`mysql` documentation](https://github.com/mysqljs/mysql#introduction), especially the sections on [connection options](https://github.com/mysqljs/mysql#connection-options), [performing queries](https://github.com/mysqljs/mysql#performing-queries), [escaping query values](https://github.com/mysqljs/mysql#escaping-query-values), and [escaping query identifiers](https://github.com/mysqljs/mysql#escaping-query-identifiers).

## Table of Contents

Expand Down Expand Up @@ -259,7 +259,7 @@ A function called with the results of a query.
<a name="PoolPlus"></a>

## PoolPlus ⇐ <code>Pool</code>
A class that extends the `mysql` module's `Pool` class with the ability to define tables
A class that extends the `mysql2` module's `Pool` class with the ability to define tables
and perform queries and transactions using promises.

**Extends**: <code>Pool</code>
Expand Down Expand Up @@ -591,7 +591,7 @@ function trxnHandler(trxn, done) {
<a name="Connection"></a>

## Connection
The `mysql` module's `Connection` class extended with one extra method. Returned by
The `mysql2` module's `Connection` class extended with one extra method. Returned by
[`mysql.createConnection()`](https://github.com/mysqljs/mysql#establishing-connections)
and [`pool.getConnection()`](https://github.com/mysqljs/mysql#pooling-connections) and
passed to [`transactionHandler`](#PoolPlus..transactionHandler).
Expand Down
2 changes: 1 addition & 1 deletion jsdoc2md/README.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

A MySQL client for Node.js that makes defining tables easy and automatically migrates table schemas.

This module extends the popular [`mysql`](https://www.npmjs.com/package/mysql) module, so it is recommended that you read the [`mysql` documentation](https://github.com/mysqljs/mysql#introduction), especially the sections on [connection options](https://github.com/mysqljs/mysql#connection-options), [performing queries](https://github.com/mysqljs/mysql#performing-queries), [escaping query values](https://github.com/mysqljs/mysql#escaping-query-values), and [escaping query identifiers](https://github.com/mysqljs/mysql#escaping-query-identifiers).
This module extends the popular [`mysql2`](https://www.npmjs.com/package/mysql2) module, so it is recommended that you read the [`mysql` documentation](https://github.com/mysqljs/mysql#introduction), especially the sections on [connection options](https://github.com/mysqljs/mysql#connection-options), [performing queries](https://github.com/mysqljs/mysql#performing-queries), [escaping query values](https://github.com/mysqljs/mysql#escaping-query-values), and [escaping query identifiers](https://github.com/mysqljs/mysql#escaping-query-identifiers).

## Table of Contents

Expand Down
2 changes: 1 addition & 1 deletion lib/ColumnDefinitions/ColumnDefinition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const mysql = require('mysql');
const mysql = require('mysql2');

function escapeDefault(value) {
if (typeof value === 'boolean') {
Expand Down
2 changes: 1 addition & 1 deletion lib/Connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Connection = require('mysql/lib/Connection');
const {Connection} = require('mysql2');

/**
* @class Connection
Expand Down
2 changes: 1 addition & 1 deletion lib/KeyDefinitions/ForeignKeyDefinition.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const arraysEqual = require('../utils/arraysEqual');
const {escapeId} = require('mysql/lib/protocol/SqlString');
const {escapeId} = require('sqlstring');

const referenceOptions = ['RESTRICT', 'CASCADE', 'SET NULL', 'NO ACTION'];

Expand Down
2 changes: 1 addition & 1 deletion lib/KeyDefinitions/IndexKeyDefinition.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const arraysEqual = require('../utils/arraysEqual');
const {escapeId} = require('mysql/lib/protocol/SqlString');
const {escapeId} = require('sqlstring');
const parseKeyParts = require('../utils/parseKeyParts');

class IndexKeyDefinition {
Expand Down
2 changes: 1 addition & 1 deletion lib/MySQLPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ColumnDefinitions = require('./ColumnDefinitions');
const KeyDefinitions = require('./KeyDefinitions');
const PoolPlus = require('./PoolPlus');

const mysql = require('mysql');
const mysql = require('mysql2');

require('./Connection'); // Extends mysql Connection

Expand Down
10 changes: 6 additions & 4 deletions lib/PoolPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const ColumnDefinitions = require('./ColumnDefinitions');
const KeyDefinitions = require('./KeyDefinitions');
const MySQLTable = require('./MySQLTable');
const Operation = require('./Operation');
const Pool = require('mysql/lib/Pool');
const PoolConfig = require('mysql/lib/PoolConfig');
const SqlString = require('mysql/lib/protocol/SqlString');
const {Pool} = require('mysql2');
const mysql = require('mysql2');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const mysql = require('mysql2');
const PoolConfig = require('mysql2/lib/pool_config');

const SqlString = require('sqlstring');
const TableDefinition = require('./TableDefinition');

const MIGRATION_STRATEGIES = [
Expand Down Expand Up @@ -44,9 +44,11 @@ function validateMigrationStrategy(strategy) {
class PoolPlus extends Pool {
constructor(config) {
const plusOptions = config.plusOptions || {};
delete config.plusOptions;
validateMigrationStrategy(plusOptions.migrationStrategy);

super({config: new PoolConfig(config)});
const poolConfig = mysql.createPool(config).config;
super({config: poolConfig});
Comment on lines +50 to +51
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're trying to be less hacky, but this is going to create an extra, unused pool. This code doesn't need to change if you do the suggestion I'll post above.


this._allowAlterInProduction = plusOptions.allowAlterInProduction || false;
this._debug = plusOptions.debug || false;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/parseKeyParts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const {escapeId} = require('mysql/lib/protocol/SqlString');
const {escapeId} = require('sqlstring');

function parseKeyParts(keyParts) {
const columnNames = [];
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"query"
],
"dependencies": {
"mysql": "^2.18.1"
"mysql2": "^3.2.0",
"sqlstring": "^2.3.3"
},
"devDependencies": {
"@nwoltman/eslint-config": "~0.6.0",
Expand All @@ -38,12 +39,12 @@
"grunt": "^1.0.4",
"grunt-env": "^1.0.1",
"grunt-eslint": "^22.0.0",
"grunt-jsdoc-to-markdown": "^5.0.0",
"grunt-jsdoc-to-markdown": "^6.0.0",
"grunt-mocha-istanbul": "^5.0.2",
"grunt-mocha-test": "^0.13.3",
"istanbul": "^0.4.5",
"jit-grunt": "^0.10.0",
"mocha": "^7.0.1",
"mocha": "^10.2.0",
Comment on lines +42 to +47
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind undoing these? I know the devDepencies are out of date, but these sorts of changes should be done separately.

"semver": "^7.1.3",
"should": "~13.2.3",
"should-sinon": "0.0.6",
Expand Down
2 changes: 1 addition & 1 deletion tasks/createTestDB.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const mysql = require('mysql');
const mysql = require('mysql2');

module.exports = function(grunt) {
grunt.registerTask('createTestDB', 'Creates an empty test database', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Connection.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const config = require('../config');
const mysql = require('mysql');
const mysql = require('mysql2');
const sinon = require('sinon');

describe('Connection', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/MySQLPlus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const KeyDefinitions = require('../../lib/KeyDefinitions');
const MySQLPlus = require('../../lib/MySQLPlus');
const PoolPlus = require('../../lib/PoolPlus');

const mysql = require('mysql');
const mysql = require('mysql2');

describe('MySQLPlus', () => {

Expand Down
10 changes: 4 additions & 6 deletions test/unit/PoolPlus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* eslint-disable no-console, padding-line-between-statements */

const ColumnDefinitions = require('../../lib/ColumnDefinitions');
const Connection = require('mysql/lib/Connection');
const {Connection} = require('mysql2');
const KeyDefinitions = require('../../lib/KeyDefinitions');
const MySQLTable = require('../../lib/MySQLTable');
const Pool = require('mysql/lib/Pool');
const {Pool} = require('mysql2');
const PoolPlus = require('../../lib/PoolPlus');
const TableDefinition = require('../../lib/TableDefinition');

Expand Down Expand Up @@ -570,7 +570,6 @@ describe('PoolPlus', () => {
describe('when defining tables', () => {

var PoolStub;
var PoolConfigStub;
var MySQLTableStub;

var MockPool;
Expand All @@ -582,8 +581,8 @@ describe('PoolPlus', () => {

// Stub some classes
class MockClass {}
PoolStub = sinon.stub(require.cache[require.resolve('mysql/lib/Pool')], 'exports').value(MockClass);
PoolConfigStub = sinon.stub(require.cache[require.resolve('mysql/lib/PoolConfig')], 'exports').value(MockClass);
const mockFunction = options => options;
PoolStub = sinon.stub(require.cache[require.resolve('mysql2')], 'exports').value({Pool: MockClass, createPool: mockFunction});
MySQLTableStub = sinon.stub(require.cache[require.resolve('../../lib/MySQLTable')], 'exports').value(MockClass);
sinon.stub(require.cache[require.resolve('../../lib/TableDefinition')], 'exports');

Expand All @@ -601,7 +600,6 @@ describe('PoolPlus', () => {
after(() => {
// Restore stubs
PoolStub.restore();
PoolConfigStub.restore();
MySQLTableStub.restore();
require.cache[require.resolve('../../lib/TableDefinition')].exports.restore();
Map.prototype.has.restore();
Expand Down