chore(medusa, utils): rename buildLegacyFieldsListFrom to objectToStringPath (#3738)

* chore(medusa): rename buildLegacyFieldsListFrom to objectToStringPath

* chore: address pr comments
This commit is contained in:
Riqwan Thamir
2023-04-07 10:27:13 +02:00
committed by GitHub
parent 08f85fa33b
commit abdb74d997
9 changed files with 26 additions and 18 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/utils": patch
---
chore(medusa, utils): rename buildLegacyFieldsListFrom to objectToStringPath

View File

@@ -3,7 +3,7 @@ import {
FilterableInventoryItemProps,
FindConfig,
} from "@medusajs/types"
import { buildLegacyFieldsListFrom, buildQuery } from "@medusajs/utils"
import { objectToStringPath, buildQuery } from "@medusajs/utils"
import { EntityManager, FindOptionsWhere, ILike } from "typeorm"
import { InventoryItem } from "../models"
@@ -60,7 +60,7 @@ export function getListQuery(
}
if (query.select) {
const legacySelect = buildLegacyFieldsListFrom(query.select)
const legacySelect = objectToStringPath(query.select)
queryBuilder.select(legacySelect.map((s) => "inv_item." + s))
}

View File

@@ -13,7 +13,7 @@ import {
queryEntityWithIds,
queryEntityWithoutRelations,
} from "../utils/repository"
import { buildLegacyFieldsListFrom } from "../utils"
import { objectToStringPath } from "@medusajs/utils"
import { dataSource } from "../loaders/database"
export type DefaultWithoutRelations = Omit<
@@ -135,10 +135,10 @@ export const CustomerGroupRepository = dataSource
return [toReturn, toReturn.length]
}
const legacyRelations = buildLegacyFieldsListFrom(relations)
const legacyRelations = objectToStringPath(relations)
const groupedRelations = getGroupedRelations(legacyRelations)
const legacySelect = buildLegacyFieldsListFrom(
const legacySelect = objectToStringPath(
idsOrOptionsWithoutRelations.select
)
const entitiesIdsWithRelations = await queryEntityWithIds(

View File

@@ -1,7 +1,7 @@
import { flatten, groupBy, map, merge } from "lodash"
import { FindManyOptions, FindOptionsRelations, In } from "typeorm"
import { Order } from "../models"
import { buildLegacyFieldsListFrom } from "../utils"
import { objectToStringPath } from "@medusajs/utils"
import { dataSource } from "../loaders/database"
const ITEMS_REL_NAME = "items"
@@ -16,7 +16,7 @@ export const OrderRepository = dataSource.getRepository(Order).extend({
const entitiesIds = entities.map(({ id }) => id)
const groupedRelations: { [topLevel: string]: string[] } = {}
for (const rel of buildLegacyFieldsListFrom(relations)) {
for (const rel of objectToStringPath(relations)) {
const [topLevel] = rel.split(".")
if (groupedRelations[topLevel]) {
groupedRelations[topLevel].push(rel)

View File

@@ -9,7 +9,7 @@ import {
import { ProductCategory } from "../models/product-category"
import { ExtendedFindConfig, QuerySelector } from "../types/common"
import { dataSource } from "../loaders/database"
import { buildLegacyFieldsListFrom } from "../utils"
import { objectToStringPath } from "@medusajs/utils"
import { isEmpty } from "lodash"
export const ProductCategoryRepository = dataSource
@@ -44,8 +44,8 @@ export const ProductCategoryRepository = dataSource
const options_ = { ...options }
options_.where = options_.where as FindOptionsWhere<ProductCategory>
const columnsSelected = buildLegacyFieldsListFrom(options_.select)
const relationsSelected = buildLegacyFieldsListFrom(options_.relations)
const columnsSelected = objectToStringPath(options_.select)
const relationsSelected = objectToStringPath(options_.relations)
const fetchSelectColumns = (relationName: string): string[] => {
const modelColumns = this.metadata.ownColumns.map(

View File

@@ -10,10 +10,10 @@ import { ExtendedFindConfig } from "../types/common"
import { dataSource } from "../loaders/database"
import { ProductFilterOptions } from "../types/product"
import {
buildLegacyFieldsListFrom,
fetchCategoryDescendantsIds,
isObject,
fetchCategoryDescendantsIds,
} from "../utils"
import { objectToStringPath } from "@medusajs/utils"
export const ProductRepository = dataSource.getRepository(Product).extend({
async bulkAddToCollection(
@@ -89,7 +89,7 @@ export const ProductRepository = dataSource.getRepository(Product).extend({
// https://github.com/typeorm/typeorm/issues/6294
// 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 ?? {}
)

View File

@@ -16,7 +16,7 @@ import {
} from "../models"
import { TaxRateListByConfig } from "../types/tax-rate"
import { isDefined } from "medusa-core-utils"
import { buildLegacyFieldsListFrom } from "../utils"
import { objectToStringPath } from "@medusajs/utils"
import { dataSource } from "../loaders/database"
const resolveableFields = [
@@ -33,7 +33,7 @@ export const TaxRateRepository = dataSource.getRepository(TaxRate).extend({
const resolverFields: string[] = []
if (isDefined(findOptions.select)) {
const selectableCols: (keyof TaxRate)[] = []
const legacySelect = buildLegacyFieldsListFrom(
const legacySelect = objectToStringPath(
findOptions.select as FindOptionsSelect<TaxRate>
)
for (const k of legacySelect) {

View File

@@ -139,6 +139,7 @@ function buildWhere<TWhereKeys extends object, TEntity>(
/**
* Revert new object structure of find options to the legacy structure of previous version
* @deprecated in favor of import { objectToStringPath } from "@medusajs/utils"
* @example
* input: {
* test: {

View File

@@ -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
* input: {
* test: {
@@ -153,7 +154,7 @@ function buildWhere<TWhereKeys extends object, TEntity>(
* output: ['test.test1', 'test.test2', 'test.test3.test4', 'test2']
* @param input
*/
export function buildLegacyFieldsListFrom<TEntity>(
export function objectToStringPath<TEntity>(
input:
| FindOptionsWhere<TEntity>
| FindOptionsSelect<TEntity>
@@ -168,7 +169,7 @@ export function buildLegacyFieldsListFrom<TEntity>(
for (const key of Object.keys(input)) {
if (input[key] != undefined && typeof input[key] === "object") {
const deepRes = buildLegacyFieldsListFrom(input[key])
const deepRes = objectToStringPath(input[key])
const items = deepRes.reduce((acc, val) => {
acc.push(`${key}.${val}`)