-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c5109a
commit 33bf23c
Showing
7 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ test | |
.vscode | ||
.nyc_output | ||
coverage | ||
examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Examples | ||
================== | ||
|
||
These examples run against the [test_db](https://github.com/datacharmer/test_db) dataset. It expects the database to be named `employees` and expects the root password to be `password`. | ||
|
||
To run a docker container with this data and bound to the correct port, you can run the following: | ||
|
||
``` | ||
docker run -p 3306:3306 --name mysql_container -e MYSQL_ROOT_PASSWORD=password -d genschsa/mysql-employees | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const mysql = require('../../index.js'); | ||
const bluebird = require('bluebird'); | ||
|
||
async function runReturn() { | ||
const connection = await mysql.createConnection({ | ||
user: 'root', | ||
password: 'password', | ||
database: 'employees', | ||
mysqlWrapper: (mysqlInstance) => { | ||
return wrapMysql(mysqlInstance, 'runReturn'); | ||
} | ||
}); | ||
|
||
connection.end(); | ||
} | ||
|
||
async function runPromise() { | ||
const connection = await mysql.createConnection({ | ||
user: 'root', | ||
password: 'password', | ||
database: 'employees', | ||
mysqlWrapper: (mysqlInstance) => { | ||
return bluebird.resolve(wrapMysql(mysqlInstance, 'runPromise')); | ||
} | ||
}); | ||
|
||
connection.end(); | ||
} | ||
|
||
async function runCallback() { | ||
const connection = await mysql.createConnection({ | ||
user: 'root', | ||
password: 'password', | ||
database: 'employees', | ||
mysqlWrapper: (mysqlInstance, callback) => { | ||
callback(null, wrapMysql(mysqlInstance, 'runCallback')); | ||
} | ||
}); | ||
|
||
connection.end(); | ||
} | ||
|
||
runReturn(); | ||
runPromise(); | ||
runCallback(); | ||
|
||
|
||
function wrapMysql(mysql, fnName) { | ||
const createConnection = mysql.createConnection; | ||
|
||
mysql.createConnection = function () { | ||
console.log(`createConnection called in ${fnName}!`); | ||
|
||
mysql.createConnection = createConnection; | ||
|
||
return createConnection(...arguments); | ||
} | ||
|
||
return mysql; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const mysql = require('../../index.js'); | ||
|
||
function run() { | ||
let connection; | ||
|
||
mysql.createConnection({ | ||
user: 'root', | ||
password: 'password', | ||
database: 'employees' | ||
}).then((conn) => { | ||
connection = conn; | ||
|
||
return connection.query('select * from employees limit 0, 10'); | ||
}).then((employees) => { | ||
employees.forEach(employee => { | ||
console.log(`The employee with the employee number ${employee.emp_no} is ${employee.first_name} ${employee.last_name}`); | ||
}); | ||
|
||
connection.end(); | ||
}); | ||
} | ||
|
||
async function runAwait() { | ||
const connection = await mysql.createConnection({ | ||
user: 'root', | ||
password: 'password', | ||
database: 'employees' | ||
}); | ||
|
||
const employees = await connection.query('select * from employees limit 0, 10'); | ||
|
||
employees.forEach(employee => { | ||
console.log(`The employee with the employee number ${employee.emp_no} is ${employee.first_name} ${employee.last_name}`); | ||
}); | ||
|
||
connection.end(); | ||
} | ||
|
||
run(); | ||
runAwait(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const mysql = require('../../index.js'); | ||
|
||
function run() { | ||
mysql.createConnection({ | ||
user: 'root', | ||
password: 'password', | ||
database: 'employees' | ||
}).then((connection) => { | ||
const employees = connection.queryStream('select * from employees limit 0, 10'); | ||
|
||
employees.on('result', (employee) => { | ||
console.log(`The employee with the employee number ${employee.emp_no} is ${employee.first_name} ${employee.last_name}`); | ||
}); | ||
|
||
employees.on('end', () => { | ||
connection.end(); | ||
}) | ||
}); | ||
} | ||
|
||
async function runAwait() { | ||
const connection = await mysql.createConnection({ | ||
user: 'root', | ||
password: 'password', | ||
database: 'employees' | ||
}); | ||
|
||
const employees = connection.queryStream('select * from employees limit 0, 10'); | ||
|
||
employees.on('result', (employee) => { | ||
console.log(`The employee with the employee number ${employee.emp_no} is ${employee.first_name} ${employee.last_name}`); | ||
}); | ||
|
||
employees.on('end', () => { | ||
connection.end(); | ||
}) | ||
} | ||
|
||
run(); | ||
runAwait(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const mysql = require('../../index.js'); | ||
|
||
function run() { | ||
let connection; | ||
|
||
mysql.createConnection({ | ||
user: 'root', | ||
password: 'password', | ||
database: 'employees', | ||
returnArgumentsArray: true | ||
}).then((conn) => { | ||
connection = conn; | ||
|
||
return connection.query('select * from employees limit 0, 10'); | ||
}).then(([data, fields, query]) => { | ||
console.log(`The SQL for the query was: ${query.sql}\n`); | ||
|
||
console.log(`The table 'employees' contains the following fields:`) | ||
fields.forEach(field => { | ||
console.log(` ${field.name}`); | ||
}) | ||
|
||
console.log('\nThe data retrieved was:') | ||
|
||
data.forEach(employee => { | ||
console.log(` Employee number ${employee.emp_no}: ${employee.first_name} ${employee.last_name}`); | ||
}); | ||
|
||
connection.end(); | ||
}); | ||
} | ||
|
||
async function runAwait() { | ||
const connection = await mysql.createConnection({ | ||
user: 'root', | ||
password: 'password', | ||
database: 'employees', | ||
returnArgumentsArray: true | ||
}); | ||
|
||
const [data, fields, query] = await connection.query('select * from employees limit 0, 10'); | ||
|
||
console.log(`The SQL for the query was: ${query.sql}\n`); | ||
|
||
console.log(`The table 'employees' contains the following fields:`) | ||
fields.forEach(field => { | ||
console.log(` ${field.name}`); | ||
}) | ||
|
||
console.log('\nThe data retrieved was:') | ||
|
||
data.forEach(employee => { | ||
console.log(` Employee number ${employee.emp_no}: ${employee.first_name} ${employee.last_name}`); | ||
}); | ||
|
||
connection.end(); | ||
} | ||
|
||
run(); | ||
runAwait(); |