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

IFindOptions can be generic ? #161

Closed
bilby91 opened this issue Oct 11, 2017 · 9 comments
Closed

IFindOptions can be generic ? #161

bilby91 opened this issue Oct 11, 2017 · 9 comments

Comments

@bilby91
Copy link
Contributor

bilby91 commented Oct 11, 2017

Hello,

I was wondering if IFindOptions could be a generic type. Sequelize WhereOptions are defined as:

type WhereOptions<T> = {
        [P in keyof T]?: string | number | WhereLogic | WhereOptions<T[P]> | col | and | or | WhereGeometryOptions | Array<string | number> | null;
    };

If we make IFindOptions generic we can take advantage of typesafety on the where key. Basically, https://github.com/RobinBuschmann/sequelize-typescript/blob/master/lib/interfaces/IFindOptions.ts#L14 would be:

import {WhereOptions} from "sequelize";

export interface IFindOptions<T> extends LoggingOptions, SearchPathOptions {
  where?: WhereOptions<T>
}
@RobinBuschmann
Copy link
Member

Yeah, makes perfect sense. So that all the static functions in Model.d.ts needs to be adjusted like findAll, which would become static findAll<T extends Model<T>>(options?: IFindOptions<T>): Promise<T[]>;

bilby91 added a commit to suttna/sequelize-typescript that referenced this issue Oct 11, 2017
@bilby91
Copy link
Contributor Author

bilby91 commented Oct 11, 2017

Hey @RobinBuschmann, PR submitted :)

@bilby91
Copy link
Contributor Author

bilby91 commented Oct 11, 2017

Awesome!!

Can we get a new beta version ?

Thanks as usual for the super fast response time!

@RobinBuschmann
Copy link
Member

Strange, the PR build succeeded but after it was merged, the build fails now. Can see the issue? tsc cannot compile some specs anymore. Can you take care of it? Otherwise I will do it tomorrow.

@bilby91
Copy link
Contributor Author

bilby91 commented Oct 11, 2017

@RobinBuschmann Check #163

@RobinBuschmann
Copy link
Member

Perfect, thanks! I will publish a new beta-release now

@RobinBuschmann
Copy link
Member

RobinBuschmann commented Oct 12, 2017

Its done! npm install sequelize-typescript@next

@schmod
Copy link
Contributor

schmod commented Oct 13, 2017

I don't quite see how this accomplishes the same thing as WhereOptions<T> in @types/sequelize, given how is typically passed into FindOptions:

findAll<TCustomAttributes>(options?: FindOptions<TAttributes & TCustomAttributes>): Promise<TInstance[]>;

In that case, the user is allowed to generify findAll to search on properties present on the model, or some other field(??) as defined in the TCustomAttributes generic.

@types/sequelize has virtually no test coverage of findAll being used in conjunction with generics, and I suspect that there are a lot of pitfalls here that we're going to be exposing ourselves to, given that their tests are all run against a very forgiving Model<any>.

In particular, I'm also concerned that this may have broken or impaired our ability to use custom operators (ie. the legacy '$and', or the current Op.and) to build complex where queries, and have removed support for passing arrays and/or things like sequelize.and into a where clause.

Admittedly, I have no clue how arrays or sequelize.and were ever intended to work as a root-level where clause. It will also be difficult for us to add typesafety around operators like Op.and until TypeScript figures out whether or not it wants to allow ES6 symbols to be used as literals.

@schmod
Copy link
Contributor

schmod commented Oct 13, 2017

As an aside, I have vague concerns about this library's "drift" away from @types/sequelize, and its heavy reliance on static methods on Model, which has led to verbose code and hard-to-debug errors in our codebase (ie. having to write out User.findAll<User>)

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

No branches or pull requests

3 participants