node.js - 如何在 sequelize 中访问访问器方法

标签 node.js postgresql express orm sequelize.js

我有一个名为 user 的模型和一个名为 access_barrier 的模型以及 useraccess_barrier 之间的连接表> 称为 barrier_users

我为他们创建了以下关联

user.belongsToMany(models.access_barrier, {
  as: "access_barriers_accessible",
  through: "barrier_users",
  foreignKey: "userId",
  onUpdate: "CASCADE",
  onDelete: "CASCADE"
});

access_barrier.belongsToMany(models.user, {
  as: "users_with_permission",
  through: "barrier_users",
  foreignKey: "barrierId",
  onUpdate: "CASCADE",
  onDelete: "CASCADE"
});

当使用以下代码查询用户时,我可以看到所有数据都正常。

return models.user
    .findAll({
        include: [
          {
            model: models.access_barrier,
            as: "access_barriers_accessible"
          }
        ]
    })
    .then(function(result) {
        resolve(result);
    })
    .catch(models.Sequelize.DatabaseError, function() {
        reject("An error occurred in the database");
    })
    .catch(function(err) {
        reject(err);
    });

sequelize 的文档提到访问器方法是在使用上述功能时创建的,如下所述

Project.belongsToMany(User, {through: 'UserProject'});

User.belongsToMany(Project, {through: 'UserProject'});

This will create a new model called UserProject with the equivalent foreign keys projectId and userId. Whether the attributes are camelcase or not depends on the two models joined by the table (in this case User and Project).

Defining through is required. Sequelize would previously attempt to autogenerate names but that would not always lead to the most logical setups.

This will add methods getUsers, setUsers, addUser,addUsers to Project, and getProjects, setProjects, addProject, and addProjects to User.

我如何知道为我的模型添加的方法名称,我可以在哪里查看它们?我如何访问它们?

我使用了 Object.keys(models.user) 得到了下面的数据

[ 'sequelize',
  'options',
  'associations',
  'underscored',
  'tableName',
  '_schema',
  '_schemaDelimiter',
  'rawAttributes',
  'primaryKeys',
  '_timestampAttributes',
  '_readOnlyAttributes',
  '_hasReadOnlyAttributes',
  '_isReadOnlyAttribute',
  '_dataTypeChanges',
  '_dataTypeSanitizers',
  '_booleanAttributes',
  '_dateAttributes',
  '_hstoreAttributes',
  '_rangeAttributes',
  '_jsonAttributes',
  '_geometryAttributes',
  '_virtualAttributes',
  '_defaultValues',
  'fieldRawAttributesMap',
  'fieldAttributeMap',
  'uniqueKeys',
  '_hasBooleanAttributes',
  '_isBooleanAttribute',
  '_hasDateAttributes',
  '_isDateAttribute',
  '_hasHstoreAttributes',
  '_isHstoreAttribute',
  '_hasRangeAttributes',
  '_isRangeAttribute',
  '_hasJsonAttributes',
  '_isJsonAttribute',
  '_hasVirtualAttributes',
  '_isVirtualAttribute',
  '_hasGeometryAttributes',
  '_isGeometryAttribute',
  '_hasDefaultValues',
  'attributes',
  'tableAttributes',
  'primaryKeyAttributes',
  'primaryKeyAttribute',
  'primaryKeyField',
  '_hasPrimaryKeys',
  '_isPrimaryKey',
  'autoIncrementAttribute',
  '_scope',
  '_scopeNames',
  'associate' ]

检查它是什么关联

{ access_barriers_incharge: access_barriers_incharge,
  access_barriers_admin: access_barriers_admin,
  admin: admin,
  access_barriers_accessible: access_barriers_accessible,
  barriers_accessed: barriers_accessed }

最佳答案

找到这篇文章,让我弄清楚什么魔法方法与 sequelize 对象相关联。

简而言之,您可以在 sequelize 对象上使用以下代码来获取其魔术方法。

console.log(Object.keys(sequelizeObject.__proto__));

Feeling The Magic with Sequelize Magic Methods

关于node.js - 如何在 sequelize 中访问访问器方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52738883/

相关文章:

Javascript 原型(prototype)返回值未定义,即使它在原型(prototype)方法中是正确的

javascript - 对于非实时网站/应用程序,node.js 是否过多?

java - 如何使用变量将数据放入 jdbc 表中?

sql - 我们可以将表上的触发器限制为仅根据它调用的函数执行插入吗? (PostgreSQL)

sql - 当前日期减去星期几

node.js - Cassandra 当前时间戳与 Node.js

javascript - 如何绑定(bind)到一些返回 Promise 的函数来表达路由?

javascript - 如何延迟 pm2 下集群进程的 "online"事件?

javascript - Node.js 架构和性能

javascript - 在express nodejs中将图像文件转换为base64