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

WIP feat(perf): Upgrade typeorm version from v0.3.11 to v0.3.20 #2686

Closed

Conversation

monrostar
Copy link
Contributor

@monrostar monrostar commented Feb 20, 2024

Description

Please include a summary of the changes and the related issue.

Breaking changes

Does this PR include any breaking changes we should be aware of?

Screenshots

You can add screenshots here if applicable.

Checklist

📌 Always:

  • I have set a clear title
  • My PR is small and contains a single feature
  • I have checked my own PR

👍 Most of the time:

  • I have added or updated test cases
  • I have updated the README if needed

@monrostar
Copy link
Contributor Author

monrostar commented Feb 20, 2024

The first change that needed to be made was to replace connection.namingStrategy.eagerJoinRelationAlias
because it was removed in this commit fix: resolve issue building eager relation alias for nested relations

image

the current state of vendure/core tests is such after the changes. Now I will edit the data types for QueryBuilder. All errors indicate that there were changes in typeorm that broke the backward capability with the current version

cross-env PACKAGE=core vitest --config ../../e2e-common/vitest.config.ts --run
image

@monrostar monrostar changed the title feat(core): Upgrade typeorm version from v0.3.11 to v0.3.20 WIP feat(core): Upgrade typeorm version from v0.3.11 to v0.3.20 Feb 20, 2024
@monrostar
Copy link
Contributor Author

monrostar commented Feb 20, 2024

New update regarding core tests

cross-env PACKAGE=core vitest --config ../../e2e-common/vitest.config.ts --run
image

@asonnleitner
Copy link
Contributor

@michaelbromley Could this PR also include changing the name of connection to dataSource and all related items, such as renaming dbConnectionOptions to dataSourceOptions, etc.? As mentioned here:

https://github.com/typeorm/typeorm/blob/83567f533482d0170c35e2f99e627962b8f99a08/src/data-source/DataSource.ts#L47-L54

Additionally, considering the naming convention, TransactionalConnection could be renamed to TransactionalDataSource, among other related changes. Both the connection and dataSource terminologies can be used for some time, with the connection-related items being annotated with @deprecated to indicate their phased removal.

@monrostar
Copy link
Contributor Author

monrostar commented Feb 20, 2024

Unfortunately, it turned out to be impossible to use DefaultNameStrategy from TypeOrm because inside SQL eager relations we have Alias' name with two __ and in DefaultNameStrategy with only one _.

So we have a difference of one underscore _.

For this variant of events, we can create our NamingStrategy pattern. But maybe there exists the way to remove double underscores

example:

SELECT
"facet". "createdAt" AS "facet_createdAt",
"facet" "updatedAt" AS "facet_updatedAt", "facet" "isPrivate" AS "facet_isPrivate", "facet". "code" AS "facet_code", "facet". "id" AS "facet_id", "face
__translations". "createdAt" AS "facet__translations_createdAt" ,
"facet__translations" "updatedAt" AS "facet__translations_updatedAt", "facet__translations" "languageCode" AS "facet__translations_la
nguageCode",
"facet__translations". "name" AS "facet__translations_name", "facet__translations". "id" AS "facet__translations_id",
"facet__translations". "baseId" AS "facet__translations_baseId" FROM "
public". "facet" "facet" LEFT JOIN "public". "facet_translation" "facet__translations" ON "facet__translations". "baseId"="facet". "id" LEFT JOIN "public". "facet_channels_channel" "facet_lqb__channel"
ON "facet_lqb__channel". "facetId"="facet". "id" LEFT JOIN "public". "channel" "Zqb__channel" ON "Zqb__channel". "id"="facet_lqb__channel". "channelId" WHERE (facet_translations name ILIKE $1) AND "Zqb__
channel". "id" = $2 ORDER BY "facet" "createdAt" DESC

Progress in tests:

cross-env PACKAGE=core vitest --config ../../e2e-common/vitest.config.ts --run
image

@michaelbromley
Copy link
Member

@monrostar great progress! Thanks for keeping us updated.

@asonnleitner regarding renaming to "dataSource" - I don't see this as a priority at the moment. We could support both, e.g. have config.dataSourceOptions be an alias to config.dbConnectionOptions but I don't see a compelling reason to add the complexity. It means more code to handle 2 code paths internally, and a bigger API surface for users.

@monrostar
Copy link
Contributor Author

monrostar commented Feb 21, 2024

Progress in tests:

cross-env PACKAGE=core vitest --config ../../e2e-common/vitest.config.ts --run

At the moment, most of the tests that failed are related to a filtration error that I will soon solve

image

@monrostar
Copy link
Contributor Author

monrostar commented Mar 1, 2024

42764b4
Related issue typeorm/typeorm#10063
This commit includes entity relationship changes because they are mostly related to ManyToOne, ManyToMany or other relationships that don't have a reverse side relationship for relationLoadStrategy: 'query'. Because the reverse relations were missing, Typeform cannot optimize queries on its own.

Progress in tests:

cross-env PACKAGE=core vitest --config ../../e2e-common/vitest.config.ts --run

image

@monrostar
Copy link
Contributor Author

monrostar commented Mar 1, 2024

ddaaaf2
A related issue in the Collection entity Tree decorators: typeorm/typeorm#9936

Special fixes and refactoring for the test case in e2e for nesting in collections

it('nested parent field in shop API', async () => {

image

@michaelbromley Please review this code. Do you agree with my solution?

The tests that remain to be fixed
image

@monrostar monrostar changed the title WIP feat(core): Upgrade typeorm version from v0.3.11 to v0.3.20 WIP feat(perf): Upgrade typeorm version from v0.3.11 to v0.3.20 Mar 1, 2024
@monrostar
Copy link
Contributor Author

monrostar commented Mar 2, 2024

ddaaaf2 A related issue in the Collection entity Tree decorators: typeorm/typeorm#9936

Special fixes and refactoring for the test case in e2e for nesting in collections

it('nested parent field in shop API', async () => {

image @michaelbromley Please review this code. Do you agree with my solution?

The tests that remain to be fixed image

I would suggest adding property to the system to add a link to relation properties that have Tree decorators, then we can work around this bug until it is fixed. Like I was added collection and collection.children and collection.parent, but we can do it more dynamically
This can be a temporary setting in vendureConfig with which you can identify entities that have these problems and just create joins according to the scheme I created

@monrostar
Copy link
Contributor Author

monrostar commented Mar 3, 2024

ddaaaf2 A related issue in the Collection entity Tree decorators: typeorm/typeorm#9936
Special fixes and refactoring for the test case in e2e for nesting in collections

it('nested parent field in shop API', async () => {

image @michaelbromley Please review this code. Do you agree with my solution? The tests that remain to be fixed image

I would suggest adding property to the system to add a link to relation properties that have Tree decorators, then we can work around this bug until it is fixed. Like I was added collection and collection.children and collection.parent, but we can do it more dynamically This can be a temporary setting in vendureConfig with which you can identify entities that have these problems and just create joins according to the scheme I created

32e0df7
@michaelbromley
This is what I was able to achieve when I made this function fully dynamic, now even if someone adds new Tree entities it won't be a problem.

image

@monrostar
Copy link
Contributor Author

monrostar commented Mar 3, 2024

f9f5260
Fixes for filtering on translations that were used in join with different aliases due to incorrect aliases

In the method, I started using the original qb.alias everywhere, and also replaced rawConnection with qb.connection

image image

@monrostar
Copy link
Contributor Author

monrostar commented Mar 4, 2024

2219b44

In this test, we have a preset of only 2 variants. I don't understand why the test was for 6 calls if we have only 2 calls to the strategy
image

@michaelbromley It was your commit, so I need to make sure I didn't break any logic here that you were adding.

monrostar@a09c2b2

@michaelbromley
Copy link
Member

@monrostar This is interesting. At first I thought the 6 calls was due to the fact that each variant is returning 3 price fields. But thinking about it further, the price and priceWithTax fields on the variant are derived from the one true price value in prices.price.

So yes, 2 calls to the transformer does indeed make more sense.

Yet, in the current version (pre your changes to TypeORM), it is indeed called 6 times. I logged the calls inside the transformer:

from: (databaseValue: string): number => {
  console.log(`transformerFromSpy: ${databaseValue}`);
  // ...
}

and got:

transformerFromSpy: 31
transformerFromSpy: 999999900
transformerFromSpy: 31
transformerFromSpy: 999999900
transformerFromSpy: 31
transformerFromSpy: 999999900

so it looks like the transformer gets called 3x for each variant. Totally strange. No idea why this would change now, but the change seems to be correct.

@monrostar
Copy link
Contributor Author

b6b6663
image

I have now finalized this PR, I need you to check this code and also try to fix or find the problem in this test. I couldn't find a solution for this test, maybe it is the configuration that is the problem

describe('customPropertyMap', () => {

image

@michaelbromley

@monrostar monrostar marked this pull request as ready for review March 6, 2024 15:07
@monrostar
Copy link
Contributor Author

I also want to try this configuration at the driver level to find the remaining problematic queries related to collections

@michaelbromley
Copy link
Member

Closed by 0afc94e

Excellent work, thank you very much!

@monrostar monrostar deleted the feat/upgrade-typeorm branch March 16, 2024 18:21
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.

None yet

3 participants