Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-garlics-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

chore(medusa): rename buildLegacyFieldsListFrom to objectToStringPath
4 changes: 2 additions & 2 deletions packages/inventory/src/utils/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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))
}

Expand Down
6 changes: 3 additions & 3 deletions packages/medusa/src/repositories/customer-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
queryEntityWithIds,
queryEntityWithoutRelations,
} from "../utils/repository"
import { buildLegacyFieldsListFrom } from "../utils"
import { objectToStringPath } from "../utils"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Import from @medusajs/utils

import { dataSource } from "../loaders/database"

export type DefaultWithoutRelations = Omit<
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/repositories/order.ts
Original file line number Diff line number Diff line change
@@ -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 "../utils"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Import from @medusajs/utils

import { dataSource } from "../loaders/database"

const ITEMS_REL_NAME = "items"
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions packages/medusa/src/repositories/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "../utils"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Import from @medusajs/utils

import { isEmpty } from "lodash"

export const ProductCategoryRepository = dataSource
Expand Down Expand Up @@ -44,8 +44,8 @@ export const ProductCategoryRepository = dataSource
const options_ = { ...options }
options_.where = options_.where as FindOptionsWhere<ProductCategory>

const legacySelect = buildLegacyFieldsListFrom(options_.select)
const legacyRelations = buildLegacyFieldsListFrom(options_.relations)
const legacySelect = objectToStringPath(options_.select)
const legacyRelations = objectToStringPath(options_.relations)

const selectStatements = (relationName: string): string[] => {
const modelColumns = this.metadata.ownColumns.map(
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/repositories/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ExtendedFindConfig } from "../types/common"
import { dataSource } from "../loaders/database"
import { ProductFilterOptions } from "../types/product"
import {
buildLegacyFieldsListFrom,
objectToStringPath,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Import from @medusajs/utils

isObject,
fetchCategoryDescendantsIds,
} from "../utils"
Expand Down Expand Up @@ -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 ?? {}
)

Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/repositories/tax-rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "../utils"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Import from @medusajs/utils

import { dataSource } from "../loaders/database"

const resolveableFields = [
Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions packages/medusa/src/utils/__tests__/build-query.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FindOptionsOrder, FindOptionsSelect, In, MoreThan, Not } from "typeorm"
import { addOrderToSelect, buildLegacyFieldsListFrom, buildQuery } from "../build-query"
import { addOrderToSelect, objectToStringPath, buildQuery } from "../build-query"

describe("buildQuery", () => {
it("successfully creates query", () => {
Expand Down Expand Up @@ -139,9 +139,9 @@ describe("buildQuery", () => {
})
})

describe("buildLegacyFieldsListFrom", () => {
describe("objectToStringPath", () => {
it("successfully build back select object shape to list", () => {
const q = buildLegacyFieldsListFrom({
const q = objectToStringPath({
order: {
items: true,
swaps: {
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("buildLegacyFieldsListFrom", () => {
})

it("successfully build back relation object shape to list", () => {
const q = buildLegacyFieldsListFrom({
const q = objectToStringPath({
order: {
items: true,
swaps: {
Expand Down Expand Up @@ -241,7 +241,7 @@ describe("buildLegacyFieldsListFrom", () => {
})

it("successfully build back order object shape to list", () => {
const q = buildLegacyFieldsListFrom({
const q = objectToStringPath({
id: "ASC",
items: {
id: "ASC",
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/utils/build-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,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>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Keep this and deprecate it to stay backward compatible. And change all occurrences to import from @medusajs/utils

input:
| FindOptionsWhere<TEntity>
| FindOptionsSelect<TEntity>
Expand All @@ -168,7 +168,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}`)
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/common/build-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,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>
Expand All @@ -168,7 +168,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}`)
Expand Down