复数如何在Sequelize中运作? [英] How do plurals work in Sequelize?

查看:207
本文介绍了复数如何在Sequelize中运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Sequelize 并阅读 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. 如果我想使用带有不规则复数的名词怎么办?作为人?

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. 在定义多对多关系时,我应该在选项中使用单数还是复数形式?

5. When defining a Many-to-many relationship, should I use the singular or the plural form in the through option?

推荐答案

Sequelize如何计算复数



在幕后,sequelize使用了一个名为 inflection 用于计算单数和复数形式的单词,几乎适用于任何英语单词,包括不规则复数等as 人物 - > 人八达通 - > Octopi牙齿 - > 牙。有几个词虽然变形有问题,但对于那些情况,或者你想强制自定义单数/复数单词的情况,请参阅下一个主题。

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.

如上所述,大多数具有不规则复数的单词将会通过拐点(包括人)正确地复数。但是有一些词语表明拐点是错误的。 Sequelize允许你
强制你想要的单数和复数形式,通常接受一个普通的对象,格式为 {singular:your-singular-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将在其上应用变形方法,以便在需要时获得单数和复数形式
: / p>

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"
    }
});






定义多对多关系



如果您有联结表本身的续集模型,最佳做法是在到选项:

// 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自己做多对多的魔术),然后你必须使用复数形式:

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,你可能会很好在模型名称中使用大写字符。

这篇关于复数如何在Sequelize中运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆