4

I am using ( SequelizeJs + NodeJs + Mysql ) in my project
Whenever I start the project I want to check the database exists or not and then if it's not there I want to create a new one.
I tried this:

const mysql        = require('mysql2');

let mysqlCon = mysql.createConnection({
    host    :config.host,
    user    :config.username,
    password:config.password
});

mysqlCon.connect(function(err) {

    //Check Database
    mysqlCon.query('SHOW DATABASES LIKE ' + config.database,
        function(err, result) {
            if(err) {

                //Create new Database
                mysqlCon.query(
                    'CREATE DATABASE ' + config.database,
                    function(err, result) {
                        if(!err){

                            //Sync sequelize js model files
                            models.sequelize.sync().then(() => {
                                console.log('Database connected successfully!');
                            }).catch((err) => {
                                console.log(err, 'Something went wrong with the Database!');
                            });

                        }
                    });
            }
        });
    if(err) {
        console.log(err.message);

    } else {
        console.log('Connected!');
    }
});

I am getting this error:

sqlMessage: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'pixljobs\' at line 1' } undefined
Suresh Pattu
  • 6,083
  • 16
  • 59
  • 91

3 Answers3

4

use following command to create "CREATE DATABASE IF NOT EXISTS DBName;"

Kumaresan
  • 41
  • 3
3

Perhaps, a different approach would be to use Sequelize migrations.

Check out the docs here: https://sequelize.org/master/manual/migrations.html

Once you have installed sequelize-cli, you init the project, add the migrations to initialise the DB schema (tables, indexes, etc) and then run the command db:create, and after it run db:migrate.

A migration file consist of two parts: the "up" function, is what you are applying to the DB, and the "down" function, what you need to rollback. It looks something like this:

module.exports = {
  up: (queryInterface, Sequelize) => {
    // logic for transforming into the new state
  },
  down: (queryInterface, Sequelize) => {
    // logic for reverting the changes
  }
}

The advantage of this approach is that you have a way to initialise different environments in an automated, scalable way.

Finally, to answer your question, you just need to make sure you run sequelize-cli db:create and sequelize-cli db:migrate on every environment you want to run your project to make sure the DB has the right schema. If you are using sequelize-cli > v5.2.0, db:create will create the DB if it does not exist, and will use an existing DB if it was already created (behind the scenes it run the command Kumaresan showed you).

Hope this helps!

germanio
  • 861
  • 1
  • 17
  • 27
0

In 2021 Sequelize hasn't implemented db:create for mariadb if you're facing the same issue, the workaround I've found was osifo's answer here: https://stackoverflow.com/a/46694727/11940283.

In my case I just created another file called create-database.js and I run this file with node create-database.js before running the migrations like: create-node database.js && npx sequelize-cli db:migrate but I think a better solution is to place this code in the first migration file before the actual migration code and just run the migrations normally.

Stefane
  • 11
  • 1