javascript - Mysql Schema in Node.js? - Stack Overflow

I am developing a web service with Node.js,but I will work with mysql, I used to use mongoDB and I coul

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

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

Share Improve this question edited Jul 10, 2015 at 5:17 Blakes Seven 50.5k14 gold badges131 silver badges136 bronze badges asked Jul 10, 2015 at 4:32 LagulLagul 1991 gold badge6 silver badges19 bronze badges 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. – Bidhan Commented Jul 10, 2015 at 4:37
  • yeah the modules are different, but with module mysql there any way to create a Schema ? – Lagul Commented Jul 10, 2015 at 4:43
  • 1 Check out docs.sequelizejs./en/latest – Ananth Commented Jul 10, 2015 at 5:20
Add a ment  | 

1 Answer 1

Reset to default 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.');
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744784307a4593529.html

相关推荐

  • javascript - Mysql Schema in Node.js? - Stack Overflow

    I am developing a web service with Node.js,but I will work with mysql, I used to use mongoDB and I coul

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信