DEV Community

mgcunado
mgcunado

Posted on

3 2

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?

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs