Socket
Socket
Sign inDemoInstall

mysql-import

Package Overview
Dependencies
11
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.1 to 1.0.5

.travis.yml

28

package.json
{
"author": {
"name": "Mark J Tyers, Rob Parham"
},
"author": "Rob Parham",
"bugs": {

@@ -10,8 +8,15 @@ "url": "https://github.com/pamblam/mysql-import/issues"

"dependencies": {
"jsonschema": "^1.1.0",
"mysql": "^2.10.2"
},
"deprecated": false,
"description": "import .sql files using NodeJS",
"devDependencies": {},
"description": "Import .sql into a MySQL database with Node.",
"devDependencies": {
"chai": "^4.1.2",
"coveralls": "^3.0.2",
"grunt": "^1.0.3",
"grunt-contrib-concat": "^1.0.1",
"grunt-string-replace": "^1.3.1",
"istanbul": "^0.4.5",
"mocha": "^5.2.0"
},
"engines": {

@@ -29,3 +34,3 @@ "node": ">5.0.0"

"license": "MIT",
"main": "index.js",
"main": "mysql-import.js",
"name": "mysql-import",

@@ -37,5 +42,8 @@ "repository": {

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "node_modules/grunt/bin/grunt",
"test": "node node_modules/.bin/mocha",
"coverage": "node node_modules/.bin/istanbul cover _mocha -- -R spec",
"coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls"
},
"version": "1.0.1"
}
"version": "1.0.5"
}
# 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.
*Version 1.0.5*
Thanks for your work, Mark.
[![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)
Import MySQL files with Node!
## Install

@@ -14,19 +17,48 @@ ```

## Introduction
## Usage
This is a node module that allows you to load sql queries from a text file and run them against your MySQL database. It supports ECMA6 promises and requires a recent version of NodeJS 5+ to work.
Include the package.
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 mysql_import = require('mysql-import');
```
const importer = require('mysql-import').config({
'host': 'localhost',
'user': 'testuser',
'password': 'testpwd',
'database': 'mydb'
});
`mysql-import` exposes two methods and `version` property. `mysql_import.version` is a string showing the current version of the package.
importer.import('mydb.sql').then(()=> {
console.log('all statements have been executed')
});
```
#### `config(Object settings)`
Prepare the package to communicate with your database and handle any errors. This method **must** be called before importing anything.
The `settings` object has 4 mandatory parameters and 1 optional parameter.
- `host` - (**mandatory**) The MySQL host to connect to.
- `user` - (**mandatory**) The MySQL user to connect with.
- `password` - (**mandatory**) The password for the user.
- `database` - (**mandatory**) The database to connect to.
- `onerror` - (**optional**) Function to handle errors. The function will receive the Error. If not provided the error will be thrown.
The `config` method returns the `mysql-import` object so it may be chained.
#### `import(String filename)`
Import a .sql file to the database.
The `import` method returns a Promise.
*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
require('mysql-import').config({
host: 'localhost',
user: 'testuser',
password: 'testpwd',
database: 'mydb',
onerror: err=>console.log(err.message)
}).import('mydb.sql').then(()=> {
console.log('all statements have been executed')
});
## 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.
Thanks for your work, Mark.
var config = {host: 'localhost', user: 'root', password: 'bijoux22', database: 'testdb'};
const expect = require('chai').expect;
const con = require('mysql').createConnection({host: config.host, user: config.user, password: config.password});
const importer = require('../index.js').config(config);
var passed = true;
const query = (sql, p=[]) => new Promise( done=> con.query(sql, p, (err, result)=>{ if (err) throw err; done(result); }));
var error_handler = err=>{
passed = false;
console.log("something went wrong: ", err.message)
};
var config = {
host: '127.0.0.1',
user: 'root',
password: '',
database: 'testdb',
onerror: error_handler
};
const con = require('mysql').createConnection({
host: config.host,
user: config.user,
password: config.password
});
const importer = require('../mysql-import.js').config(config);
const query = sql => new Promise(done=>{
con.query(sql, (err, result)=>{
if(err) error_handler(err);
else done(result);
});
});
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(()=>{
describe('All tests passed.', ()=>{
it('No errors thrown.', done=>{
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.`);
console.log("Creating test DB");
query("create database if not exists testdb").then(()=>query("use testdb")).then(()=>{
query("drop database testdb").then(()=>{
var time = new Date().getTime() - startTime;
console.log("test complete in "+time+"ms");
process.exit();
console.log("Importing test dump");
importer.import(__dirname+'/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");
expect(passed).to.be.true;
done();
process.exit();
});
});
});
});
});
});
});
}).timeout(0);
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc