简体   繁体   English

如何在 Sequelize 中进行查询?

[英]How to do a query in Sequelize?

I've an existing Postgres database.我有一个现有的 Postgres 数据库。 Throught sequelize-auto, I generated the models for the existing database.通过 sequelize-auto,我为现有数据库生成了模型。 For example, let's look at the tc_devices table ( tc_devices.js ):例如,让我们看一下tc_devices表 ( tc_devices.js ):

const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
  return sequelize.define('tc_devices', {
    id: {
      autoIncrement: true,
      autoIncrementIdentity: true,
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true
    },
    name: {
      type: DataTypes.STRING(128),
      allowNull: false
    },
    uniqueid: {
      type: DataTypes.STRING(128),
      allowNull: false,
      unique: "tc_devices_uniqueid_key"
    },
    lastupdate: {
      type: DataTypes.DATE,
      allowNull: true
    },
    positionid: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    groupid: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'tc_groups',
        key: 'id'
      }
    },
    attributes: {
      type: DataTypes.STRING(4000),
      allowNull: true
    },
    phone: {
      type: DataTypes.STRING(128),
      allowNull: true
    },
    model: {
      type: DataTypes.STRING(128),
      allowNull: true
    },
    contact: {
      type: DataTypes.STRING(512),
      allowNull: true
    },
    category: {
      type: DataTypes.STRING(128),
      allowNull: true
    },
    disabled: {
      type: DataTypes.BOOLEAN,
      allowNull: true,
      defaultValue: false
    }
  }, {
    sequelize,
    tableName: 'tc_devices',
    schema: 'public',
    timestamps: false,
    indexes: [
      {
        name: "tc_devices_pkey",
        unique: true,
        fields: [
          { name: "id" },
        ]
      },
      {
        name: "tc_devices_uniqueid_key",
        unique: true,
        fields: [
          { name: "uniqueid" },
        ]
      },
    ]
  });
};

In database.js , I connect to the database:database.js中,我连接到数据库:

const { Sequelize } = require('sequelize');

const sequelize = new Sequelize(database, user, password, {
    host,
    port,
    dialect: 'postgres',
    logging: false
  })

async function db_connection(){  
try{
  await sequelize.authenticate();
  console.log('Connection has been estabilished successfully.');
}catch{
  console.log('Unable to connect to the database.');
}
}

db_connection()

How can I do a simple query on the tc_devices table?如何对 tc_devices 表进行简单查询? what should I import in database.js ?我应该在database.js中导入什么? in tc_devices I export function(sequelize, DataTypes) ... but I can't understand how to do a query in database.js with this function...could you help me?tc_devices我导出function(sequelize, DataTypes) ......但我不明白如何使用这个函数在database.js中进行查询......你能帮我吗? thank you so much.太感谢了。

You need to register all models and their associations before executing the first query with these models.在使用这些模型执行第一个查询之前,您需要注册所有模型及其关联。 Look at my answer here .看看我这里的回答
As soon as you do it you can execute queries like this:一旦你这样做,你可以执行这样的查询:

// assuming db already stores all model definitions
const allRecords = await db.tcDevices.findAll({})

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

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