Skip to content

Conversation

@CodeDredd
Copy link
Owner

πŸ”— Linked issue

closes #1461

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This PR makes some improvements to orderBy.

  • orderBy is now excuted after relation ship loading so you can access nested data
  • orderBy support now dot notation role.name
  • it's now also possible to order by pivot data with some small help of useSotrBy
class User extends Model {
      static entity = 'users'

      @Attr() id!: number
      @Str('') name!: string
      @BelongsToMany(() => Role, () => RoleUser, 'user_id', 'role_id')
      roles!: Role[]
    }

    class Role extends Model {
      static entity = 'roles'

      @Attr() id!: number
      @BelongsToMany(() => User, () => RoleUser, 'role_id', 'user_id')
      users!: User[]

      pivot!: RoleUser
    }

    class RoleUser extends Model {
      static entity = 'roleUser'

      static primaryKey = ['role_id', 'user_id']

      @Attr() role_id!: number
      @Attr() user_id!: number
      @Attr() level!: number
    }

const users = userRepo.with('roles').orderBy((user) => {
      user.roles = useSortBy(user.roles, [['pivot.level', 'asc']])
    }).get()

// this returns

users = [
      { id: 1, name: 'James', roles: [
        { id: 2, users: [], pivot_roleUser: null, pivot: ... },
        { id: 1, users: [], pivot_roleUser: null, pivot: ... },
      ],
      },
      { id: 2, name: 'Andy', roles: [
        { id: 1, users: [], pivot_roleUser: null, pivot: ... },
      ],
      },
      { id: 3, name: 'David', roles: [] },
    ]

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Many-to-many orderBy pivot

2 participants