Sequelize Docs 中文版

此项目同步自 sequelize / sequelize 项目.
由于v7现阶段处于alpha阶段, 变更较为频繁, 生产环境建议使用v6
Sequelize 是一个易用且基于 promise 的 Node.js ORM 工具 适用于 Postgres, MySQL, MariaDB, SQLite, Microsoft SQL Server, Amazon Redshift 和 Snowflake’s Data Cloud. 它具有强大的事务支持, 关联关系, 预读和延迟加载,读取复制等功能.
Sequelize 遵从 语义版本控制 和 官方 Node.js LTS 版本. Sequelize v7 版本正式支持 Node.js >=18.0.0.
你目前正在查看 Sequelize 的教程和指南.你可能还对API 参考 (英文)感兴趣.

文档版本
主要版本变更日志
在此处可以找到主要版本的升级信息:
文档(v7-alpha)
注意 由于当前alpha阶段api调整, 文档中的API参考指向尚未确定. 可前往 V7 API 参考自行查询.
核心概念
- Getting Started - 入门
- Model Basics - 模型基础
- Model Instances - 模型实例
- Model Querying - Basics - 模型查询(基础)
- Model Querying - Finders - 模型查询(查找器)
- Getters, Setters & Virtuals - 获取器, 设置器 & 虚拟字段
- Validations & Constraints - 验证 & 约束
- Raw Queries - 原始查询
- Associations - 关联
- Paranoid - 偏执表
高级关联概念
- Eager Loading - 预先加载
- Creating with Associations - 创建关联
- Advanced M:N Associations - 高级 M:N 关联
- Association Scopes - 关联作用域
- Polymorphic Associations - 多态关联
其它主题
- Dialect-Specific Things - 方言特定事项
- Transactions - 事务
- Hooks - 钩子
- Query Interface - 查询接口
- Naming Strategies - 命名策略
- Scopes - 作用域
- Sub Queries - 子查询
- Other Data Types - 其他数据类型
- Constraints & Circularities - 约束 & 循环
- Extending Data Types - 扩展数据类型
- Indexes - 索引
- Optimistic Locking - 乐观锁定
- Read Replication - 读取复制
- Connection Pool - 连接池
- Working with Legacy Tables - 使用遗留表
- Migrations - 迁移
- TypeScript
- Resources - 资源
- Terminology - 术语
简单示例
TypeScript
import {
Sequelize,
Model,
DataTypes,
InferAttributes,
InferCreationAttributes,
} from '@sequelize/core';
import { Attribute } from '@sequelize/core/decorators-legacy';
import { SqliteDialect } from '@sequelize/sqlite3';
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
@Attribute(DataTypes.STRING)
declare username: string | null;
@Attribute(DataTypes.DATE)
declare birthday: Date | null;
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
models: [User],
});
await sequelize.sync();
const jane = await User.create({
username: 'janedoe',
birthday: new Date(1980, 6, 20),
});
console.log(jane.toJSON());
JavaScript
import { Sequelize, Model, DataTypes } from '@sequelize/core';
import { Attribute } from '@sequelize/core/decorators-legacy';
import { SqliteDialect } from '@sequelize/sqlite3';
class User extends Model {
@Attribute(DataTypes.STRING)
username;
@Attribute(DataTypes.DATE)
birthday;
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
models: [User],
});
await sequelize.sync();
const jane = await User.create({
username: 'janedoe',
birthday: new Date(1980, 6, 20),
});
console.log(jane.toJSON());
请通过 Getting started - 入门 来学习更多相关内容. 如果你想要学习 Sequelize API 请通过 API 参考 (英文).