Categories
JavaScript Answers

How to get the sha1 hash of a string in Node.js?

Sometimes, we want to get the sha1 hash of a string in Node.js.

In this article, we’ll look at how to get the sha1 hash of a string in Node.js.

How to get the sha1 hash of a string in Node.js?

To get the sha1 hash of a string in Node.js, we can use the crypto module.

For instance, we write

const crypto = require('crypto')
const shasum = crypto.createHash('sha1')
shasum.update('foo')
const s = shasum.digest('hex')

to call crypto.createHash to create the shasum object.

Then we call shasum.update with the string we want to create the hash from.

Finally, we call shasum.digest with 'hex' to return the hex digest.

Conclusion

To get the sha1 hash of a string in Node.js, we can use the crypto module.

Categories
JavaScript Answers

How to see the SQL generated by Sequelize.js?

Sometimes, we want to see the SQL generated by Sequelize.js.

In this article, we’ll look at how to see the SQL generated by Sequelize.js.

How to see the SQL generated by Sequelize.js?

To see the SQL generated by Sequelize.js, we can use the sequelize.sync method or set the logging option when we create our Sequelize instance.

For instance, we write

const sequelize = new Sequelize('database', 'username', 'password', {
  logging: console.log
});

to connect to our database by creating a Sequelize instance with the 'database' name, 'username' and 'password'.

We log the SQL code generated by Sequelize by setting logging to console.log.

Also, we can use sequelize.sync by writing

sequelize.sync({
  logging: console.log
})

to let us log the generated SQL in the console.

Conclusion

To see the SQL generated by Sequelize.js, we can use the sequelize.sync method or set the logging option when we create our Sequelize instance.

Categories
JavaScript Answers

How to change working directory in my current shell context when running a Node.js script?

Sometimes, we want to change working directory in my current shell context when running a Node.js script.

In this article, we’ll look at how to change working directory in my current shell context when running a Node.js script.

How to change working directory in my current shell context when running a Node.js script?

To change working directory in my current shell context when running a Node.js script, we can use the process.chdir method.

For instance, we write

console.log('Starting directory', process.cwd());
try {
  process.chdir('/tmp');
  console.log('New directory', process.cwd());
} catch (err) {
  console.log(err);
}

to get the current working directory with process.cwd.

Then we call process.chdir to change the current working directory to /tmp.

Conclusion

To change working directory in my current shell context when running a Node.js script, we can use the process.chdir method.

Categories
JavaScript Answers

How to read keystrokes from stdin with Node.js?

Sometimes, we want to read keystrokes from stdin with Node.js.

In this article, we’ll look at how to read keystrokes from stdin with Node.js.

How to read keystrokes from stdin with Node.js?

To read keystrokes from stdin with Node.js, we can use the process.stdin method.

For instance, we write

const {
  stdin
} = process;

stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');

stdin.on('data', (key) => {
  if (key === '\u0003') {
    process.exit();
  }
  process.stdout.write(key);
});

to call setRawMode to true to let us listen to keystrokes.

Then we call resume to resume the parent process.

Next we call setEncoding to 'utf8' to set the encoding of the data we get from the 'data' event to 'utf8'.

Next, we call stdin.on with 'data' and a event handler callback to listen for the data event.

In the callback, we check if ctrl+c with key === '\u0003'.

If it’s true, we call process.exit to exit the program.

Otherwise, we call process.stdout.write to write the key value to the screen.

Conclusion

To read keystrokes from stdin with Node.js, we can use the process.stdin method.

Categories
JavaScript Answers

How to create and use enums in Mongoose?

Sometimes, we want to create and use enums in Mongoose.

In this article, we’ll look at how to create and use enums in Mongoose.

How to create and use enums in Mongoose?

To create and use enums in Mongoose, we can set the enum property when we create our schema.

For instance, we write

const UserSchema = new Schema({
  userType: {
    type: String,
    enum: ['user', 'admin'],
    default: 'user'
  },
})

to create the UserSchema with the userType column.

We make it a string column by setting type to String.

And we set enum to an array of possible values we want the userType to have.

Also, we set the default value for userType to 'user'.

Conclusion

To create and use enums in Mongoose, we can set the enum property when we create our schema.