**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>
174 lines
4.8 KiB
TypeScript
174 lines
4.8 KiB
TypeScript
import {
|
|
MedusaError,
|
|
RuleOperator,
|
|
isObject,
|
|
isString,
|
|
pickValueFromObject,
|
|
} from "@medusajs/framework/utils"
|
|
|
|
/**
|
|
* The rule engine here is kept inside the module as of now, but it could be moved
|
|
* to the utils package and be used across the different modules that provides context
|
|
* based rule filtering.
|
|
*
|
|
* TODO: discussion around that should happen at some point
|
|
*/
|
|
|
|
export type Rule = {
|
|
attribute: string
|
|
operator: Lowercase<keyof typeof RuleOperator> | (string & {})
|
|
value: string | string[] | null
|
|
}
|
|
|
|
export const availableOperators = Object.values(RuleOperator)
|
|
|
|
const isDate = (str: string) => {
|
|
return !isNaN(Date.parse(str))
|
|
}
|
|
|
|
const operatorsPredicate = {
|
|
in: (contextValue: string, ruleValue: string[]) =>
|
|
ruleValue.includes(contextValue),
|
|
nin: (contextValue: string, ruleValue: string[]) =>
|
|
!ruleValue.includes(contextValue),
|
|
eq: (contextValue: string, ruleValue: string) => contextValue === ruleValue,
|
|
ne: (contextValue: string, ruleValue: string) => contextValue !== ruleValue,
|
|
gt: (contextValue: string, ruleValue: string) => {
|
|
if (isDate(contextValue) && isDate(ruleValue)) {
|
|
return new Date(contextValue) > new Date(ruleValue)
|
|
}
|
|
return Number(contextValue) > Number(ruleValue)
|
|
},
|
|
gte: (contextValue: string, ruleValue: string) => {
|
|
if (isDate(contextValue) && isDate(ruleValue)) {
|
|
return new Date(contextValue) >= new Date(ruleValue)
|
|
}
|
|
return Number(contextValue) >= Number(ruleValue)
|
|
},
|
|
lt: (contextValue: string, ruleValue: string) => {
|
|
if (isDate(contextValue) && isDate(ruleValue)) {
|
|
return new Date(contextValue) < new Date(ruleValue)
|
|
}
|
|
return Number(contextValue) < Number(ruleValue)
|
|
},
|
|
lte: (contextValue: string, ruleValue: string) => {
|
|
if (isDate(contextValue) && isDate(ruleValue)) {
|
|
return new Date(contextValue) <= new Date(ruleValue)
|
|
}
|
|
return Number(contextValue) <= Number(ruleValue)
|
|
},
|
|
}
|
|
|
|
/**
|
|
* Validate contextValue context object from contextValue set of rules.
|
|
* By default, all rules must be valid to return true unless the option atLeastOneValidRule is set to true.
|
|
* @param context
|
|
* @param rules
|
|
* @param options
|
|
*/
|
|
export function isContextValid(
|
|
context: Record<string, any>,
|
|
rules: Rule[],
|
|
options: {
|
|
someAreValid: boolean
|
|
} = {
|
|
someAreValid: false,
|
|
}
|
|
): boolean {
|
|
const { someAreValid } = options
|
|
|
|
const loopComparator = someAreValid ? rules.some : rules.every
|
|
const predicate = (rule) => {
|
|
const { attribute, operator, value } = rule
|
|
const contextValue = pickValueFromObject(attribute, context)
|
|
|
|
return operatorsPredicate[operator](
|
|
`${contextValue}`,
|
|
value as string & string[]
|
|
)
|
|
}
|
|
|
|
return loopComparator.apply(rules, [predicate])
|
|
}
|
|
|
|
/**
|
|
* Validate contextValue rule object
|
|
* @param rule
|
|
*/
|
|
export function validateRule(rule: Record<string, unknown>): boolean {
|
|
if (!rule.attribute || !rule.operator || !rule.value) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
"Rule must have an attribute, an operator and a value"
|
|
)
|
|
}
|
|
|
|
if (!isString(rule.attribute)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
"Rule attribute must be a string"
|
|
)
|
|
}
|
|
|
|
if (!isString(rule.operator)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
"Rule operator must be a string"
|
|
)
|
|
}
|
|
|
|
if (!availableOperators.includes(rule.operator as RuleOperator)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
`Rule operator ${
|
|
rule.operator
|
|
} is not supported. Must be one of ${availableOperators.join(", ")}`
|
|
)
|
|
}
|
|
|
|
if (rule.operator === RuleOperator.IN || rule.operator === RuleOperator.NIN) {
|
|
if (!Array.isArray(rule.value)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
"Rule value must be an array for in/nin operators"
|
|
)
|
|
}
|
|
} else {
|
|
if (Array.isArray(rule.value) || isObject(rule.value)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
`Rule value must be a string, bool, number value for the selected operator ${rule.operator}`
|
|
)
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
export function normalizeRulesValue<T extends Partial<Rule>>(rules: T[]): void {
|
|
rules.forEach((rule) => {
|
|
/**
|
|
* If a string is provided, then we don't want jsonb to convert to the primitive value based on the RFC
|
|
*/
|
|
if (rule.value === "true" || rule.value === "false") {
|
|
rule.value = rule.value === "true" ? '"true"' : '"false"'
|
|
}
|
|
|
|
return rule
|
|
})
|
|
}
|
|
|
|
export function validateAndNormalizeRules<T extends Partial<Rule>>(rules: T[]) {
|
|
rules.forEach(validateRule)
|
|
normalizeRulesValue(rules)
|
|
}
|
|
|
|
/**
|
|
* Validate contextValue set of rules
|
|
* @param rules
|
|
*/
|
|
export function validateRules(rules: Record<string, unknown>[]): boolean {
|
|
rules.forEach(validateRule)
|
|
return true
|
|
}
|