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

fix(types): change attributes type to 'object | undefined' #13467

Closed
wants to merge 4 commits into from

Conversation

aryem
Copy link
Contributor

@aryem aryem commented Aug 28, 2021

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

change attributes type to 'object':

  • attributes param in QueryInterface.bulkInsert() change to 'object | undefined' from 'string[] | string | undefined'.
  • attributes param in QueryInterface.bulkUpdate() change to 'object | undefined' from 'string[] | string | undefined'.
  • add '{}' as last param in bulkInsert() & bulkUpdate() tests

Closes #13466

 change attributes type to 'object':
 - attributes param in QueryInterface.bulkInsert() change to 'object | undefined' from 'string[] | string | undefined'.
 - attributes param in QueryInterface.bulkUpdate() change to 'object | undefined' from 'string[] | string | undefined'.
 - add '{}' as last param in bulkInsert() & bulkUpdate() tests

Closes sequelize#13466
Copy link
Member

@wbourne0 wbourne0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, why are you using the query interface directly? Is this correct on the model definition but not queryInterface?

@@ -498,7 +498,7 @@ export class QueryInterface {
tableName: TableName,
records: object[],
options?: QueryOptions,
attributes?: string[] | string
attributes?: object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't right either actually.

I don't know if there's already a type for this or not. At the top of my head, it should be moreso like:

type Attribute = Literal | string | [Literal | Fn | Col , string]

type Attributes = Array<Attribute> | { include?: Array<Attribute>, exclude: Array<Attribute> }

note: this is mostly psuedocode, I don't remember the exact typings for col, fn, and literals.

Copy link
Contributor Author

@aryem aryem Oct 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allawesome497 Thanks for the suggestions and you are right. It isn't correct either. I think ModelAttributes from Model.d.ts seems to be appropriate here. What do you say?.

export type ModelAttributes<M extends Model = Model, TCreationAttributes = any> = {

I might be wrong here, But my suggestion are deduced from its usage in the following methods:

bulkInsertQuery(tableName, fieldValueHashes, options, fieldMappedAttributes) {

updateQuery(tableName, attrValueHash, where, options, attributes) {

Copy link
Member

@wbourne0 wbourne0 Oct 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such as this?

type Attribute<
  ModelAttr extends string
> = Literal | ModelAttr | [Literal | Fn | Col, ModelAttr];

type Attributes<
  ModelAttr extends string = string
> = Array<
  Attribute<ModelAttr> | {
    include?: Array<Attribute<ModelAttr>>,
    exclude:? Array<Attribute<ModelAttr>>,
  }
>;

In usage:

attributes?: Attributes<keyof ModelAttributes>

If so, then I think that would be the ideal solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allawesome497 Sorry, I don't understand why do we need { include?: Array<Attribute<ModelAttr>>, exclude:? Array<Attribute<ModelAttr>>, } and in what scenario Attribute could be Literal | ModelAttr | [Literal | Fn | Col ?.

Based on the usage in QueryInterface.BulkUpdate & QueryInterface.BulkInsert type of attributes should:

attributes?: ModelAttributes<any, any>

ModelAttributes interface is already defined in model.d.ts

export type ModelAttributes<M extends Model = Model, TCreationAttributes = any> = {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So { include?: Array<Attribute<ModelAttr>>, exclude:? Array<Attribute<ModelAttr>>, } is needed because you can specify those instead of an array. It's mentioned here, though it doesn't go into depth about it.

image

in what scenario Attribute could be Literal | ModelAttr | [Literal | Fn | Col]
So sequelize allows the following attributes:

  • col: the name of a column. Might not be accepted in sequelize, not sure.
  • fn: A function that is to server as an attribute. Say, fn('COUNT', col('my_column')) for example. This might need to have an alias
  • literal: Literal sql to be injected as an attribute. I believe this needs an alias specified, so it would only be valid in that tuple.
  • [item, alias]: this is translated to ${item} AS ${alias}.

I could be wrong on a few things here though, since this is all from memory. I'm confident on fn and literal being usable in attributes though, at least with an alias specified.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I've recently seen another type that I didn't realized existed - SequelizeMethod. This is going to be what we want, rather than Literal | Fn | Col as those functions extend this.

@wbourne0 wbourne0 added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. labels Oct 3, 2021
 - In BulkUpdate function update attributes type to ModelAttributes.
 - In BulkInsert function update attributes type to ModelAttributes.
@github-actions github-actions bot added the stale label Oct 31, 2021
@github-actions github-actions bot closed this Nov 6, 2021
@fzn0x fzn0x reopened this Nov 10, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
@github-actions github-actions bot added the stale label Nov 30, 2021
@sdepold sdepold removed the stale label Dec 22, 2021
@WikiRik WikiRik added status: awaiting maintainer and removed status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action labels Dec 26, 2021
@github-actions github-actions bot added the stale label Jan 10, 2022
@ephys ephys self-assigned this Jan 28, 2022
@ephys ephys self-requested a review January 28, 2022 10:12
@ephys
Copy link
Member

ephys commented Feb 26, 2022

We merged #13945 a little while ago which updated bulkInsert. I'm pretty sure the new typings for bulkInsert are correct and should be an object instead of an array based on their usage in query-generator.js

bulkUpdate hasn't been touched by that other PR however based on their usage in updateQuery in query-generator, it looks like their type should be an object too, not an array

If you're still willing to update this pretty old PR (sorry about that), I can take a look into merging this

@ephys
Copy link
Member

ephys commented Dec 7, 2022

Looks like this has since been fixed by another. Thank you for opening this one nonetheless. Sorry we never got around to merging it :)

@ephys ephys closed this Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typing for QueryInterface's bulkInsert & bulkUpdate attributes parameter is incorrect
6 participants