feat(medusa): Improve buildQuery as well as refactor the cart service as an example

This commit is contained in:
adrien2p
2022-04-21 13:48:16 +02:00
parent e7e715ac17
commit b90291b18d
6 changed files with 86 additions and 69 deletions
+16 -3
View File
@@ -1,7 +1,20 @@
import { FindConfig, Writable } from "../types/common"
import {
DateComparisonOperator,
FindConfig,
NumericalComparisonOperator,
StringComparisonOperator,
Writable,
} from "../types/common"
import { FindOperator, In, Raw } from "typeorm"
type Selector<TEntity> = { [key in keyof TEntity]?: unknown }
type Selector<TEntity> = {
[key in keyof TEntity]?: TEntity[key]
| TEntity[key][]
| DateComparisonOperator
| StringComparisonOperator
| NumericalComparisonOperator
}
/**
* Used to build TypeORM queries.
* @param selector The selector
@@ -16,7 +29,7 @@ export function buildQuery<TEntity = unknown>(
withDeleted?: boolean
} {
const build = (
obj: Record<string, unknown>
obj: Selector<TEntity>
): Partial<Writable<TEntity>> => {
return Object.entries(obj).reduce((acc, [key, value]: any) => {
// Undefined values indicate that they have no significance to the query.
+3
View File
@@ -0,0 +1,3 @@
export * from './build-query'
export * from './set-metadata'
export * from './validate-id'
+1 -1
View File
@@ -6,7 +6,7 @@ import { MedusaError } from "medusa-core-utils/dist"
* @param metadata - the metadata to set
* @return resolves to the updated result.
*/
export function setMetadata_(
export function setMetadata(
obj: { metadata: Record<string, unknown> },
metadata: Record<string, unknown>
): Record<string, unknown> {
+1 -1
View File
@@ -8,7 +8,7 @@
*/
import { MedusaError } from "medusa-core-utils/dist"
export function validateId_(
export function validateId(
rawId: string,
config: { prefix?: string; length?: number } = {}
): string {