在浏览sequelize docs,更具体地说是documentations about associations时,我看到指南随便地显示了setTasks()
,addTask()
,setProject()
等读者方法,这些方法似乎是通过sequelize为所有模型实例创建的关联自动创建的。
我找不到有关可用方法的详细信息,以及它们是使用单数版本还是复数版本创建的(例如,因为有setTasks()
和setProject()
),以及它们所期望的参数究竟是什么等等。 The docs显然只是随便在例子中提到它们......
那么,当建立关联时,什么方法/ mixins sequelize会增加模型?什么是参数和返回值,即这些方法的文档是什么?或者,至少,我在哪里可以找到它们?
The documentation about associations you linked虽然托管在一个名为docs.sequelize.js的地址中,但它不是真正的续集文档(如完全覆盖的文档中所有详细信息,通常是良好的文档提供)。这更像是初学者的教程/指南。
real sequelize docs是通过点击你提到的链接的侧面菜单中的“参考”链接找到的(我花了很长时间才找到它 - 它甚至看起来不像是一个可点击的东西IMO)。
您在这里感兴趣的部分是这些:
由于上面链接的文档可能非常混乱,因此这里有一个解释来帮助您理解文档。
例如,让我们假设我们在Person
和Hypothesis
之间属于许多关联。请注意,他们的复数形式People
和Hypotheses
由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模型的实例中。在那里,我们可以找到下表:
要理解此表的含义,请回想一下,在文档的该页面的开头,它表示“在下面的API参考中,将关联的名称添加到方法”。最令人困惑的部分是,当您应该添加名称的单数版本以及何时应添加复数版本时,不太清楚。但是虽然文档没有说清楚,但我向你保证,你可以用常识来猜测。如果您认为这两个版本都有意义(例如,对于add
),请注意实际上两个版本都可用。因此,从上表中我们可以得出结论:
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()
方法都使用相同的参数,可以是单个值,也可以是数组。换句话说,源代码中的方法add
和addMultiple
实际上是相同的,最后。这同样适用于remove()
和removeMultiple()
,以及hasSingle()
和hasAll()
。
希望通过这种方式,您现在可以了解Sequelize文档对这些表格的真正含义。
如果您希望直接检查源代码,类似于我上面显示的内容,这些是其他类型关联的相关行:
this.accessors = {
get: 'get' + singular,
set: 'set' + singular,
create: 'create' + singular
};
this.accessors = {
get: 'get' + singular,
set: 'set' + singular,
create: 'create' + singular
};
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
};
要获取添加方法的列表,请尝试:
const model = %yourSequelizeModel%
for (let assoc of Object.keys(model.associations)) {
for (let accessor of Object.keys(model.associations[assoc].accessors)) {
console.log(model.name + '.' + model.associations[assoc].accessors[accessor]+'()');
}
}
归功于https://gist.github.com/Ivan-Feofanov/eefe489a2131f3ec43cfa3c7feb36490
要调整关联名称,请使用“as”选项:
Model.hasOne(models.series_promotions, { as: 'seriesPromotions' });
这改变了关联方法名称:
series.getSeries_promotion()
series.setSeries_promotion()
series.createSeries_promotion()
至
series.getSeriesPromotions()
series.setSeriesPromotions()
series.createSeriesPromotions()
基于上面的代码片段。