Why doesn't sequelize include associated model to a nested object in the results?

Advertisements I have two associated models: TariffsModel and PoliciesModel. PoliciesModel has tariff_id which refers to specific tariff by its ID. And this works but Sequelize returns the result in a weird way: instead of returning: PoliciesModel { id: 1, owner_party_id: 2, tariff: { id: 1, is_active: true, … }, … } it returns: PoliciesModel {… Read More Why doesn't sequelize include associated model to a nested object in the results?

modify query function in Sequelize

Advertisements I’m learning "Sequelize". I went through documentation and got this code somewhere else. Model = require(‘../models/Salesman’) module.exports.creareSalesman = (req, res, next) => { Model.create(req.body).then( result => { res.status.json({data: result}) }).catch(err => console.log(err)) } but I want this in the below structure, Model = require(‘../models/Salesman’) module.exports.creareSalesman = (req, res, next) => { Model.create(req.body, function (result,… Read More modify query function in Sequelize

How to adding default value to Sequelize model with NodeJS?

Advertisements i have created a controller that to adding new value into the database, but now i do not know how to set up the default value into Sequelize. Let me demonstrate my code below Model User: module.exports = (sequelize, Sequelize) => { const User = sequelize.define("user", { user_id: { type: Sequelize.STRING, autoIncrement: true, primaryKey:… Read More How to adding default value to Sequelize model with NodeJS?

User.create is not a function (Sequelize Error)

Advertisements I successfully created migrations using sequelize cli, but now when trying to make request in postman I’m getting the error: User.create is not a function I don’t know what’s wrong with my application’s model, I also tried changing the import on the page to const { Customer } = require(‘../../models/customer’); rather than const Customer… Read More User.create is not a function (Sequelize Error)

Is there a way to pass my types from the sequelize model to the typescript?

Advertisements Is there a way to pass my types from the sequelize model to the typescript? My model code: import { Table, Column, Model, DataType } from ‘sequelize-typescript’; @Table export class User extends Model { @Column({ type: DataType.UUID, defaultValue: DataType.UUIDV4 }) userId!: string; @Column({ allowNull: false }) username!: string; @Column({ allowNull: false, unique: true })… Read More Is there a way to pass my types from the sequelize model to the typescript?

Sequelize: How to find all rows where amount of likes are equal to 2

Advertisements I Have the next attempt to do that, but is says, that "likesAmount" column doesn’t exists. What should I do? let pictures = await models.Picture.findAll({ attributes: { include: [ [sequelize.literal(‘(SELECT COUNT(*) FROM "pictureLikes" WHERE "pictureLikes"."pictureId"=picture.id)’), ‘likesAmount’], [sequelize.literal(‘(SELECT COUNT(*) FROM comments WHERE comments."pictureId"=picture.id)’), ‘commentsAmount’] ] }, where:{likesAmount: 2} , }); >Solution : You should use… Read More Sequelize: How to find all rows where amount of likes are equal to 2

how to set order on include aggregate function alias in sequelize?

Advertisements Below is my code export async function getMany(page: number, recordsPerPage: number, condition: any = {}, order: any, attributes: string[] = [], other: object = {}) { try { let { count, rows }: any = await User.findAndCountAll({ attributes: { include: [[sequelize.literal(‘(SELECT SUM(reputation) FROM scores where scores.user_id = User.id)’), ‘reputation’]], exclude: attributes, }, where: condition,… Read More how to set order on include aggregate function alias in sequelize?

Count records for each day between the startdate and enddate in sequelize ORM (Performing this query in nodejs)

Advertisements I am passing start_time and end_time as a query parameter in an API and want to have count of the records on each day from start_time and end_time. It is giving me total count of records between the specified start and end_time but I want count for each day individually. Example start_time: 2017-10-07 and… Read More Count records for each day between the startdate and enddate in sequelize ORM (Performing this query in nodejs)