Nibha Jain
Nibha Jain

Reputation: 8141

create stored procedure in mysql from node.js

In my project I have to a configure database as soon as registration complete. I have a dump.sql file which I am able to import and create all the required tables from .sql file using this library but my stored procedures are not getting imported in my database. It is a big procedure. Is there a way to create a stored procedure from node.js. I tried this but getting error. Any help would be greatly appreciated.

TypeError: StoredProcedures is not a constructor

Upvotes: 1

Views: 1193

Answers (2)

Nibha Jain
Nibha Jain

Reputation: 8141

I got the solution of my this query and answering it so that could help anyone else in future. I am importing my sp.sql file using require('require-sql'); Then replacing \n\r with space. It works and create stored procedure in respective database.

 var query = require(path.join(_dirname, '/app/helper/sql/sp_distribute_all_data.sql'));
        try {
            conn(dbname).query(query.replace(/\r?\n|\r/g, " "), function (err, result) {
                if (err) {
                    console.log("ERROR:::", err);
                    resolve({
                        status: false,
                        feedback: err
                    });
                } else {
                    console.log("RESULT:::", result);
                    resolve({
                        status: true,
                        companyId: companyId,
                        feedback: 'Company configured successfully !!'
                    });
                }

            });
        } catch (error) {
            var stack = error.stack;

            console.log(stack);
        }

Upvotes: 1

matcheek
matcheek

Reputation: 5147

Beauty of OSS is that anything can be published. A big thanks to the authors of the mysql-import but nothing can change the fact that it has got 840 weekly downloads. While there is nothing wrong with 840 downloads per week you are essentially a tester for the package. Which might be just fine depending on your goals and time constraints.

To answer your question, standard way in MySQL to dump and restore databases is the mysqldump tool. A tool used by a lot more people than mysql-import.

You could create external processes directly from node to manage that with the means of child_process.exec

Then you could execute your dump and restore commands in a way similar to the below:

const exec = require('child_process').exec;
exec('mysqldump -u root -p dbname > dump.sql');

Upvotes: 1

Related Questions