chore(utils): clean util package deps (#4146)

This commit is contained in:
Frane Polić
2023-05-26 13:20:12 +02:00
committed by GitHub
parent f47b11293e
commit db41995307
36 changed files with 593 additions and 996 deletions
+26 -164
View File
@@ -17,6 +17,17 @@ import { ExtendedFindConfig, FindConfig } from "../types/common"
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
import { isObject } from "./is-object"
import { buildOrder, buildRelations, buildSelects } from "@medusajs/utils"
const operatorsMap = {
lt: (value) => LessThan(value),
gt: (value) => MoreThan(value),
lte: (value) => LessThanOrEqual(value),
gte: (value) => MoreThanOrEqual(value),
contains: (value) => ILike(`%${value}%`),
starts_with: (value) => ILike(`${value}%`),
ends_with: (value) => ILike(`%${value}`),
}
/**
* Used to build TypeORM queries.
@@ -45,15 +56,19 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
}
if (config.relations) {
query.relations = buildRelations<TEntity>(config.relations)
query.relations = buildRelations(
config.relations
) as FindOptionsRelations<TEntity>
}
if (config.select) {
query.select = buildSelects<TEntity>(config.select as string[])
query.select = buildSelects(
config.select as string[]
) as FindOptionsSelect<TEntity>
}
if (config.order) {
query.order = buildOrder<TEntity>(config.order)
query.order = buildOrder(config.order) as FindOptionsOrder<TEntity>
}
return query
@@ -109,34 +124,14 @@ function buildWhere<TWhereKeys extends object, TEntity>(
if (typeof value === "object") {
Object.entries(value).forEach(([objectKey, objectValue]) => {
where[key] = where[key] || []
switch (objectKey) {
case "lt":
where[key].push(LessThan(objectValue))
break
case "gt":
where[key].push(MoreThan(objectValue))
break
case "lte":
where[key].push(LessThanOrEqual(objectValue))
break
case "gte":
where[key].push(MoreThanOrEqual(objectValue))
break
case "contains":
where[key].push(ILike(`%${objectValue}%`))
break
case "starts_with":
where[key].push(ILike(`${objectValue}%`))
break
case "ends_with":
where[key].push(ILike(`%${objectValue}`))
break
default:
if (objectValue != undefined && typeof objectValue === "object") {
where[key] = buildWhere<any, TEntity>(objectValue)
return
}
where[key] = value
if (operatorsMap[objectKey]) {
where[key].push(operatorsMap[objectKey](objectValue))
} else {
if (objectValue != undefined && typeof objectValue === "object") {
where[key] = buildWhere<any, TEntity>(objectValue)
return
}
where[key] = value
}
return
})
@@ -209,20 +204,6 @@ export function buildLegacyFieldsListFrom<TEntity>(
return Array.from(output) as (keyof TEntity)[]
}
export function buildSelects<TEntity>(
selectCollection: string[]
): FindOptionsSelect<TEntity> {
return buildRelationsOrSelect(selectCollection) as FindOptionsSelect<TEntity>
}
export function buildRelations<TEntity>(
relationCollection: string[]
): FindOptionsRelations<TEntity> {
return buildRelationsOrSelect(
relationCollection
) as FindOptionsRelations<TEntity>
}
export function addOrderToSelect<TEntity>(
order: FindOptionsOrder<TEntity>,
select: FindOptionsSelect<TEntity>
@@ -241,125 +222,6 @@ export function addOrderToSelect<TEntity>(
}
}
/**
* Convert an collection of dot string into a nested object
* @example
* input: [
* order,
* order.items,
* order.swaps,
* order.swaps.additional_items,
* order.discounts,
* order.discounts.rule,
* order.claims,
* order.claims.additional_items,
* additional_items,
* additional_items.variant,
* return_order,
* return_order.items,
* return_order.shipping_method,
* return_order.shipping_method.tax_lines
* ]
* output: {
* "order": {
* "items": true,
* "swaps": {
* "additional_items": true
* },
* "discounts": {
* "rule": true
* },
* "claims": {
* "additional_items": true
* }
* },
* "additional_items": {
* "variant": true
* },
* "return_order": {
* "items": true,
* "shipping_method": {
* "tax_lines": true
* }
* }
* }
* @param collection
*/
export function buildRelationsOrSelect<TEntity>(
collection: string[]
): FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> {
const output: FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> = {}
for (const relation of collection) {
if (relation.indexOf(".") > -1) {
const nestedRelations = relation.split(".")
let parent = output
while (nestedRelations.length > 1) {
const nestedRelation = nestedRelations.shift() as string
parent = parent[nestedRelation] =
parent[nestedRelation] !== true &&
typeof parent[nestedRelation] === "object"
? parent[nestedRelation]
: {}
}
parent[nestedRelations[0]] = true
continue
}
output[relation] = output[relation] ?? true
}
return output
}
/**
* Convert an order of dot string into a nested object
* @example
* input: { id: "ASC", "items.title": "ASC", "items.variant.title": "ASC" }
* output: {
* "id": "ASC",
* "items": {
* "id": "ASC",
* "variant": {
* "title": "ASC"
* }
* },
* }
* @param orderBy
*/
function buildOrder<TEntity>(orderBy: {
[k: string]: "ASC" | "DESC"
}): FindOptionsOrder<TEntity> {
const output: FindOptionsOrder<TEntity> = {}
const orderKeys = Object.keys(orderBy)
for (const order of orderKeys) {
if (order.indexOf(".") > -1) {
const nestedOrder = order.split(".")
let parent = output
while (nestedOrder.length > 1) {
const nestedRelation = nestedOrder.shift() as string
parent = parent[nestedRelation] = parent[nestedRelation] ?? {}
}
parent[nestedOrder[0]] = orderBy[order]
continue
}
output[order] = orderBy[order]
}
return output
}
export function nullableValue(value: any): FindOperator<any> {
if (value === null) {
return IsNull()