Skip to content

Commit

Permalink
chore(medusa): Migrate product tag repository (medusajs#3726)
Browse files Browse the repository at this point in the history
* chore(medusa): Migrate product tag repository

* chore(medusa): cleanup

* chore(medusa): cleanup

* Create healthy-lies-eat.md

* chore(medusa): naming

* fix repo
  • Loading branch information
adrien2p authored Apr 5, 2023
1 parent 4d69d8e commit eab2d22
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
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

0 comments on commit eab2d22

Please sign in to comment.