mysql-import
Advanced tools
Comparing version 1.0.0 to 1.0.1
58
index.js
'use strict'; | ||
const mysql = require('mysql'); | ||
const fs = require('fs-promise'); | ||
const fs = require('fs'); | ||
const Validator = require('jsonschema').Validator; | ||
@@ -9,12 +9,11 @@ const v = new Validator(); | ||
const query = (sql, p=[]) => new Promise((resolve, reject)=> conn.query(sql, p, (err, result)=>{ if (err) reject(err); else resolve(result); })); | ||
const importer = { | ||
import: filename => { | ||
return new Promise( (resolve, reject) => { | ||
fs.readFile(filename, 'utf8').then(arraySplit).then(runQueries).then(()=>{ | ||
resolve('all tables created') | ||
}).catch( err => { | ||
reject(`error: ${err}`) | ||
}); | ||
}); | ||
var queriesString = fs.readFileSync(filename, 'utf8'); | ||
var queries = parseQueries(queriesString); | ||
return slowLoop(queries, (q,i,d)=>query(q).then(d)) // 47668ms | ||
//return Promise.all(queries.map(query)); // 48921ms | ||
}, | ||
@@ -30,3 +29,3 @@ | ||
if(!valid) throw new Error("Invalid host, user, password, or database parameters"); | ||
conn = data; | ||
conn = mysql.createConnection(data); | ||
return importer; | ||
@@ -39,20 +38,29 @@ } | ||
function arraySplit(str) { return new Promise(resolve=>resolve(parseQueries(str))); } | ||
function runQueries(arr) { | ||
let db = mysql.createConnection(conn); | ||
Promise.all( arr.map( item => { | ||
db.query(item, (err, rows) => { | ||
if (err) { | ||
throw 'ERROR: '+err | ||
} | ||
return 'ROWS: '+rows | ||
}) | ||
})).then( () => { | ||
console.log('DONE!') | ||
}, (e) => { | ||
console.log(`error: ${e}`) | ||
}) | ||
/** | ||
* Execute the loopBody function once for each item in the items array, | ||
* waiting for the done function (which is passed into the loopBody function) | ||
* to be called before proceeding to the next item in the array. | ||
* @param {Array} items - The array of items to iterate through | ||
* @param {Function} loopBody - A function to execute on each item in the array. | ||
* This function is passed 3 arguments - | ||
* 1. The item in the current iteration, | ||
* 2. The index of the item in the array, | ||
* 3. A function to be called when the iteration may continue. | ||
* @returns {Promise} - A promise that is resolved when all the items in the | ||
* in the array have been iterated through. | ||
*/ | ||
function slowLoop(items, loopBody) { | ||
return new Promise(f => { | ||
let done = arguments[2] || f; | ||
let idx = arguments[3] || 0; | ||
let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done; | ||
loopBody(items[idx], idx, cb); | ||
}); | ||
} | ||
/** | ||
* Split up the dump file into a bunch of seperate queries | ||
* @param {type} queriesString | ||
* @returns {Array|parseQueries.queries|nm$_index.parseQueries.queries} | ||
*/ | ||
function parseQueries(queriesString) { | ||
@@ -59,0 +67,0 @@ var quoteType = false; |
@@ -10,3 +10,2 @@ { | ||
"dependencies": { | ||
"fs-promise": "^0.5.0", | ||
"jsonschema": "^1.1.0", | ||
@@ -17,7 +16,3 @@ "mysql": "^2.10.2" | ||
"description": "import .sql files using NodeJS", | ||
"devDependencies": { | ||
"eslint": "^2.5.1", | ||
"eslint-plugin-jasmine": "^1.6.0", | ||
"eslint-plugin-node": "^1.0.0" | ||
}, | ||
"devDependencies": {}, | ||
"engines": { | ||
@@ -44,3 +39,3 @@ "node": ">5.0.0" | ||
}, | ||
"version": "1.0.0" | ||
"version": "1.0.1" | ||
} |
# Hello.... | ||
This is a fork of the node package [node-mysql-importer](https://www.npmjs.com/package/node-mysql-importer) originally created by [some European dude](https://github.com/marktyers/). I was using this as a dependency in another project and had a few issues. 1- it logged **everything** to the console, including my passwords and sensitive info and 2- if there're any semicolons in the datatabse all hell breaks loose. I left an issue on his repo and he promptly deleted (or hid) the repo, so I fixed and will maintain my own copy. Thanks for your work, Mark. | ||
# mysql-import | ||
This is a fork of the node package [node-mysql-importer](https://www.npmjs.com/package/node-mysql-importer) originally created by [some European dude](https://github.com/marktyers/). I was using this as a dependency in another project and had a few issues. I left an issue on his repo and he promptly deleted (or hid) the repo, so I fixed it myself and will maintain my own copy. This one has a much more robust pre-parser, and is almost entirely re-written. | ||
Thanks for your work, Mark. | ||
## Install | ||
@@ -15,8 +18,6 @@ ``` | ||
Here is an example of usage. Note that each query in the text file must terminate with a semicolon followed by a newline or the end of the file(;). | ||
Here is an example of usage. Note that each query in the text file must terminate with an unquoted semicolon (;) followed by a newline or the end of the file. | ||
``` | ||
const importer = require('mysql-import') | ||
importer.config({ | ||
const importer = require('mysql-import').config({ | ||
'host': 'localhost', | ||
@@ -26,9 +27,7 @@ 'user': 'testuser', | ||
'database': 'mydb' | ||
}) | ||
}); | ||
importer.importSQL('mydb.sql').then( () => { | ||
importer.import('mydb.sql').then(()=> { | ||
console.log('all statements have been executed') | ||
}).catch( err => { | ||
console.log(`error: ${err}`) | ||
}) | ||
}); | ||
``` |
var config = {host: 'localhost', user: 'root', password: 'password', database: 'testdb'}; | ||
var config = {host: 'localhost', user: 'root', password: 'bijoux22', database: 'testdb'}; | ||
@@ -9,5 +9,28 @@ const con = require('mysql').createConnection({host: config.host, user: config.user, password: config.password}); | ||
query("create database testdb").then(()=>query("use testdb")).then(()=>{ | ||
importer.import('test.sql').then(console.log('farts')); | ||
var startTime = new Date().getTime(); | ||
console.log("Creating test DB"); | ||
query("create database if not exists testdb").then(()=>query("use testdb")).then(()=>{ | ||
console.log("Importing test dump"); | ||
importer.import('test.sql').then(()=>{ | ||
query("select * from importtest").then(res=>{ | ||
console.log(`${res.length} rows inputted.`); | ||
query("select * from importtest where doc like \"%;%\"").then(res=>{ | ||
console.log(`There are ${res.length} entries with a semicolon.`); | ||
query("drop database testdb").then(()=>{ | ||
var time = new Date().getTime() - startTime; | ||
console.log("test complete in "+time+"ms"); | ||
process.exit(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
128069
2
0
116
5
31
- Removedfs-promise@^0.5.0
- Removedany-promise@1.3.0(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedfs-extra@0.26.7(transitive)
- Removedfs-promise@0.5.0(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedinflight@1.0.6(transitive)
- Removedjsonfile@2.4.0(transitive)
- Removedklaw@1.3.1(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedmz@2.7.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedrimraf@2.7.1(transitive)
- Removedthenify@3.3.1(transitive)
- Removedthenify-all@1.6.0(transitive)
- Removedwrappy@1.0.2(transitive)