fix(typings): restrict update typings (#13216) · sequelize/sequelize@63ceb73

2 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -2087,7 +2087,9 @@ export abstract class Model<TModelAttributes extends {} = any, TCreationAttribut

20872087

*/

20882088

public static update<M extends Model>(

20892089

this: ModelStatic<M>,

2090-

values: Partial<M['_attributes']>,

2090+

values: {

2091+

[key in keyof M['_attributes']]?: M['_attributes'][key] | Fn | Col | Literal;

2092+

},

20912093

options: UpdateOptions<M['_attributes']>

20922094

): Promise<[number, M[]]>;

20932095

@@ -2744,8 +2746,13 @@ export abstract class Model<TModelAttributes extends {} = any, TCreationAttribut

27442746

/**

27452747

* This is the same as calling `set` and then calling `save`.

27462748

*/

2747-

public update<K extends keyof this>(key: K, value: this[K], options?: InstanceUpdateOptions<TModelAttributes>): Promise<this>;

2748-

public update(keys: object, options?: InstanceUpdateOptions<TModelAttributes>): Promise<this>;

2749+

public update<K extends keyof TModelAttributes>(key: K, value: TModelAttributes[K] | Col | Fn | Literal, options?: InstanceUpdateOptions<TModelAttributes>): Promise<this>;

2750+

public update(

2751+

keys: {

2752+

[key in keyof TModelAttributes]?: TModelAttributes[key] | Fn | Col | Literal;

2753+

},

2754+

options?: InstanceUpdateOptions<TModelAttributes>

2755+

): Promise<this>;

27492756
27502757

/**

27512758

* Destroy the row corresponding to this instance. Depending on your setting for paranoid, the row will

Original file line numberDiff line numberDiff line change

@@ -1,4 +1,4 @@

1-

import { Model } from 'sequelize';

1+

import { Model, fn, col, literal } from 'sequelize';

22

import { User } from './models/User';

33
44

class TestModel extends Model {

@@ -11,8 +11,27 @@ TestModel.update({}, { where: {}, returning: ['foo'] });

1111
1212
1313

User.update({}, { where: {} });

14+

User.update({

15+

id: 123,

16+

username: fn('FN'),

17+

firstName: col('id'),

18+

lastName: literal('Smith'),

19+

}, { where: {} });

1420

User.update({}, { where: {}, returning: true });

1521

User.update({}, { where: {}, returning: false });

1622

User.update({}, { where: {}, returning: ['username'] });

17-

// @ts-expect-error

23+

User.build().update({

24+

id: 123,

25+

username: fn('FN'),

26+

firstName: col('id'),

27+

lastName: literal('Smith'),

28+

});

29+

// @ts-expect-error invalid `returning`

1830

User.update({}, { where: {}, returning: ['foo'] });

31+

// @ts-expect-error no `where`

32+

User.update({}, {});

33+

// @ts-expect-error invalid attribute

34+

User.update({ foo: '<bar>' }, { where: {} });

35+

// @ts-expect-error invalid attribute

36+

User.build().update({ foo: '<bar>' });

37+