Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmichael123 committed Jan 9, 2025
1 parent 9d590b7 commit b2f742c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion storage/framework/core/orm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function getRelations(model: Model, modelName: string): Promise<Rel

for (const relation of relationsArray) {
if (hasRelations(model, relation)) {
for (const relationInstance of (model[relation as keyof Model] as any[]) || []) {
for (const relationInstance of model[relation]) {
let relationModel = relationInstance.model
let modelRelation: Model
if (isString(relationInstance)) {
Expand Down
74 changes: 39 additions & 35 deletions storage/framework/core/types/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ import type { SearchOptions } from './search-engine'

export type Model = Partial<ModelOptions>

interface BaseRelation {
foreignKey?: string
relationName?: string
}

interface Relation<T = string> extends BaseRelation {
model: T
}

interface HasOne<T = string> extends Array<Relation<T>> {}
interface HasMany<T = string> extends Array<Relation<T>> {}
interface BelongsTo<T = string> extends Array<Relation<T>> {}
interface BelongsToMany<T = string> extends Array<T> {}

interface HasOneThrough<T = string> extends Array<{
model: T
through: T
foreignKey?: string
throughForeignKey?: string
relationName?: string
}> {}


export interface FieldArrayElement {
entity: string
charValue?: string | null
Expand Down Expand Up @@ -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<ModelNames> | string[]
hasMany?: HasMany<ModelNames> | ModelNames[]
belongsTo?: BelongsTo<ModelNames> | ModelNames[]
belongsToMany?: BelongsToMany<ModelNames> | ModelNames[]
hasOneThrough?: HasOneThrough<ModelNames>
}

export interface ApiSettings {
Expand Down Expand Up @@ -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<ModelNames> | string[]

hasMany?: HasMany<ModelNames> | ModelNames[]

belongsTo?: BelongsTo<ModelNames> | ModelNames[]

belongsToMany?: BelongsToMany<ModelNames> | ModelNames[]

hasOneThrough?: HasOneThrough<ModelNames>

scopes?: {
[key: string]: (value: any) => any
Expand Down

0 comments on commit b2f742c

Please sign in to comment.