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:
@@ -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)
|
.into(Image)
|
||||||
.values(data)
|
.values(data)
|
||||||
|
|
||||||
// TODO: remove if statement once this issue is resolved https://github.com/typeorm/typeorm/issues/9850
|
|
||||||
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
|
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
|
||||||
const rawImages = await queryBuilder.execute()
|
const rawImages = await queryBuilder.execute()
|
||||||
return rawImages.generatedMaps.map((d) => this.create(d)) as Image[]
|
return rawImages.generatedMaps.map((d) => this.create(d)) as Image[]
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ export const MoneyAmountRepository = dataSource
|
|||||||
.into(MoneyAmount)
|
.into(MoneyAmount)
|
||||||
.values(data)
|
.values(data)
|
||||||
|
|
||||||
// TODO: remove if statement once this issue is resolved https://github.com/typeorm/typeorm/issues/9850
|
|
||||||
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
|
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
|
||||||
const rawMoneyAmounts = await queryBuilder.execute()
|
const rawMoneyAmounts = await queryBuilder.execute()
|
||||||
return rawMoneyAmounts.generatedMaps.map((d) =>
|
return rawMoneyAmounts.generatedMaps.map((d) =>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { In } from "typeorm"
|
|||||||
import { ProductTag } from "../models/product-tag"
|
import { ProductTag } from "../models/product-tag"
|
||||||
import { ExtendedFindConfig } from "../types/common"
|
import { ExtendedFindConfig } from "../types/common"
|
||||||
import { dataSource } from "../loaders/database"
|
import { dataSource } from "../loaders/database"
|
||||||
|
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"
|
||||||
|
|
||||||
type UpsertTagsInput = (Partial<ProductTag> & {
|
type UpsertTagsInput = (Partial<ProductTag> & {
|
||||||
value: string
|
value: string
|
||||||
@@ -26,18 +27,32 @@ export type FindWithoutRelationsOptions = DefaultWithoutRelations & {
|
|||||||
export const ProductTagRepository = dataSource
|
export const ProductTagRepository = dataSource
|
||||||
.getRepository(ProductTag)
|
.getRepository(ProductTag)
|
||||||
.extend({
|
.extend({
|
||||||
async listTagsByUsage(count = 10): Promise<ProductTag[]> {
|
async insertBulk(
|
||||||
return await this.query(
|
data: QueryDeepPartialEntity<ProductTag>[]
|
||||||
`
|
): Promise<ProductTag[]> {
|
||||||
SELECT id, COUNT(pts.product_tag_id) as usage_count, pt.value
|
const queryBuilder = this.createQueryBuilder()
|
||||||
FROM product_tag pt
|
.insert()
|
||||||
LEFT JOIN product_tags pts ON pt.id = pts.product_tag_id
|
.into(ProductTag)
|
||||||
GROUP BY id
|
.values(data)
|
||||||
ORDER BY usage_count DESC
|
|
||||||
LIMIT $1
|
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
|
||||||
`,
|
const rawTags = await queryBuilder.execute()
|
||||||
[count]
|
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[]> {
|
async upsertTags(tags: UpsertTagsInput): Promise<ProductTag[]> {
|
||||||
@@ -52,6 +67,7 @@ export const ProductTagRepository = dataSource
|
|||||||
)
|
)
|
||||||
|
|
||||||
const upsertedTags: ProductTag[] = []
|
const upsertedTags: ProductTag[] = []
|
||||||
|
const tagsToCreate: QueryDeepPartialEntity<ProductTag>[] = []
|
||||||
|
|
||||||
for (const tag of tags) {
|
for (const tag of tags) {
|
||||||
const aTag = existingTagsMap.get(tag.value)
|
const aTag = existingTagsMap.get(tag.value)
|
||||||
@@ -59,11 +75,15 @@ export const ProductTagRepository = dataSource
|
|||||||
upsertedTags.push(aTag)
|
upsertedTags.push(aTag)
|
||||||
} else {
|
} else {
|
||||||
const newTag = this.create(tag)
|
const newTag = this.create(tag)
|
||||||
const savedTag = await this.save(newTag)
|
tagsToCreate.push(newTag as QueryDeepPartialEntity<ProductTag>)
|
||||||
upsertedTags.push(savedTag)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (tagsToCreate.length) {
|
||||||
|
const newTags = await this.insertBulk(tagsToCreate)
|
||||||
|
upsertedTags.push(...newTags)
|
||||||
|
}
|
||||||
|
|
||||||
return upsertedTags
|
return upsertedTags
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ export const StagedJobRepository = dataSource.getRepository(StagedJob).extend({
|
|||||||
.into(StagedJob)
|
.into(StagedJob)
|
||||||
.values(jobToCreates)
|
.values(jobToCreates)
|
||||||
|
|
||||||
// TODO: remove if statement once this issue is resolved https://github.com/typeorm/typeorm/issues/9850
|
|
||||||
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
|
if (!queryBuilder.connection.driver.isReturningSqlSupported("insert")) {
|
||||||
const rawStagedJobs = await queryBuilder.execute()
|
const rawStagedJobs = await queryBuilder.execute()
|
||||||
return rawStagedJobs.generatedMaps.map((d) => this.create(d))
|
return rawStagedJobs.generatedMaps.map((d) => this.create(d))
|
||||||
|
|||||||
@@ -326,12 +326,12 @@ class ProductService extends TransactionBaseService {
|
|||||||
return await productTypeRepository.find({})
|
return await productTypeRepository.find({})
|
||||||
}
|
}
|
||||||
|
|
||||||
async listTagsByUsage(count = 10): Promise<ProductTag[]> {
|
async listTagsByUsage(take = 10): Promise<ProductTag[]> {
|
||||||
const productTagRepo = this.activeManager_.withRepository(
|
const productTagRepo = this.activeManager_.withRepository(
|
||||||
this.productTagRepository_
|
this.productTagRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
return await productTagRepo.listTagsByUsage(count)
|
return await productTagRepo.listTagsByUsage(take)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user