Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda