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
2023-11-27 10:17:42 +00:00
committed by GitHub
parent faa528159d
commit 18afe0b9ad
19 changed files with 103 additions and 45 deletions

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

View File

@@ -25,6 +25,7 @@ export async function prepareCreatePriceLists({
const variables = {
variant_id: variantIds,
take: null,
}
const query = {

View File

@@ -30,6 +30,7 @@ export async function prepareRemovePriceListPrices({
"price_set_money_amount",
"price_set_money_amount.price_list",
],
take: null,
}
)

View File

@@ -19,6 +19,7 @@ export async function prepareRemoveProductPrices({
const variables = {
id: product_ids,
take: null,
}
const query = {

View File

@@ -19,6 +19,7 @@ export async function prepareRemoveVariantPrices({
const variables = {
variant_id: variant_ids,
take: null,
}
const query = {

View File

@@ -28,6 +28,7 @@ export async function prepareUpdatePriceLists({
const variables = {
variant_id: variantIds,
take: null,
}
const query = {

View File

@@ -22,6 +22,7 @@ export async function removePriceListPriceSetPrices({
},
{
relations: ["money_amount"],
take: null,
}
)

View File

@@ -58,6 +58,7 @@ export async function updateProductVariantsPrepareData({
},
{
select: ["id", "product_id"],
take: null,
}
)

View File

@@ -53,15 +53,18 @@ export async function updateProductsPrepareData({
"collection",
"sales_channels",
],
take: null,
}
)
const productsMap = new Map(products.map((product) => [product.id, product]))
data.products.forEach((productInput) => {
const removedChannels: string[] = []
const addedChannels: string[] = []
const currentProduct = products.find(
(p) => p.id === productInput.id
const currentProduct = productsMap.get(
productInput.id
) as unknown as ProductWithSalesChannelsDTO
if (productInput.sales_channels) {

View File

@@ -35,6 +35,7 @@ export async function updateProducts({
// "profiles",
// "sales_channels",
],
take: null,
}
)
}

View File

@@ -152,6 +152,7 @@ export async function upsertVariantPrices({
},
{
select: ["id", "currency_code", "amount", "min_quantity", "max_quantity"],
take: null,
}
)

View File

@@ -46,11 +46,11 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
}
if ("skip" in config) {
;(query as FindManyOptions<TEntity>).skip = config.skip
;(query as FindManyOptions<TEntity>).skip = config.skip ?? undefined
}
if ("take" in config) {
;(query as FindManyOptions<TEntity>).take = config.take
;(query as FindManyOptions<TEntity>).take = config.take ?? undefined
}
if (config.relations) {

View File

@@ -206,6 +206,7 @@ class PricingService extends TransactionBaseService {
) {
const variables = {
variant_id: variantPriceData.map((pricedata) => pricedata.variantId),
take: null,
}
const query = {
@@ -685,6 +686,7 @@ class PricingService extends TransactionBaseService {
): Promise<Map<string, MoneyAmount[]>> {
const variables = {
variant_id: variantIds,
take: null,
}
const query = {
@@ -713,6 +715,7 @@ class PricingService extends TransactionBaseService {
price_set_id: priceSetIds,
},
{
take: null,
relations: [
"money_amount",
"price_list",

View File

@@ -48,11 +48,11 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
}
if ("skip" in config) {
;(query as FindManyOptions<TEntity>).skip = config.skip
;(query as FindManyOptions<TEntity>).skip = config.skip ?? undefined
}
if ("take" in config) {
;(query as FindManyOptions<TEntity>).take = config.take
;(query as FindManyOptions<TEntity>).take = config.take ?? undefined
}
if (config.relations) {

View File

@@ -288,6 +288,7 @@ export default class PricingModuleService<
{ id: priceSets.filter((p) => !!p).map((p) => p!.id) },
{
relations: ["rule_types", "money_amounts", "price_rules"],
take: null,
},
sharedContext
)
@@ -310,7 +311,7 @@ export default class PricingModuleService<
{
rule_attribute: ruleAttributes,
},
{},
{ take: null },
sharedContext
)
@@ -460,7 +461,7 @@ export default class PricingModuleService<
): Promise<PricingTypes.PriceSetDTO[]> {
const priceSets = await this.priceSetService_.list(
{ id: inputs.map((d) => d.priceSetId) },
{ relations: ["rule_types"] },
{ relations: ["rule_types"], take: null },
sharedContext
)
@@ -492,7 +493,7 @@ export default class PricingModuleService<
.map((d) => d.rules.map((r) => r.attribute))
.flat(),
},
{},
{ take: null },
sharedContext
)
@@ -565,7 +566,7 @@ export default class PricingModuleService<
return (await this.list(
{ id: input.map((d) => d.priceSetId) },
{ relations: ["money_amounts"] },
{ relations: ["money_amounts"], take: null },
sharedContext
)) as unknown as PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO
}
@@ -577,7 +578,7 @@ export default class PricingModuleService<
) {
const priceSets = await this.list(
{ id: input.map((d) => d.priceSetId) },
{ relations: ["rule_types"] },
{ relations: ["rule_types"], take: null },
sharedContext
)
@@ -683,7 +684,7 @@ export default class PricingModuleService<
{
id: data.map((d) => d.id),
},
undefined,
{ take: null },
sharedContext
)
const priceSetIds = priceSets.map((ps) => ps.id)
@@ -692,7 +693,7 @@ export default class PricingModuleService<
{
rule_attribute: data.map((d) => d.rules || []).flat(),
},
undefined,
{ take: null },
sharedContext
)
const ruleTypeIds = ruleTypes.map((rt) => rt.id)
@@ -702,7 +703,7 @@ export default class PricingModuleService<
price_set_id: priceSetIds,
rule_type_id: ruleTypeIds,
},
undefined,
{ take: null },
sharedContext
)
@@ -713,6 +714,7 @@ export default class PricingModuleService<
},
{
select: ["price_set_money_amount"],
take: null,
},
sharedContext
)
@@ -1389,9 +1391,12 @@ export default class PricingModuleService<
.map((priceListData) => Object.keys(priceListData.rules || {}))
.flat()
const ruleTypes = await this.listRuleTypes({
rule_attribute: ruleAttributes,
})
const ruleTypes = await this.listRuleTypes(
{
rule_attribute: ruleAttributes,
},
{ take: null }
)
const ruleTypeMap: Map<string, RuleTypeDTO> = new Map(
ruleTypes.map((rt) => [rt.rule_attribute, rt])
@@ -1514,7 +1519,7 @@ export default class PricingModuleService<
const existingPriceLists = await this.listPriceLists(
{ id: priceListIds },
{ relations: ["price_list_rules"] },
{ relations: ["price_list_rules"], take: null },
sharedContext
)
@@ -1526,7 +1531,7 @@ export default class PricingModuleService<
{
id: priceListRuleIds,
},
{},
{ take: null },
sharedContext
)
@@ -1541,7 +1546,7 @@ export default class PricingModuleService<
{
rule_attribute: ruleAttributes,
},
{},
{ take: null },
sharedContext
)
@@ -1747,7 +1752,9 @@ export default class PricingModuleService<
): Promise<PricingTypes.PriceListDTO[]> {
const priceLists = await this.listPriceLists(
{ id: data.map((d) => d.priceListId) },
{},
{
take: null,
},
sharedContext
)
@@ -1807,15 +1814,23 @@ export default class PricingModuleService<
): Promise<PricingTypes.PriceListDTO[]> {
const priceLists = await this.priceListService_.list(
{ 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
)
const priceListMap = new Map(priceLists.map((p) => [p.id, p]))
const ruleTypes = await this.listRuleTypes({
rule_attribute: data.map((d) => Object.keys(d.rules)).flat(),
})
const ruleTypes = await this.listRuleTypes(
{
rule_attribute: data.map((d) => Object.keys(d.rules)).flat(),
},
{
take: null,
}
)
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([
this.priceListRuleService_.create(rulesToCreate),
this.priceListRuleValueService_.list({
price_list_rule_id: ruleIdsToUpdate,
}),
this.priceListRuleValueService_.list(
{
price_list_rule_id: ruleIdsToUpdate,
},
{ take: null }
),
])
const priceListRuleValuesToCreate: CreatePriceListRuleValueDTO[] = []
@@ -1936,7 +1954,10 @@ export default class PricingModuleService<
): Promise<PricingTypes.PriceListDTO[]> {
const priceLists = await this.priceListService_.list(
{ 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
)

View File

@@ -255,7 +255,9 @@ export default class ProductModuleService<
const productOptions = await this.listOptions(
{ id: productOptionIds },
{},
{
take: null,
},
sharedContext
)
@@ -345,7 +347,7 @@ export default class ProductModuleService<
const variantIdsToUpdate = data.map(({ id }) => id)
const variants = await this.listVariants(
{ id: variantIdsToUpdate },
{ relations: ["options", "options.option"] },
{ relations: ["options", "options.option"], take: null },
sharedContext
)
const variantsMap = new Map(
@@ -1053,7 +1055,9 @@ export default class ProductModuleService<
const productIds = data.map((pd) => pd.id)
const existingProductVariants = await this.productVariantService_.list(
{ product_id: productIds },
{},
{
take: null,
},
sharedContext
)

View File

@@ -46,11 +46,11 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
}
if ("skip" in config) {
;(query as FindManyOptions<TEntity>).skip = config.skip
;(query as FindManyOptions<TEntity>).skip = config.skip ?? undefined
}
if ("take" in config) {
;(query as FindManyOptions<TEntity>).take = config.take
;(query as FindManyOptions<TEntity>).take = config.take ?? undefined
}
if (config.relations) {

View File

@@ -4,11 +4,11 @@ import {
FindOperator,
FindOptionsSelect,
FindOptionsWhere,
OrderByCondition,
} from "typeorm"
OrderByCondition
} from "typeorm";
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations"
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder";
import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations";
/**
* Utility type used to remove some optional attributes (coming from K) from a type T
@@ -42,8 +42,8 @@ export type Writable<T> = {
/**
* @interface
*
* An object that is used to configure how an entity is retrieved from the database. It accepts as a typed parameter an `Entity` class,
*
* An object that is used to configure how an entity is retrieved from the database. It accepts as a typed parameter an `Entity` class,
* which provides correct typing of field names in its properties.
*/
export interface FindConfig<Entity> {
@@ -54,17 +54,17 @@ export interface FindConfig<Entity> {
/**
* 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.
*/
take?: number
take?: number | null | undefined
/**
* An array of strings, each being relation names of the entity to retrieve in the result.
*/
relations?: string[]
/**
* An object used to specify how to sort the returned records. Its keys are the names of attributes of the entity, and a key's value can either be `ASC`
* An object used to specify how to sort the returned records. Its keys are the names of attributes of the entity, and a key's value can either be `ASC`
* to sort retrieved records in an ascending order, or `DESC` to sort retrieved records in a descending order.
*/
order?: { [K: string]: "ASC" | "DESC" }
@@ -165,6 +165,7 @@ export type DeleteResponse = {
}
export interface EmptyQueryParams {}
// TODO: Build a tree repository options from this
export interface RepositoryTransformOptions {}

View File

@@ -13,8 +13,16 @@ export function buildQuery<T = any, TDto = any>(
const findOptions: DAL.OptionsQuery<T, any> = {
populate: deduplicate(config.relations ?? []),
fields: config.select as string[],
limit: config.take ?? 15,
offset: config.skip ?? 0,
limit:
(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) {