tommybananas / finale

Create flexible REST endpoints and controllers from Sequelize models in your Express app
187 stars 36 forks source link

Sorting ignored when obtaining hasMany relationship models #33

Open kevboutin opened 5 years ago

kevboutin commented 5 years ago

When I have the following setup:

const Group = database.define('groups', {
    name: Sequelize.STRING,
    status: Sequelize.INTEGER,
    userId: {
        type: Sequelize.INTEGER,
        references: {
            model: 'users',
            key: 'id'
        }
    }
});

const User = database.define('users', {
    type: Sequelize.INTEGER,
    name: Sequelize.STRING
});

// Ensure one to many relationships are set-up.
Group.hasMany(User);

finale.resource({
    model: Group,
    endpoints: ['/groups', '/groups/:id'],
    associations: true
});

finale.resource({
    model: User,
    actions: ['list', 'read', 'update'],
    endpoints: ['/users', '/users/:id'],
    sort: {
        default: 'name'
    }
});

If I use a GET to /users, the sorting works as expected and the ORDER BY is seen in the SQL output to the logs. However, if I use a GET to /groups the users array will be sorted by the Id and not using the default sort given.

Please offer suggestions for work-around or to fork and fix.

Thanks!