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

HPC-8569: Add count function and distinct option to models library #152

Merged
merged 2 commits into from
Aug 6, 2024

Conversation

manelcecs
Copy link
Contributor

@manelcecs manelcecs commented Jul 11, 2024

This PR aims to add support for common database functions, such as:

  • count
  • distinct
  • orderBy: 'nulls last' (delayed until Knex gets updated)

This changes applies to the previously defined function: 'find' - and add a new function: 'count'

@manelcecs manelcecs requested a review from a team as a code owner July 11, 2024 09:10
@manelcecs manelcecs added ready for review All comments have been addressed, and the Pull Request is ready for review needs unit tests labels Jul 11, 2024
Copy link
Contributor

@Pl217 Pl217 left a comment

Choose a reason for hiding this comment

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

Thanks for adding these new capabilities to our model library. New options are awesome!

I didn't even know about some possibilities, like DISTINCT ON, which made me read a lot of documentation and play around with PostgreSQL. I did spend entire afternoon researching about various options and thinking about our needs, much more than I anticipated when I started review :/

My responses are long and detailed, but addressing my review comments should take little time.

@@ -45,6 +45,8 @@ export type FindFn<F extends FieldDefinition, AdditionalArgs = {}> = (
*/
skipValidation?: boolean;
trx?: Knex.Transaction<any, any>;
orderByNullsLast?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

orderByNullsLast should be an option in OrderByCond type, because it is related to ordering, not directly related to find. What's more, if we look at Knex docs for orderBy function, we see that it has single and multi-column variant.

If you look at this part of our code

if (orderBy !== undefined) {
if (!Array.isArray(orderBy)) {
orderBy = [orderBy];
}
query.orderBy(orderBy);
}

you can see that we're always using multi-column approach, because we are defining OrderByCond as an object:
export type OrderByCond<F extends FieldDefinition> = {
column: keyof InstanceDataOf<F>;
order?: 'asc' | 'desc';
};

Knex docs give this example:

knex('users').orderBy([
  { column: 'email' },
  { column: 'age', order: 'desc', nulls: 'last' },
]);

So, since we already support column and order keys, we should just add third optional key nulls?: 'first' | 'last'; to OrderByCond and this feature will be fully usable with our models.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not found that example because that is what we needed - definitely I'll be adding that change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Crap, we cannot use nulls option, because it only exists in newer versions of Knex. The feature was added by this PR, which was released in v0.95.12 of Knex.

There was also a bug with the implementation in PostgreSQL, which was resolved in this PR. That was released in v1.0.2, so we may need to update to at least that version.

I think we should update now and HPC-8383 should be a blocker for this work, because we are long overdue to update Knex, since we are at v0.21.1, released 28 April, 2020!

src/db/util/raw-model.ts Outdated Show resolved Hide resolved
src/db/util/raw-model.ts Show resolved Hide resolved
src/db/util/raw-model.ts Show resolved Hide resolved
src/db/util/raw-model.ts Show resolved Hide resolved
src/db/util/raw-model.ts Outdated Show resolved Hide resolved
src/db/util/raw-model.ts Outdated Show resolved Hide resolved
@Pl217 Pl217 removed their assignment Jul 11, 2024
@Pl217 Pl217 added needs minor changes There are review or issue comments to address and removed ready for review All comments have been addressed, and the Pull Request is ready for review labels Jul 11, 2024
@manelcecs manelcecs added ready for review All comments have been addressed, and the Pull Request is ready for review and removed needs minor changes There are review or issue comments to address labels Jul 15, 2024
@manelcecs manelcecs assigned Pl217 and unassigned manelcecs Jul 15, 2024
Copy link
Contributor

@Pl217 Pl217 left a comment

Choose a reason for hiding this comment

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

We cannot add nulls option until we update to a more recent version of Knex, as I wrote in this comment.

If you want you can drop the first commit which adds nulls option and merge the rest (after addressing really minor comments I have inline now), but you may also want to block the entire work on HPC-8569 until Knex is updated, because you cannot add nulls last to your queries.

Discuss with Kashif (once he's back next week) whether we want to update Knex now and block this work. Otherwise, I would implement nulls last functionality in code and write a TODO comment to refactor it once we can add that option in models library.

src/db/util/raw-model.ts Outdated Show resolved Hide resolved
src/db/util/raw-model.ts Outdated Show resolved Hide resolved
@Pl217 Pl217 assigned manelcecs and unassigned Pl217 Jul 16, 2024
@Pl217 Pl217 added needs minor changes There are review or issue comments to address and removed ready for review All comments have been addressed, and the Pull Request is ready for review labels Jul 16, 2024
@manelcecs manelcecs removed the needs minor changes There are review or issue comments to address label Jul 18, 2024
@manelcecs manelcecs force-pushed the HPC-8569 branch 4 times, most recently from 43cb236 to f3bfe69 Compare July 23, 2024 12:17
@manelcecs
Copy link
Contributor Author

We cannot add nulls option until we update to a more recent version of Knex, as I wrote in this comment.

If you want you can drop the first commit which adds nulls option and merge the rest (after addressing really minor comments I have inline now), but you may also want to block the entire work on HPC-8569 until Knex is updated, because you cannot add nulls last to your queries.

Discuss with Kashif (once he's back next week) whether we want to update Knex now and block this work. Otherwise, I would implement nulls last functionality in code and write a TODO comment to refactor it once we can add that option in models library.

@Pl217 After discuss this issue with Kashif - We have decided to drop the commit and after the implementation of the new version of Knex we update both HPC-API-CORE in order to add the 'nulls last' functionality and after, update HPC-API to add the order to the needed queries.

@Pl217 Pl217 changed the title HPC-8569: create and update model functions HPC-8569: Add count function and distinct option to models library Aug 6, 2024
@Pl217 Pl217 merged commit 34a04c5 into develop Aug 6, 2024
2 checks passed
@Pl217 Pl217 deleted the HPC-8569 branch August 6, 2024 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants