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 :: js add element to front of array 
Javascript :: sequelize sync 
Javascript :: get hours and minutes and seconds from date in javascript 
Javascript :: regex date yyyy-mm-dd 
Javascript :: click outside javascript 
Javascript :: js localstorage 
Javascript :: input to state 
Javascript :: javascript run two functions at the same time 
Javascript :: angular decode url 
Javascript :: unique string id js 
Javascript :: contenteditable javascript 
Javascript :: javascript sum array 
Javascript :: js spread exclude property 
Javascript :: ajax post variable values 
Javascript :: on scroll change navbar color 
Javascript :: javascript check if is image 
Javascript :: set view engine to ejs in express 
Javascript :: prevent form submission on onsubmit function calls 
Javascript :: target url javascript 
Javascript :: material ui location icon 
Javascript :: react native no android sdk found 
Javascript :: check if at least one checkbox is checked 
Javascript :: cryptojs decrypt 
Javascript :: remove underline from hyperlink react 
Javascript :: how to use br tag in javascript string 
Javascript :: how to get element of an array in javascript 
Javascript :: js add more text to element 
Javascript :: javascript password generator 
Javascript :: javascript get element tag 
Javascript :: axios.post request with custom headers 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =