Skip to content

Commit

Permalink
fix(utils): DML one to one definition (#10415)
Browse files Browse the repository at this point in the history
RESOLVES FRMW-2819

**What**
Fix one to one definition.
  • Loading branch information
adrien2p authored Dec 5, 2024
1 parent 70d77ea commit b160fd3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-turkeys-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---

fix(utils): DML one to one definition
27 changes: 21 additions & 6 deletions packages/core/utils/src/dml/__tests__/entity-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2690,6 +2690,7 @@ describe("Entity builder", () => {
nullable: false,
mappedBy: "user",
cascade: ["persist", "soft-remove"],
onDelete: "cascade",
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -2853,6 +2854,7 @@ describe("Entity builder", () => {
nullable: false,
mappedBy: "user",
cascade: ["persist", "soft-remove"],
onDelete: "cascade",
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -2918,22 +2920,24 @@ describe("Entity builder", () => {
},
user: {
entity: "User",
fieldName: "user_id",
mappedBy: "email",
name: "user",
nullable: false,
onDelete: "cascade",
owner: true,
reference: "1:1",
cascade: ["persist", "soft-remove"],
},
user_id: {
columnType: "text",
getter: false,
persist: false,
name: "user_id",
nullable: false,
reference: "scalar",
setter: false,
type: "string",
persist: false,
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -3612,6 +3616,8 @@ describe("Entity builder", () => {
onDelete: "cascade",
owner: true,
reference: "1:1",
cascade: ["persist", "soft-remove"],
fieldName: "user_id",
},
user_id: {
columnType: "text",
Expand Down Expand Up @@ -4551,21 +4557,23 @@ describe("Entity builder", () => {
},
user: {
name: "user",
fieldName: "user_id",
reference: "1:1",
entity: "User",
nullable: false,
mappedBy: "email",
onDelete: undefined,
owner: true,
},
user_id: {
reference: "scalar",
persist: false,
type: "string",
columnType: "text",
nullable: false,
name: "user_id",
getter: false,
setter: false,
persist: false,
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -4747,21 +4755,23 @@ describe("Entity builder", () => {
},
user: {
name: "user",
fieldName: "user_id",
reference: "1:1",
entity: "User",
nullable: true,
onDelete: undefined,
mappedBy: "email",
owner: true,
},
user_id: {
reference: "scalar",
persist: false,
type: "string",
columnType: "text",
nullable: true,
name: "user_id",
getter: false,
setter: false,
persist: false,
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -5379,21 +5389,23 @@ describe("Entity builder", () => {
},
user: {
name: "user",
fieldName: "user_id",
reference: "1:1",
entity: "User",
nullable: false,
mappedBy: "email",
onDelete: undefined,
owner: true,
},
user_id: {
reference: "scalar",
type: "string",
persist: false,
columnType: "text",
nullable: false,
name: "user_id",
getter: false,
setter: false,
persist: false,
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -5577,21 +5589,23 @@ describe("Entity builder", () => {
},
user: {
name: "user",
fieldName: "user_id",
reference: "1:1",
entity: "User",
nullable: false,
mappedBy: "email",
onDelete: undefined,
owner: true,
},
user_id: {
reference: "scalar",
persist: false,
type: "string",
columnType: "text",
nullable: false,
name: "user_id",
getter: false,
setter: false,
persist: false,
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -5796,6 +5810,7 @@ describe("Entity builder", () => {
},
parent: {
name: "parent",
fieldName: "parent_id",
mappedBy: "child",
reference: "1:1",
entity: "User",
Expand All @@ -5807,8 +5822,8 @@ describe("Entity builder", () => {
name: "parent_id",
type: "string",
columnType: "text",
persist: false,
reference: "scalar",
persist: false,
getter: false,
setter: false,
nullable: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@medusajs/types"
import {
BeforeCreate,
Cascade,
ManyToMany,
ManyToOne,
OneToMany,
Expand Down Expand Up @@ -147,14 +148,18 @@ export function defineHasOneRelationship(
mappedBy = relationship.mappedBy
}

OneToOne({
const oneToOneOptions = {
entity: relatedModelName,
nullable: relationship.nullable,
...(mappedBy ? { mappedBy } : {}),
cascade: shouldRemoveRelated
? (["persist", "soft-remove"] as any)
: undefined,
} as OneToOneOptions<any, any>)(MikroORMEntity.prototype, relationship.name)
onDelete: shouldRemoveRelated ? "cascade" : undefined,
} as OneToOneOptions<any, any>

if (shouldRemoveRelated) {
oneToOneOptions.cascade = ["persist", "soft-remove"] as any
}

OneToOne(oneToOneOptions)(MikroORMEntity.prototype, relationship.name)
}

/**
Expand Down Expand Up @@ -368,14 +373,6 @@ export function defineBelongsToRelationship(
) {
const foreignKeyName = camelToSnakeCase(`${relationship.name}Id`)

OneToOne({
entity: relatedModelName,
nullable: relationship.nullable,
mappedBy: mappedBy,
owner: true,
onDelete: shouldCascade ? "cascade" : undefined,
})(MikroORMEntity.prototype, relationship.name)

Object.defineProperty(MikroORMEntity.prototype, foreignKeyName, {
value: null,
configurable: true,
Expand All @@ -384,12 +381,27 @@ export function defineBelongsToRelationship(
})

Property({
type: "string",
columnType: "text",
type: "string",
nullable: relationship.nullable,
persist: false,
})(MikroORMEntity.prototype, foreignKeyName)

const oneToOneOptions: Parameters<typeof OneToOne>[0] = {
entity: relatedModelName,
nullable: relationship.nullable,
mappedBy: mappedBy,
fieldName: foreignKeyName,
owner: true,
onDelete: shouldCascade ? "cascade" : undefined,
}

if (shouldCascade) {
oneToOneOptions.cascade = [Cascade.PERSIST, "soft-remove"] as any
}

OneToOne(oneToOneOptions)(MikroORMEntity.prototype, relationship.name)

const { tableName } = parseEntityName(entity)
applyEntityIndexes(MikroORMEntity, tableName, [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("hasOne - belongTo", () => {
const team = model.define("team", {
id: model.id().primaryKey(),
name: model.text(),
user: model.belongsTo(() => user, { mappedBy: "team" }),
user: model.belongsTo(() => user, { mappedBy: "team" }).nullable(),
})

const user = model.define("user", {
Expand Down

0 comments on commit b160fd3

Please sign in to comment.