Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Sequelize find sort order

Post.findAll({ limit: 10, order: [['updatedAt', 'DESC']]});
Comment

sequelize order by

order: [["createdAt", "DESC"]]
Comment

sequelize order by

Subtask.findAll({
  order: [
    // Will escape title and validate DESC against a list of valid direction parameters
    ['title', 'DESC'],

    // Will order by max(age)
    sequelize.fn('max', sequelize.col('age')),

    // Will order by max(age) DESC
    [sequelize.fn('max', sequelize.col('age')), 'DESC'],

    // Will order by  otherfunction(`col1`, 12, 'lalala') DESC
    [sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'DESC'],

    // Will order an associated model's createdAt using the model name as the association's name.
    [Task, 'createdAt', 'DESC'],

    // Will order through an associated model's createdAt using the model names as the associations' names.
    [Task, Project, 'createdAt', 'DESC'],

    // Will order by an associated model's createdAt using the name of the association.
    ['Task', 'createdAt', 'DESC'],

    // Will order by a nested associated model's createdAt using the names of the associations.
    ['Task', 'Project', 'createdAt', 'DESC'],

    // Will order by an associated model's createdAt using an association object. (preferred method)
    [Subtask.associations.Task, 'createdAt', 'DESC'],

    // Will order by a nested associated model's createdAt using association objects. (preferred method)
    [Subtask.associations.Task, Task.associations.Project, 'createdAt', 'DESC'],

    // Will order by an associated model's createdAt using a simple association object.
    [{model: Task, as: 'Task'}, 'createdAt', 'DESC'],

    // Will order by a nested associated model's createdAt simple association objects.
    [{model: Task, as: 'Task'}, {model: Project, as: 'Project'}, 'createdAt', 'DESC']
  ],

  // Will order by max age descending
  order: sequelize.literal('max(age) DESC'),

  // Will order by max age ascending assuming ascending is the default order when direction is omitted
  order: sequelize.fn('max', sequelize.col('age')),

  // Will order by age ascending assuming ascending is the default order when direction is omitted
  order: sequelize.col('age'),

  // Will order randomly based on the dialect (instead of fn('RAND') or fn('RANDOM'))
  order: sequelize.random()
});

Foo.findOne({
  order: [
    // will return `name`
    ['name'],
    // will return `username` DESC
    ['username', 'DESC'],
    // will return max(`age`)
    sequelize.fn('max', sequelize.col('age')),
    // will return max(`age`) DESC
    [sequelize.fn('max', sequelize.col('age')), 'DESC'],
    // will return otherfunction(`col1`, 12, 'lalala') DESC
    [sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'DESC'],
    // will return otherfunction(awesomefunction(`col`)) DESC, This nesting is potentially infinite!
    [sequelize.fn('otherfunction', sequelize.fn('awesomefunction', sequelize.col('col'))), 'DESC']
  ]
});
Comment

sequelize order with include

Users.findAll({
  ...
  order: [
    [Roles, 'created_at', 'asc']
  ],
  
  include: [
    {
      model: Roles,
    },
  ],
  ...
});
Comment

sequelize order by

Users.findAll({ order: [['updatedAt', 'DESC']]});
Comment

sort include sequelize

const categories = await models.Category.findAll({
  attributes: ['id', 'title', 'description'],
  order: [['title', 'ASC'], [models.Product, models.Price, 'createdAt', 'DESC']],
  include: [
    {
      model: models.Product,
      attributes: ['id', 'title'],
      through: { attributes: [] },
      include: [
        {
          model: models.Price,
          attributes: ['id', 'amount', 'createdAt'],
          separate: true,
          limit: 1,
        },
      ],
    },
  ],
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: invoke lambda nodejs 
Javascript :: js simulate click 
Javascript :: ClassNotFoundException: com.fasterxml.jackson.core.JsonGenerator 
Javascript :: if else jquery click function 
Javascript :: remove event listener react hooks 
Javascript :: javascript get index of object with value in array 
Javascript :: loop array javascript 
Javascript :: vanilla javascript set display 
Javascript :: hasOwnProperty with more than one property 
Javascript :: javascript date get current date 
Javascript :: js check if number is divisible by 2 
Javascript :: npx nestjs 
Javascript :: javascript size of variable in kb 
Javascript :: float force loopback 
Javascript :: javascript ceiling 
Javascript :: socket io get ip 
Javascript :: node js try catch 
Javascript :: find unique elements in array javascript 
Javascript :: get unique numbers of an array in javascript using for loop 
Javascript :: javascript reverse string without reverse method 
Javascript :: common character count javascript 
Javascript :: save json file python 
Javascript :: javascript get random string char 
Javascript :: javascript scroll to bottom of div 
Javascript :: remove extra spaces javascript 
Javascript :: get previous url javascript 
Javascript :: react chrome get language 
Javascript :: js push param to url 
Javascript :: Could not resolve dependency: npm ERR! peer reac 
Javascript :: Sorting an array of objects on both ascending and descending order on same click 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =