From 568b33e4c6edc0db640cf8dcc2cbf6e04dc92cc5 Mon Sep 17 00:00:00 2001 From: Fenying Date: Fri, 4 May 2018 03:37:35 +0800 Subject: [PATCH] [sequelize] Added exact key matching and partial update. (#25311) * More exactly key matching. * Applied more exact limitation on updating values --- types/sequelize/index.d.ts | 43 +++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/types/sequelize/index.d.ts b/types/sequelize/index.d.ts index fd56cdbd7c..4adf48948d 100644 --- a/types/sequelize/index.d.ts +++ b/types/sequelize/index.d.ts @@ -2819,12 +2819,15 @@ declare namespace sequelize { /** * Get the value of the underlying data value */ - getDataValue(key: string): any; + getDataValue(key: keyof TAttributes): any; /** * Update the underlying data value */ - setDataValue(key: string, value: any): void; + setDataValue( + key: K, + value: TAttributes[K] + ): void; /** * If no key is given, returns all values of the instance, also invoking virtual getters. @@ -2834,7 +2837,7 @@ declare namespace sequelize { * * @param options.plain If set to true, included instances will be returned as plain objects */ - get(key: string, options?: { plain?: boolean, clone?: boolean }): any; + get(key: keyof TAttributes, options?: { plain?: boolean, clone?: boolean }): any; get(options?: { plain?: boolean, clone?: boolean }): TAttributes; /** @@ -2861,9 +2864,17 @@ declare namespace sequelize { * @param options.raw If set to true, field and virtual setters will be ignored * @param options.reset Clear all previously set data values */ - set(key: string, value: any, options?: InstanceSetOptions): this; + set( + key: K, + value: TAttributes[K], + options?: InstanceSetOptions + ): this; set(keys: Object, options?: InstanceSetOptions): this; - setAttributes(key: string, value: any, options?: InstanceSetOptions): this; + setAttributes( + key: K, + value: TAttributes[K], + options?: InstanceSetOptions + ): this; setAttributes(keys: Object, options?: InstanceSetOptions): this; /** @@ -2874,13 +2885,13 @@ declare namespace sequelize { * * If changed is called without an argument and no keys have changed, it will return `false`. */ - changed(key: string): boolean; + changed(key: keyof TAttributes): boolean; changed(): boolean | string[]; /** * Returns the previous value for key from `_previousDataValues`. */ - previous(key: string): any; + previous(key: keyof TAttributes): any; /** * Validate this instance, and if the validation passes, persist it to the database. @@ -2912,9 +2923,17 @@ declare namespace sequelize { /** * This is the same as calling `set` and then calling `save`. */ - update(key: string, value: any, options?: InstanceUpdateOptions): Promise; + update( + key: K, + value: TAttributes[K], + options?: InstanceUpdateOptions + ): Promise; update(keys: Object, options?: InstanceUpdateOptions): Promise; - updateAttributes(key: string, value: any, options?: InstanceUpdateOptions): Promise; + updateAttributes( + key: K, + value: TAttributes[K], + options?: InstanceUpdateOptions + ): Promise; updateAttributes(keys: Object, options?: InstanceUpdateOptions): Promise; /** @@ -2948,7 +2967,7 @@ declare namespace sequelize { * If an array is provided, the same is true for each column. * If and object is provided, each column is incremented by the value given. */ - increment(fields: string | string[] | Object, + increment(fields: Partial | Array | keyof TAttributes, options?: InstanceIncrementDecrementOptions): Promise; /** @@ -2971,7 +2990,7 @@ declare namespace sequelize { * If an array is provided, the same is true for each column. * If and object is provided, each column is decremented by the value given */ - decrement(fields: string | string[] | Object, + decrement(fields: Partial | Array | keyof TAttributes, options?: InstanceIncrementDecrementOptions): Promise; /** @@ -4050,7 +4069,7 @@ declare namespace sequelize { * elements. The first element is always the number of affected rows, while the second element is the actual * affected rows (only supported in postgres with `options.returning` true.) */ - update(values: TAttributes, options?: UpdateOptions): Promise<[number, TInstance[]]>; + update(values: Partial, options?: UpdateOptions): Promise<[number, TInstance[]]>; /** * Run a describe query on the table. The result will be return to the listener as a hash of attributes and