-
Notifications
You must be signed in to change notification settings - Fork 286
Description
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
- Define a Sequelize model
- Use the
findOrBuildstatic method withwhereobject - Try to omit the
whereobject properties from thedefaultsobject
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, { ...; }>>>>