代码
require('dotenv/config');
const Sequelize = require('sequelize');
const connection = new Sequelize(process.env.DATABASE, process.env.USER, process.env.PASSWORD, {
host: process.env.HOST,
dialect: 'mysql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
});
var Article = connection.define('article', {
title: Sequelize.STRING,
body: Sequelize.TEXT
});
connection.sync()
.then(function () {
// Article.create({
// title: 'demo title',
// body: 'Blah'
// });
Article.findById(1).then(function (article) {
console.log(article.dataValues);
});
});
// .then(function () { process.exit(); });
的console.log
PS C:\code\basic-sequelize> npm start
> basic-sequelize@1.0.0 start C:\code\basic-sequelize
> node app.js
Executing (default): CREATE TABLE IF NOT EXISTS `articles` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255), `body` TEXT,
`createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `articles`
Executing (default): SELECT `id`, `title`, `body`, `createdAt`, `updatedAt` FROM `articles` AS `article` WHERE `article`.`id` = 1;
{ id: 1,
title: 'demo title',
body: 'Blah',
createdAt: 2018-06-12T17:07:21.000Z,
updatedAt: 2018-06-12T17:07:21.000Z }
答案 0 :(得分:1)
这些是来自Sequelize的电话。如果您愿意,可以使用logging: false,
const connection = new Sequelize(process.env.DATABASE, process.env.USER, process.env.PASSWORD, {
host: process.env.HOST,
dialect: 'mysql',
operatorsAliases: false,
logging: false, // <--- Disable logging
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
});
答案 1 :(得分:1)
这是Sequelize的预期行为。
您是通过.sync()
来电询问的。
来自docs:
在开始一个新项目时,你不会拥有数据库结构并使用你不需要的Sequelize。只需指定您的模型结构,让库完成其余的工作。目前支持的是创建和删除表。