Fix(medusa, inventory, stock-location, types, utils): Modules sdk build query (#5713)

* remove defaults

* take 2

* works now

* add changeset

* pricing module and pricing service list take null updates

* update handlers

* update product module service with take:null where relevant

* no spread

* note to self:default offset should be 0, not 15
This commit is contained in:
Philip Korsholm authored and GitHub committed 2023-11-27 10:17:42 +00:00
1 parent faa528159d
commit 18afe0b9ad
19 files changed
+97 -39

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
---
"@medusajs/stock-location": patch
"@medusajs/inventory": patch
"@medusajs/medusa": patch
"@medusajs/types": patch
"@medusajs/utils": patch
---
fix(stock-location, inventory, medusa, types, utils): allow buildQuery to take null as an argument in order to prevent default pagination
@@ -25,6 +25,7 @@ export async function prepareCreatePriceLists({
const variables = { const variables = {
variant_id: variantIds, variant_id: variantIds,
take: null,
} }
const query = { const query = {
@@ -30,6 +30,7 @@ export async function prepareRemovePriceListPrices({
"price_set_money_amount", "price_set_money_amount",
"price_set_money_amount.price_list", "price_set_money_amount.price_list",
], ],
take: null,
} }
) )
@@ -19,6 +19,7 @@ export async function prepareRemoveProductPrices({
const variables = { const variables = {
id: product_ids, id: product_ids,
take: null,
} }
const query = { const query = {
@@ -19,6 +19,7 @@ export async function prepareRemoveVariantPrices({
const variables = { const variables = {
variant_id: variant_ids, variant_id: variant_ids,
take: null,
} }
const query = { const query = {
@@ -28,6 +28,7 @@ export async function prepareUpdatePriceLists({
const variables = { const variables = {
variant_id: variantIds, variant_id: variantIds,
take: null,
} }
const query = { const query = {
@@ -22,6 +22,7 @@ export async function removePriceListPriceSetPrices({
}, },
{ {
relations: ["money_amount"], relations: ["money_amount"],
take: null,
} }
) )
@@ -58,6 +58,7 @@ export async function updateProductVariantsPrepareData({
}, },
{ {
select: ["id", "product_id"], select: ["id", "product_id"],
take: null,
} }
) )
@@ -53,15 +53,18 @@ export async function updateProductsPrepareData({
"collection", "collection",
"sales_channels", "sales_channels",
], ],
take: null,
} }
) )
const productsMap = new Map(products.map((product) => [product.id, product]))
data.products.forEach((productInput) => { data.products.forEach((productInput) => {
const removedChannels: string[] = [] const removedChannels: string[] = []
const addedChannels: string[] = [] const addedChannels: string[] = []
const currentProduct = products.find( const currentProduct = productsMap.get(
(p) => p.id === productInput.id productInput.id
) as unknown as ProductWithSalesChannelsDTO ) as unknown as ProductWithSalesChannelsDTO
if (productInput.sales_channels) { if (productInput.sales_channels) {
@@ -35,6 +35,7 @@ export async function updateProducts({
// "profiles", // "profiles",
// "sales_channels", // "sales_channels",
], ],
take: null,
} }
) )
} }
@@ -152,6 +152,7 @@ export async function upsertVariantPrices({
}, },
{ {
select: ["id", "currency_code", "amount", "min_quantity", "max_quantity"], select: ["id", "currency_code", "amount", "min_quantity", "max_quantity"],
take: null,
} }
) )
+2 -2
View File
@@ -46,11 +46,11 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
} }
if ("skip" in config) { if ("skip" in config) {
;(query as FindManyOptions<TEntity>).skip = config.skip ;(query as FindManyOptions<TEntity>).skip = config.skip ?? undefined
} }
if ("take" in config) { if ("take" in config) {
;(query as FindManyOptions<TEntity>).take = config.take ;(query as FindManyOptions<TEntity>).take = config.take ?? undefined
} }
if (config.relations) { if (config.relations) {
+3
View File
@@ -206,6 +206,7 @@ class PricingService extends TransactionBaseService {
) { ) {
const variables = { const variables = {
variant_id: variantPriceData.map((pricedata) => pricedata.variantId), variant_id: variantPriceData.map((pricedata) => pricedata.variantId),
take: null,
} }
const query = { const query = {
@@ -685,6 +686,7 @@ class PricingService extends TransactionBaseService {
): Promise<Map<string, MoneyAmount[]>> { ): Promise<Map<string, MoneyAmount[]>> {
const variables = { const variables = {
variant_id: variantIds, variant_id: variantIds,
take: null,
} }
const query = { const query = {
@@ -713,6 +715,7 @@ class PricingService extends TransactionBaseService {
price_set_id: priceSetIds, price_set_id: priceSetIds,
}, },
{ {
take: null,
relations: [ relations: [
"money_amount", "money_amount",
"price_list", "price_list",
+2 -2
View File
@@ -48,11 +48,11 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
} }
if ("skip" in config) { if ("skip" in config) {
;(query as FindManyOptions<TEntity>).skip = config.skip ;(query as FindManyOptions<TEntity>).skip = config.skip ?? undefined
} }
if ("take" in config) { if ("take" in config) {
;(query as FindManyOptions<TEntity>).take = config.take ;(query as FindManyOptions<TEntity>).take = config.take ?? undefined
} }
if (config.relations) { if (config.relations) {
+41 -20
View File
@@ -288,6 +288,7 @@ export default class PricingModuleService<
{ id: priceSets.filter((p) => !!p).map((p) => p!.id) }, { id: priceSets.filter((p) => !!p).map((p) => p!.id) },
{ {
relations: ["rule_types", "money_amounts", "price_rules"], relations: ["rule_types", "money_amounts", "price_rules"],
take: null,
}, },
sharedContext sharedContext
) )
@@ -310,7 +311,7 @@ export default class PricingModuleService<
{ {
rule_attribute: ruleAttributes, rule_attribute: ruleAttributes,
}, },
{}, { take: null },
sharedContext sharedContext
) )
@@ -460,7 +461,7 @@ export default class PricingModuleService<
): Promise<PricingTypes.PriceSetDTO[]> { ): Promise<PricingTypes.PriceSetDTO[]> {
const priceSets = await this.priceSetService_.list( const priceSets = await this.priceSetService_.list(
{ id: inputs.map((d) => d.priceSetId) }, { id: inputs.map((d) => d.priceSetId) },
{ relations: ["rule_types"] }, { relations: ["rule_types"], take: null },
sharedContext sharedContext
) )
@@ -492,7 +493,7 @@ export default class PricingModuleService<
.map((d) => d.rules.map((r) => r.attribute)) .map((d) => d.rules.map((r) => r.attribute))
.flat(), .flat(),
}, },
{}, { take: null },
sharedContext sharedContext
) )
@@ -565,7 +566,7 @@ export default class PricingModuleService<
return (await this.list( return (await this.list(
{ id: input.map((d) => d.priceSetId) }, { id: input.map((d) => d.priceSetId) },
{ relations: ["money_amounts"] }, { relations: ["money_amounts"], take: null },
sharedContext sharedContext
)) as unknown as PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO )) as unknown as PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO
} }
@@ -577,7 +578,7 @@ export default class PricingModuleService<
) { ) {
const priceSets = await this.list( const priceSets = await this.list(
{ id: input.map((d) => d.priceSetId) }, { id: input.map((d) => d.priceSetId) },
{ relations: ["rule_types"] }, { relations: ["rule_types"], take: null },
sharedContext sharedContext
) )
@@ -683,7 +684,7 @@ export default class PricingModuleService<
{ {
id: data.map((d) => d.id), id: data.map((d) => d.id),
}, },
undefined, { take: null },
sharedContext sharedContext
) )
const priceSetIds = priceSets.map((ps) => ps.id) const priceSetIds = priceSets.map((ps) => ps.id)
@@ -692,7 +693,7 @@ export default class PricingModuleService<
{ {
rule_attribute: data.map((d) => d.rules || []).flat(), rule_attribute: data.map((d) => d.rules || []).flat(),
}, },
undefined, { take: null },
sharedContext sharedContext
) )
const ruleTypeIds = ruleTypes.map((rt) => rt.id) const ruleTypeIds = ruleTypes.map((rt) => rt.id)
@@ -702,7 +703,7 @@ export default class PricingModuleService<
price_set_id: priceSetIds, price_set_id: priceSetIds,
rule_type_id: ruleTypeIds, rule_type_id: ruleTypeIds,
}, },
undefined, { take: null },
sharedContext sharedContext
) )
@@ -713,6 +714,7 @@ export default class PricingModuleService<
}, },
{ {
select: ["price_set_money_amount"], select: ["price_set_money_amount"],
take: null,
}, },
sharedContext sharedContext
) )
@@ -1389,9 +1391,12 @@ export default class PricingModuleService<
.map((priceListData) => Object.keys(priceListData.rules || {})) .map((priceListData) => Object.keys(priceListData.rules || {}))
.flat() .flat()
const ruleTypes = await this.listRuleTypes({ const ruleTypes = await this.listRuleTypes(
{
rule_attribute: ruleAttributes, rule_attribute: ruleAttributes,
}) },
{ take: null }
)
const ruleTypeMap: Map<string, RuleTypeDTO> = new Map( const ruleTypeMap: Map<string, RuleTypeDTO> = new Map(
ruleTypes.map((rt) => [rt.rule_attribute, rt]) ruleTypes.map((rt) => [rt.rule_attribute, rt])
@@ -1514,7 +1519,7 @@ export default class PricingModuleService<
const existingPriceLists = await this.listPriceLists( const existingPriceLists = await this.listPriceLists(
{ id: priceListIds }, { id: priceListIds },
{ relations: ["price_list_rules"] }, { relations: ["price_list_rules"], take: null },
sharedContext sharedContext
) )
@@ -1526,7 +1531,7 @@ export default class PricingModuleService<
{ {
id: priceListRuleIds, id: priceListRuleIds,
}, },
{}, { take: null },
sharedContext sharedContext
) )
@@ -1541,7 +1546,7 @@ export default class PricingModuleService<
{ {
rule_attribute: ruleAttributes, rule_attribute: ruleAttributes,
}, },
{}, { take: null },
sharedContext sharedContext
) )
@@ -1747,7 +1752,9 @@ export default class PricingModuleService<
): Promise<PricingTypes.PriceListDTO[]> { ): Promise<PricingTypes.PriceListDTO[]> {
const priceLists = await this.listPriceLists( const priceLists = await this.listPriceLists(
{ id: data.map((d) => d.priceListId) }, { id: data.map((d) => d.priceListId) },
{}, {
take: null,
},
sharedContext sharedContext
) )
@@ -1807,15 +1814,23 @@ export default class PricingModuleService<
): Promise<PricingTypes.PriceListDTO[]> { ): Promise<PricingTypes.PriceListDTO[]> {
const priceLists = await this.priceListService_.list( const priceLists = await this.priceListService_.list(
{ id: data.map((d) => d.priceListId) }, { id: data.map((d) => d.priceListId) },
{ relations: ["price_list_rules", "price_list_rules.rule_type"] }, {
relations: ["price_list_rules", "price_list_rules.rule_type"],
take: null,
},
sharedContext sharedContext
) )
const priceListMap = new Map(priceLists.map((p) => [p.id, p])) const priceListMap = new Map(priceLists.map((p) => [p.id, p]))
const ruleTypes = await this.listRuleTypes({ const ruleTypes = await this.listRuleTypes(
{
rule_attribute: data.map((d) => Object.keys(d.rules)).flat(), rule_attribute: data.map((d) => Object.keys(d.rules)).flat(),
}) },
{
take: null,
}
)
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt])) const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]))
@@ -1874,9 +1889,12 @@ export default class PricingModuleService<
const [createdRules, priceListValuesToDelete] = await Promise.all([ const [createdRules, priceListValuesToDelete] = await Promise.all([
this.priceListRuleService_.create(rulesToCreate), this.priceListRuleService_.create(rulesToCreate),
this.priceListRuleValueService_.list({ this.priceListRuleValueService_.list(
{
price_list_rule_id: ruleIdsToUpdate, price_list_rule_id: ruleIdsToUpdate,
}), },
{ take: null }
),
]) ])
const priceListRuleValuesToCreate: CreatePriceListRuleValueDTO[] = [] const priceListRuleValuesToCreate: CreatePriceListRuleValueDTO[] = []
@@ -1936,7 +1954,10 @@ export default class PricingModuleService<
): Promise<PricingTypes.PriceListDTO[]> { ): Promise<PricingTypes.PriceListDTO[]> {
const priceLists = await this.priceListService_.list( const priceLists = await this.priceListService_.list(
{ id: data.map((d) => d.priceListId) }, { id: data.map((d) => d.priceListId) },
{ relations: ["price_list_rules", "price_list_rules.rule_type"] }, {
relations: ["price_list_rules", "price_list_rules.rule_type"],
take: null,
},
sharedContext sharedContext
) )
@@ -255,7 +255,9 @@ export default class ProductModuleService<
const productOptions = await this.listOptions( const productOptions = await this.listOptions(
{ id: productOptionIds }, { id: productOptionIds },
{}, {
take: null,
},
sharedContext sharedContext
) )
@@ -345,7 +347,7 @@ export default class ProductModuleService<
const variantIdsToUpdate = data.map(({ id }) => id) const variantIdsToUpdate = data.map(({ id }) => id)
const variants = await this.listVariants( const variants = await this.listVariants(
{ id: variantIdsToUpdate }, { id: variantIdsToUpdate },
{ relations: ["options", "options.option"] }, { relations: ["options", "options.option"], take: null },
sharedContext sharedContext
) )
const variantsMap = new Map( const variantsMap = new Map(
@@ -1053,7 +1055,9 @@ export default class ProductModuleService<
const productIds = data.map((pd) => pd.id) const productIds = data.map((pd) => pd.id)
const existingProductVariants = await this.productVariantService_.list( const existingProductVariants = await this.productVariantService_.list(
{ product_id: productIds }, { product_id: productIds },
{}, {
take: null,
},
sharedContext sharedContext
) )
@@ -46,11 +46,11 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
} }
if ("skip" in config) { if ("skip" in config) {
;(query as FindManyOptions<TEntity>).skip = config.skip ;(query as FindManyOptions<TEntity>).skip = config.skip ?? undefined
} }
if ("take" in config) { if ("take" in config) {
;(query as FindManyOptions<TEntity>).take = config.take ;(query as FindManyOptions<TEntity>).take = config.take ?? undefined
} }
if (config.relations) { if (config.relations) {
+7 -6
View File
@@ -4,11 +4,11 @@ import {
FindOperator, FindOperator,
FindOptionsSelect, FindOptionsSelect,
FindOptionsWhere, FindOptionsWhere,
OrderByCondition, OrderByCondition
} from "typeorm" } from "typeorm";
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder" import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder";
import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations" import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations";
/** /**
* Utility type used to remove some optional attributes (coming from K) from a type T * Utility type used to remove some optional attributes (coming from K) from a type T
@@ -54,11 +54,11 @@ export interface FindConfig<Entity> {
/** /**
* A number indicating the number of records to skip before retrieving the results. * A number indicating the number of records to skip before retrieving the results.
*/ */
skip?: number skip?: number | null | undefined
/** /**
* A number indicating the number of records to return in the result. * A number indicating the number of records to return in the result.
*/ */
take?: number take?: number | null | undefined
/** /**
* An array of strings, each being relation names of the entity to retrieve in the result. * An array of strings, each being relation names of the entity to retrieve in the result.
*/ */
@@ -165,6 +165,7 @@ export type DeleteResponse = {
} }
export interface EmptyQueryParams {} export interface EmptyQueryParams {}
// TODO: Build a tree repository options from this // TODO: Build a tree repository options from this
export interface RepositoryTransformOptions {} export interface RepositoryTransformOptions {}
+10 -2
View File
@@ -13,8 +13,16 @@ export function buildQuery<T = any, TDto = any>(
const findOptions: DAL.OptionsQuery<T, any> = { const findOptions: DAL.OptionsQuery<T, any> = {
populate: deduplicate(config.relations ?? []), populate: deduplicate(config.relations ?? []),
fields: config.select as string[], fields: config.select as string[],
limit: config.take ?? 15, limit:
offset: config.skip ?? 0, (Number.isSafeInteger(config.take) && config.take! >= 0) ||
null === config.take
? config.take ?? undefined
: 15,
offset:
(Number.isSafeInteger(config.skip) && config.skip! >= 0) ||
null === config.skip
? config.skip ?? undefined
: 0,
} }
if (config.order) { if (config.order) {