**What**
- Allow to provide `foreignKeyName` option for hasOne and belongsTo relationships
- `model.hasOne(() => OtherEntity, { foreignKey: true, foreignKeyName: 'other_entity_something_id' })`
- The above will also output a generated type that takes into consideration the custom fk name 🔽
- Update types to account for defined custom foreign key name
- Fix joiner config linkable generation to account for custom linkable keys that provide a public API for their model but are not part of the list of the models included in the MedusaService
- This was supposed to be handled correctly but the implementation was not considering that custom linkable keys could reference models not part of the one provided to medusa service
- Migrate fulfillment module to DML
- Fix has one with fk behaviour and hooks (the relation should be assigned but not the fk)
- Fix has one belongsTo hooks (the relation should be assigned but not the fk)
- Fix hasOneWithFk and belongsTo non persisted fk to be selectable
- Allow to define `belongsTo` without other side definition for `ManyToOne` with no counter part defined
- Meaning that if a user defined `belongsTo` on one side andnot mapped by and no counter part on the other entity it will be considered as a `ManyToOne`
- `orphanRemoval` on `OneToOne` have been removed, this means that when assigning a new object relation to an entity, the previous one gets deconected but not deleted automatically. This prevent removing data un volountarely
**NOTE**
As per our convention here are some information to keep in mind
**HasOne <> BelongsTo**
Define `OneToOne`, The foreign key is owned by the belongs to and the relation needs to be provided to cascade if wanted
**HasMany <> BelongsTo**
Define `OneToMane` <> `ManyToOne`, the foreign key is owned by the many to one and for those relation no cascade will be performed, the foreign key must be provided. For the `HasMany` the cascade is available
**HasOne (with FK)**
Will act similarly to belongs to with **HasOne <> BelongsTo**
Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import {
|
|
BelongsTo,
|
|
DmlEntity,
|
|
DMLEntitySchemaBuilder,
|
|
HasMany,
|
|
IdProperty,
|
|
JSONProperty,
|
|
model,
|
|
NullableModifier,
|
|
PrimaryKeyModifier,
|
|
TextProperty,
|
|
} from "@medusajs/framework/utils"
|
|
|
|
import { FulfillmentSet } from "./fulfillment-set"
|
|
import { GeoZone } from "./geo-zone"
|
|
import { ShippingOption } from "./shipping-option"
|
|
|
|
export type ServiceZoneSchema = {
|
|
id: PrimaryKeyModifier<string, IdProperty>
|
|
name: TextProperty
|
|
fulfillment_set: BelongsTo<() => typeof FulfillmentSet>
|
|
geo_zones: HasMany<() => typeof GeoZone>
|
|
shipping_options: HasMany<() => typeof ShippingOption>
|
|
metadata: NullableModifier<Record<string, unknown>, JSONProperty>
|
|
}
|
|
|
|
export const ServiceZone = model
|
|
.define("service_zone", {
|
|
id: model.id({ prefix: "serzo" }).primaryKey(),
|
|
name: model.text(),
|
|
fulfillment_set: model.belongsTo<() => typeof FulfillmentSet>(
|
|
() => FulfillmentSet,
|
|
{
|
|
mappedBy: "service_zones",
|
|
}
|
|
),
|
|
geo_zones: model.hasMany<() => typeof GeoZone>(() => GeoZone, {
|
|
mappedBy: "service_zone",
|
|
}),
|
|
shipping_options: model.hasMany<() => typeof ShippingOption>(
|
|
() => ShippingOption,
|
|
{
|
|
mappedBy: "service_zone",
|
|
}
|
|
),
|
|
metadata: model.json().nullable(),
|
|
})
|
|
.indexes([
|
|
{
|
|
on: ["name"],
|
|
unique: true,
|
|
where: "deleted_at IS NULL",
|
|
},
|
|
])
|
|
.cascades({
|
|
delete: ["geo_zones", "shipping_options"],
|
|
}) as unknown as DmlEntity<
|
|
DMLEntitySchemaBuilder<ServiceZoneSchema>,
|
|
"ServiceZone"
|
|
>
|