How to do a query in Sequelize?

Issue

I’ve an existing Postgres database. Throught sequelize-auto, I generated the models for the existing database. For example, let’s look at the tc_devices table (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:

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? what should I import in 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? thank you so much.

Solution

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({})

Answered By – Anatoly

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.