Categories
JavaScript Answers

How to add a where statement with date with Sequelize?

Sometimes, we want to add a where statement with date with Sequelize.

In this article, we’ll look at how to add a where statement with date with Sequelize.

How to add a where statement with date with Sequelize?

To add a where statement with date with Sequelize, we can use a JavaScript date object in where.

For instance, we write

const {
  Op
} = require('sequelize')

model.findAll({
  where: {
    start_datetime: {
      [Op.gte]: moment().subtract(7, 'days').toDate()
    }
  }
})

to query for items with the start_datetime value greater than or equal to 7 days or later than today with

[Op.gte]: moment().subtract(7, 'days').toDate()

We use the Op.gte operator to search for a column greater than or equal to a value.

Conclusion

To add a where statement with date with Sequelize, we can use a JavaScript date object in where.

Categories
JavaScript Answers

How to run app inside Docker as non-root user?

Sometimes, we want to run app inside Docker as non-root user.

In this article, we’ll look at how to run app inside Docker as non-root user.

How to run app inside Docker as non-root user?

To run app inside Docker as non-root user, we can use the adduser command to add a non-root user.

For instance, in our Dockerfile, we add

adduser --disabled-password --gecos '' r
adduser r sudo
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
su -m r -c /home/r/script.sh

to add the user r which is a non-root user.

We then run su with user r to run the /home/r/script.sh script.

Conclusion

To run app inside Docker as non-root user, we can use the adduser command to add a non-root user.

Categories
JavaScript Answers

How to add comments in Jade or Pug templates?

Sometimes, we want to add comments in Jade or Pug templates.

In this article, we’ll look at how to add comments in Jade or Pug templates.

How to add comments in Jade or Pug templates?

To add comments in Jade or Pug templates, we can add //- before our comment.

For instance, we write

doctype html

html(lang='en')
    body
        //- 
            This should be a comment

to add the This should be a comment comment in the body element.

Conclusion

To add comments in Jade or Pug templates, we can add //- before our comment.

Categories
JavaScript Answers

How to erase characters printed in console with Node.js?

Sometimes, we want to erase characters printed in console with Node.js.

In this article, we’ll look at how to erase characters printed in console with Node.js.

How to erase characters printed in console with Node.js?

To erase characters printed in console with Node.js, we can use the process.stdout.write method with a carriage return.

For instance, we write

process.stdout.write("\r");

to to move the cursor pack to the start of the line.

Then we can call process.stdout.write again to write something else to the same line.

Conclusion

To erase characters printed in console with Node.js, we can use the process.stdout.write method with a carriage return.

Categories
JavaScript Answers

How to make a Node.js app not exit on error?

Sometimes, we want to make a Node.js app not exit on error.

In this article, we’ll look at how to make a Node.js app not exit on error.

How to make a Node.js app not exit on error?

To make a Node.js app not exit on error, we can add a listener for the uncaughtException with process.on.

For instance, we write

process.on('uncaughtException', (err) => {
  console.log('Caught exception: ', err);
});

to call process.on with 'uncaughtException' and a callback that runs when the uncaughtException event is emitted.

The uncaughtException event is triggered when there’s an unhandled error occurring in the app.

In the callback, we get the error object from err.

Conclusion

To make a Node.js app not exit on error, we can add a listener for the uncaughtException with process.on.