feat(utils,types): infer keys in query builder from schema (#7861)
This commit is contained in:
@@ -196,6 +196,8 @@ export type SimpleQueryValue = string | number | boolean | null
|
|||||||
export type NeQueryValue = { $ne: SimpleQueryValue }
|
export type NeQueryValue = { $ne: SimpleQueryValue }
|
||||||
export type QueryValue = SimpleQueryValue | NeQueryValue
|
export type QueryValue = SimpleQueryValue | NeQueryValue
|
||||||
|
|
||||||
export interface QueryCondition {
|
export type QueryCondition<T extends DMLSchema = DMLSchema> = {
|
||||||
[key: string]: QueryValue | QueryCondition | QueryCondition[]
|
[K in keyof IDmlEntity<T>["schema"]]?: T[K] extends object
|
||||||
|
? QueryValue
|
||||||
|
: QueryCondition<T>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,9 +125,9 @@ export class DmlEntity<Schema extends DMLSchema> implements IDmlEntity<Schema> {
|
|||||||
/**
|
/**
|
||||||
* Adds indexes to be created at during model creation on the DML entity.
|
* Adds indexes to be created at during model creation on the DML entity.
|
||||||
*/
|
*/
|
||||||
indexes(indexes: EntityIndex<Schema, string | QueryCondition>[]) {
|
indexes(indexes: EntityIndex<Schema, string | QueryCondition<Schema>>[]) {
|
||||||
for (const index of indexes) {
|
for (const index of indexes) {
|
||||||
index.where = transformIndexWhere(index)
|
index.where = transformIndexWhere<Schema>(index)
|
||||||
index.unique ??= false
|
index.unique ??= false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { DMLSchema, EntityIndex, QueryCondition } from "@medusajs/types"
|
import { DMLSchema, EntityIndex, QueryCondition } from "@medusajs/types"
|
||||||
import { isObject, isPresent } from "../../../common"
|
import { isObject, isPresent } from "../../../common"
|
||||||
|
import { DateTimeProperty } from "../../properties/date-time"
|
||||||
import { buildWhereQuery } from "./query-builder"
|
import { buildWhereQuery } from "./query-builder"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -9,14 +10,16 @@ import { buildWhereQuery } from "./query-builder"
|
|||||||
this will need to be updated to include that case.
|
this will need to be updated to include that case.
|
||||||
*/
|
*/
|
||||||
export function transformIndexWhere<TSchema extends DMLSchema>(
|
export function transformIndexWhere<TSchema extends DMLSchema>(
|
||||||
index: EntityIndex<TSchema, string | QueryCondition>
|
index: EntityIndex<TSchema, string | QueryCondition<TSchema>>
|
||||||
): string {
|
): string {
|
||||||
return isObject(index.where)
|
return isObject(index.where)
|
||||||
? transformWhereQb(index.where)
|
? transformWhereQb<TSchema>(index.where)
|
||||||
: transformWhere(index.where)
|
: transformWhere(index.where)
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformWhereQb(where: QueryCondition): string {
|
function transformWhereQb<TSchema extends DMLSchema>(
|
||||||
|
where: QueryCondition<TSchema & { deleted_at: DateTimeProperty }>
|
||||||
|
): string {
|
||||||
if (!isPresent(where.deleted_at)) {
|
if (!isPresent(where.deleted_at)) {
|
||||||
where.deleted_at = null
|
where.deleted_at = null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,16 @@ export function buildWhereQuery(query: QueryCondition): string {
|
|||||||
const conditions: string[] = []
|
const conditions: string[] = []
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(query)) {
|
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)) {
|
if (isQueryCondition(value)) {
|
||||||
conditions.push(buildWhereQuery(value))
|
conditions.push(buildWhereQuery(value))
|
||||||
} else {
|
} else {
|
||||||
conditions.push(buildCondition(key, value as QueryValue))
|
conditions.push(buildCondition(key, value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user