chore(medusa, utils): rename buildLegacyFieldsListFrom to objectToStringPath (#3738)
* chore(medusa): rename buildLegacyFieldsListFrom to objectToStringPath * chore: address pr comments
This commit is contained in:
1 parent
08f85fa33b
commit
abdb74d997
9 files changed
+26
-18
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
"@medusajs/utils": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore(medusa, utils): rename buildLegacyFieldsListFrom to objectToStringPath
|
||||||
@@ -3,7 +3,7 @@ import {
|
|||||||
FilterableInventoryItemProps,
|
FilterableInventoryItemProps,
|
||||||
FindConfig,
|
FindConfig,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { buildLegacyFieldsListFrom, buildQuery } from "@medusajs/utils"
|
import { objectToStringPath, buildQuery } from "@medusajs/utils"
|
||||||
import { EntityManager, FindOptionsWhere, ILike } from "typeorm"
|
import { EntityManager, FindOptionsWhere, ILike } from "typeorm"
|
||||||
import { InventoryItem } from "../models"
|
import { InventoryItem } from "../models"
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ export function getListQuery(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (query.select) {
|
if (query.select) {
|
||||||
const legacySelect = buildLegacyFieldsListFrom(query.select)
|
const legacySelect = objectToStringPath(query.select)
|
||||||
queryBuilder.select(legacySelect.map((s) => "inv_item." + s))
|
queryBuilder.select(legacySelect.map((s) => "inv_item." + s))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
queryEntityWithIds,
|
queryEntityWithIds,
|
||||||
queryEntityWithoutRelations,
|
queryEntityWithoutRelations,
|
||||||
} from "../utils/repository"
|
} from "../utils/repository"
|
||||||
import { buildLegacyFieldsListFrom } from "../utils"
|
import { objectToStringPath } from "@medusajs/utils"
|
||||||
import { dataSource } from "../loaders/database"
|
import { dataSource } from "../loaders/database"
|
||||||
|
|
||||||
export type DefaultWithoutRelations = Omit<
|
export type DefaultWithoutRelations = Omit<
|
||||||
@@ -135,10 +135,10 @@ export const CustomerGroupRepository = dataSource
|
|||||||
return [toReturn, toReturn.length]
|
return [toReturn, toReturn.length]
|
||||||
}
|
}
|
||||||
|
|
||||||
const legacyRelations = buildLegacyFieldsListFrom(relations)
|
const legacyRelations = objectToStringPath(relations)
|
||||||
const groupedRelations = getGroupedRelations(legacyRelations)
|
const groupedRelations = getGroupedRelations(legacyRelations)
|
||||||
|
|
||||||
const legacySelect = buildLegacyFieldsListFrom(
|
const legacySelect = objectToStringPath(
|
||||||
idsOrOptionsWithoutRelations.select
|
idsOrOptionsWithoutRelations.select
|
||||||
)
|
)
|
||||||
const entitiesIdsWithRelations = await queryEntityWithIds(
|
const entitiesIdsWithRelations = await queryEntityWithIds(
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { flatten, groupBy, map, merge } from "lodash"
|
import { flatten, groupBy, map, merge } from "lodash"
|
||||||
import { FindManyOptions, FindOptionsRelations, In } from "typeorm"
|
import { FindManyOptions, FindOptionsRelations, In } from "typeorm"
|
||||||
import { Order } from "../models"
|
import { Order } from "../models"
|
||||||
import { buildLegacyFieldsListFrom } from "../utils"
|
import { objectToStringPath } from "@medusajs/utils"
|
||||||
import { dataSource } from "../loaders/database"
|
import { dataSource } from "../loaders/database"
|
||||||
|
|
||||||
const ITEMS_REL_NAME = "items"
|
const ITEMS_REL_NAME = "items"
|
||||||
@@ -16,7 +16,7 @@ export const OrderRepository = dataSource.getRepository(Order).extend({
|
|||||||
const entitiesIds = entities.map(({ id }) => id)
|
const entitiesIds = entities.map(({ id }) => id)
|
||||||
|
|
||||||
const groupedRelations: { [topLevel: string]: string[] } = {}
|
const groupedRelations: { [topLevel: string]: string[] } = {}
|
||||||
for (const rel of buildLegacyFieldsListFrom(relations)) {
|
for (const rel of objectToStringPath(relations)) {
|
||||||
const [topLevel] = rel.split(".")
|
const [topLevel] = rel.split(".")
|
||||||
if (groupedRelations[topLevel]) {
|
if (groupedRelations[topLevel]) {
|
||||||
groupedRelations[topLevel].push(rel)
|
groupedRelations[topLevel].push(rel)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
import { ProductCategory } from "../models/product-category"
|
import { ProductCategory } from "../models/product-category"
|
||||||
import { ExtendedFindConfig, QuerySelector } from "../types/common"
|
import { ExtendedFindConfig, QuerySelector } from "../types/common"
|
||||||
import { dataSource } from "../loaders/database"
|
import { dataSource } from "../loaders/database"
|
||||||
import { buildLegacyFieldsListFrom } from "../utils"
|
import { objectToStringPath } from "@medusajs/utils"
|
||||||
import { isEmpty } from "lodash"
|
import { isEmpty } from "lodash"
|
||||||
|
|
||||||
export const ProductCategoryRepository = dataSource
|
export const ProductCategoryRepository = dataSource
|
||||||
@@ -44,8 +44,8 @@ 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 columnsSelected = buildLegacyFieldsListFrom(options_.select)
|
const columnsSelected = objectToStringPath(options_.select)
|
||||||
const relationsSelected = buildLegacyFieldsListFrom(options_.relations)
|
const relationsSelected = objectToStringPath(options_.relations)
|
||||||
|
|
||||||
const fetchSelectColumns = (relationName: string): string[] => {
|
const fetchSelectColumns = (relationName: string): string[] => {
|
||||||
const modelColumns = this.metadata.ownColumns.map(
|
const modelColumns = this.metadata.ownColumns.map(
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ import { ExtendedFindConfig } from "../types/common"
|
|||||||
import { dataSource } from "../loaders/database"
|
import { dataSource } from "../loaders/database"
|
||||||
import { ProductFilterOptions } from "../types/product"
|
import { ProductFilterOptions } from "../types/product"
|
||||||
import {
|
import {
|
||||||
buildLegacyFieldsListFrom,
|
|
||||||
fetchCategoryDescendantsIds,
|
|
||||||
isObject,
|
isObject,
|
||||||
|
fetchCategoryDescendantsIds,
|
||||||
} from "../utils"
|
} from "../utils"
|
||||||
|
import { objectToStringPath } from "@medusajs/utils"
|
||||||
|
|
||||||
export const ProductRepository = dataSource.getRepository(Product).extend({
|
export const ProductRepository = dataSource.getRepository(Product).extend({
|
||||||
async bulkAddToCollection(
|
async bulkAddToCollection(
|
||||||
@@ -89,7 +89,7 @@ export const ProductRepository = dataSource.getRepository(Product).extend({
|
|||||||
// https://github.com/typeorm/typeorm/issues/6294
|
// https://github.com/typeorm/typeorm/issues/6294
|
||||||
// Cleanup the repo and fix order/skip/take and relation load strategy when those issues are resolved
|
// Cleanup the repo and fix order/skip/take and relation load strategy when those issues are resolved
|
||||||
|
|
||||||
const orderFieldsCollectionPointSeparated = buildLegacyFieldsListFrom(
|
const orderFieldsCollectionPointSeparated = objectToStringPath(
|
||||||
options.order ?? {}
|
options.order ?? {}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import {
|
|||||||
} from "../models"
|
} from "../models"
|
||||||
import { TaxRateListByConfig } from "../types/tax-rate"
|
import { TaxRateListByConfig } from "../types/tax-rate"
|
||||||
import { isDefined } from "medusa-core-utils"
|
import { isDefined } from "medusa-core-utils"
|
||||||
import { buildLegacyFieldsListFrom } from "../utils"
|
import { objectToStringPath } from "@medusajs/utils"
|
||||||
import { dataSource } from "../loaders/database"
|
import { dataSource } from "../loaders/database"
|
||||||
|
|
||||||
const resolveableFields = [
|
const resolveableFields = [
|
||||||
@@ -33,7 +33,7 @@ export const TaxRateRepository = dataSource.getRepository(TaxRate).extend({
|
|||||||
const resolverFields: string[] = []
|
const resolverFields: string[] = []
|
||||||
if (isDefined(findOptions.select)) {
|
if (isDefined(findOptions.select)) {
|
||||||
const selectableCols: (keyof TaxRate)[] = []
|
const selectableCols: (keyof TaxRate)[] = []
|
||||||
const legacySelect = buildLegacyFieldsListFrom(
|
const legacySelect = objectToStringPath(
|
||||||
findOptions.select as FindOptionsSelect<TaxRate>
|
findOptions.select as FindOptionsSelect<TaxRate>
|
||||||
)
|
)
|
||||||
for (const k of legacySelect) {
|
for (const k of legacySelect) {
|
||||||
|
|||||||
@@ -139,6 +139,7 @@ function buildWhere<TWhereKeys extends object, TEntity>(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Revert new object structure of find options to the legacy structure of previous version
|
* Revert new object structure of find options to the legacy structure of previous version
|
||||||
|
* @deprecated in favor of import { objectToStringPath } from "@medusajs/utils"
|
||||||
* @example
|
* @example
|
||||||
* input: {
|
* input: {
|
||||||
* test: {
|
* test: {
|
||||||
|
|||||||
@@ -138,7 +138,8 @@ function buildWhere<TWhereKeys extends object, TEntity>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Revert new object structure of find options to the legacy structure of previous version
|
* Converts a typeorms structure of find options to an
|
||||||
|
* array of string paths
|
||||||
* @example
|
* @example
|
||||||
* input: {
|
* input: {
|
||||||
* test: {
|
* test: {
|
||||||
@@ -153,7 +154,7 @@ function buildWhere<TWhereKeys extends object, TEntity>(
|
|||||||
* output: ['test.test1', 'test.test2', 'test.test3.test4', 'test2']
|
* output: ['test.test1', 'test.test2', 'test.test3.test4', 'test2']
|
||||||
* @param input
|
* @param input
|
||||||
*/
|
*/
|
||||||
export function buildLegacyFieldsListFrom<TEntity>(
|
export function objectToStringPath<TEntity>(
|
||||||
input:
|
input:
|
||||||
| FindOptionsWhere<TEntity>
|
| FindOptionsWhere<TEntity>
|
||||||
| FindOptionsSelect<TEntity>
|
| FindOptionsSelect<TEntity>
|
||||||
@@ -168,7 +169,7 @@ export function buildLegacyFieldsListFrom<TEntity>(
|
|||||||
|
|
||||||
for (const key of Object.keys(input)) {
|
for (const key of Object.keys(input)) {
|
||||||
if (input[key] != undefined && typeof input[key] === "object") {
|
if (input[key] != undefined && typeof input[key] === "object") {
|
||||||
const deepRes = buildLegacyFieldsListFrom(input[key])
|
const deepRes = objectToStringPath(input[key])
|
||||||
|
|
||||||
const items = deepRes.reduce((acc, val) => {
|
const items = deepRes.reduce((acc, val) => {
|
||||||
acc.push(`${key}.${val}`)
|
acc.push(`${key}.${val}`)
|
||||||
|
|||||||
Reference in new issue
Block a user