Raw query in sequilize model
mgcunado
Posted on March 25, 2022
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,
},
...
});
}
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,
},
...
how can i get that?
💖 💪 🙅 🚩
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.