chore(medusa): Migrate product tag repository (#3726)
* chore(medusa): Migrate product tag repository * chore(medusa): cleanup * chore(medusa): cleanup * Create healthy-lies-eat.md * chore(medusa): naming * fix repo
This commit is contained in:
committed by
GitHub
parent
4d69d8ef6a
commit
eab2d22f7d
5
.changeset/healthy-lies-eat.md
Normal file
5
.changeset/healthy-lies-eat.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
chore(medusa): Migrate product tag repository
|
||||
@@ -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[]
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
@@ -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
|
||||
@@ -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[]> {
|
||||
@@ -52,6 +67,7 @@ export const ProductTagRepository = dataSource
|
||||
)
|
||||
|
||||
const upsertedTags: ProductTag[] = []
|
||||
const tagsToCreate: QueryDeepPartialEntity<ProductTag>[] = []
|
||||
|
||||
for (const tag of tags) {
|
||||
const aTag = existingTagsMap.get(tag.value)
|
||||
@@ -59,11 +75,15 @@ export const ProductTagRepository = dataSource
|
||||
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
|
||||
},
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user