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

Add types declaration file, update objection to ^2.2.0 #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

geopic
Copy link

@geopic geopic commented Jul 4, 2020

Resolves #17. As mentioned in the issue, updating objection to a more recent version is required to allow TypeScript support. It works fine from my own tests working from the code in the Readme but if there are any issues then please let me know.

Comment on lines +4 to +12
class SDQueryBuilder<M extends Model> {
SingleQueryBuilderType: SDQueryBuilder<M>;

delete(): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
hardDelete(): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
undelete(): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
whereDeleted(): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
whereNotDeleted(): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
}
Copy link

@loteoo loteoo Sep 30, 2020

Choose a reason for hiding this comment

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

I manually used your PR in my project, but I made the types for the SDQueryBuilder class as such:

    import { Model, QueryBuilder } from 'objection';

    class SDQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<M, R> {
        ArrayQueryBuilderType!: SDQueryBuilder<M>;

        NumberQueryBuilderType!: SDQueryBuilder<M, number>;

        delete(): this['NumberQueryBuilderType'];
        hardDelete(): this['NumberQueryBuilderType'];
        undelete(): this['NumberQueryBuilderType'];
        whereDeleted(): this['ArrayQueryBuilderType'];
        whereNotDeleted(): this['ArrayQueryBuilderType'];
    }

// And changed SDInstance to:

    interface SDInstance<T extends typeof Model> {
        QueryBuilderType: SDQueryBuilder<this>;
    }

Copy link
Author

Choose a reason for hiding this comment

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

Looks cleaner. If it works then all's good 👍

Copy link

@loteoo loteoo Oct 13, 2020

Choose a reason for hiding this comment

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

Just updated the comment, we didn't need the SingleQueryBuilderType and the PageQueryBuilderType so I removed them. We would need them if the plugin added methods like .findById() and .page().

.delete(), .hardDelete() and undelete() internally use .patch(), which returns the NumberQueryBuilder.

.whereDeleted() and .whereNotDeleted() internally use .where() and .whereNot(), which returns the ArrayQueryBuilder.

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

Successfully merging this pull request may close these issues.

TypeScript types declaration file
4 participants