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

chore(medusa): Migrate product tag repository #3726

Merged
merged 6 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-lies-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

chore(medusa): Migrate product tag repository
1 change: 0 additions & 1 deletion packages/medusa/src/repositories/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const ImageRepository = dataSource.getRepository(Image).extend({
.into(Image)
.values(data)

// TODO: remove if statement once this issue is resolved https://github.com/typeorm/typeorm/issues/9850
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
const rawImages = await queryBuilder.execute()
return rawImages.generatedMaps.map((d) => this.create(d)) as Image[]
Expand Down
1 change: 0 additions & 1 deletion packages/medusa/src/repositories/money-amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const MoneyAmountRepository = dataSource
.into(MoneyAmount)
.values(data)

// TODO: remove if statement once this issue is resolved https://github.com/typeorm/typeorm/issues/9850
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
const rawMoneyAmounts = await queryBuilder.execute()
return rawMoneyAmounts.generatedMaps.map((d) =>
Expand Down
48 changes: 34 additions & 14 deletions packages/medusa/src/repositories/product-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { In } from "typeorm"
import { ProductTag } from "../models/product-tag"
import { ExtendedFindConfig } from "../types/common"
import { dataSource } from "../loaders/database"
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"

type UpsertTagsInput = (Partial<ProductTag> & {
value: string
Expand All @@ -26,18 +27,32 @@ export type FindWithoutRelationsOptions = DefaultWithoutRelations & {
export const ProductTagRepository = dataSource
.getRepository(ProductTag)
.extend({
async listTagsByUsage(count = 10): Promise<ProductTag[]> {
return await this.query(
`
SELECT id, COUNT(pts.product_tag_id) as usage_count, pt.value
FROM product_tag pt
LEFT JOIN product_tags pts ON pt.id = pts.product_tag_id
GROUP BY id
ORDER BY usage_count DESC
LIMIT $1
`,
[count]
)
async insertBulk(
data: QueryDeepPartialEntity<ProductTag>[]
): Promise<ProductTag[]> {
const queryBuilder = this.createQueryBuilder()
.insert()
.into(ProductTag)
.values(data)

if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
const rawTags = await queryBuilder.execute()
return rawTags.generatedMaps.map((d) => this.create(d)) as ProductTag[]
}

const rawTags = await queryBuilder.returning("*").execute()
return rawTags.generatedMaps.map((d) => this.create(d))
},

async listTagsByUsage(take = 10): Promise<ProductTag[]> {
const qb = this.createQueryBuilder("pt")
.select(["id", "COUNT(pts.product_tag_id) as usage_count", "value"])
.leftJoin("product_tags", "pts", "pt.id = pts.product_tag_id")
.groupBy("id")
.orderBy("usage_count", "DESC")
.limit(take)

return await qb.getRawMany()
},

async upsertTags(tags: UpsertTagsInput): Promise<ProductTag[]> {
Expand All @@ -52,18 +67,23 @@ export const ProductTagRepository = dataSource
)

const upsertedTags: ProductTag[] = []
const tagsToCreate: QueryDeepPartialEntity<ProductTag>[] = []

for (const tag of tags) {
const aTag = existingTagsMap.get(tag.value)
if (aTag) {
upsertedTags.push(aTag)
} else {
const newTag = this.create(tag)
const savedTag = await this.save(newTag)
upsertedTags.push(savedTag)
tagsToCreate.push(newTag as QueryDeepPartialEntity<ProductTag>)
}
}

if (tagsToCreate.length) {
const newTags = await this.insertBulk(tagsToCreate)
upsertedTags.push(...newTags)
}

return upsertedTags
},

Expand Down
1 change: 0 additions & 1 deletion packages/medusa/src/repositories/staged-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const StagedJobRepository = dataSource.getRepository(StagedJob).extend({
.into(StagedJob)
.values(jobToCreates)

// TODO: remove if statement once this issue is resolved https://github.com/typeorm/typeorm/issues/9850
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
const rawStagedJobs = await queryBuilder.execute()
return rawStagedJobs.generatedMaps.map((d) => this.create(d))
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,12 @@ class ProductService extends TransactionBaseService {
return await productTypeRepository.find({})
}

async listTagsByUsage(count = 10): Promise<ProductTag[]> {
async listTagsByUsage(take = 10): Promise<ProductTag[]> {
const productTagRepo = this.activeManager_.withRepository(
this.productTagRepository_
)

return await productTagRepo.listTagsByUsage(count)
return await productTagRepo.listTagsByUsage(take)
}

/**
Expand Down