Skip to content

Wrong association between defaults property definition and what the findOrBuild method actually does #1729

@bogdan-gherghina

Description

@bogdan-gherghina

Issue

Versions

  • sequelize: 6.37.1
  • sequelize-typescript: 2.1.5
  • typescript: 5.4.5

Issue type

  • bug report
  • feature request

Actual behavior

When using findOrBuild static method, the defaults property from the options object requires the parameters already stated in the where object, if they are set as not null and creation mandatory in the model definition.

Expected behavior

I should expect to let me use the function without specifying them in defaults clause, because it forces me to duplicate the parameters already found in the where clause, since the function works just as fine without specifying them. Anther thing is that I can't wrap them with CreationOptional, because then it wouldn't work correctly on create anymore.

Steps to reproduce

  1. Define a Sequelize model
  2. Use the findOrBuild static method with where object
  3. Try to omit the where object properties from the defaults object

Related code

// my-model.ts
import { Column, Model, PrimaryKey, Table } from 'sequelize-typescript'
import { InferAttributes, InferCreationAttributes } from 'sequelize';

@Table
export class MyModel extends Model<
  InferAttributes<MyModel>,
  InferCreationAttributes<MyModel>
> {
  @PrimaryKey
  @Column
  declare userId: number;

  @Column
  declare name: string;
}

import { Injectable, Module } from '@nestjs/common';
import { InjectModel, SequelizeModule } from '@nestjs/sequelize';
import { MyModel } from '../wonder/models/my-model.model';
import { ModelStatic } from 'sequelize';

@Injectable()
export class TestSequelizeService {
  constructor(
    @InjectModel(MyModel)
    private readonly myModel: ModelStatic<MyModel>,
  ) {}

  async execute() {
    const [model] = await this.myModel.findOrBuild({
      where: {
        userId: 1,
      },
      defaults: { name: 'foo' },
      logging: console.log,
    });

    console.log(model.userId);
  }
}

@Module({
  imports: [SequelizeModule.forFeature([MyModel])],
  providers: [TestSequelizeService],
})
export class TestSequelizeModule {}

TS2322: Type { name: string; } is not assignable to type
Optional<InferCreationAttributes<MyModel, { omit: never; }>, NullishPropertiesOf<InferCreationAttributes<MyModel, { omit: never; }>>>
Property userId is missing in type { name: string; } but required in type
Omit<InferCreationAttributes<MyModel, { omit: never; }>, NullishPropertiesOf<InferCreationAttributes<MyModel, { omit: never; }>>>
my-model. model. ts(11, 11): userId is declared here.
model. d. ts(1004, 3): The expected type comes from property defaults which is declared here on type
FindOrBuildOptions<InferAttributes<MyModel, { omit: never; }>, Optional<InferCreationAttributes<MyModel, { omit: never; }>, NullishPropertiesOf<InferCreationAttributes<MyModel, { ...; }>>>>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions