sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

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

Open bogdan-gherghina opened this issue 1 year ago • 0 comments

Issue

Versions

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

Issue type

  • [x] 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, { ...; }>>>>

bogdan-gherghina avatar Aug 30 '24 14:08 bogdan-gherghina