Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 2 tasks
bogdan-gherghina opened this issue Aug 30, 2024 · 0 comments

Comments

@bogdan-gherghina
Copy link

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, { ...; }>>>>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant