这是我的代码:
models/index.js
:
var Sequelize = require('sequelize');
// initialize database connection
var sequelize = new Sequelize('somedb', '', '', {
host: 'localhost',
dialect: 'sqlite',
pool:{
max: 5,
min: 0,
idle: 10000
},
storage: "db/main.db"
});
// load models
var models = [
'Users',"Clients"
];
models.forEach(function(model) {
module.exports[model] = sequelize.import(__dirname + '/' + model);
module.exports[model].sync();
});
// export connection
module.exports.sequelize = sequelize;
app.js
:
SQLized.sync().then(function(err){
//Settings Environmental Variables
var env = process.env.STAGE;
var config = require("./config.js")[env];
...
});
models/Users.js
:
var Sequelize = require("sequelize");
var hash = require("../modules/util/hash");
module.exports=function(sequelize, DataTypes){
return Users = sequelize.define("Users", {
id: {
type: DataTypes.INTEGER,
field: "id",
autoIncrement: !0,
primaryKey: !0
},
firstName: {
type: DataTypes.STRING,
field: "first_name"
},
lastName: {
type: DataTypes.STRING,
field: "last_name"
},
username: {
type: DataTypes.STRING,
field: "username"
},
email: {
type: DataTypes.STRING,
field: "email"
},
password: {
type: DataTypes.STRING,
field: "password"
},
token: {
type: DataTypes.STRING,
field: "access_token"
},
isAdmin: {
type: DataTypes.BOOLEAN,
field: "isAdmin"
}
}, {
freezeTableName: true, // Model tableName will be the same as the model name
classMethods:{
usernameExists: function(username, _clb){
},
userExists: function(username, _clb){
},
signup: function(params, _clb){
var fullnm_splt = params.fullname.split(' ');
params.firstName = (fullnm_splt.length >2) ? fullnm_splt.slice(0, -1).join(" ") : fullnm_splt[0];
params.lastName = (fullnm_splt.length >2) ? fullnm_splt.slice(-1).join(" ") : fullnm_splt[1];
res.redirect("/users/" + params.username);
}
},
instanceMethods:{
isValidPassword: function(password, _clb){
var _self = this;
hash(password, function(err, password_encr){
if(err){
return _clb(err);
}
if(password_encr == _self.password){
return _clb(null, true);
}
return _clb(null, false);
});
}
}
});
};
models/Clients.js
var Sequelize = require("sequelize");
module.exports=function(sequelize, DataTypes){
return Clients = sequelize.define("Clients", {
id:{
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
client_name: {
type: DataTypes.STRING,
field: "client_name"
},
client_host_name: {
type: DataTypes.STRING,
field: "client_host_name"
},
ip: {
type: DataTypes.STRING,
field: "ip"
},
mac: {
type: DataTypes.STRING,
field: "mac"
},
previous_ip: {
type: DataTypes.STRING,
field: "previous_ip"
}
}, {
freezeTableName: true, // Model tableName will be the same as the model name
classMethods:{
},
instanceMethods:{
}
});
};
当我尝试使用下面的函数将某些内容保存到DB时,它会在日志中指出INSERT INTO
查询,但db文件中没有任何更改。
create_client
:
Clients
.create({client_host_name: params.client_host_name, ip: params.ip, mac: params.mac})
.then(function(err){
res.send("OK");
});
我该如何解决这个问题?
答案 0 :(得分:-1)
我已经测试过您的应用,所有内容都可以插入https://github.com/finfort/SequelizeTestRepo