什么方法/ mixins sequelize在建立关联时会增加模型?

编程入门 行业动态 更新时间:2024-10-24 00:28:22
本文介绍了什么方法/ mixins sequelize在建立关联时会增加模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在浏览

要理解此表的含义,请回想一下,在文档的该页面的开头,它表示在下面的API参考中,将关联的名称添加到方法。最令人困惑的部分是,当您应该添加名称的单数版本以及何时应添加复数版本时,不太清楚。但是虽然文档没有说清楚,但我向你保证,你可以用常识来猜测。如果您认为这两个版本都有意义(例如,对于添加),请注意实际上两个版本都可用。因此,从上表中我们可以得出结论:

  • 添加到Person模型实例的方法:

    • addHypothesis()
    • addHypotheses ()
    • countHypotheses()
    • createHypothesis()
    • getHypotheses()
    • hasHypothesis()
    • hasHypotheses()
    • removeHypothesis()
    • removeHypotheses()
    • setHypotheses()
  • 添加到假设模型实例的方法:

    • addPerson()
    • addPeople()
    • countPeople()
    • createPerson()
    • getPeople()
    • hasPerson()
    • hasPeople ()
    • removePerson()
    • removePeople()
    • setPeople()

另一种解决这个问题的方法是毫无疑问的检查Sequelize源代码本身,即这里,我们可以找到:

this.accessors = { get:'get'+复数,设置:'set'+复数, addMultiple:'add'+复数, add:'add'+ singular, create:'create'+ singular,删除:'删除'+单数, removeMultiple:'删除'+复数, hasSingle:'has'+ singular, hasAll:'has'+复数, count:'count'+复数};

注意:虽然看起来可能违反直觉,但实际上两种方法都是 addPerson上面提到的()和 addPeople()使用相同的参数,可以是单个值,也可以是数组。换句话说,源代码中的方法 add 和 addMultiple 实际上是相同的,最后。这同样适用于 remove()和 removeMultiple(),以及 hasSingle()和 hasAll()。

希望通过这个,您现在可以了解Sequelize文档到底是什么表示这些表。

如果您希望直接检查源代码,类似于我上面显示的内容,这些是其他类型关联的相关行:

  • BelongsTo :这里

    this.accessors = { get:'get'+ singular, set:'set'+ singular, create:'create'+ singular };

  • HasOne :这里

    this.accessors = { get:'get'+ singular, set:'set'+ singular, create:'create'+ singular };

  • HasMany :这里

    this.accessors = { get:'get'+复数, set:'set'+复数, addMultiple:'add'+复数, add:'add'+ singular, create:'create'+ singular, remove:'remove'+ singular, removeMultiple:'remove'+ plural, hasSingle:'has'+ singular, hasAll:'has'+复数, count:'count'+复数};

While going through the sequelize docs, more specifically the documentations about associations, I see that the guide casually shows the reader methods such as setTasks(), addTask(), setProject(), that seem to be automatically created by sequelize for all model instances with respect to the created associations.

I couldn't find detailed information on what methods are available, and whether they are created with the singular version or plural version (since there is both setTasks() and setProject(), for example), and what exactly are the parameters they expect, and such. The docs apparently just casually mention them inside the examples...

So, what methods/mixins sequelize adds to the models when an association is made? And what are the parameters and return values, i.e. what's the documentation for those methods? Or, at least, where can I find them?

解决方案

The documentation about associations you linked, although hosted in an address called docs.sequelize.js, isn't the real sequelize docs (as in fully covered docs with all details that a good documentation usually provides). That is more like a tutorial / guide for starters.

The real sequelize docs are found by clicking on the "Reference" link available in the side menu of the links you mentioned (and it took me quite a while to find that - it doesn't even look like a clickable thing IMO).

The parts you're interested here are these:

  • Sequelize docs for BelongsTo type of associations: here
  • Sequelize docs for BelongsToMany type of associations: here
  • Sequelize docs for HasMany type of associations: here
  • Sequelize docs for HasOne type of associations: here
Understanding the docs

Since the docs linked above can be very confusing, here is an explanation to assist you to understand the docs.

Let's assume, for example, that we have a belongs to many association between Person and Hypothesis. Note that their plural forms, People and Hypotheses, are automatically inferred by Sequelize. This magic is done under the hood by the awesome library called inflection - see How do plurals work in Sequelize? for more details.

// Assuming that the models Person, Hypothesis and Person_Hypothesis are already defined Person.belongsToMany(Hypothesis, { through: Person_Hypothesis }); Hypothesis.belongsToMany(Person, { through: Person_Hypothesis });

And we want to use the Sequelize docs for BelongsToMany type of associations to learn what methods were automatically added to instances of the Person and Hypothesis models. There, we can find the following table:

To understand what this table means, recall that in the beginning of that page of the docs it says that "In the API reference below, add the name of the association to the method". The most confusing part of this is that it's not so clear when you should add the singular version of the name and when you should add the plural version. But although the docs do not make this clear, I assure you that you can just use common sense to guess. And if you think both versions could make sense (for example, for add), be surprised that actually both versions are available. Therefore, from the table above, we can conclude:

  • Methods added to instances of Person models:

    • addHypothesis()
    • addHypotheses()
    • countHypotheses()
    • createHypothesis()
    • getHypotheses()
    • hasHypothesis()
    • hasHypotheses()
    • removeHypothesis()
    • removeHypotheses()
    • setHypotheses()
  • Methods added to instances of Hypothesis models:

    • addPerson()
    • addPeople()
    • countPeople()
    • createPerson()
    • getPeople()
    • hasPerson()
    • hasPeople()
    • removePerson()
    • removePeople()
    • setPeople()

Another way to figure this out without room for doubt is by checking the Sequelize source code itself, namely here, where we can find:

this.accessors = { get: 'get' + plural, set: 'set' + plural, addMultiple: 'add' + plural, add: 'add' + singular, create: 'create' + singular, remove: 'remove' + singular, removeMultiple: 'remove' + plural, hasSingle: 'has' + singular, hasAll: 'has' + plural, count: 'count' + plural };

Note: although it might seem counter-intuitive, in fact both methods addPerson() and addPeople() mentioned above work with the same parameters, which can be either a single value or an array. In other words, the methods add and addMultiple from the source code are actually the same, in the end. The same applies to remove() and removeMultiple(), and hasSingle() and hasAll().

Hopefully with this you can now understand what the Sequelize docs really mean with those tables.

If you prefer to check the source code directly, analogously to what I showed above, these are the relevant lines for the other kinds of associations:

  • BelongsTo: here

    this.accessors = { get: 'get' + singular, set: 'set' + singular, create: 'create' + singular };

  • HasOne: here

    this.accessors = { get: 'get' + singular, set: 'set' + singular, create: 'create' + singular };

  • HasMany: here

    this.accessors = { get: 'get' + plural, set: 'set' + plural, addMultiple: 'add' + plural, add: 'add' + singular, create: 'create' + singular, remove: 'remove' + singular, removeMultiple: 'remove' + plural, hasSingle: 'has' + singular, hasAll: 'has' + plural, count: 'count' + plural };

更多推荐

什么方法/ mixins sequelize在建立关联时会增加模型?

本文发布于:2023-10-30 23:51:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时会   模型   方法   sequelize   mixins

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!