diff --git a/storage/framework/core/orm/src/utils.ts b/storage/framework/core/orm/src/utils.ts index 7624738d47..c180b4e5f8 100644 --- a/storage/framework/core/orm/src/utils.ts +++ b/storage/framework/core/orm/src/utils.ts @@ -62,7 +62,7 @@ export async function getRelations(model: Model, modelName: string): Promise +interface BaseRelation { + foreignKey?: string + relationName?: string +} + +interface Relation extends BaseRelation { + model: T +} + +interface HasOne extends Array> {} +interface HasMany extends Array> {} +interface BelongsTo extends Array> {} +interface BelongsToMany extends Array {} + +interface HasOneThrough extends Array<{ + model: T + through: T + foreignKey?: string + throughForeignKey?: string + relationName?: string +}> {} + + export interface FieldArrayElement { entity: string charValue?: string | null @@ -42,12 +65,13 @@ interface ActivityLogOption { logOnly: LogAttribute[] } -interface BelongsToManyType { - model: ModelNames - firstForeignKey?: string - secondForeignKey?: string - pivotTable?: string - relationName?: string + +export interface Relations { + hasOne?: HasOne | string[] + hasMany?: HasMany | ModelNames[] + belongsTo?: BelongsTo | ModelNames[] + belongsToMany?: BelongsToMany | ModelNames[] + hasOneThrough?: HasOneThrough } export interface ApiSettings { @@ -118,35 +142,15 @@ export interface ModelOptions extends Base { attributes?: Attributes // relationships - hasOne?: - | { - model: ModelNames - foreignKey?: string - relationName?: string - }[] - | string[] - hasMany?: - | { - model: ModelNames // should be typed as ModelName - foreignKey?: string - relationName?: string - }[] - | ModelNames[] - belongsTo?: - | { - model: ModelNames // should be typed as ModelName - foreignKey?: string - relationName?: string - }[] - | ModelNames[] // belongsTo: 'User' - belongsToMany?: BelongsToManyType[] | ModelNames[] - hasOneThrough?: { - model: ModelNames - through: ModelNames - foreignKey?: string - throughForeignKey?: string - relationName?: string - }[] + hasOne?: HasOne | string[] + + hasMany?: HasMany | ModelNames[] + + belongsTo?: BelongsTo | ModelNames[] + + belongsToMany?: BelongsToMany | ModelNames[] + + hasOneThrough?: HasOneThrough scopes?: { [key: string]: (value: any) => any