Skip to content

Commit

Permalink
feat: add support for defining hasOne with FK (medusajs#10441)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored and hirotaka committed Dec 7, 2024
1 parent b1f6c79 commit 1a4ccfe
Show file tree
Hide file tree
Showing 11 changed files with 826 additions and 15 deletions.
9 changes: 5 additions & 4 deletions packages/core/types/src/dml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type KnownDataTypes =
*/
export type RelationshipTypes =
| "hasOne"
| "hasOneWithFK"
| "hasMany"
| "belongsTo"
| "manyToMany"
Expand Down Expand Up @@ -111,7 +112,7 @@ export type RelationshipMetadata = {
name: string
type: RelationshipTypes
entity: unknown
nullable: boolean
nullable?: boolean
mappedBy?: string
searchable: boolean
options: Record<string, any>
Expand Down Expand Up @@ -142,9 +143,9 @@ export interface EntityConstructor<Props> extends Function {
* "belongsTo" relation meaning "hasOne" and "ManyToOne"
*/
export type InferForeignKeys<Schema extends DMLSchema> = {
[K in keyof Schema as Schema[K] extends { type: "belongsTo" }
[K in keyof Schema as Schema[K] extends { $foreignKey: true }
? `${K & string}_id`
: never]: Schema[K] extends { type: "belongsTo" }
: never]: Schema[K] extends { $foreignKey: true }
? null extends Schema[K]["$dataType"]
? string | null
: string
Expand Down Expand Up @@ -191,7 +192,7 @@ export type InferSchemaFields<Schema extends DMLSchema> = Prettify<
[K in keyof Schema]: Schema[K] extends RelationshipType<any>
? Schema[K]["type"] extends "belongsTo"
? InferBelongsToFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasOne"
: Schema[K]["type"] extends "hasOne" | "hasOneWithFK"
? InferHasOneFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasMany"
? InferHasManyFields<Schema[K]["$dataType"]>
Expand Down
Loading

0 comments on commit 1a4ccfe

Please sign in to comment.