Skip to content

Commit

Permalink
update v5.18.1
Browse files Browse the repository at this point in the history
update v5.18.1
  • Loading branch information
demopark committed Sep 5, 2019
1 parent 719965a commit b35574a
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 64 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 更新记录

## 2019-9-5
* master分支同步翻译到v5文档 5.18.1

## 2019-5-28
* master分支同步翻译到v5文档 5.8.6

Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Sequelize 遵从 [SEMVER](http://semver.org). 支持 Node v6 及更高版本以

## 版本

### [v4 中文文档](https://github.com/demopark/sequelize-docs-Zh-CN/tree/v4)
### [v4 中文文档](https://github.com/demopark/sequelize-docs-Zh-CN/tree/v4)(停止更新)

### [v5 中文文档](https://github.com/demopark/sequelize-docs-Zh-CN/tree/master)

Expand Down
64 changes: 64 additions & 0 deletions associations.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,70 @@ Person.belongsToMany(Person, { as: 'Children', through: 'PersonChildren' })

```

#### 来源(Source)键 和 目标(Target)键

如果要创建不使用默认主键的 belongsToMany 关系,则需要进行一些设置工作.
你必须为 belongsToMany 的两端设置恰当的 `sourceKey` (`targetKey` 可选).此外,你还必须确保在关系中创建适合的索引.例如:

```js
const User = this.sequelize.define('User', {
id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
field: 'user_id'
},
userSecondId: {
type: DataTypes.UUID,
allowNull: false,
defaultValue: DataTypes.UUIDV4,
field: 'user_second_id'
}
}, {
tableName: 'tbl_user',
indexes: [
{
unique: true,
fields: ['user_second_id']
}
]
});

const Group = this.sequelize.define('Group', {
id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
field: 'group_id'
},
groupSecondId: {
type: DataTypes.UUID,
allowNull: false,
defaultValue: DataTypes.UUIDV4,
field: 'group_second_id'
}
}, {
tableName: 'tbl_group',
indexes: [
{
unique: true,
fields: ['group_second_id']
}
]
});

User.belongsToMany(Group, {
through: 'usergroups',
sourceKey: 'userSecondId'
});
Group.belongsToMany(User, {
through: 'usergroups',
sourceKey: 'groupSecondId'
});
```

如果你想要连接表中的其他属性,则可以在定义关联之前为连接表定义一个模型,然后再说明它应该使用该模型进行连接,而不是创建一个新的关联:

```js
Expand Down
8 changes: 5 additions & 3 deletions data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const sequelizeConfig = require('../config/sequelize')
const sequelizeAdditions = require('./sequelize-additions')

// 添加新数据类型的函数
sequelizeAdditions(Sequelize.DataTypes)
sequelizeAdditions(Sequelize)

// 在这个例子中,创建并导出Sequelize实例
const sequelize = new Sequelize(sequelizeConfig)
Expand Down Expand Up @@ -225,13 +225,15 @@ modules.exports = function sequelizeAdditions(Sequelize) {
return Number.parseInt(value)
}
}

DataTypes.NEWTYPE = NEWTYPE;

// 强制,设置 Key
DataTypes.NEWTYPE.prototype.key = DataTypes.NEWTYPE.key = 'NEWTYPE'

// 可选, 在stringifier后禁用转义. 不建议.
// 警告:禁用针对SQL注入的Sequelize保护
//DataTypes.NEWTYPE.escape = false
// DataTypes.NEWTYPE.escape = false

// 为了简便`classToInvokable` 允许你使用没有 `new` 的数据类型
Sequelize.NEWTYPE = Sequelize.Utils.classToInvokable(DataTypes.NEWTYPE)
Expand Down Expand Up @@ -276,7 +278,7 @@ modules.exports = function sequelizeAdditions(Sequelize) {

// 强制, 创建,覆盖或重新分配postgres特定的解析器
//PgTypes.NEWTYPE.parse = value => value;
PgTypes.NEWTYPE.parse = BaseTypes.NEWTYPE.parse;
PgTypes.NEWTYPE.parse = DataTypes.NEWTYPE.parse;

// 可选, 添加或覆盖 postgres 特定数据类型的方法
// 比如 toSql, escape, validate, _stringify, _sanitize...
Expand Down
2 changes: 1 addition & 1 deletion dialects.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const sequelize = new Sequelize('database', 'username', 'password', {
})
```

**注意:** 你可以通过设置 `dialectOptions` 参数将选项直接传递给方言库.参考[参数](/manual/usage.html#options).
**注意:** 你可以通过设置 `dialectOptions` 参数将选项直接传递给方言库.

## MariaDB

Expand Down
2 changes: 1 addition & 1 deletion getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class Bar extends Model {}
Bar.init({ /* ... */ }, { sequelize, timestamps: true });
```

你可以在[Model.init API 参考](/class/lib/model.js~Model.html#static-method-init)[sequelize.define API 参考](/class/lib/sequelize.js~Sequelize.html#instance-method-define) 中阅读有关创建模型的更多信息.
你可以在[Model.init API 参考](http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-init)[sequelize.define API 参考](http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html#instance-method-define) 中阅读有关创建模型的更多信息.

## 将模型与数据库同步

Expand Down
4 changes: 3 additions & 1 deletion hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@ new Sequelize(..., {

### 连接 Hook

Sequelize 提供了两个在获取数据库连接之前和之后立即执行的 hook:
Sequelize 提供了四个在获取或释放数据库连接之前和之后立即执行的 hook:

```text
beforeConnect(config)
afterConnect(connection, config)
beforeDisconnect(connection)
afterDisconnect(connection)
```

如果需要异步获取数据库凭据,或者需要在创建后直接访问低级数据库连接,这些 hook 非常有用.
Expand Down
28 changes: 27 additions & 1 deletion instances.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ task.destroy({ force: true })
Task.create({ title: 'a task' }).then(task => {
// 进行软删除...
return task.destroy();
}).then(() => {
}).then((task) => {
  // 恢复软删除...
return task.restore();
})
Expand Down Expand Up @@ -170,6 +170,32 @@ User.bulkCreate([
})
```

插入多行并返回所有列(仅限Postgres):

```js
User.bulkCreate([
{ username: 'barfooz', isAdmin: true },
{ username: 'foo', isAdmin: true },
{ username: 'bar', isAdmin: false }
], { returning: true }) // 将返回插入的每一行的所有列
.then((result) => {
console.log(result);
});
```

插入多行并返回特定列(仅限Postgres):

```js
User.bulkCreate([
{ username: 'barfooz', isAdmin: true },
{ username: 'foo', isAdmin: true },
{ username: 'bar', isAdmin: false }
], { returning: ['username'] }) // 将仅返回插入的每行的指定列
.then((result) => {
console.log(result);
});
```

一次更新几行:

```js
Expand Down
90 changes: 88 additions & 2 deletions migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ $ npx sequelize-cli init
}
```

现在编辑此文件并设置正确的数据库凭据和方言.
现在编辑此文件并设置正确的数据库凭据和方言.对象的键(例如 "development")用于 `model/index.js` 以匹配 `process.env.NODE_ENV`(当未定义时,默认值是 "development").

**注意:** _如果你的数据库还不存在,你可以调用 `db:create` 命令. 通过正确的访问,它将为你创建该数据库._

Expand Down Expand Up @@ -139,7 +139,9 @@ module.exports = {
return queryInterface.bulkInsert('Users', [{
firstName: 'John',
lastName: 'Doe',
email: 'demo@demo.com'
email: 'demo@demo.com',
createdAt: new Date(),
updatedAt: new Date()
}], {});
},

Expand Down Expand Up @@ -250,6 +252,90 @@ module.exports = {
};
```

下一个是具有外键的迁移示例. 你可以使用 references 来指定外键:

```js
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Person', {
name: Sequelize.STRING,
isBetaMember: {
type: Sequelize.BOOLEAN,
defaultValue: false,
allowNull: false
},
userId: {
type: Sequelize.INTEGER,
references: {
model: {
tableName: 'users',
schema: 'schema'
}
key: 'id'
},
allowNull: false
},
});
},

down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Person');
}
}

```

下一个是使用 async/await 的迁移示例,你可以在其中为新列创建唯一索引:

```js
module.exports = {
async up(queryInterface, Sequelize) {
const transaction = await queryInterface.sequelize.transaction();
try {
await queryInterface.addColumn(
'Person',
'petName',
{
type: Sequelize.STRING,
},
{ transaction }
);
await queryInterface.addIndex(
'Person',
'petName',
{
fields: 'petName',
unique: true,
},
{ transaction }
);
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
}
},

async down(queryInterface, Sequelize) {
const transaction = await queryInterface.sequelize.transaction();
try {
await queryInterface.removeColumn(
'Person',
'petName',
{
type: Sequelize.STRING,
},
{ transaction }
);
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
}
},
};
```

### `.sequelizerc` 文件

这是一个特殊的配置文件. 它允许你指定通常作为参数传递给CLI的各种选项. 在某些情况下,你可以使用它.
Expand Down
6 changes: 3 additions & 3 deletions models-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -709,15 +709,15 @@ User.init({}, {
// 具有有序字段的BTREE索引
{
name: 'title_index',
method: 'BTREE',
using: 'BTREE',
fields: ['author', {attribute: 'title', collate: 'en_US', order: 'DESC', length: 5}]
}
],
sequelize
});
```

[0]: /manual/models-definition.html#configuration
[1]: /manual/data-types.html
[0]: models-definition.html#configuration
[1]: data-types.html
[3]: https://github.com/chriso/validator.js
[5]: /docs/final/misc#asynchronicity
3 changes: 3 additions & 0 deletions models-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Project.findOne({

假设我们有一个空的数据库,一个 `User` 模型有一个 `username``job`.

`where` 参数将添加到创建的案例的 `defaults`.


```js
User
.findOrCreate({where: {username: 'sdepold'}, defaults: {job: 'Technical Lead JavaScript'}})
Expand Down
2 changes: 2 additions & 0 deletions querying.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const Op = Sequelize.Op
[Op.lte]: 10, // id <= 10
[Op.ne]: 20, // id != 20
[Op.eq]: 3, // = 3
[Op.is]: null // 为 NULL
[Op.not]: true, // 不是 TRUE
[Op.between]: [6, 10], // 在 6 和 10 之间
[Op.notBetween]: [11, 15], // 不在 11 和 15 之间
Expand All @@ -171,6 +172,7 @@ const Op = Sequelize.Op
[Op.any]: [2,3] // 任何数组[2, 3]::INTEGER (仅限PG)

[Op.col]: 'user.organization_id' // = 'user'.'organization_id', 使用数据库语言特定的列标识符, 本例使用 PG
[Op.gt]: { [Op.all]: literal('SELECT 1') } // > ALL (SELECT 1)
```

#### 范围选项
Expand Down
Loading

0 comments on commit b35574a

Please sign in to comment.