Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize list of functions on the object

Tags:

sequelize.js

Is there a way in using Sequelize (sequelizejs.com) to output all the functions magically created on objects when I set associations.

for example; I have a User model and I set

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

now i can call

User.create({
    name: Faker.name.findName()
 }).then((user) => {
     user.createFriend({
         name: Faker.name.findName()
     })
});

The createFriend function is possible because of the belongsToMany relation.

Is there a way to output all functions created on User (or user)?

Sometimes to me it's not really clear what I can call on a object, it would help me if i could output it in some way.

like image 536
Laurens Kling Avatar asked Mar 28 '16 15:03

Laurens Kling


3 Answers

I wasn't able to find a way to easily log out all of the magic functions. But if you take a look at the source code they're all under accessors for each type of relationship with explanations on what they do.

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

Belongs-To-Many:

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

Belongs-To:

get: 'get' + singular,
set: 'set' + singular,
create: 'create' + singular

Has-Many:

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-One:

get: 'get' + singular,
set: 'set' + singular,
create: 'create' + singular
like image 146
LT- Avatar answered Nov 03 '22 15:11

LT-


I also ran into this issue so I did some digging and came up with a way to list out all of the useful attributes/association functions/common functions. Keep in mind some of these functions may have one or more parameters. This may also not work with your version of Sequelize as I am digging quite deep into the internals to output this data and they are of course subject to change. Hopefully this helps you!

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+'()');
  }
}

and the output will look something like:

 ----------------------------------
 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()
like image 13
TeeTwoEx Avatar answered Nov 03 '22 15:11

TeeTwoEx


For any future visitor TeeTwoEx code doesn't work as of the present date BUT if you are only looking to get only the functions of you models like yours truly :) there is a way to work it around . TeeTwoEx saving lives and soul sanity years after an unsung hero if you ask me .

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]+'()');
    }
  }
}
like image 2
Stelios Mantzouranis Avatar answered Nov 03 '22 16:11

Stelios Mantzouranis