0

I am trying to convert my MongoDB database of MERN project to MySQL Database. Can someone please help me with the steps on how to change the steps in my project backend. I stored cors.js and env.js in utils folder and below are the code : env.js

module.exports = {
    mongo: 'mongodb+srv://admin:[email protected]/sp?retryWrites=true'
}

cors.js

module.exports = function(req, res, next) { 
    res.header("Access-Control-Allow-Origin", "*");
     res.header('Access-Control-Allow-Methods', 'DELETE, PUT, GET, POST'); 
     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
     res.header("Access-Control-Allow-Headers","*")
     next();
    }

connection.js

const url = require('../utils/env');
const mongoose = require('mongoose');
mongoose.connect(url.mongo);

mongoose.connection.on('open',()=>{
    console.log("connected to database");
})

module.exports = mongoose;

userschema.js

const mongoose = require('../connection');

const Schema = mongoose.Schema;

const UserSchema = new Schema ({
    username: {type: String,required: true,unique: true},
    email: {type: String, required: true,unique:true},
    password: {type: String, required: true},
})

const userModel = mongoose.model('user',UserSchema);

module.exports = userModel;

Please help me with any useful links or anything related articles for understanding as I am trying to learn as a beginner.

2
  • 1
    do you mean nodejs application? reactjs is a frontend library Commented Mar 9, 2021 at 9:53
  • Yes, for my node application, sorry for the error ! Edited out now Commented Mar 9, 2021 at 20:26

2 Answers 2

1

You can actually set up a very similar connection using Sequelize. If you are using MySQL, you would most likely also install mysql2 with npm. (You can also use Sequelize with something like POSTGRES, sqlite3, MariaDB, and a few others.)

Also, I would suggest a connection pool, rather than creating multiple instances. This will help to minimize some of the overhead. Here are a few links to help with all that I mentioned above:

Getting Started with Sequelize & Connecting to a Database: https://sequelize.org/master/manual/getting-started.html

Connection Pool: https://sequelize.org/master/manual/connection-pool.html Sequelize constructor API Reference: https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor

Or you can find more at the https://sequelize.org site about Models, and other important concepts about using the Sequelize ORM for MySQL.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use Sequelize ORM https://sequelize.org/ , it is similar to mongoose but it allows you to use and manipulate SQL dbs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.