简体   繁体   English

在 Nodejs Sequelize 模型中,如何获取属性的类型信息?

[英]In a Nodejs Sequelize model, how to obtain type information of attributes?

I have a working model with Postgres and sequelize in NodeJS .我有一个工作模型PostgressequelizeNodeJS Say the model is Person and has name and age fields.假设模型是Person并且有nameage字段。 Now I want to dynamically inspect the model class and obtain information about it's attributes, like their name and most of all type .现在我想动态检查模型类并获取有关它的属性的信息,例如它们的name和最重要的type

Using Person.attributes I get some information:使用Person.attributes我得到一些信息:

name:
{ type:
  { options: [Object],
    _binary: undefined,
    _length: 255 },

But as you can see the type object does not inform about name being a varchar or boolean .但是正如您所看到的, type对象并没有告知namevarcharboolean

Does anyone know, how to get this information with sequelize有谁知道,如何使用sequelize获取这些信息

You can iterate over rawAtributes of Model您可以迭代模型的rawAtributes

for( let key in Model.rawAttributes ){
    console.log('Field: ', key); // this is name of the field
    console.log('Type: ', Model.rawAttributes[key].type.key); // Sequelize type of field
}

So the example for name being a Sequelize.STRING would be因此, nameSequelize.STRING将是

Field: name
Type: STRING

Or you can do almost the same iteration but instead using Model.rawAttributes[key].type.key you can use Model.rawAttributes[key].type.toSql() , which would generate this result或者你可以做几乎相同的迭代,但是使用Model.rawAttributes[key].type.key你可以使用Model.rawAttributes[key].type.toSql() ,这会产生这个结果

Field: name
Type: VARCHAR(255)

EDIT编辑

Accessing defaultValue of field:访问字段的defaultValue值:

Model.rawAttributes[field].defaultValue

Checking if field allows NULL values:检查字段是否允许NULL值:

Model.rawAttributes[field].allowNull

You are looking for native type information, it seems.您似乎正在寻找本机类型信息。

I'm not familiar with Sequelize, except I know it uses node-postgres driver underneath, which automatically provides the type information with every query that you make.我不熟悉 Sequelize,但我知道它在下面使用node-postgres驱动程序,它会自动为您进行的每个查询提供类型信息。

Below is a simple example of dynamically getting type details for any_table , using pg-promise :下面是一个使用pg-promise动态获取any_table类型详细信息的简单示例:

var pgp = require('pg-promise')(/*initialization options*/);
var db = pgp(/*connection details*/);

db.result('SELECT * FROM any_table LIMIT 0', [], a => a.fields)
    .then(fields => {
        // complete details for each column 
    })
    .catch(error => {
        // an error occurred
    });

There are several fields available for each column there, including name and dataTypeID that you are looking for ;)那里的每一列都有几个可用的字段,包括您要查找的namedataTypeID ;)

As an update , following the answer that does use Sequelize for it...作为更新,遵循确实使用 Sequelize 的答案......

The difference is that here we get direct raw values as they are provided by PostgreSQL, so dataTypeID is raw type Id exactly as PostgreSQL supports it, while TYPE: STRING is an interpreted type as implemented by Sequelize.不同之处在于,这里我们直接获取原始值,因为它们由 PostgreSQL 提供,因此dataTypeID是原始类型 Id,正如 PostgreSQL 所支持的那样,而TYPE: STRING是由 Sequelize 实现的解释类型。 Also, we are getting the type details dynamically here, versus statically in that Sequelize example.此外,我们在这里动态获取类型详细信息,而不是在 Sequelize 示例中静态获取。

item.rawAttributes[key].type.key === this.DataTypes.JSONB.key; item.rawAttributes[key].type.key === this.DataTypes.JSONB.key; @piotrbienias Is above comparison valid ? @piotrbienias 以上比较有效吗? (Sorry for writing here as I can't add comments) (很抱歉在这里写,因为我无法添加评论)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM