Home > Enterprise >  What type should I assign to the parameter so that it does not give me this error?
What type should I assign to the parameter so that it does not give me this error?

Time:06-21

import { Model, DataTypes } from 'sequelize';

interface IExtracciones {
  id_extraccion: Number | null | undefined;
  monto_extraido: Number;
  fecha_de_extraccion: Date;
  numero_de_cuenta: Number;
}

export default class Extracciones extends Model<IExtracciones> {}

Extracciones.init(
  {
    id_extraccion: {
      type: DataTypes.INTEGER,
      primaryKey: true,
    },
    monto_extraido: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    fecha_de_extraccion: {
      type: DataTypes.DATE,
      allowNull: false,
    },
    numero_de_cuenta: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
  },

  {
    sequelize: bd,
    tableName: 'extracciones',
  }
);
function agregaRelacion(m1: typeof Model /*error here*/, m2: any, fk: String) {
    m1.hasOne(m2, { foreignKey: fk });
    m2.hasOne(m1, { foreignKey: fk });
  }

The 'this' context of type 'typeof Model' is not assignable to method's 'this' of type 'ModelStatic<Model<{}, {}>>'. Type 'typeof Model' is not assignable to type 'new () => Model<{}, {}>'. Cannot assign an abstract constructor type to a non-abstract constructor type.ts(2684)

CodePudding user response:

    import { Model, DataTypes, Sequelize, ModelStatic } from 'sequelize';
    
    interface IExtracciones {
      id_extraccion: Number | null | undefined;
      monto_extraido: Number;
      fecha_de_extraccion: Date;
      numero_de_cuenta: Number;
    }
    
    export default class Extracciones extends Model<IExtracciones> {}
    
    Extracciones.init(
      {
        id_extraccion: {
          type: DataTypes.INTEGER,
          primaryKey: true,
        },
        monto_extraido: {
          type: DataTypes.INTEGER,
          allowNull: false,
        },
        fecha_de_extraccion: {
          type: DataTypes.DATE,
          allowNull: false,
        },
        numero_de_cuenta: {
          type: DataTypes.INTEGER,
          allowNull: false,
        },
      },
    
      {
// change new Sequeilze()
        sequelize: new Sequelize(),
        tableName: 'extracciones',
      }
    );
    
    // m2 replace any with appropriate modelname if needed
    /* corrected fk type (String compiles to javascript, shouldn't used as type) */
    function agregaRelacion(m1: ModelStatic<Extracciones>, m2: ModelStatic<any>, fk: string) {
        m1.hasOne(m2, { foreignKey: fk });
        m2.hasOne(m1, { foreignKey: fk });
      }

CodePudding user response:

Solution

imagine we have a users table and our users have skills so it is a one to many right?

UserModel

import { BuildOptions, DataTypes, Model, Sequelize } from "sequelize";

export interface UserAttributes {
    id: number;
    name: string;
    email: string;
    password: string;
    middleName: string;
    lastName: string;
    secondLastName: string;
    active: boolean;
    createdAt?: Date;
    updatedAt?: Date;
}
export interface UserModel extends Model<UserAttributes>, UserAttributes {}
export class User extends Model<UserModel, UserAttributes> {}

export type UserStatic = typeof Model & {
    new (values?: object, options?: BuildOptions): UserModel;
};

export function UserFactory (sequelize: Sequelize) {
    return <UserStatic>sequelize.define("users", {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true,
        },
        email: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true,
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        middleName: {
            type: DataTypes.STRING,
            allowNull: true,
        },
        lastName: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        secondLastName: {
            type: DataTypes.STRING,
            allowNull: true,
        },
        createdAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW,
        },
        updatedAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW,
        },
    });
}

SkillsModel

import { BuildOptions, DataTypes, Model, Sequelize } from "sequelize";

export interface SkillsAttributes {
    skill: string;
}

export interface SkillsModel
    extends Model<SkillsAttributes>,
        SkillsAttributes {}

export type SkillsStatic = typeof Model & {
    new (values?: object, options?: BuildOptions): SkillsModel;
};
export function SkillsFactory (sequelize: Sequelize) {
    return <SkillsStatic>sequelize.define("skills", {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true,
        },
        skill: {
            type: DataTypes.STRING,
            unique: true,
        },
    });
}

now imagine we have a dir call anyName and in there we have

enter image description here

we are going to export users and skills from our index file

index

import { Sequelize } from "sequelize";
import { CourseFactory, CourseStatic } from "./courses";
import { EducationFactory, EducationStatic } from "./education";
import { ExperienceFactory, ExperienceStatic } from "./experience";
import { FieldsFactory, FieldsStatic } from "./fields-of-interets";
import { GeneralFactory, GeneralStatic } from "./general";
import { SkillsFactory, SkillsStatic } from "./skills";
import { SkillsTypeFactory, SkillsTypeStatic } from "./skills-type";
import { SocialMediaFactory, SocialMediaStatic } from "./social-media";
import { UserFactory, UserStatic } from "./users";

export interface DB {
    sequelize: Sequelize;
    User: UserStatic;
    Skills: SkillsStatic;
    SkillsType: SkillsTypeStatic;
    Experience: ExperienceStatic;
    Education: EducationStatic;
    Course: CourseStatic;
    General: GeneralStatic;
    SocialMedia: SocialMediaStatic;
    FieldsOfInterest: FieldsStatic;
}

const sequelize = new Sequelize(
    (process.env.DB_NAME = "rest_resume_api"),
    (process.env.DB_USER = "john"),
    (process.env.DB_PASSWORD = "password"),
    {
        port: Number(process.env.DB_PORT) || 54320,
        host: process.env.DB_HOST || "localhost",
        dialect: "postgres",
        pool: {
            min: 0,
            max: 5,
            acquire: 30000,
            idle: 10000,
        },
    }
);

const User = UserFactory(sequelize);
const Skills = SkillsFactory(sequelize);
const SkillsType = SkillsTypeFactory(sequelize);
const Experience = ExperienceFactory(sequelize);
const Education = EducationFactory(sequelize);
const Course = CourseFactory(sequelize);
const General = GeneralFactory(sequelize);
const SocialMedia = SocialMediaFactory(sequelize);
const FieldsOfInterest = FieldsFactory(sequelize);

SkillsType.belongsTo(Skills);
SkillsType.belongsToMany(User, { through: "users_has_skills" });
User.belongsToMany(SkillsType, { through: "users_has_skills" });
Experience.belongsToMany(User, { through: "user_has_experience" });
User.belongsToMany(Experience, { through: "user_has_experience" });
Education.belongsToMany(User, { through: "user_has_education" });
User.belongsToMany(Education, { through: "user_has_education" });
Course.belongsTo(User);
General.belongsTo(User);
SocialMedia.belongsToMany(User, { through: "user_has_social" });
User.belongsToMany(SocialMedia, { through: "user_has_social" });
FieldsOfInterest.belongsToMany(User, { through: "user_has_fields" });
User.belongsToMany(FieldsOfInterest, { through: "user_has_fields" });

export const db: DB = {
    sequelize,
    User,
    Skills,
};
  • Related