sequelize-sortable
is a Sequelize plugin to support simple sort query using scope.
Just pass Sequelize model class to make the emodel class sortable.
// Initialize
const Sorter = require('sequelize-sortable');
// Model definition
const Project = sequelize.define('project', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
allowNull: false,
type: Sequelize.STRING
}
});
// CASE: Use just sortable method if accept all of attributes as sort key.
Sorter.sortable(Project);
// CASE: Use sortable method with options which has sortableKeys property if accept part of attributes as sort key.
// (ex: only "id", other keys are ignored.)
Sorter.sortable(Project, {sortableKeys: ["id"]})
Use sortable
scope on fetching the resources.
const params = {sort: "id,-name"};
Project.scope({method: ["sortable", params]).findAll();
// => SELECT "id", "name" FROM "users" AS "user" ORDER BY "user"."id" ASC, "user"."name" DESC;
The value of the sort
property is a comma separated string.
Each component of the comma separated string are the model's properties
which may decorat with +
/-
.
$ cd path/to/this/repo/root
$ npm install
export TEST_DB=postgres://postgres:pass@127.0.0.1:5432/testdb
$ docker-compose up
Use tasks in package.json.
$ npm run ci