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
1 Answer
Reset to default 4Unlike 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
评论列表(0条)