当我设置关联时,有没有办法使用Sequelize(sequelizejs.com)输出神奇地在对象上创建的所有函数。
例如;我有一个用户模型,我设置
User.belongsToMany(User, { as: 'Friends', through: 'UserFriend' });
现在我可以打电话了
User.create({
name: Faker.name.findName()
}).then((user) => {
user.createFriend({
name: Faker.name.findName()
})
});
由于belongsToMany关系,createFriend
函数是可能的。
有没有办法输出在用户(或用户)上创建的所有功能?
有时对我来说,我不能清楚我能在一个物体上调用什么,如果我能以某种方式输出它会对我有帮助。
答案 0 :(得分:8)
我无法找到一种轻松退出所有魔术功能的方法。但是如果你看一下源代码,他们就会在每种类型的关系中都有访问者,并解释他们的工作。
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
- https://github.com/sequelize/sequelize/tree/master/lib/associations
属于一对多:
get: 'get' + singular,
set: 'set' + singular,
create: 'create' + singular
属于-到:
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
HAS-许多:
get: 'get' + singular,
set: 'set' + singular,
create: 'create' + singular
有酮:
yourDataFrame.saveAsTable("YourTableName")
答案 1 :(得分:7)
我也遇到了这个问题所以我做了一些挖掘,想出了一种方法来列出所有有用的属性/关联函数/常用函数。请记住,其中一些功能可能包含一个或多个参数。这也可能不适用于您的Sequelize版本,因为我正在深入挖掘内部输出这些数据,它们当然可能会发生变化。希望这对你有帮助!
const models = require('./models');
for (let model of Object.keys(models)) {
if(!models[model].name)
continue;
console.log("\n\n----------------------------------\n",
models[model].name,
"\n----------------------------------");
console.log("\nAttributes");
for (let attr of Object.keys(models[model].attributes)) {
console.log(models[model].name + '.' + attr);
}
console.log("\nAssociations");
for (let assoc of Object.keys(models[model].associations)) {
for (let accessor of Object.keys(models[model].associations[assoc].accessors)) {
console.log(models[model].name + '.' + models[model].associations[assoc].accessors[accessor]+'()');
}
}
console.log("\nCommon");
for (let func of Object.keys(models[model].Instance.super_.prototype)) {
if(func === 'constructor' || func === 'sequelize')
continue;
console.log(models[model].name + '.' + func+'()');
}
}
,输出结果如下:
----------------------------------
Assignment
----------------------------------
Attributes
Assignment.id
Assignment.type
Assignment.type_id
Assignment.created_at
Assignment.updated_at
Assignment.user_id
Assignment.reporter_id
Associations
Assignment.getUser()
Assignment.setUser()
Assignment.createUser()
Assignment.getReporter()
Assignment.setReporter()
Assignment.createReporter()
Assignment.getPost_assignment()
Assignment.setPost_assignment()
Assignment.createPost_assignment()
Assignment.getHistory()
Assignment.setHistory()
Assignment.addHistory()
Assignment.addHistory()
Assignment.createHistory()
Assignment.removeHistory()
Assignment.removeHistory()
Assignment.hasHistory()
Assignment.hasHistory()
Assignment.countHistory()
Common
Assignment.where()
Assignment.toString()
Assignment.getDataValue()
Assignment.setDataValue()
Assignment.get()
Assignment.set()
Assignment.setAttributes()
Assignment.changed()
Assignment.previous()
Assignment._setInclude()
Assignment.save()
Assignment.reload()
Assignment.validate()
Assignment.hookValidate()
Assignment.update()
Assignment.updateAttributes()
Assignment.destroy()
Assignment.restore()
Assignment.increment()
Assignment.decrement()
Assignment.equals()
Assignment.equalsOneOf()
Assignment.setValidators()
Assignment.toJSON()
答案 2 :(得分:0)
对于任何未来的访问者,TeeTwoEx 代码在当前日期不起作用,但是如果您只想获得真正像您这样的模型的功能:) 有一种方法可以解决它。如果你问我的话,TeeTwoEx 在一个无名英雄之后拯救了生命和灵魂的理智。
const models = require('./src/models/index');
for (let model of Object.keys(models)) {
if(models[model].name === 'Sequelize')
continue;
if(!models[model].name)
continue;
console.log("\n\n----------------------------------\n",
models[model].name,
"\n----------------------------------");
console.log("\nAssociations");
for (let assoc of Object.keys(models[model].associations)) {
for (let accessor of Object.keys(models[model].associations[assoc].accessors)) {
console.log(models[model].name + '.' + models[model].associations[assoc].accessors[accessor]+'()');
}
}
}