升级到 v6
Sequelize v6 是继 v5 之后的下一个主要版本。以下是帮助您升级的一系列重大更改。
重大更改
支持 Node 10 及更高版本
Sequelize v6 仅支持 Node 10 及更高版本 #10821.
CLS
您现在应该使用 cls-hooked 包来支持 CLS。
const cls = require('cls-hooked');
const namespace = cls.createNamespace('....');
const Sequelize = require('sequelize');
Sequelize.useCLS(namespace);
数据库引擎支持
我们更新了最低支持的数据库引擎版本。使用旧版本的数据库引擎将显示 SEQUELIZE0006
弃用警告。请查看 发布页面 获取版本表。
Sequelize
- Bluebird 已被移除。现在所有方法都在内部使用 async/await。公共 API 现在返回原生 promise。感谢 Andy Edwards 完成了这项重构工作。
Sequelize.Promise
不再可用。sequelize.import
方法已被移除。CLI 用户应更新到sequelize-cli@6
。- 所有 QueryInterface 和 QueryGenerator 的实例都已重命名为其 lowerCamelCase 形式,例如在用作 Model 和 Dialect 上的属性名称时为 queryInterface 和 queryGenerator,类名保持不变。
模型
options.returning
选项 returning: true
将不再返回未在模型中定义的属性。可以通过使用 returning: ['*']
来实现旧的行为。
Model.changed()
Sequelize 不检测深度突变。为了避免 save
出现问题,您应该将每个属性视为不可变,并且只分配新值。
深度突变属性的示例
const instance = await MyModel.findOne();
// Sequelize will not detect this change
instance.jsonField.jsonProperty = 12345;
console.log(instance.changed()); // false
// You can workaround this by telling Sequelize the property changed:
instance.changed('jsonField', true);
console.log(instance.changed()); // true
如果您将每个属性视为不可变的示例
const instance = await MyModel.findOne();
// Sequelize will detect this change
instance.jsonField = {
...instance.jsonField,
jsonProperty: 12345,
};
console.log(instance.changed()); // true
Model.bulkCreate()
此方法现在抛出 Sequelize.AggregateError
而不是 Bluebird.AggregateError
。所有错误现在都作为 errors
键公开。
Model.upsert()
现在所有方言都支持原生 upsert。
const [instance, created] = await MyModel.upsert({});
此方法的签名已更改为 Promise<Model,boolean | null>
。第一个索引包含已插入的 instance
,第二个索引包含一个布尔值(或 null
),指示记录是创建还是更新。对于 SQLite/Postgres,created
值将始终为 null
。
- MySQL - 使用 ON DUPLICATE KEY UPDATE 实现
- PostgreSQL - 使用 ON CONFLICT DO UPDATE 实现
- SQLite - 使用 ON CONFLICT DO UPDATE 实现
- MSSQL - 使用 MERGE 语句实现
针对 Postgres 用户的说明: 如果 upsert 负载包含 PK 字段,则 PK 将用作冲突目标。否则,将选择第一个唯一约束作为冲突键。
QueryInterface
addConstraint
此方法现在只接受两个参数,tableName
和 options
。以前第二个参数可以是应用约束的列名列表,现在此列表必须作为 options.fields
属性传递。
变更日志
6.0.0-beta.7
- docs(associations): belongs to many create with through table
- docs(query-interface): fix broken links #12272
- docs(sequelize): omitNull only works for CREATE/UPDATE queries
- docs: asyncify #12297
- docs: responsive #12308
- docs: update feature request template
- feat(postgres): native upsert #12301
- feat(sequelize): allow passing dialectOptions.options from url #12404
- fix(include): check if attributes specified for included through model #12316
- fix(model.destroy): return 0 with truncate #12281
- fix(mssql): empty order array generates invalid FETCH statement #12261
- fix(postgres): parse enums correctly when describing a table #12409
- fix(query): ensure correct return signature for QueryTypes.RAW #12305
- fix(query): preserve cls context for logger #12328
- fix(query-generator): do not generate GROUP BY clause if options.group is empty #12343
- fix(reload): include default scope #12399
- fix(types): add Association into OrderItem type #12332
- fix(types): add clientMinMessages to Options interface #12375
- fix(types): transactionType in Options #12377
- fix(types): add support for optional values in "where" clauses #12337
- fix(types): add missing fields to 'FindOrCreateType' #12338
- fix: add missing sql and parameters properties to some query errors #12299
- fix: remove custom inspect #12262
- refactor: cleanup query generators #12304
6.0.0-beta.6
- docs(add-constraint): options.fields support
- docs(association): document uniqueKey for belongs to many #12166
- docs(association): options.through.where support
- docs(association): use and instead of 'a nd' #12191
- docs(association): use correct scope name #12204
- docs(manuals): avoid duplicate header ids #12201
- docs(model): correct syntax error in example code #12137
- docs(query-interface): removeIndex indexNameOrAttributes #11947
- docs(resources): add sequelize-guard library #12235
- docs(typescript): fix confusing comments #12226
- docs(v6-guide): bluebird removal API changes
- docs: database version support info #12168
- docs: remove remaining bluebird references #12167
- feat(belongs-to-many): allow creation of paranoid join tables #12088
- feat(belongs-to-many): get/has/count for paranoid join table #12256
- feat(pool): 暴露 maxUses 池配置选项 #12101
- feat(postgres): 限制包含别名最小化 #11940
- feat(sequelize): 处理查询字符串主机值 #12041
- fix(associations): 确保所有生成的属性具有正确的模式 #12258
- fix(docs/instances): 使用正确的变量进行增量 #12087
- fix(include): 分开的查询不是子查询 #12144
- fix(model): 修复 bulkCreate 中关联逻辑中的未链接 Promise #12163
- fix(model): updateOnDuplicate 处理复合键 #11984
- fix(model.count): 没有列的 distinct 生成无效的 SQL #11946
- fix(model.reload): 忽略 options.where 并始终使用 this.where() #12211
- fix(mssql) 插入记录失败,因为 BOOLEAN 列类型 #12090
- fix(mssql): 在查询生成器中强制转换 sql_variant #11994
- fix(mssql): 不要在没有返回的情况下使用 OUTPUT INSERTED 进行更新 #12260
- fix(mssql): FETCH/NEXT 查询中的重复排序 #12257
- fix(mssql): 为 float 设置正确的刻度 #11962
- fix(mssql): tedious v9 需要连接调用 #12182
- fix(mssql): 对引擎表和列使用大写 #12212
- fix(pool): 当引擎不支持时显示弃用警告 #12218
- fix(postgres): addColumn 支持 ARRAY(ENUM) #12259
- fix(query): 不要绑定整个单词中的 $ #12250
- fix(query-generator): 处理基于子字符串运算符的文字 #12210
- fix(query-interface): 允许为查询接口插入传递 null #11931
- fix(query-interface): 允许 sequelize.fn 和 sequelize.literal 在 IndexesOptions 的字段中 #12224
- fix(scope): 不要修改原始范围定义 #12207
- fix(sqlite): 多个主键会导致语法错误 #12237
- fix(sync): 将选项传递给所有查询方法 #12208
- fix(typings): 将 type_helpers 添加到文件列表 #12000
- fix(typings): 修正 Model.init 返回类型 #12148
- fix(typings): fn 可分配给 where #12040
- fix(typings): getForeignKeysForTables 参数定义 #12084
- fix(typings): 使 between 操作符接受日期范围 #12162
- refactor(ci): 改进数据库等待脚本 #12132
- refactor(tsd-test-setup): 添加 & 设置 dtslint #11879
- refactor: 将所有方言条件逻辑移到子类中 #12217
- refactor: 删除 sequelize.import 助手 #12175
- refactor: 使用原生版本 #12159
- refactor: 使用对象展开而不是 Object.assign #12213
6.0.0-beta.5
- fix(find-all): 在属性为空时抛出异常 #11867
- fix(types):
queryInterface.addIndex
#11844 - fix(types):
sequelize.query
中的plain
选项 #11596 - fix(types): 修正重载方法顺序 #11727
- fix(types):
Sequelize.where
的comparator
参数 #11843 - fix(types): 修复 BelongsToManyGetAssociationsMixinOptions #11818
- fix(types): 将
hooks
添加到CreateOptions
#11736 - fix(increment): 错误的查询 #11852
- fix(associations): 获取多对多关联,目标键不为主键 #11778
- fix: 正确选择 SRID(如果存在) #11763
- feat(sqlite): 为
options.storage
自动提供路径 #11853 - feat(postgres):
idle_in_transaction_session_timeout
连接选项 #11775 - feat(index): 改进以支持具有运算符的多个字段 #11934
- docs(transactions): 修复 addIndex 示例和语法 #11759
- docs(raw-queries): 删除过时的信息 #11833
- docs(optimistic-locking): 修复缺少的手册 #11850
- docs(model): findOne 为空结果的返回值 #11762
- docs(model-querying-basics.md): 添加一些逗号 #11891
- docs(manuals): 修复缺少的 models-definition 页面 #11838
- docs(manuals): 大量重写 #11825
- docs(dialect-specific): 添加 MSSQL 域身份验证示例 #11799
- docs(associations): 修复关联手册中的错字 #11888
- docs(associations): 修复错字 #11869
6.0.0-beta.4
- feat(sync): 允许在同步时使用 alter 启用绕过 drop 语句 #11708
- fix(model): 在包含的模型上注入 DependentVirtualAttrs #11713
- fix(model): 正确生成 ON CONFLICT ... DO UPDATE #11666
- fix(mssql): 优化 formatError RegEx #11725
- fix(types): 添加 getForeignKeyReferencesForTable 类型 #11738
- fix(types): 将“restore”钩子添加到类型 #11730
- fix(types): 将“fieldMaps”添加到 QueryOptions 类型定义 #11702
- fix(types): 将 isSoftDeleted 添加到 Model #11628
- fix(types): 修复 upsert 类型定义 #11674
- fix(types): 为字段中的 getter 和 setter 指定“this” #11648
- fix(types): 将 paranoid 添加到 UpdateOptions 接口 #11647
- fix(types): 在 IncludeThroughOptions 定义中包含“as” #11624
- fix(types): 将 Includeable 添加到 IncludeOptions.include 类型 #11622
- fix(types): 事务锁定 #11620
- fix(sequelize.fn): 转义美元符号 (#11533) #11606
- fix(types): 将 nested 添加到 Includeable #11354
- fix(types): 将 date 添加到 where #11612
- fix(types): 添加 getDatabaseName (#11431) #11614
- fix(types): beforeDestroy #11618
- fix(types): 查询接口表模式 #11582
- docs: README.md #11698
- docs(sequelize): 详细说明 options.retry 的用法 #11643
- docs: 澄清 Sequelize 构造函数中的 logging 选项 #11653
- docs(migrations): 修复示例中的语法错误 #11626
- docs: 描述 logging 选项 #11654
- docs(transaction): 修复错字 #11659
- docs(hooks): 添加有关 belongs-to-many 的信息 #11601
- docs(associations): 修复错字 #11592
6.0.0-beta.3
- feat: 支持 cls-hooked / 测试 #11584