Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
How to do a query in Sequelize?

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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({})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!