From 789d545b9e0c3f0aefe650dc7136118c0dc5d977 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 27 Jun 2024 20:38:06 +0200 Subject: [PATCH] feat(utils,types): infer keys in query builder from schema (#7861) --- packages/core/types/src/dml/index.ts | 6 ++++-- packages/core/utils/src/dml/entity.ts | 4 ++-- .../src/dml/helpers/entity-builder/build-indexes.ts | 9 ++++++--- .../src/dml/helpers/entity-builder/query-builder.ts | 8 +++++++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/core/types/src/dml/index.ts b/packages/core/types/src/dml/index.ts index cbf256ab10..66bfc24652 100644 --- a/packages/core/types/src/dml/index.ts +++ b/packages/core/types/src/dml/index.ts @@ -196,6 +196,8 @@ export type SimpleQueryValue = string | number | boolean | null export type NeQueryValue = { $ne: SimpleQueryValue } export type QueryValue = SimpleQueryValue | NeQueryValue -export interface QueryCondition { - [key: string]: QueryValue | QueryCondition | QueryCondition[] +export type QueryCondition = { + [K in keyof IDmlEntity["schema"]]?: T[K] extends object + ? QueryValue + : QueryCondition } diff --git a/packages/core/utils/src/dml/entity.ts b/packages/core/utils/src/dml/entity.ts index 51a80d9066..8296fdc3b3 100644 --- a/packages/core/utils/src/dml/entity.ts +++ b/packages/core/utils/src/dml/entity.ts @@ -125,9 +125,9 @@ export class DmlEntity implements IDmlEntity { /** * Adds indexes to be created at during model creation on the DML entity. */ - indexes(indexes: EntityIndex[]) { + indexes(indexes: EntityIndex>[]) { for (const index of indexes) { - index.where = transformIndexWhere(index) + index.where = transformIndexWhere(index) index.unique ??= false } diff --git a/packages/core/utils/src/dml/helpers/entity-builder/build-indexes.ts b/packages/core/utils/src/dml/helpers/entity-builder/build-indexes.ts index aecac73774..9326da5620 100644 --- a/packages/core/utils/src/dml/helpers/entity-builder/build-indexes.ts +++ b/packages/core/utils/src/dml/helpers/entity-builder/build-indexes.ts @@ -1,5 +1,6 @@ import { DMLSchema, EntityIndex, QueryCondition } from "@medusajs/types" import { isObject, isPresent } from "../../../common" +import { DateTimeProperty } from "../../properties/date-time" import { buildWhereQuery } from "./query-builder" /* @@ -9,14 +10,16 @@ import { buildWhereQuery } from "./query-builder" this will need to be updated to include that case. */ export function transformIndexWhere( - index: EntityIndex + index: EntityIndex> ): string { return isObject(index.where) - ? transformWhereQb(index.where) + ? transformWhereQb(index.where) : transformWhere(index.where) } -function transformWhereQb(where: QueryCondition): string { +function transformWhereQb( + where: QueryCondition +): string { if (!isPresent(where.deleted_at)) { where.deleted_at = null } diff --git a/packages/core/utils/src/dml/helpers/entity-builder/query-builder.ts b/packages/core/utils/src/dml/helpers/entity-builder/query-builder.ts index 5cfb874862..0b12ddea73 100644 --- a/packages/core/utils/src/dml/helpers/entity-builder/query-builder.ts +++ b/packages/core/utils/src/dml/helpers/entity-builder/query-builder.ts @@ -17,10 +17,16 @@ export function buildWhereQuery(query: QueryCondition): string { const conditions: string[] = [] for (const [key, value] of Object.entries(query)) { + if (!isDefined(value)) { + throw new Error( + `value cannot be undefined when building where query for an index` + ) + } + if (isQueryCondition(value)) { conditions.push(buildWhereQuery(value)) } else { - conditions.push(buildCondition(key, value as QueryValue)) + conditions.push(buildCondition(key, value)) } }