You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/**
* Sync this Model to the DB, that is create the table. Upon success, the callback will be called with the model instance (this)
* @see {Sequelize#sync} for options
* @return {Promise<this>}
*/
Model.prototype.sync = function(options) {
options = options || {};
options.hooks = options.hooks === undefined ? true : !!options.hooks;
options = Utils._.extend({}, this.options, options);
var self = this
, attributes = this.tableAttributes;
return Promise.try(function () {
if (options.hooks) {
return self.runHooks('beforeSync', options);
}
}).then(function () {
if (options.force) {
return self.drop(options); // 这里啊亲!删除这张表哎呦喂!
}
})
问题
鉴于直接使用mysql包操作数据库带来的各种不方便,见 #43 ,我找到了sequelize,试用了一下,觉得还不错。
使用过程
1. 安装
sequelize支持如下数据库:MySQL, MariaDB, SQLite, PostgreSQL and MSSQL,不同的数据库需要安装各自的包,因为我操作的是mysql,所以要安装mysql包
2. 创建连接
#43 说过,每次操作都需要打开和关闭连接既耗费性能,也代码冗余。mysql包本身提供方法创建连接池。
sequelize创建连接池
3. 定义数据结构(也就是model啦)
4. 插入数据
上面的代码有几个地方需要说明。
我不单单想掺入数据,我还想更新,删除,各种查询怎么办?看这里。
5. 存在问题
The text was updated successfully, but these errors were encountered: