1

I am developing a web service with Node.js,but I will work with mysql, I used to use mongoDB and I could create an Schema:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/webservice', function(err) {
  if (err) {
      throw err;
  } else {
      console.log('Connected');
  }
});

var UserSchema = mongoose.Schema({
   name: String,
   email: String,
   city: String,
   age: String
 });


var User = mongoose.model('users', UserSchema);
module.exports = User;

But with Mysql? I have the next code:

var mysql = require('mysql');

var connection = mysql.createConnection({
  host : 'localhost',
  user: 'root',
  password:''
});

connection.connect(function(err){
   if(err){
      console.log(err);
   }
  console.log("Connected...");  
 });

Thanks :D

3
  • Well you can't use Mongoose to create a MySQl Schema if that's what you're asking. You'll need some other library. Commented Jul 10, 2015 at 4:37
  • yeah the modules are different, but with module mysql there any way to create a Schema ? Commented Jul 10, 2015 at 4:43
  • 1
    Check out docs.sequelizejs.com/en/latest Commented Jul 10, 2015 at 5:20

1 Answer 1

4

Unlike mongoDB where Collections are created if they don't exist, you have to manually create tables in MySQL. You can create a table for your users with the following query:

CREATE TABLE NOT EXISTS Users (id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR(30),email VARCHAR(255),city VARCHAR(50),age INT);

connection.query('CREATE TABLE NOT EXISTS Users (id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR(30),email VARCHAR(255),city VARCHAR(50),age INT);', function(err) {
  if (err) throw err;
  console.log('Users TABLE created.');
});
Sign up to request clarification or add additional context in comments.

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.