复数如何在 Sequelize 中工作?
在使用 Sequelize 并阅读 the Sequelize docs,我观察到有时模型名称以单数形式使用,有时以复数形式使用.通过关联自动添加到模型中的方法有些是单数形式,有些是复数形式.
While using Sequelize and reading the Sequelize docs, I observed that sometimes model names are used in singular and sometimes in plural. Some methods automatically added to models by associations have the singular form and some have the plural form.
1. Sequelize 如何计算复数?它只是在字符串上附加一个s"吗?
1. How does Sequelize compute the plurals? Does it simply append an "s" to the string?
2.如果我想使用不规则复数形式的名词,例如Person",该怎么办?
2. What if I want to use a noun with an irregular plural, such as "Person"?
3.定义实例时,应该使用单数还是复数形式?
3. When defining an instance, should I use the singular or the plural form?
4.定义别名时,应该使用单数还是复数形式?
4. When defining an alias, should I use the singular or the plural form?
5.在定义多对多关系时,我应该在through
选项中使用单数还是复数形式?
5. When defining a Many-to-many relationship, should I use the singular or the plural form in the through
option?
推荐答案
Sequelize 如何计算复数形式
在底层,sequelize 使用了一个很棒的库,叫做 inflection 来计算单数和复数形式words,几乎适用于任何英语单词,包括不规则复数,例如 "Person" ->人"
,章鱼"->"章鱼"
, "牙齿" ->牙齿"
.不过,有些词的变形会出错,对于那些情况,或者您想强制使用自定义单/复数词的情况,请参阅下一个主题.
How Sequelize computes plurals
Under the hood, sequelize uses an awesome library called inflection to compute singular and plural forms of words, which works with virtually any english word, including irregular plurals such as "Person" -> "People"
, "Octopus" -> "Octopi"
, "Tooth" -> "Teeth"
. There are a few words that inflection gets wrong though, and for those cases, or cases in which you want to force your custom singular/plural words, see the next topic.
如上所述,大多数不规则复数的单词都会通过屈折正确复数(包括Person").但是有一些词的变化是错误的.Sequelize 允许您强制使用你想要的单数和复数形式,通常接受一个普通的对象,形式为 { single: "your-singular-here", multiply: "your-plural-here" }
代替名称字符串.有关详细信息,请参阅下一个主题.
As said above, most words with irregular plurals will be pluralized correctly by inflection (including "Person"). But there are a few words that inflection gets wrong. Sequelize allows you
to force the singular and plural forms you wish, usually accepting a plain object with the form { singular: "your-singular-here", plural: "your-plural-here" }
in place of the name string. See the next topics for more details.
定义实例时,标准的方式是使用单数名称,但你也可以使用复数名称,因为 sequelize 会对其应用变形方法来获得单数和复数形式随时需要:
When defining an instance, the standard way is to use the singular name, but you can also use the plural name, since sequelize will apply inflection methods on it to get the singular and plural form whenever needed:
const Foo = sequelize.define("foo", {
// attributes
});
如果要强制自定义单复数形式,请使用第三个参数指定名称选项:
If you want to force your custom singular and plural forms, use the third parameter to specify the name option:
const Foo = sequelize.define("foo", {
// attributes
}, {
name: {
singular: "mycustomsingularstring",
plural: "mycustompluralstring"
}
});
<小时>
定义别名时
为 hasOne
或 belongsTo
关联定义别名时,必须使用单数形式:
When defining an alias
When defining an alias for a hasOne
or belongsTo
association, you must use the singular form:
Foo.belongsTo(Bar, { as: "person" });
为 hasMany
或 belongsToMany
关联定义别名时,必须使用复数形式:
When defining an alias for a hasMany
or belongsToMany
association, you must use the plural form:
Foo.belongsToMany(Bar, { through: Foo_Bar, as: "people" });
或者,如果您想强制自定义单/复数字符串:
Or, if you want to force your custom singular/plural strings:
Foo.belongsToMany(Bar, {
through: Foo_Bar,
as: {
singular: "mycustomsingularstring",
plural: "mycustompluralstring"
}
});
<小时>
定义多对多关系时
如果您有联结表本身的 sequelize 模型,最佳做法是在 through
选项中使用模型本身:
When defining a Many-to-many relationship
If you have a sequelize model for the junction table itself, the best practice is to use the model itself in the through
option:
// If you have this somewhere
const Foo_Bar = sequelize.define("foo_bar", {
// attributes
});
// Then the best practice is to pass the model itself
Foo.belongsToMany(Bar, { through: Foo_Bar });
如果您没有用于连接表本身的 sequelize 模型,这并不罕见(您让 sequelize 自己发挥多对多的魔力),那么您必须使用复数形式:
If you don't have a sequelize model for the junction table itself, which is not unusual (you are letting sequelize do the many-to-many magic by itself), then you must use the plural form:
Foo.belongsToMany(Bar, { through: "foo_bars" });
<小时>
顺便说一句,您可能已经注意到我的示例中的所有模型定义都是小写的,这是为了遵循 PostgreSQL 的约定(如果您在 PostgreSQL 中的表中有大写字母,您可能会遇到问题),但是如果您不使用 PostgreSQL,您可能可以在模型名称中使用大写字符.
相关文章