[ Attributes, Operators and Ordering ]
- 屬性,添加屬性篩選去返回
- 運算,使用Sequelize package 運算,簡化條件篩選
- 排列,依照遞增遞減序排列
(async () => {
await db.sequelize.sync({ force: true });
try {
// ... All model instances
const movies = await Movie.findAll({
attributes: ['id', 'title'], // return only id and title
where: {
isAvailableOnVHS: true,
releaseDate: {
[Op.gte]: '2004-01-01', // greater than or equal to the date
},
runtime: {
[Op.gt]: 95, // greater than 95
[Op.between]: [75, 115], // between 75 and 115
},
title: {
[Op.endsWith]: 'story'
},
},
order: [['id', 'DESC']] // IDs in descending order
order: [['releaseDate', 'ASC']], // dates in ascending order
});
console.log( movies.map(movie => movie.toJSON()) );
} catch(error) {
...
}
})();
const db = require('./db');
const { Movie, Person } = db.models;
const { Op } = db.Sequelize;
// in app.js