Raw query in sequilize model

mgcunado

mgcunado

Posted on March 25, 2022

Raw query in sequilize model

Good Morning,

I have this in my sportcenter's model using sequelize:

import Sequelize from 'sequelize';

export default (sequelize, DataTypes) => {
    const Sportcenter = sequelize.define('Sportcenter', {
        id: {
            autoIncrement: true,
            type: DataTypes.INTEGER.UNSIGNED,
            allowNull: false,
            primaryKey: true,
        },
        name: {
            type: DataTypes.STRING(500),
            allowNull: true,
        },
        counter: {
            type: DataTypes.VIRTUAL,
        },
      ...
    });
}
Enter fullscreen mode Exit fullscreen mode

The fields id and name are in database table, but counter is not. I want to relate "counter" with the raw query (e.a. "select count(id) from sportcenter") and then see it in the json output in the api:

{
    "id": 1,
    "name": "Gorka",
    "counter": 26,
},
{
    "id": 2,
    "name": "Pedro",
    "counter": 26,
},
...
Enter fullscreen mode Exit fullscreen mode

how can i get that?

💖 💪 🙅 🚩
mgcunado
mgcunado

Posted on March 25, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Raw query in sequilize model
sequelize Raw query in sequilize model

March 25, 2022