mysql-import
Advanced tools
Comparing version 1.0.5 to 1.0.7
/** | ||
* mysql-import - v1.0.5 | ||
* mysql-import - v1.0.7 | ||
* Import .sql into a MySQL database with Node. | ||
@@ -13,10 +13,9 @@ * @author Rob Parham | ||
const fs = require('fs'); | ||
var conn, err_handler; | ||
const importer = { | ||
version: '1.0.5', | ||
import: filename => { | ||
class importer{ | ||
constructor(conn, err_handler){ | ||
this.conn = conn; | ||
this.err_handler = err_handler; | ||
} | ||
import(filename){ | ||
var queriesString = fs.readFileSync(filename, 'utf8'); | ||
@@ -28,5 +27,5 @@ | ||
try{ | ||
conn.query(q, err=>{ | ||
this.conn.query(q, err=>{ | ||
/* istanbul ignore next */ | ||
if (err) err_handler(err); | ||
if (err) this.err_handler(err); | ||
else d(); | ||
@@ -36,30 +35,27 @@ }); | ||
/* istanbul ignore next */ | ||
err_handler(err); | ||
this.err_handler(e); | ||
} | ||
}); | ||
}, | ||
config: settings => { | ||
const valid = settings.hasOwnProperty('host') && typeof settings.host === "string" && | ||
settings.hasOwnProperty('user') && typeof settings.user === "string" && | ||
settings.hasOwnProperty('password') && typeof settings.password === "string" && | ||
settings.hasOwnProperty('database') && typeof settings.database === "string"; | ||
/* istanbul ignore next */ | ||
if(!settings.hasOwnProperty("onerror") || typeof settings.onerror !== "function"){ | ||
settings.onerror = err=>{ throw err }; | ||
} | ||
err_handler = settings.onerror; | ||
/* istanbul ignore next */ | ||
if(!valid) return settings.onerror(new Error("Invalid host, user, password, or database parameters")); | ||
conn = mysql.createConnection(settings); | ||
return importer; | ||
} | ||
} | ||
importer.version = '1.0.7'; | ||
importer.config = function(settings){ | ||
const valid = settings.hasOwnProperty('host') && typeof settings.host === "string" && | ||
settings.hasOwnProperty('user') && typeof settings.user === "string" && | ||
settings.hasOwnProperty('password') && typeof settings.password === "string" && | ||
settings.hasOwnProperty('database') && typeof settings.database === "string"; | ||
/* istanbul ignore next */ | ||
if(!settings.hasOwnProperty("onerror") || typeof settings.onerror !== "function"){ | ||
settings.onerror = err=>{ throw err }; | ||
} | ||
var err_handler = settings.onerror; | ||
/* istanbul ignore next */ | ||
if(!valid) return settings.onerror(new Error("Invalid host, user, password, or database parameters")); | ||
var conn = mysql.createConnection(settings); | ||
return new importer(conn, err_handler); | ||
}; | ||
@@ -66,0 +62,0 @@ |
@@ -45,3 +45,3 @@ { | ||
}, | ||
"version": "1.0.5" | ||
"version": "1.0.7" | ||
} |
@@ -6,3 +6,3 @@ | ||
*Version 1.0.5* | ||
*Version 1.0.7* | ||
@@ -24,5 +24,5 @@ [![Build Status](https://api.travis-ci.org/Pamblam/mysql-import.svg?branch=master)](https://travis-ci.org/Pamblam/mysql-import/) [![Coverage Status](https://coveralls.io/repos/github/Pamblam/mysql-import/badge.svg?branch=master)](https://coveralls.io/github/Pamblam/mysql-import?branch=master) | ||
`mysql-import` exposes two methods and `version` property. `mysql_import.version` is a string showing the current version of the package. | ||
`mysql-import` exposes one methods and `version` property. `mysql_import.version` is a string showing the current version of the package. | ||
#### `config(Object settings)` | ||
#### `mysql-import.config(Object settings)` | ||
@@ -39,14 +39,16 @@ Prepare the package to communicate with your database and handle any errors. This method **must** be called before importing anything. | ||
The `config` method returns the `mysql-import` object so it may be chained. | ||
The `config` method returns a new `importer` instance. | ||
#### `import(String filename)` | ||
#### `importer.import(String filename)` | ||
Import a .sql file to the database. | ||
Import an `.sql` file to the database. | ||
The `import` method returns a Promise. | ||
The `import` method returns a Promise which is resolved when (and if) the file has completely imported. This promise is never rejected, if there is an error, the `onerror` function passed to the `config` method is called with the error object passed into it. | ||
*Note that each query in the text file must terminate with an unquoted semicolon (;) followed by a newline or the end of the file.* | ||
#### Example | ||
#### Examples | ||
Do it in a single call if you only need to import a single file to a single DB: | ||
require('mysql-import').config({ | ||
@@ -62,6 +64,30 @@ host: 'localhost', | ||
If you need to import to more than one database: | ||
const mysql_import = require('mysql-import'); | ||
var importer1 = mysql_import.config({ | ||
host: 'localhost', | ||
user: 'testuser', | ||
password: 'testpwd', | ||
database: 'mydb', | ||
onerror: err=>console.log(err.message) | ||
}); | ||
var importer2 = mysql_import.config({ | ||
host: 'localhost', | ||
user: 'testuser', | ||
password: 'testpwd', | ||
database: 'mydb2', | ||
onerror: err=>console.log(err.message) | ||
}); | ||
importer.import('mydb.sql').then(()=> { | ||
console.log('DB1 has finished importing') | ||
}); | ||
importer2.import('mydb2.sql').then(()=> { | ||
console.log('DB2 has finished importing') | ||
}); | ||
## Credit where credit is due | ||
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 (namely, semicolons in the import data causing errors). 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. | ||
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 (namely, semicolons in the import data causing errors). 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. |
@@ -5,2 +5,1 @@ 'use strict'; | ||
const fs = require('fs'); | ||
var conn, err_handler; |
@@ -1,7 +0,7 @@ | ||
const importer = { | ||
version: '{{ VERSION }}', | ||
import: filename => { | ||
class importer{ | ||
constructor(conn, err_handler){ | ||
this.conn = conn; | ||
this.err_handler = err_handler; | ||
} | ||
import(filename){ | ||
var queriesString = fs.readFileSync(filename, 'utf8'); | ||
@@ -13,5 +13,5 @@ | ||
try{ | ||
conn.query(q, err=>{ | ||
this.conn.query(q, err=>{ | ||
/* istanbul ignore next */ | ||
if (err) err_handler(err); | ||
if (err) this.err_handler(err); | ||
else d(); | ||
@@ -21,32 +21,29 @@ }); | ||
/* istanbul ignore next */ | ||
err_handler(err); | ||
this.err_handler(e); | ||
} | ||
}); | ||
}, | ||
config: settings => { | ||
const valid = settings.hasOwnProperty('host') && typeof settings.host === "string" && | ||
settings.hasOwnProperty('user') && typeof settings.user === "string" && | ||
settings.hasOwnProperty('password') && typeof settings.password === "string" && | ||
settings.hasOwnProperty('database') && typeof settings.database === "string"; | ||
/* istanbul ignore next */ | ||
if(!settings.hasOwnProperty("onerror") || typeof settings.onerror !== "function"){ | ||
settings.onerror = err=>{ throw err }; | ||
} | ||
err_handler = settings.onerror; | ||
/* istanbul ignore next */ | ||
if(!valid) return settings.onerror(new Error("Invalid host, user, password, or database parameters")); | ||
conn = mysql.createConnection(settings); | ||
return importer; | ||
} | ||
} | ||
importer.version = '{{ VERSION }}'; | ||
importer.config = function(settings){ | ||
const valid = settings.hasOwnProperty('host') && typeof settings.host === "string" && | ||
settings.hasOwnProperty('user') && typeof settings.user === "string" && | ||
settings.hasOwnProperty('password') && typeof settings.password === "string" && | ||
settings.hasOwnProperty('database') && typeof settings.database === "string"; | ||
/* istanbul ignore next */ | ||
if(!settings.hasOwnProperty("onerror") || typeof settings.onerror !== "function"){ | ||
settings.onerror = err=>{ throw err }; | ||
} | ||
var err_handler = settings.onerror; | ||
/* istanbul ignore next */ | ||
if(!valid) return settings.onerror(new Error("Invalid host, user, password, or database parameters")); | ||
var conn = mysql.createConnection(settings); | ||
return new importer(conn, err_handler); | ||
}; | ||
module.exports = importer; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
90
2
0
138309
12
340