chore(utils): clean util package deps (#4146)
This commit is contained in:
8
.changeset/twelve-otters-jam.md
Normal file
8
.changeset/twelve-otters-jam.md
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
"@medusajs/stock-location": minor
|
||||
"@medusajs/inventory": minor
|
||||
"@medusajs/utils": minor
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
chore(medusa, utils, inventory, stock-location): clear deps in the utils package
|
||||
@@ -1,8 +1,29 @@
|
||||
import { BeforeInsert, Column, Entity, Index } from "typeorm"
|
||||
import { SoftDeletableEntity, generateEntityId } from "@medusajs/utils"
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
import {
|
||||
BeforeInsert,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
|
||||
@Entity()
|
||||
export class InventoryItem extends SoftDeletableEntity {
|
||||
export class InventoryItem {
|
||||
@PrimaryColumn()
|
||||
id: string
|
||||
|
||||
@CreateDateColumn({ type: "timestamptz" })
|
||||
created_at: Date
|
||||
|
||||
@UpdateDateColumn({ type: "timestamptz" })
|
||||
updated_at: Date
|
||||
|
||||
@DeleteDateColumn({ type: "timestamptz" })
|
||||
deleted_at: Date | null
|
||||
|
||||
@Index({ unique: true })
|
||||
@Column({ type: "text", nullable: true })
|
||||
sku: string | null
|
||||
|
||||
@@ -1,9 +1,30 @@
|
||||
import { generateEntityId, SoftDeletableEntity } from "@medusajs/utils"
|
||||
import { BeforeInsert, Column, Entity, Index } from "typeorm"
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
import {
|
||||
BeforeInsert,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
|
||||
@Entity()
|
||||
@Index(["inventory_item_id", "location_id"], { unique: true })
|
||||
export class InventoryLevel extends SoftDeletableEntity {
|
||||
export class InventoryLevel {
|
||||
@PrimaryColumn()
|
||||
id: string
|
||||
|
||||
@CreateDateColumn({ type: "timestamptz" })
|
||||
created_at: Date
|
||||
|
||||
@UpdateDateColumn({ type: "timestamptz" })
|
||||
updated_at: Date
|
||||
|
||||
@DeleteDateColumn({ type: "timestamptz" })
|
||||
deleted_at: Date | null
|
||||
|
||||
@Index()
|
||||
@Column({ type: "text" })
|
||||
inventory_item_id: string
|
||||
|
||||
@@ -1,8 +1,29 @@
|
||||
import { BeforeInsert, Column, Entity, Index } from "typeorm"
|
||||
import { SoftDeletableEntity, generateEntityId } from "@medusajs/utils"
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
import {
|
||||
BeforeInsert,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
|
||||
@Entity()
|
||||
export class ReservationItem extends SoftDeletableEntity {
|
||||
export class ReservationItem {
|
||||
@PrimaryColumn()
|
||||
id: string
|
||||
|
||||
@CreateDateColumn({ type: "timestamptz" })
|
||||
created_at: Date
|
||||
|
||||
@UpdateDateColumn({ type: "timestamptz" })
|
||||
updated_at: Date
|
||||
|
||||
@DeleteDateColumn({ type: "timestamptz" })
|
||||
deleted_at: Date | null
|
||||
|
||||
@Index()
|
||||
@Column({ type: "text", nullable: true })
|
||||
line_item_id: string | null
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
SharedContext,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
buildQuery,
|
||||
InjectEntityManager,
|
||||
isDefined,
|
||||
MedusaContext,
|
||||
@@ -16,6 +15,7 @@ import {
|
||||
import { DeepPartial, EntityManager, FindManyOptions } from "typeorm"
|
||||
import { InventoryItem } from "../models"
|
||||
import { getListQuery } from "../utils/query"
|
||||
import { buildQuery } from "../utils/build-query"
|
||||
|
||||
type InjectedDependencies = {
|
||||
eventBusService: IEventBusService
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
SharedContext,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
buildQuery,
|
||||
InjectEntityManager,
|
||||
isDefined,
|
||||
MedusaContext,
|
||||
@@ -14,6 +13,7 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
import { DeepPartial, EntityManager, FindManyOptions, In } from "typeorm"
|
||||
import { InventoryLevel } from "../models"
|
||||
import { buildQuery } from "../utils/build-query"
|
||||
|
||||
type InjectedDependencies = {
|
||||
eventBusService: IEventBusService
|
||||
|
||||
@@ -10,12 +10,12 @@ import {
|
||||
InjectEntityManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
buildQuery,
|
||||
isDefined,
|
||||
} from "@medusajs/utils"
|
||||
import { EntityManager, FindManyOptions } from "typeorm"
|
||||
import { InventoryLevelService } from "."
|
||||
import { ReservationItem } from "../models"
|
||||
import { buildQuery } from "../utils/build-query"
|
||||
|
||||
type InjectedDependencies = {
|
||||
eventBusService: IEventBusService
|
||||
|
||||
154
packages/inventory/src/utils/build-query.ts
Normal file
154
packages/inventory/src/utils/build-query.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
import { ExtendedFindConfig, FindConfig } from "@medusajs/types"
|
||||
import {
|
||||
And,
|
||||
FindManyOptions,
|
||||
FindOperator,
|
||||
FindOptionsOrder,
|
||||
FindOptionsRelations,
|
||||
FindOptionsSelect,
|
||||
FindOptionsWhere,
|
||||
ILike,
|
||||
In,
|
||||
IsNull,
|
||||
LessThan,
|
||||
LessThanOrEqual,
|
||||
MoreThan,
|
||||
MoreThanOrEqual,
|
||||
} from "typeorm"
|
||||
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.
|
||||
* @param selector The selector
|
||||
* @param config The config
|
||||
* @return The QueryBuilderConfig
|
||||
*/
|
||||
export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
|
||||
selector: TWhereKeys,
|
||||
config: FindConfig<TEntity> = {}
|
||||
) {
|
||||
const query: ExtendedFindConfig<TEntity> = {
|
||||
where: buildWhere<TWhereKeys, TEntity>(selector),
|
||||
}
|
||||
|
||||
if ("deleted_at" in selector) {
|
||||
query.withDeleted = true
|
||||
}
|
||||
|
||||
if ("skip" in config) {
|
||||
;(query as FindManyOptions<TEntity>).skip = config.skip
|
||||
}
|
||||
|
||||
if ("take" in config) {
|
||||
;(query as FindManyOptions<TEntity>).take = config.take
|
||||
}
|
||||
|
||||
if (config.relations) {
|
||||
query.relations = buildRelations(
|
||||
config.relations
|
||||
) as FindOptionsRelations<TEntity>
|
||||
}
|
||||
|
||||
if (config.select) {
|
||||
query.select = buildSelects(
|
||||
config.select as string[]
|
||||
) as FindOptionsSelect<TEntity>
|
||||
}
|
||||
|
||||
if (config.order) {
|
||||
query.order = buildOrder(config.order) as FindOptionsOrder<TEntity>
|
||||
}
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
/**
|
||||
* @param constraints
|
||||
*
|
||||
* @example
|
||||
* const q = buildWhere(
|
||||
* {
|
||||
* id: "1234",
|
||||
* test1: ["123", "12", "1"],
|
||||
* test2: Not("this"),
|
||||
* date: { gt: date },
|
||||
* amount: { gt: 10 },
|
||||
* },
|
||||
*)
|
||||
*
|
||||
* // Output
|
||||
* {
|
||||
* id: "1234",
|
||||
* test1: In(["123", "12", "1"]),
|
||||
* test2: Not("this"),
|
||||
* date: MoreThan(date),
|
||||
* amount: MoreThan(10)
|
||||
* }
|
||||
*/
|
||||
function buildWhere<TWhereKeys extends object, TEntity>(
|
||||
constraints: TWhereKeys
|
||||
): FindOptionsWhere<TEntity> {
|
||||
const where: FindOptionsWhere<TEntity> = {}
|
||||
for (const [key, value] of Object.entries(constraints)) {
|
||||
if (value === undefined) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
where[key] = IsNull()
|
||||
continue
|
||||
}
|
||||
|
||||
if (value instanceof FindOperator) {
|
||||
where[key] = value
|
||||
continue
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
where[key] = In(value)
|
||||
continue
|
||||
}
|
||||
|
||||
if (typeof value === "object") {
|
||||
Object.entries(value).forEach(([objectKey, objectValue]) => {
|
||||
where[key] = where[key] || []
|
||||
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
|
||||
})
|
||||
|
||||
if (!Array.isArray(where[key])) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (where[key].length === 1) {
|
||||
where[key] = where[key][0]
|
||||
} else {
|
||||
where[key] = And(...where[key])
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
where[key] = value
|
||||
}
|
||||
|
||||
return where
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
import { Brackets, EntityManager, FindOptionsWhere } from "typeorm"
|
||||
import {
|
||||
ExtendedFindConfig,
|
||||
FilterableInventoryItemProps,
|
||||
FindConfig,
|
||||
} from "@medusajs/types"
|
||||
import { buildQuery, objectToStringPath } from "@medusajs/utils"
|
||||
import { objectToStringPath } from "@medusajs/utils"
|
||||
import { EntityManager, FindOptionsWhere, Brackets } from "typeorm"
|
||||
|
||||
import { InventoryItem } from "../models"
|
||||
import { buildQuery } from "./build-query"
|
||||
|
||||
export function getListQuery(
|
||||
manager: EntityManager,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { TransactionBaseService } from "@medusajs/utils"
|
||||
import { TransactionBaseService } from "./transaction-base-service"
|
||||
import { IdempotencyKey } from "../models"
|
||||
import { RequestContext } from "../types/request"
|
||||
|
||||
|
||||
@@ -82,7 +82,9 @@ export const CustomerGroupRepository = dataSource
|
||||
: { ...idsOrOptionsWithoutRelations.order }
|
||||
const originalSelect = isOptionsArray
|
||||
? undefined
|
||||
: objectToStringPath(idsOrOptionsWithoutRelations.select)
|
||||
: (objectToStringPath(
|
||||
idsOrOptionsWithoutRelations.select
|
||||
) as (keyof CustomerGroup)[])
|
||||
const clonedOptions = isOptionsArray
|
||||
? idsOrOptionsWithoutRelations
|
||||
: cloneDeep(idsOrOptionsWithoutRelations)
|
||||
|
||||
@@ -479,7 +479,9 @@ export const ProductRepository = dataSource.getRepository(Product).extend({
|
||||
: { ...idsOrOptionsWithoutRelations.order }
|
||||
const originalSelect = isOptionsArray
|
||||
? undefined
|
||||
: objectToStringPath(idsOrOptionsWithoutRelations.select)
|
||||
: (objectToStringPath(
|
||||
idsOrOptionsWithoutRelations.select
|
||||
) as (keyof Product)[])
|
||||
const clonedOptions = isOptionsArray
|
||||
? idsOrOptionsWithoutRelations
|
||||
: cloneDeep(idsOrOptionsWithoutRelations)
|
||||
|
||||
@@ -35,7 +35,7 @@ export const TaxRateRepository = dataSource.getRepository(TaxRate).extend({
|
||||
const selectableCols: (keyof TaxRate)[] = []
|
||||
const legacySelect = objectToStringPath(
|
||||
findOptions.select as FindOptionsSelect<TaxRate>
|
||||
)
|
||||
) as (keyof TaxRate)[]
|
||||
for (const k of legacySelect) {
|
||||
if (!resolveableFields.includes(k)) {
|
||||
selectableCols.push(k)
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { isDefined, MedusaError, TransactionBaseService } from "@medusajs/utils"
|
||||
import {
|
||||
buildRelations,
|
||||
buildSelects,
|
||||
isDefined,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
EntityManager,
|
||||
FindManyOptions,
|
||||
@@ -45,6 +50,7 @@ import {
|
||||
Swap,
|
||||
TrackingLink,
|
||||
} from "../models"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
import { AddressRepository } from "../repositories/address"
|
||||
import { OrderRepository } from "../repositories/order"
|
||||
import { FindConfig, QuerySelector, Selector } from "../types/common"
|
||||
@@ -54,13 +60,7 @@ import {
|
||||
} from "../types/fulfillment"
|
||||
import { TotalsContext, UpdateOrderInput } from "../types/orders"
|
||||
import { CreateShippingMethodDto } from "../types/shipping-options"
|
||||
import {
|
||||
buildQuery,
|
||||
buildRelations,
|
||||
buildSelects,
|
||||
isString,
|
||||
setMetadata,
|
||||
} from "../utils"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
import { FlagRouter } from "../utils/flag-router"
|
||||
import EventBusService from "./event-bus"
|
||||
|
||||
|
||||
@@ -10,8 +10,9 @@ import {
|
||||
ReserveQuantityContext,
|
||||
} from "@medusajs/types"
|
||||
import { LineItem, Product, ProductVariant } from "../models"
|
||||
import { MedusaError, TransactionBaseService, isDefined } from "@medusajs/utils"
|
||||
import { MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { PricedProduct, PricedVariant } from "../types/pricing"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
|
||||
import { ProductVariantInventoryItem } from "../models/product-variant-inventory-item"
|
||||
import ProductVariantService from "./product-variant"
|
||||
|
||||
@@ -40,7 +40,6 @@ import {
|
||||
} from "../types/product-variant"
|
||||
import {
|
||||
buildQuery,
|
||||
buildRelations,
|
||||
hasChanges,
|
||||
isObject,
|
||||
isString,
|
||||
@@ -54,6 +53,7 @@ import { ProductRepository } from "../repositories/product"
|
||||
import { ProductOptionValueRepository } from "../repositories/product-option-value"
|
||||
import EventBusService from "./event-bus"
|
||||
import RegionService from "./region"
|
||||
import { buildRelations } from "@medusajs/utils"
|
||||
|
||||
class ProductVariantService extends TransactionBaseService {
|
||||
static Events = {
|
||||
|
||||
@@ -31,15 +31,14 @@ import {
|
||||
ProductSelector,
|
||||
UpdateProductInput,
|
||||
} from "../types/product"
|
||||
import {
|
||||
buildQuery,
|
||||
buildRelationsOrSelect,
|
||||
isString,
|
||||
setMetadata,
|
||||
} from "../utils"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
import { FlagRouter } from "../utils/flag-router"
|
||||
import EventBusService from "./event-bus"
|
||||
import { objectToStringPath } from "@medusajs/utils"
|
||||
import {
|
||||
buildRelations,
|
||||
buildSelects,
|
||||
objectToStringPath,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
manager: EntityManager
|
||||
@@ -967,11 +966,11 @@ class ProductService extends TransactionBaseService {
|
||||
query.order = config.order
|
||||
|
||||
if (config.relations && config.relations.length > 0) {
|
||||
query.relations = buildRelationsOrSelect(config.relations)
|
||||
query.relations = buildRelations(config.relations)
|
||||
}
|
||||
|
||||
if (config.select && config.select.length > 0) {
|
||||
query.select = buildRelationsOrSelect(config.select)
|
||||
query.select = buildSelects(config.select)
|
||||
}
|
||||
|
||||
const rels = objectToStringPath(query.relations)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { EventBusTypes, IInventoryService } from "@medusajs/types"
|
||||
import { TransactionBaseService } from "@medusajs/utils"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
import { EntityManager } from "typeorm"
|
||||
import SalesChannelLocationService from "./sales-channel-location"
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -1,8 +1,29 @@
|
||||
import { generateEntityId, SoftDeletableEntity } from "@medusajs/utils"
|
||||
import { BeforeInsert, Column, Entity, Index } from "typeorm"
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
import {
|
||||
BeforeInsert,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
|
||||
@Entity()
|
||||
export class StockLocationAddress extends SoftDeletableEntity {
|
||||
export class StockLocationAddress {
|
||||
@PrimaryColumn()
|
||||
id: string
|
||||
|
||||
@CreateDateColumn({ type: "timestamptz" })
|
||||
created_at: Date
|
||||
|
||||
@UpdateDateColumn({ type: "timestamptz" })
|
||||
updated_at: Date
|
||||
|
||||
@DeleteDateColumn({ type: "timestamptz" })
|
||||
deleted_at: Date | null
|
||||
|
||||
@Column({ type: "text" })
|
||||
address_1: string
|
||||
|
||||
|
||||
@@ -1,16 +1,32 @@
|
||||
import { generateEntityId, SoftDeletableEntity } from "@medusajs/utils"
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
import {
|
||||
BeforeInsert,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
import { StockLocationAddress } from "./stock-location-address"
|
||||
|
||||
@Entity()
|
||||
export class StockLocation extends SoftDeletableEntity {
|
||||
export class StockLocation {
|
||||
@PrimaryColumn()
|
||||
id: string
|
||||
|
||||
@CreateDateColumn({ type: "timestamptz" })
|
||||
created_at: Date
|
||||
|
||||
@UpdateDateColumn({ type: "timestamptz" })
|
||||
updated_at: Date
|
||||
|
||||
@DeleteDateColumn({ type: "timestamptz" })
|
||||
deleted_at: Date | null
|
||||
|
||||
@Column({ type: "text" })
|
||||
name: string
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import {
|
||||
UpdateStockLocationInput,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
buildQuery,
|
||||
InjectEntityManager,
|
||||
isDefined,
|
||||
MedusaContext,
|
||||
@@ -18,7 +17,9 @@ import {
|
||||
setMetadata,
|
||||
} from "@medusajs/utils"
|
||||
import { EntityManager } from "typeorm"
|
||||
|
||||
import { StockLocation, StockLocationAddress } from "../models"
|
||||
import { buildQuery } from "../utils/build-query"
|
||||
|
||||
type InjectedDependencies = {
|
||||
manager: EntityManager
|
||||
|
||||
154
packages/stock-location/src/utils/build-query.ts
Normal file
154
packages/stock-location/src/utils/build-query.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
import { ExtendedFindConfig, FindConfig } from "@medusajs/types"
|
||||
import {
|
||||
And,
|
||||
FindManyOptions,
|
||||
FindOperator,
|
||||
FindOptionsOrder,
|
||||
FindOptionsRelations,
|
||||
FindOptionsSelect,
|
||||
FindOptionsWhere,
|
||||
ILike,
|
||||
In,
|
||||
IsNull,
|
||||
LessThan,
|
||||
LessThanOrEqual,
|
||||
MoreThan,
|
||||
MoreThanOrEqual,
|
||||
} from "typeorm"
|
||||
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.
|
||||
* @param selector The selector
|
||||
* @param config The config
|
||||
* @return The QueryBuilderConfig
|
||||
*/
|
||||
export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
|
||||
selector: TWhereKeys,
|
||||
config: FindConfig<TEntity> = {}
|
||||
) {
|
||||
const query: ExtendedFindConfig<TEntity> = {
|
||||
where: buildWhere<TWhereKeys, TEntity>(selector),
|
||||
}
|
||||
|
||||
if ("deleted_at" in selector) {
|
||||
query.withDeleted = true
|
||||
}
|
||||
|
||||
if ("skip" in config) {
|
||||
;(query as FindManyOptions<TEntity>).skip = config.skip
|
||||
}
|
||||
|
||||
if ("take" in config) {
|
||||
;(query as FindManyOptions<TEntity>).take = config.take
|
||||
}
|
||||
|
||||
if (config.relations) {
|
||||
query.relations = buildRelations(
|
||||
config.relations
|
||||
) as FindOptionsRelations<TEntity>
|
||||
}
|
||||
|
||||
if (config.select) {
|
||||
query.select = buildSelects(
|
||||
config.select as string[]
|
||||
) as FindOptionsSelect<TEntity>
|
||||
}
|
||||
|
||||
if (config.order) {
|
||||
query.order = buildOrder(config.order) as FindOptionsOrder<TEntity>
|
||||
}
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
/**
|
||||
* @param constraints
|
||||
*
|
||||
* @example
|
||||
* const q = buildWhere(
|
||||
* {
|
||||
* id: "1234",
|
||||
* test1: ["123", "12", "1"],
|
||||
* test2: Not("this"),
|
||||
* date: { gt: date },
|
||||
* amount: { gt: 10 },
|
||||
* },
|
||||
*)
|
||||
*
|
||||
* // Output
|
||||
* {
|
||||
* id: "1234",
|
||||
* test1: In(["123", "12", "1"]),
|
||||
* test2: Not("this"),
|
||||
* date: MoreThan(date),
|
||||
* amount: MoreThan(10)
|
||||
* }
|
||||
*/
|
||||
function buildWhere<TWhereKeys extends object, TEntity>(
|
||||
constraints: TWhereKeys
|
||||
): FindOptionsWhere<TEntity> {
|
||||
const where: FindOptionsWhere<TEntity> = {}
|
||||
for (const [key, value] of Object.entries(constraints)) {
|
||||
if (value === undefined) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
where[key] = IsNull()
|
||||
continue
|
||||
}
|
||||
|
||||
if (value instanceof FindOperator) {
|
||||
where[key] = value
|
||||
continue
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
where[key] = In(value)
|
||||
continue
|
||||
}
|
||||
|
||||
if (typeof value === "object") {
|
||||
Object.entries(value).forEach(([objectKey, objectValue]) => {
|
||||
where[key] = where[key] || []
|
||||
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
|
||||
})
|
||||
|
||||
if (!Array.isArray(where[key])) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (where[key].length === 1) {
|
||||
where[key] = where[key][0]
|
||||
} else {
|
||||
where[key] = And(...where[key])
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
where[key] = value
|
||||
}
|
||||
|
||||
return where
|
||||
}
|
||||
@@ -26,9 +26,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"awilix": "^8.0.0",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.0",
|
||||
"typeorm": "^0.3.16",
|
||||
"ulid": "^2.3.0"
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@@ -1,318 +0,0 @@
|
||||
import {
|
||||
And,
|
||||
FindOptionsOrder,
|
||||
FindOptionsSelect,
|
||||
In,
|
||||
LessThanOrEqual,
|
||||
MoreThan,
|
||||
MoreThanOrEqual,
|
||||
Not,
|
||||
} from "typeorm"
|
||||
import {
|
||||
addOrderToSelect,
|
||||
buildQuery,
|
||||
objectToStringPath,
|
||||
} from "../build-query"
|
||||
|
||||
describe("buildQuery", () => {
|
||||
it("successfully creates query", () => {
|
||||
const date = new Date()
|
||||
|
||||
const q = buildQuery(
|
||||
{
|
||||
id: "1234",
|
||||
test1: ["123", "12", "1"],
|
||||
test2: Not("this"),
|
||||
date: { gt: date },
|
||||
amount: { gt: 10 },
|
||||
rule: {
|
||||
type: "fixed",
|
||||
},
|
||||
updated_at: {
|
||||
gte: "value",
|
||||
lte: "value",
|
||||
},
|
||||
},
|
||||
{
|
||||
select: [
|
||||
"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",
|
||||
],
|
||||
relations: [
|
||||
"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",
|
||||
"items.variants",
|
||||
"items.variants.product",
|
||||
"items",
|
||||
"items.tax_lines",
|
||||
"items.adjustments",
|
||||
],
|
||||
order: {
|
||||
id: "ASC",
|
||||
"items.id": "ASC",
|
||||
"items.variant.id": "ASC",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(q).toEqual({
|
||||
where: {
|
||||
id: "1234",
|
||||
test1: In(["123", "12", "1"]),
|
||||
test2: Not("this"),
|
||||
date: MoreThan(date),
|
||||
amount: MoreThan(10),
|
||||
rule: {
|
||||
type: "fixed",
|
||||
},
|
||||
updated_at: And(MoreThanOrEqual("value"), LessThanOrEqual("value")),
|
||||
},
|
||||
select: {
|
||||
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,
|
||||
},
|
||||
},
|
||||
},
|
||||
relations: {
|
||||
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,
|
||||
},
|
||||
},
|
||||
items: {
|
||||
variants: {
|
||||
product: true,
|
||||
},
|
||||
tax_lines: true,
|
||||
adjustments: true,
|
||||
},
|
||||
},
|
||||
order: {
|
||||
id: "ASC",
|
||||
items: {
|
||||
id: "ASC",
|
||||
variant: {
|
||||
id: "ASC",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("objectToStringPath", () => {
|
||||
it("successfully build back select object shape to list", () => {
|
||||
const q = objectToStringPath({
|
||||
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,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(q.length).toBe(14)
|
||||
expect(q).toEqual(
|
||||
expect.arrayContaining([
|
||||
"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",
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("successfully build back relation object shape to list", () => {
|
||||
const q = objectToStringPath({
|
||||
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,
|
||||
},
|
||||
},
|
||||
items: {
|
||||
variants: {
|
||||
product: true,
|
||||
},
|
||||
tax_lines: true,
|
||||
adjustments: true,
|
||||
},
|
||||
})
|
||||
|
||||
expect(q.length).toBe(19)
|
||||
expect(q).toEqual(
|
||||
expect.arrayContaining([
|
||||
"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",
|
||||
"items.variants",
|
||||
"items.variants.product",
|
||||
"items",
|
||||
"items.tax_lines",
|
||||
"items.adjustments",
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("successfully build back order object shape to list", () => {
|
||||
const q = objectToStringPath({
|
||||
id: "ASC",
|
||||
items: {
|
||||
id: "ASC",
|
||||
variant: {
|
||||
id: "ASC",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(q.length).toBe(5)
|
||||
expect(q).toEqual(
|
||||
expect.arrayContaining([
|
||||
"id",
|
||||
"items",
|
||||
"items.id",
|
||||
"items.variant",
|
||||
"items.variant.id",
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
describe("addOrderToSelect", function () {
|
||||
it("successfully add the order fields to the select object", () => {
|
||||
const select: FindOptionsSelect<any> = {
|
||||
item: {
|
||||
variant: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const order: FindOptionsOrder<any> = {
|
||||
item: {
|
||||
variant: {
|
||||
rank: "ASC",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
addOrderToSelect(order, select)
|
||||
|
||||
expect(select).toEqual({
|
||||
item: {
|
||||
variant: {
|
||||
id: true,
|
||||
rank: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
21
packages/utils/src/common/__tests__/is-email.spec.ts
Normal file
21
packages/utils/src/common/__tests__/is-email.spec.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { validateEmail } from "../is-email"
|
||||
|
||||
describe("validateEmail", () => {
|
||||
it("successfully validates an email", () => {
|
||||
expect(validateEmail("test@email.com")).toBe("test@email.com")
|
||||
expect(validateEmail("test.test@email.com")).toBe("test.test@email.com")
|
||||
expect(validateEmail("test.test123@email.com")).toBe(
|
||||
"test.test123@email.com"
|
||||
)
|
||||
})
|
||||
|
||||
it("throws on an invalidates email", () => {
|
||||
expect.assertions(1)
|
||||
|
||||
try {
|
||||
validateEmail("not-an-email")
|
||||
} catch (e) {
|
||||
expect(e.message).toBe("The email is not valid")
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -1,248 +1,27 @@
|
||||
import {
|
||||
And,
|
||||
FindManyOptions,
|
||||
FindOperator,
|
||||
FindOptionsRelations,
|
||||
FindOptionsSelect,
|
||||
FindOptionsWhere,
|
||||
ILike,
|
||||
In,
|
||||
IsNull,
|
||||
LessThan,
|
||||
LessThanOrEqual,
|
||||
MoreThan,
|
||||
MoreThanOrEqual,
|
||||
} from "typeorm"
|
||||
import { ExtendedFindConfig, FindConfig } from "@medusajs/types"
|
||||
// Those utils are used in a typeorm context and we can't be sure that they can be used elsewhere
|
||||
|
||||
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
|
||||
import { isObject } from "./is-object"
|
||||
type Order = {
|
||||
[key: string]: "ASC" | "DESC" | Order
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to build TypeORM queries.
|
||||
* @param selector The selector
|
||||
* @param config The config
|
||||
* @return The QueryBuilderConfig
|
||||
*/
|
||||
export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
|
||||
selector: TWhereKeys,
|
||||
config: FindConfig<TEntity> = {}
|
||||
) {
|
||||
const query: ExtendedFindConfig<TEntity> = {
|
||||
where: buildWhere<TWhereKeys, TEntity>(selector),
|
||||
}
|
||||
type Selects = {
|
||||
[key: string]: boolean | Selects
|
||||
}
|
||||
|
||||
if ("deleted_at" in selector) {
|
||||
query.withDeleted = true
|
||||
}
|
||||
type Relations = {
|
||||
[key: string]: boolean | Relations
|
||||
}
|
||||
|
||||
if ("skip" in config) {
|
||||
;(query as FindManyOptions<TEntity>).skip = config.skip
|
||||
}
|
||||
export function buildSelects(selectCollection: string[]): Selects {
|
||||
return buildRelationsOrSelect(selectCollection)
|
||||
}
|
||||
|
||||
if ("take" in config) {
|
||||
;(query as FindManyOptions<TEntity>).take = config.take
|
||||
}
|
||||
|
||||
if (config.relations) {
|
||||
query.relations = buildRelations<TEntity>(config.relations)
|
||||
}
|
||||
|
||||
if (config.select) {
|
||||
query.select = buildSelects<TEntity>(config.select as string[])
|
||||
}
|
||||
|
||||
if (config.order) {
|
||||
query.order = buildOrder<TEntity>(config.order)
|
||||
}
|
||||
|
||||
return query
|
||||
export function buildRelations(relationCollection: string[]): Relations {
|
||||
return buildRelationsOrSelect(relationCollection)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param constraints
|
||||
*
|
||||
* @example
|
||||
* const q = buildWhere(
|
||||
* {
|
||||
* id: "1234",
|
||||
* test1: ["123", "12", "1"],
|
||||
* test2: Not("this"),
|
||||
* date: { gt: date },
|
||||
* amount: { gt: 10 },
|
||||
* },
|
||||
*)
|
||||
*
|
||||
* // Output
|
||||
* {
|
||||
* id: "1234",
|
||||
* test1: In(["123", "12", "1"]),
|
||||
* test2: Not("this"),
|
||||
* date: MoreThan(date),
|
||||
* amount: MoreThan(10)
|
||||
* }
|
||||
*/
|
||||
function buildWhere<TWhereKeys extends object, TEntity>(
|
||||
constraints: TWhereKeys
|
||||
): FindOptionsWhere<TEntity> {
|
||||
const where: FindOptionsWhere<TEntity> = {}
|
||||
for (const [key, value] of Object.entries(constraints)) {
|
||||
if (value === undefined) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
where[key] = IsNull()
|
||||
continue
|
||||
}
|
||||
|
||||
if (value instanceof FindOperator) {
|
||||
where[key] = value
|
||||
continue
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
where[key] = In(value)
|
||||
continue
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
return
|
||||
})
|
||||
|
||||
if (!Array.isArray(where[key])) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (where[key].length === 1) {
|
||||
where[key] = where[key][0]
|
||||
} else {
|
||||
where[key] = And(...where[key])
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
where[key] = value
|
||||
}
|
||||
|
||||
return where
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a typeorms structure of find options to an
|
||||
* array of string paths
|
||||
* @example
|
||||
* input: {
|
||||
* test: {
|
||||
* test1: true,
|
||||
* test2: true,
|
||||
* test3: {
|
||||
* test4: true
|
||||
* },
|
||||
* },
|
||||
* test2: true
|
||||
* }
|
||||
* output: ['test.test1', 'test.test2', 'test.test3.test4', 'test2']
|
||||
* @param input
|
||||
*/
|
||||
export function objectToStringPath<TEntity>(
|
||||
input:
|
||||
| FindOptionsWhere<TEntity>
|
||||
| FindOptionsSelect<TEntity>
|
||||
| FindOptionsOrder<TEntity>
|
||||
| FindOptionsRelations<TEntity> = {}
|
||||
): (keyof TEntity)[] {
|
||||
if (!Object.keys(input).length) {
|
||||
return []
|
||||
}
|
||||
|
||||
const output: Set<string> = new Set(Object.keys(input))
|
||||
|
||||
for (const key of Object.keys(input)) {
|
||||
if (input[key] != undefined && typeof input[key] === "object") {
|
||||
const deepRes = objectToStringPath(input[key])
|
||||
|
||||
const items = deepRes.reduce((acc, val) => {
|
||||
acc.push(`${key}.${val}`)
|
||||
return acc
|
||||
}, [] as string[])
|
||||
|
||||
items.forEach((item) => output.add(item))
|
||||
continue
|
||||
}
|
||||
|
||||
output.add(key)
|
||||
}
|
||||
|
||||
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>
|
||||
): void {
|
||||
for (const orderBy of Object.keys(order)) {
|
||||
if (isObject(order[orderBy])) {
|
||||
select[orderBy] =
|
||||
select[orderBy] && isObject(select[orderBy]) ? select[orderBy] : {}
|
||||
addOrderToSelect(order[orderBy], select[orderBy])
|
||||
continue
|
||||
}
|
||||
|
||||
select[orderBy] = isObject(select[orderBy])
|
||||
? { ...select[orderBy], id: true, [orderBy]: true }
|
||||
: true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an collection of dot string into a nested object
|
||||
* Convert a collection of dot string into a nested object
|
||||
* @example
|
||||
* input: [
|
||||
* order,
|
||||
@@ -285,10 +64,8 @@ export function addOrderToSelect<TEntity>(
|
||||
* }
|
||||
* @param collection
|
||||
*/
|
||||
function buildRelationsOrSelect<TEntity>(
|
||||
collection: string[]
|
||||
): FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> {
|
||||
const output: FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> = {}
|
||||
function buildRelationsOrSelect(collection: string[]): Selects | Relations {
|
||||
const output: Selects | Relations = {}
|
||||
|
||||
for (const relation of collection) {
|
||||
if (relation.indexOf(".") > -1) {
|
||||
@@ -298,11 +75,12 @@ function buildRelationsOrSelect<TEntity>(
|
||||
|
||||
while (nestedRelations.length > 1) {
|
||||
const nestedRelation = nestedRelations.shift() as string
|
||||
parent = parent[nestedRelation] =
|
||||
parent = parent[nestedRelation] = (
|
||||
parent[nestedRelation] !== true &&
|
||||
typeof parent[nestedRelation] === "object"
|
||||
? parent[nestedRelation]
|
||||
: {}
|
||||
) as Selects | Relations
|
||||
}
|
||||
|
||||
parent[nestedRelations[0]] = true
|
||||
@@ -331,10 +109,8 @@ function buildRelationsOrSelect<TEntity>(
|
||||
* }
|
||||
* @param orderBy
|
||||
*/
|
||||
function buildOrder<TEntity>(orderBy: {
|
||||
[k: string]: "ASC" | "DESC"
|
||||
}): FindOptionsOrder<TEntity> {
|
||||
const output: FindOptionsOrder<TEntity> = {}
|
||||
export function buildOrder<T>(orderBy: { [k: string]: "ASC" | "DESC" }): Order {
|
||||
const output: Order = {}
|
||||
|
||||
const orderKeys = Object.keys(orderBy)
|
||||
|
||||
@@ -346,7 +122,7 @@ function buildOrder<TEntity>(orderBy: {
|
||||
|
||||
while (nestedOrder.length > 1) {
|
||||
const nestedRelation = nestedOrder.shift() as string
|
||||
parent = parent[nestedRelation] = parent[nestedRelation] ?? {}
|
||||
parent = (parent[nestedRelation] as Order) ??= {}
|
||||
}
|
||||
|
||||
parent[nestedOrder[0]] = orderBy[order]
|
||||
@@ -359,11 +135,3 @@ function buildOrder<TEntity>(orderBy: {
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
export function nullableValue(value: any): FindOperator<any> {
|
||||
if (value === null) {
|
||||
return IsNull()
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
import { Column, ColumnOptions, ColumnType } from "typeorm"
|
||||
|
||||
export function resolveDbType(pgSqlType: ColumnType): ColumnType {
|
||||
return pgSqlType
|
||||
}
|
||||
|
||||
export function resolveDbGenerationStrategy(
|
||||
pgSqlType: "increment" | "uuid" | "rowid"
|
||||
): "increment" | "uuid" | "rowid" {
|
||||
return pgSqlType
|
||||
}
|
||||
|
||||
export function DbAwareColumn(columnOptions: ColumnOptions): PropertyDecorator {
|
||||
const pre = columnOptions.type
|
||||
if (columnOptions.type) {
|
||||
columnOptions.type = resolveDbType(columnOptions.type)
|
||||
}
|
||||
|
||||
if (pre === "jsonb" && pre !== columnOptions.type) {
|
||||
if ("default" in columnOptions) {
|
||||
columnOptions.default = JSON.stringify(columnOptions.default)
|
||||
}
|
||||
}
|
||||
|
||||
return Column(columnOptions)
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
export * from "./build-query"
|
||||
export * from "./db-aware-column"
|
||||
export * from "./errors"
|
||||
export * from "./generate-entity-id"
|
||||
export * from "./get-config-file"
|
||||
@@ -8,8 +6,8 @@ export * from "./is-defined"
|
||||
export * from "./is-email"
|
||||
export * from "./is-object"
|
||||
export * from "./is-string"
|
||||
export * from "./object-to-string-path"
|
||||
export * from "./medusa-container"
|
||||
export * from "./models"
|
||||
export * from "./set-metadata"
|
||||
export * from "./transaction-base-service"
|
||||
export * from "./wrap-handler"
|
||||
export * from "./build-query"
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
import { isEmail } from "class-validator"
|
||||
import { MedusaError } from "./errors"
|
||||
|
||||
const EMAIL_REGEX =
|
||||
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
||||
|
||||
/**
|
||||
* Check whether provided string is an email.
|
||||
* @param email - string to check
|
||||
*/
|
||||
function isEmail(email: string) {
|
||||
return email.toLowerCase().match(EMAIL_REGEX)
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to validate user email.
|
||||
* @param {string} email - email to validate
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { CreateDateColumn, PrimaryColumn, UpdateDateColumn } from "typeorm"
|
||||
import { resolveDbType } from "../db-aware-column"
|
||||
|
||||
/**
|
||||
* Base abstract entity for all entities
|
||||
*/
|
||||
export abstract class BaseEntity {
|
||||
@PrimaryColumn()
|
||||
id: string
|
||||
|
||||
@CreateDateColumn({ type: resolveDbType("timestamptz") })
|
||||
created_at: Date
|
||||
|
||||
@UpdateDateColumn({ type: resolveDbType("timestamptz") })
|
||||
updated_at: Date
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./base-entity"
|
||||
export * from "./soft-deletable-entity"
|
||||
@@ -1,8 +0,0 @@
|
||||
import { DeleteDateColumn } from "typeorm"
|
||||
import { resolveDbType } from "../db-aware-column"
|
||||
import { BaseEntity } from "./base-entity"
|
||||
|
||||
export abstract class SoftDeletableEntity extends BaseEntity {
|
||||
@DeleteDateColumn({ type: resolveDbType("timestamptz") })
|
||||
deleted_at: Date | null
|
||||
}
|
||||
44
packages/utils/src/common/object-to-string-path.ts
Normal file
44
packages/utils/src/common/object-to-string-path.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
/**
|
||||
* Converts a structure of find options to an
|
||||
* array of string paths
|
||||
* @example
|
||||
* input: {
|
||||
* test: {
|
||||
* test1: true,
|
||||
* test2: true,
|
||||
* test3: {
|
||||
* test4: true
|
||||
* },
|
||||
* },
|
||||
* test2: true
|
||||
* }
|
||||
* output: ['test.test1', 'test.test2', 'test.test3.test4', 'test2']
|
||||
* @param input
|
||||
*/
|
||||
export function objectToStringPath(input: object = {}): string[] {
|
||||
if (!isObject(input) || !Object.keys(input).length) {
|
||||
return []
|
||||
}
|
||||
|
||||
const output: Set<string> = new Set(Object.keys(input))
|
||||
|
||||
for (const key of Object.keys(input)) {
|
||||
if (input[key] != undefined && typeof input[key] === "object") {
|
||||
const deepRes = objectToStringPath(input[key])
|
||||
|
||||
const items = deepRes.reduce((acc, val) => {
|
||||
acc.push(`${key}.${val}`)
|
||||
return acc
|
||||
}, [] as string[])
|
||||
|
||||
items.forEach((item) => output.add(item))
|
||||
continue
|
||||
}
|
||||
|
||||
output.add(key)
|
||||
}
|
||||
|
||||
return Array.from(output)
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
import { EntityManager } from "typeorm"
|
||||
import { IsolationLevel } from "typeorm/driver/types/IsolationLevel"
|
||||
|
||||
export abstract class TransactionBaseService {
|
||||
protected manager_: EntityManager
|
||||
protected transactionManager_: EntityManager | undefined
|
||||
|
||||
protected get activeManager_(): EntityManager {
|
||||
return this.transactionManager_ ?? this.manager_
|
||||
}
|
||||
|
||||
protected constructor(
|
||||
protected readonly __container__: any,
|
||||
protected readonly __configModule__?: Record<string, unknown>,
|
||||
protected readonly __moduleDeclaration__?: Record<string, unknown>
|
||||
) {
|
||||
this.manager_ = __container__.manager
|
||||
}
|
||||
|
||||
withTransaction(transactionManager?: EntityManager): this {
|
||||
if (!transactionManager) {
|
||||
return this
|
||||
}
|
||||
|
||||
const cloned = new (this.constructor as any)(
|
||||
this.__container__,
|
||||
this.__configModule__,
|
||||
this.__moduleDeclaration__
|
||||
)
|
||||
|
||||
cloned.manager_ = transactionManager
|
||||
cloned.transactionManager_ = transactionManager
|
||||
|
||||
return cloned
|
||||
}
|
||||
|
||||
protected shouldRetryTransaction_(
|
||||
err: { code: string } | Record<string, unknown>
|
||||
): boolean {
|
||||
if (!(err as { code: string })?.code) {
|
||||
return false
|
||||
}
|
||||
const code = (err as { code: string })?.code
|
||||
return code === "40001" || code === "40P01"
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps some work within a transactional block. If the service already has
|
||||
* a transaction manager attached this will be reused, otherwise a new
|
||||
* transaction manager is created.
|
||||
* @param work - the transactional work to be done
|
||||
* @param isolationOrErrorHandler - the isolation level to be used for the work.
|
||||
* @param maybeErrorHandlerOrDontFail Potential error handler
|
||||
* @return the result of the transactional work
|
||||
*/
|
||||
protected async atomicPhase_<TResult, TError>(
|
||||
work: (transactionManager: EntityManager) => Promise<TResult | never>,
|
||||
isolationOrErrorHandler?:
|
||||
| IsolationLevel
|
||||
| ((error: TError) => Promise<never | TResult | void>),
|
||||
maybeErrorHandlerOrDontFail?: (
|
||||
error: TError
|
||||
) => Promise<never | TResult | void>
|
||||
): Promise<never | TResult> {
|
||||
let errorHandler = maybeErrorHandlerOrDontFail
|
||||
let isolation:
|
||||
| IsolationLevel
|
||||
| ((error: TError) => Promise<never | TResult | void>)
|
||||
| undefined
|
||||
| null = isolationOrErrorHandler
|
||||
let dontFail = false
|
||||
if (typeof isolationOrErrorHandler === "function") {
|
||||
isolation = null
|
||||
errorHandler = isolationOrErrorHandler
|
||||
dontFail = !!maybeErrorHandlerOrDontFail
|
||||
}
|
||||
|
||||
if (this.transactionManager_) {
|
||||
const doWork = async (m: EntityManager): Promise<never | TResult> => {
|
||||
this.manager_ = m
|
||||
this.transactionManager_ = m
|
||||
try {
|
||||
return await work(m)
|
||||
} catch (error) {
|
||||
if (errorHandler) {
|
||||
const queryRunner = this.transactionManager_.queryRunner
|
||||
if (queryRunner && queryRunner.isTransactionActive) {
|
||||
await queryRunner.rollbackTransaction()
|
||||
}
|
||||
|
||||
await errorHandler(error)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
return await doWork(this.transactionManager_)
|
||||
} else {
|
||||
const temp = this.manager_
|
||||
const doWork = async (m: EntityManager): Promise<never | TResult> => {
|
||||
this.manager_ = m
|
||||
this.transactionManager_ = m
|
||||
try {
|
||||
const result = await work(m)
|
||||
this.manager_ = temp
|
||||
this.transactionManager_ = undefined
|
||||
return result
|
||||
} catch (error) {
|
||||
this.manager_ = temp
|
||||
this.transactionManager_ = undefined
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
if (isolation && this.manager_) {
|
||||
let result
|
||||
try {
|
||||
result = await this.manager_.transaction(
|
||||
isolation as IsolationLevel,
|
||||
async (m) => doWork(m)
|
||||
)
|
||||
return result
|
||||
} catch (error) {
|
||||
if (this.shouldRetryTransaction_(error)) {
|
||||
return this.manager_.transaction(
|
||||
isolation as IsolationLevel,
|
||||
async (m): Promise<never | TResult> => doWork(m)
|
||||
)
|
||||
} else {
|
||||
if (errorHandler) {
|
||||
await errorHandler(error)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.manager_.transaction(async (m) => doWork(m))
|
||||
} catch (error) {
|
||||
if (errorHandler) {
|
||||
const result = await errorHandler(error)
|
||||
if (dontFail) {
|
||||
return result as TResult
|
||||
}
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user