Skip to content

Instantly share code, notes, and snippets.

@ziglee
Last active August 21, 2021 15:05
Show Gist options
  • Save ziglee/89b88ba6bf67c1a282805146c4d00f38 to your computer and use it in GitHub Desktop.
Save ziglee/89b88ba6bf67c1a282805146c4d00f38 to your computer and use it in GitHub Desktop.
Listing all Costumers with birthday today or tomorrow using Sequelize.js.
router.get('/api/birthday', function (req, res, next) {
var month = moment().month() + 1;
var today = moment().date();
var tomorrow = moment().add(1, 'days').date();
Costumer.findAll({
attributes: ['id','name','birthDate'],
where: {
$and: [
sequelize.where(sequelize.fn('month', sequelize.col("birth_date")), month)
],
$or: [
sequelize.where(sequelize.fn('day', sequelize.col("birth_date")), today),
sequelize.where(sequelize.fn('day', sequelize.col("birth_date")), tomorrow)
]
},
order: [
sequelize.fn('month', sequelize.col("birth_date")),
sequelize.fn('day', sequelize.col("birth_date")),
'name'
]
}).then(function (result) {
res.status(200).json(result);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment