fix: relationships to accept ids (#11399)
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/types": patch
|
||||||
|
"@medusajs/utils": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: relationships to accept ids
|
||||||
@@ -213,6 +213,27 @@ export type InferSchemaFields<Schema extends DMLSchema> = Prettify<
|
|||||||
} & InferForeignKeys<Schema>
|
} & InferForeignKeys<Schema>
|
||||||
>
|
>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Infers the types of the schema fields from the DML entity
|
||||||
|
* for module services
|
||||||
|
*/
|
||||||
|
export type InferSchemaFieldsForModuleServices<Schema extends DMLSchema> =
|
||||||
|
Prettify<
|
||||||
|
{
|
||||||
|
[K in keyof Schema]: Schema[K] extends RelationshipType<any>
|
||||||
|
? Schema[K]["type"] extends "belongsTo"
|
||||||
|
? string
|
||||||
|
: Schema[K]["type"] extends "hasOne" | "hasOneWithFK"
|
||||||
|
? string
|
||||||
|
: Schema[K]["type"] extends "hasMany"
|
||||||
|
? string[]
|
||||||
|
: Schema[K]["type"] extends "manyToMany"
|
||||||
|
? string[]
|
||||||
|
: never
|
||||||
|
: Schema[K]["$dataType"]
|
||||||
|
} & InferForeignKeys<Schema>
|
||||||
|
>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Infers the schema properties without the relationships
|
* Infers the schema properties without the relationships
|
||||||
*/
|
*/
|
||||||
@@ -247,6 +268,13 @@ export type Infer<T> = T extends IDmlEntity<infer Schema, any>
|
|||||||
? EntityConstructor<InferSchemaFields<Schema>>
|
? EntityConstructor<InferSchemaFields<Schema>>
|
||||||
: never
|
: never
|
||||||
|
|
||||||
|
export type InferEntityForModuleService<T> = T extends IDmlEntity<
|
||||||
|
infer Schema,
|
||||||
|
any
|
||||||
|
>
|
||||||
|
? InferSchemaFieldsForModuleServices<Schema>
|
||||||
|
: never
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The actions to cascade from a given entity to its
|
* The actions to cascade from a given entity to its
|
||||||
* relationship.
|
* relationship.
|
||||||
|
|||||||
@@ -6,9 +6,28 @@ import { InferTypeOf } from "@medusajs/types"
|
|||||||
const Blog = model.define("Blog", {
|
const Blog = model.define("Blog", {
|
||||||
id: model.text(),
|
id: model.text(),
|
||||||
title: model.text(),
|
title: model.text(),
|
||||||
|
tags: model.manyToMany(() => Tag),
|
||||||
|
comments: model.hasMany(() => Comment),
|
||||||
description: model.text().nullable(),
|
description: model.text().nullable(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const Tag = model.define("Tag", {
|
||||||
|
id: model.text(),
|
||||||
|
title: model.text(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const Comment = model.define("Comment", {
|
||||||
|
id: model.text(),
|
||||||
|
post: model.belongsTo(() => Blog),
|
||||||
|
author: model.belongsTo(() => User),
|
||||||
|
description: model.text().nullable(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const User = model.define("User", {
|
||||||
|
id: model.text(),
|
||||||
|
username: model.text(),
|
||||||
|
})
|
||||||
|
|
||||||
type BlogDTO = {
|
type BlogDTO = {
|
||||||
id: number
|
id: number
|
||||||
title: string
|
title: string
|
||||||
@@ -55,7 +74,7 @@ const containerMock = {
|
|||||||
describe("Medusa Service typings", () => {
|
describe("Medusa Service typings", () => {
|
||||||
describe("create<Service>", () => {
|
describe("create<Service>", () => {
|
||||||
test("type-hint model properties", () => {
|
test("type-hint model properties", () => {
|
||||||
class BlogService extends MedusaService({ Blog }) {}
|
class BlogService extends MedusaService({ Blog, Comment }) {}
|
||||||
const blogService = new BlogService(containerMock)
|
const blogService = new BlogService(containerMock)
|
||||||
|
|
||||||
expectTypeOf(blogService.createBlogs).parameters.toEqualTypeOf<
|
expectTypeOf(blogService.createBlogs).parameters.toEqualTypeOf<
|
||||||
@@ -63,6 +82,8 @@ describe("Medusa Service typings", () => {
|
|||||||
Partial<{
|
Partial<{
|
||||||
id: string | undefined
|
id: string | undefined
|
||||||
title: string | undefined
|
title: string | undefined
|
||||||
|
comments: string[] | undefined
|
||||||
|
tags: string[] | undefined
|
||||||
description: string | null | undefined
|
description: string | null | undefined
|
||||||
}>,
|
}>,
|
||||||
...rest: any[]
|
...rest: any[]
|
||||||
@@ -71,6 +92,36 @@ describe("Medusa Service typings", () => {
|
|||||||
Partial<{
|
Partial<{
|
||||||
id: string | undefined
|
id: string | undefined
|
||||||
title: string | undefined
|
title: string | undefined
|
||||||
|
comments: string[] | undefined
|
||||||
|
tags: string[] | undefined
|
||||||
|
description: string | null | undefined
|
||||||
|
}>[],
|
||||||
|
...rest: any[]
|
||||||
|
]
|
||||||
|
>()
|
||||||
|
expectTypeOf(blogService.createBlogs).returns.toEqualTypeOf<
|
||||||
|
Promise<InferTypeOf<typeof Blog>> | Promise<InferTypeOf<typeof Blog>[]>
|
||||||
|
>()
|
||||||
|
|
||||||
|
expectTypeOf(blogService.createComments).parameters.toEqualTypeOf<
|
||||||
|
| [
|
||||||
|
Partial<{
|
||||||
|
id: string | undefined
|
||||||
|
post: string | undefined
|
||||||
|
author: string | undefined
|
||||||
|
post_id: string | undefined
|
||||||
|
author_id: string | undefined
|
||||||
|
description: string | null | undefined
|
||||||
|
}>,
|
||||||
|
...rest: any[]
|
||||||
|
]
|
||||||
|
| [
|
||||||
|
Partial<{
|
||||||
|
id: string | undefined
|
||||||
|
post: string | undefined
|
||||||
|
author: string | undefined
|
||||||
|
post_id: string | undefined
|
||||||
|
author_id: string | undefined
|
||||||
description: string | null | undefined
|
description: string | null | undefined
|
||||||
}>[],
|
}>[],
|
||||||
...rest: any[]
|
...rest: any[]
|
||||||
@@ -143,6 +194,8 @@ describe("Medusa Service typings", () => {
|
|||||||
Partial<{
|
Partial<{
|
||||||
id: string | undefined
|
id: string | undefined
|
||||||
title: string | undefined
|
title: string | undefined
|
||||||
|
comments: string[] | undefined
|
||||||
|
tags: string[] | undefined
|
||||||
description: string | null | undefined
|
description: string | null | undefined
|
||||||
}>,
|
}>,
|
||||||
...rest: any[]
|
...rest: any[]
|
||||||
@@ -152,6 +205,8 @@ describe("Medusa Service typings", () => {
|
|||||||
| Partial<{
|
| Partial<{
|
||||||
id: string | undefined
|
id: string | undefined
|
||||||
title: string | undefined
|
title: string | undefined
|
||||||
|
comments: string[] | undefined
|
||||||
|
tags: string[] | undefined
|
||||||
description: string | null | undefined
|
description: string | null | undefined
|
||||||
}>[]
|
}>[]
|
||||||
| {
|
| {
|
||||||
@@ -160,11 +215,15 @@ describe("Medusa Service typings", () => {
|
|||||||
| Partial<{
|
| Partial<{
|
||||||
id: string | undefined
|
id: string | undefined
|
||||||
title: string | undefined
|
title: string | undefined
|
||||||
|
comments: string[] | undefined
|
||||||
|
tags: string[] | undefined
|
||||||
description: string | null | undefined
|
description: string | null | undefined
|
||||||
}>
|
}>
|
||||||
| Partial<{
|
| Partial<{
|
||||||
id: string | undefined
|
id: string | undefined
|
||||||
title: string | undefined
|
title: string | undefined
|
||||||
|
comments: string[] | undefined
|
||||||
|
tags: string[] | undefined
|
||||||
description: string | null | undefined
|
description: string | null | undefined
|
||||||
}>[]
|
}>[]
|
||||||
}
|
}
|
||||||
@@ -174,11 +233,15 @@ describe("Medusa Service typings", () => {
|
|||||||
| Partial<{
|
| Partial<{
|
||||||
id: string | undefined
|
id: string | undefined
|
||||||
title: string | undefined
|
title: string | undefined
|
||||||
|
comments: string[] | undefined
|
||||||
|
tags: string[] | undefined
|
||||||
description: string | null | undefined
|
description: string | null | undefined
|
||||||
}>
|
}>
|
||||||
| Partial<{
|
| Partial<{
|
||||||
id: string | undefined
|
id: string | undefined
|
||||||
title: string | undefined
|
title: string | undefined
|
||||||
|
comments: string[] | undefined
|
||||||
|
tags: string[] | undefined
|
||||||
description: string | null | undefined
|
description: string | null | undefined
|
||||||
}>[]
|
}>[]
|
||||||
}[]
|
}[]
|
||||||
|
|||||||
@@ -3,11 +3,12 @@ import {
|
|||||||
Context,
|
Context,
|
||||||
FindConfig,
|
FindConfig,
|
||||||
IDmlEntity,
|
IDmlEntity,
|
||||||
InferEntityType,
|
|
||||||
Pluralize,
|
Pluralize,
|
||||||
Prettify,
|
Prettify,
|
||||||
RestoreReturn,
|
RestoreReturn,
|
||||||
SoftDeleteReturn,
|
SoftDeleteReturn,
|
||||||
|
InferEntityType,
|
||||||
|
InferEntityForModuleService,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { DmlEntity } from "../../dml"
|
import { DmlEntity } from "../../dml"
|
||||||
|
|
||||||
@@ -45,7 +46,7 @@ export type ModelConfigurationsToConfigTemplate<T extends ModelEntries> = {
|
|||||||
? InstanceType<T[Key]>
|
? InstanceType<T[Key]>
|
||||||
: any
|
: any
|
||||||
inputDto: T[Key] extends DmlEntity<any, any>
|
inputDto: T[Key] extends DmlEntity<any, any>
|
||||||
? Omit<InferEntityType<T[Key]>, DMLDTOExcludeProperties>
|
? Omit<InferEntityForModuleService<T[Key]>, DMLDTOExcludeProperties>
|
||||||
: T[Key] extends Constructor<any>
|
: T[Key] extends Constructor<any>
|
||||||
? InstanceType<T[Key]>
|
? InstanceType<T[Key]>
|
||||||
: any
|
: any
|
||||||
|
|||||||
Reference in New Issue
Block a user