DEV Community

mgcunado
mgcunado

Posted on

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?

Top comments (0)