gpt4 book ai didi

javascript - 建立关联时,哪些方法/mixin sequelize 添加到模型中?

转载 作者:数据小太阳 更新时间:2023-10-29 05:42:32 25 4
gpt4 key购买 nike

同时通过sequelize docs ,更具体地说是 documentations about associations ,我看到指南随便给了读者方法如setTasks() , addTask() , setProject() ,这似乎是由 sequelize 自动为所有与创建的关联相关的模型实例创建的。

我找不到有关哪些方法可用的详细信息,以及它们是使用单数版本还是复数版本创建的(例如,因为有 setTasks()setProject() ),以及他们期望的参数究竟是什么,诸如此类。 The docs显然只是在例子中随便提到它们......

那么,当建立关联时,哪些方法/mixins sequelize 添加到模型中? 参数和返回值是什么,即这些方法的文档是什么?或者,至少,我在哪里可以找到它们?

最佳答案

The documentation about associations you linked ,尽管托管在名为 docs.sequelize.js 的地址中,但不是 真正的 Sequelize 文档 (如在包含良好文档通常提供的所有详细信息的完整文档中)。这更像是初学者的教程/指南。

real sequelize docs 通过单击您提到的链接的侧边菜单中可用的“引用”链接找到(我花了很长时间才找到它 - 它甚至看起来不像 IMO 可点击的东西)。

您在这里感兴趣的部分是这些:

  • 的 Sequelize 文档属于关联类型:here
  • 的 Sequelize 文档BelongsToMany 关联类型:here
  • 的 Sequelize 文档HasMany 关联类型:here
  • 的 Sequelize 文档HasOne 关联类型:here


  • 了解文档

    由于上面链接的文档可能非常困惑,这里有一个解释来帮助您理解文档。

    例如,让我们假设我们在 Person 之间有一个属于多个关联。和 Hypothesis .注意它们的复数形式, PeopleHypotheses , 由 Sequelize 自动推断。这个魔法是由名为 inflection 的很棒的库在幕后完成的。 - 见 How do plurals work in Sequelize?更多细节。
    // Assuming that the models Person, Hypothesis and Person_Hypothesis are already defined
    Person.belongsToMany(Hypothesis, { through: Person_Hypothesis });
    Hypothesis.belongsToMany(Person, { through: Person_Hypothesis });

    我们想使用 Sequelize docs for BelongsToMany type of associations了解哪些方法被自动添加到 Person 和 Hypothesis 模型的实例中。在那里,我们可以找到下表:

    Table with the Method Summary of the Public Methods from the sequelize docs

    要理解此表的含义,请回想一下在该文档页面的开头,它说“在下面的 API 引用中,将关联的名称添加到方法中”。其中最令人困惑的部分是不清楚何时应该添加名称的单数形式以及何时应该添加复数形式。但是,尽管文档没有明确说明这一点,但我向您保证,您可以仅凭常识进行猜测。如果您认为这两个版本都有意义(例如,对于 add ),请惊讶于实际上两个版本都可用。因此,从上表中,我们可以得出结论:
  • 添加到 Person 模型实例的方法:
  • addHypothesis()
  • addHypotheses()
  • countHypotheses()
  • createHypothesis()
  • getHypotheses()
  • hasHypothesis()
  • hasHypotheses()
  • removeHypothesis()
  • removeHypotheses()
  • setHypotheses()
  • 添加到假设模型实例的方法:
  • addPerson()
  • addPeople()
  • countPeople()
  • createPerson()
  • getPeople()
  • hasPerson()
  • hasPeople()
  • removePerson()
  • removePeople()
  • setPeople()

  • 另一种毫无疑问地解决这个问题的方法是检查 Sequelize 源代码本身,即 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
    };

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

    希望通过这个,您现在可以理解 Sequelize 文档对这些表格的真正含义。

    如果您喜欢直接检查源代码,类似于我上面显示的内容,这些是其他类型关联的相关行:
  • 属于 :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
    };
  • 关于javascript - 建立关联时,哪些方法/mixin sequelize 添加到模型中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49467654/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com