create stored procedure in mysql from node.js

筅森魡賤 提交于 2020-06-18 02:50:20

问题


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

回答1:


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');



回答2:


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);
        }


来源:https://stackoverflow.com/questions/57738442/create-stored-procedure-in-mysql-from-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!