Sequelize对象上的函数列表

Lau*_*ing 6 sequelize.js

当我设置关联时,有没有办法使用Sequelize(sequelizejs.com)输出神奇地在对象上创建的所有函数.

例如; 我有一个用户模型,我设置

User.belongsToMany(User, { as: 'Friends', through: 'UserFriend' });

现在我可以打电话了

User.create({
    name: Faker.name.findName()
 }).then((user) => {
     user.createFriend({
         name: Faker.name.findName()
     })
});
Run Code Online (Sandbox Code Playgroud)

createFriend由于belongsToMany关系,该功能是可能的.

有没有办法输出在用户(或用户)上创建的所有功能?

有时对我来说,我不能清楚我可以调用一个对象,如果我能以某种方式输出它会对我有所帮助.

LT-*_*LT- 13

我无法找到一种轻松退出所有魔术功能的方法.但是如果你看一下源代码,他们都会在每种类型的关系中都有访问者,并解释他们的工作.

sequelize/lib/associations/- https://github.com/sequelize/sequelize/tree/master/lib/associations

属于一对多:

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
Run Code Online (Sandbox Code Playgroud)

属于:

get: 'get' + singular,
set: 'set' + singular,
create: 'create' + singular
Run Code Online (Sandbox Code Playgroud)

有很多:

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
Run Code Online (Sandbox Code Playgroud)

具有多功能一体机:

get: 'get' + singular,
set: 'set' + singular,
create: 'create' + singular
Run Code Online (Sandbox Code Playgroud)


小智 9

我也遇到了这个问题,所以我做了一些挖掘,想出了一种方法来列出所有有用的属性/关联函数/常用函数.请记住,其中一些功能可能包含一个或多个参数.这也可能不适用于您的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+'()');
  }
}
Run Code Online (Sandbox Code Playgroud)

输出看起来像:

 ----------------------------------
 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()
Run Code Online (Sandbox Code Playgroud)