chore(medusa): migrate SalesChannel / ProductCategory repository (#3728)
* chore: Migrate sales channel repository * chore: migrated product categories * chore: remove alias + rename changset * chore(medusa): Cleanup publishable api key sales channel repo * rm comment --------- Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
This commit is contained in:
1 parent
30ee10fddd
commit
713d85a92c
5 files changed
+86
-67
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore(medusa): migrate SalesChannel / ProductCategory repository
|
||||||
@@ -44,14 +44,14 @@ export const ProductCategoryRepository = dataSource
|
|||||||
const options_ = { ...options }
|
const options_ = { ...options }
|
||||||
options_.where = options_.where as FindOptionsWhere<ProductCategory>
|
options_.where = options_.where as FindOptionsWhere<ProductCategory>
|
||||||
|
|
||||||
const legacySelect = buildLegacyFieldsListFrom(options_.select)
|
const columnsSelected = buildLegacyFieldsListFrom(options_.select)
|
||||||
const legacyRelations = buildLegacyFieldsListFrom(options_.relations)
|
const relationsSelected = buildLegacyFieldsListFrom(options_.relations)
|
||||||
|
|
||||||
const selectStatements = (relationName: string): string[] => {
|
const fetchSelectColumns = (relationName: string): string[] => {
|
||||||
const modelColumns = this.metadata.ownColumns.map(
|
const modelColumns = this.metadata.ownColumns.map(
|
||||||
(column) => column.propertyName
|
(column) => column.propertyName
|
||||||
)
|
)
|
||||||
const selectColumns = legacySelect.length ? legacySelect : modelColumns
|
const selectColumns = columnsSelected.length ? columnsSelected : modelColumns
|
||||||
|
|
||||||
return selectColumns.map((column) => {
|
return selectColumns.map((column) => {
|
||||||
return `${relationName}.${column}`
|
return `${relationName}.${column}`
|
||||||
@@ -59,7 +59,7 @@ export const ProductCategoryRepository = dataSource
|
|||||||
}
|
}
|
||||||
|
|
||||||
const queryBuilder = this.createQueryBuilder(entityName)
|
const queryBuilder = this.createQueryBuilder(entityName)
|
||||||
.select(selectStatements(entityName))
|
.select(fetchSelectColumns(entityName))
|
||||||
.skip(options_.skip)
|
.skip(options_.skip)
|
||||||
.take(options_.take)
|
.take(options_.take)
|
||||||
.addOrderBy(`${entityName}.rank`, "ASC")
|
.addOrderBy(`${entityName}.rank`, "ASC")
|
||||||
@@ -69,18 +69,18 @@ export const ProductCategoryRepository = dataSource
|
|||||||
delete options_.where?.name
|
delete options_.where?.name
|
||||||
delete options_.where?.handle
|
delete options_.where?.handle
|
||||||
|
|
||||||
queryBuilder.where(
|
options_.where = [{
|
||||||
new Brackets((bracket) => {
|
...options_.where,
|
||||||
bracket
|
name: ILike(`%${q}%`)
|
||||||
.where({ name: ILike(`%${q}%`) })
|
}, {
|
||||||
.orWhere({ handle: ILike(`%${q}%`) })
|
...options_.where,
|
||||||
})
|
handle: ILike(`%${q}%`)
|
||||||
)
|
}]
|
||||||
}
|
}
|
||||||
|
|
||||||
queryBuilder.andWhere(options_.where)
|
queryBuilder.where(options_.where)
|
||||||
|
|
||||||
const includedTreeRelations: string[] = legacyRelations.filter((rel) =>
|
const includedTreeRelations: string[] = relationsSelected.filter((rel) =>
|
||||||
ProductCategory.treeRelations.includes(rel)
|
ProductCategory.treeRelations.includes(rel)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -96,12 +96,12 @@ export const ProductCategoryRepository = dataSource
|
|||||||
treeWhere,
|
treeWhere,
|
||||||
treeScope
|
treeScope
|
||||||
)
|
)
|
||||||
.addSelect(selectStatements(treeRelation))
|
.addSelect(fetchSelectColumns(treeRelation))
|
||||||
.addOrderBy(`${treeRelation}.rank`, "ASC")
|
.addOrderBy(`${treeRelation}.rank`, "ASC")
|
||||||
.addOrderBy(`${treeRelation}.handle`, "ASC")
|
.addOrderBy(`${treeRelation}.handle`, "ASC")
|
||||||
})
|
})
|
||||||
|
|
||||||
const nonTreeRelations: string[] = legacyRelations.filter(
|
const nonTreeRelations: string[] = relationsSelected.filter(
|
||||||
(rel) => !ProductCategory.treeRelations.includes(rel)
|
(rel) => !ProductCategory.treeRelations.includes(rel)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -128,15 +128,15 @@ export const ProductCategoryRepository = dataSource
|
|||||||
productCategoryId: string,
|
productCategoryId: string,
|
||||||
productIds: string[]
|
productIds: string[]
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
const valuesToInsert = productIds.map((id) => ({
|
||||||
|
product_category_id: productCategoryId,
|
||||||
|
product_id: id,
|
||||||
|
}))
|
||||||
|
|
||||||
await this.createQueryBuilder()
|
await this.createQueryBuilder()
|
||||||
.insert()
|
.insert()
|
||||||
.into(ProductCategory.productCategoryProductJoinTable)
|
.into(ProductCategory.productCategoryProductJoinTable)
|
||||||
.values(
|
.values(valuesToInsert)
|
||||||
productIds.map((id) => ({
|
|
||||||
product_category_id: productCategoryId,
|
|
||||||
product_id: id,
|
|
||||||
}))
|
|
||||||
)
|
|
||||||
.orIgnore()
|
.orIgnore()
|
||||||
.execute()
|
.execute()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import { dataSource } from "../loaders/database"
|
|||||||
import { ProductFilterOptions } from "../types/product"
|
import { ProductFilterOptions } from "../types/product"
|
||||||
import {
|
import {
|
||||||
buildLegacyFieldsListFrom,
|
buildLegacyFieldsListFrom,
|
||||||
isObject,
|
|
||||||
fetchCategoryDescendantsIds,
|
fetchCategoryDescendantsIds,
|
||||||
|
isObject,
|
||||||
} from "../utils"
|
} from "../utils"
|
||||||
|
|
||||||
export const ProductRepository = dataSource.getRepository(Product).extend({
|
export const ProductRepository = dataSource.getRepository(Product).extend({
|
||||||
@@ -123,7 +123,6 @@ export const ProductRepository = dataSource.getRepository(Product).extend({
|
|||||||
delete options_.where.include_category_children
|
delete options_.where.include_category_children
|
||||||
delete options_.where.categories
|
delete options_.where.categories
|
||||||
|
|
||||||
// TODO: move back to the service layer
|
|
||||||
if (q) {
|
if (q) {
|
||||||
options_.relations = options_.relations ?? {}
|
options_.relations = options_.relations ?? {}
|
||||||
options_.relations.variants = options_.relations.variants ?? true
|
options_.relations.variants = options_.relations.variants ?? true
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ import { Brackets, In } from "typeorm"
|
|||||||
|
|
||||||
import { PublishableApiKeySalesChannel, SalesChannel } from "../models"
|
import { PublishableApiKeySalesChannel, SalesChannel } from "../models"
|
||||||
import { dataSource } from "../loaders/database"
|
import { dataSource } from "../loaders/database"
|
||||||
|
import SalesChannelRepository from "./sales-channel"
|
||||||
|
|
||||||
|
const publishableApiKeySalesChannelAlias = "PublishableKeySalesChannel"
|
||||||
|
|
||||||
export const PublishableApiKeySalesChannelRepository = dataSource
|
export const PublishableApiKeySalesChannelRepository = dataSource
|
||||||
.getRepository(PublishableApiKeySalesChannel)
|
.getRepository(PublishableApiKeySalesChannel)
|
||||||
@@ -16,35 +19,38 @@ export const PublishableApiKeySalesChannelRepository = dataSource
|
|||||||
publishableApiKeyId: string,
|
publishableApiKeyId: string,
|
||||||
config?: { q?: string }
|
config?: { q?: string }
|
||||||
): Promise<SalesChannel[]> {
|
): Promise<SalesChannel[]> {
|
||||||
const query = this.createQueryBuilder("PublishableKeySalesChannel")
|
const salesChannelAlias = "sales_channel"
|
||||||
.select("PublishableKeySalesChannel.sales_channel_id")
|
|
||||||
.innerJoinAndMapOne(
|
const queryBuilder = this.createQueryBuilder(
|
||||||
"PublishableKeySalesChannel.sales_channel_id",
|
publishableApiKeySalesChannelAlias
|
||||||
SalesChannel,
|
)
|
||||||
"SalesChannel",
|
.select(`${salesChannelAlias}.*`)
|
||||||
"PublishableKeySalesChannel.sales_channel_id = SalesChannel.id"
|
.from(SalesChannel, salesChannelAlias)
|
||||||
)
|
|
||||||
.where(
|
.where(
|
||||||
"PublishableKeySalesChannel.publishable_key_id = :publishableApiKeyId",
|
`${publishableApiKeySalesChannelAlias}.publishable_key_id = :publishableApiKeyId`,
|
||||||
{
|
{
|
||||||
publishableApiKeyId,
|
publishableApiKeyId,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
.andWhere(
|
||||||
|
`${publishableApiKeySalesChannelAlias}.sales_channel_id = ${salesChannelAlias}.id`
|
||||||
|
)
|
||||||
|
|
||||||
if (config?.q) {
|
if (config?.q) {
|
||||||
query.andWhere(
|
queryBuilder.andWhere(
|
||||||
new Brackets((qb) => {
|
new Brackets((qb) => {
|
||||||
qb.where(`SalesChannel.description ILIKE :q`, {
|
qb.where(`${salesChannelAlias}.description ILIKE :q`, {
|
||||||
q: `%${config.q}%`,
|
q: `%${config.q}%`,
|
||||||
}).orWhere(`SalesChannel.name ILIKE :q`, { q: `%${config.q}%` })
|
}).orWhere(`${salesChannelAlias}.name ILIKE :q`, {
|
||||||
|
q: `%${config.q}%`,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const records = await query.getMany()
|
const records = await queryBuilder.getRawMany()
|
||||||
|
return records.map((salesChannel: SalesChannel) =>
|
||||||
return records.map(
|
SalesChannelRepository.create(salesChannel)
|
||||||
(record) => record.sales_channel_id as unknown as SalesChannel
|
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -60,7 +66,7 @@ export const PublishableApiKeySalesChannelRepository = dataSource
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await this.createQueryBuilder()
|
await this.createQueryBuilder()
|
||||||
.insert()
|
.insert()
|
||||||
.into("publishable_api_key_sales_channel")
|
.into(PublishableApiKeySalesChannel)
|
||||||
.values(
|
.values(
|
||||||
salesChannelIds.map((id) => ({
|
salesChannelIds.map((id) => ({
|
||||||
sales_channel_id: id,
|
sales_channel_id: id,
|
||||||
@@ -83,7 +89,7 @@ export const PublishableApiKeySalesChannelRepository = dataSource
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await this.createQueryBuilder()
|
await this.createQueryBuilder()
|
||||||
.delete()
|
.delete()
|
||||||
.from("publishable_api_key_sales_channel")
|
.from(PublishableApiKeySalesChannel)
|
||||||
.where({
|
.where({
|
||||||
sales_channel_id: In(salesChannelIds),
|
sales_channel_id: In(salesChannelIds),
|
||||||
publishable_key_id: publishableApiKeyId,
|
publishable_key_id: publishableApiKeyId,
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import { Brackets, DeleteResult, FindOptionsWhere, In } from "typeorm"
|
import { Brackets, DeleteResult, FindOptionsWhere, In, ILike } from "typeorm"
|
||||||
import { SalesChannel } from "../models"
|
import { SalesChannel } from "../models"
|
||||||
import { ExtendedFindConfig } from "../types/common"
|
import { ExtendedFindConfig } from "../types/common"
|
||||||
import { dataSource } from "../loaders/database"
|
import { dataSource } from "../loaders/database"
|
||||||
|
|
||||||
|
const productSalesChannelTable = "product_sales_channel"
|
||||||
|
|
||||||
export const SalesChannelRepository = dataSource
|
export const SalesChannelRepository = dataSource
|
||||||
.getRepository(SalesChannel)
|
.getRepository(SalesChannel)
|
||||||
.extend({
|
.extend({
|
||||||
@@ -13,25 +15,29 @@ export const SalesChannelRepository = dataSource
|
|||||||
}
|
}
|
||||||
): Promise<[SalesChannel[], number]> {
|
): Promise<[SalesChannel[], number]> {
|
||||||
const options_ = { ...options }
|
const options_ = { ...options }
|
||||||
options_.where = options_.where as FindOptionsWhere<SalesChannel>
|
|
||||||
|
|
||||||
|
options_.where = options_.where as FindOptionsWhere<SalesChannel>
|
||||||
delete options_?.where?.name
|
delete options_?.where?.name
|
||||||
delete options_?.where?.description
|
delete options_?.where?.description
|
||||||
|
|
||||||
let qb = this.createQueryBuilder("sales_channel")
|
options_.where = [
|
||||||
|
{
|
||||||
|
...options_.where,
|
||||||
|
name: ILike(`%${q}%`),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...options_.where,
|
||||||
|
description: ILike(`%${q}%`),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
let qb = this.createQueryBuilder()
|
||||||
.select()
|
.select()
|
||||||
.where(options_.where)
|
.where(options_.where)
|
||||||
.andWhere(
|
.skip(options_.skip)
|
||||||
new Brackets((qb) => {
|
.take(options_.take)
|
||||||
qb.where(`sales_channel.description ILIKE :q`, {
|
|
||||||
q: `%${q}%`,
|
|
||||||
}).orWhere(`sales_channel.name ILIKE :q`, { q: `%${q}%` })
|
|
||||||
})
|
|
||||||
)
|
|
||||||
.skip(options.skip)
|
|
||||||
.take(options.take)
|
|
||||||
|
|
||||||
if (options.withDeleted) {
|
if (options_.withDeleted) {
|
||||||
qb = qb.withDeleted()
|
qb = qb.withDeleted()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,13 +48,15 @@ export const SalesChannelRepository = dataSource
|
|||||||
salesChannelId: string,
|
salesChannelId: string,
|
||||||
productIds: string[]
|
productIds: string[]
|
||||||
): Promise<DeleteResult> {
|
): Promise<DeleteResult> {
|
||||||
|
const whereOptions = {
|
||||||
|
sales_channel_id: salesChannelId,
|
||||||
|
product_id: In(productIds),
|
||||||
|
}
|
||||||
|
|
||||||
return await this.createQueryBuilder()
|
return await this.createQueryBuilder()
|
||||||
.delete()
|
.delete()
|
||||||
.from("product_sales_channel")
|
.from(productSalesChannelTable)
|
||||||
.where({
|
.where(whereOptions)
|
||||||
sales_channel_id: salesChannelId,
|
|
||||||
product_id: In(productIds),
|
|
||||||
})
|
|
||||||
.execute()
|
.execute()
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -56,17 +64,18 @@ export const SalesChannelRepository = dataSource
|
|||||||
salesChannelId: string,
|
salesChannelId: string,
|
||||||
productIds: string[]
|
productIds: string[]
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
const valuesToInsert = productIds.map((id) => ({
|
||||||
|
sales_channel_id: salesChannelId,
|
||||||
|
product_id: id,
|
||||||
|
}))
|
||||||
|
|
||||||
await this.createQueryBuilder()
|
await this.createQueryBuilder()
|
||||||
.insert()
|
.insert()
|
||||||
.into("product_sales_channel")
|
.into(productSalesChannelTable)
|
||||||
.values(
|
.values(valuesToInsert)
|
||||||
productIds.map((id) => ({
|
|
||||||
sales_channel_id: salesChannelId,
|
|
||||||
product_id: id,
|
|
||||||
}))
|
|
||||||
)
|
|
||||||
.orIgnore()
|
.orIgnore()
|
||||||
.execute()
|
.execute()
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
export default SalesChannelRepository
|
export default SalesChannelRepository
|
||||||
Reference in new issue
Block a user