Categories
JavaScript Answers

How to make connection to Postgres via Node.js?

Sometimes, we want to make connection to Postgres via Node.js.

In this article, we’ll look at how to make connection to Postgres via Node.js.

How to make connection to Postgres via Node.js?

To make connection to Postgres via Node.js, we can use the pg module.

To install it, we run

npm i pg

Then we use it by writing

const {
  Pool
} = require('pg');

const config = {
  user: 'foo',
  database: 'my_db',
  password: 'secret',
  host: 'localhost',
  port: 5432,
  max: 10,
  idleTimeoutMillis: 30000
};

const pool = new Pool(config);
pool.on('error', (err, client) => {
  console.error('idle client error', err.message, err.stack);
});
pool.query('SELECT $1::int AS number', ['2'], (err, res) => {
  if (err) {
    return console.error('error running query', err);
  }
  console.log('number:', res.rows[0].number);
});

to create a new Pool instance with the config object that has the database host, name, port, and credentials.

Then we call pool.on with 'error' to catch any errors with the connection.

And then we call pool.query to make a database query with the parameterized SQL string, values, and a callback that has the result of the query.

Conclusion

To make connection to Postgres via Node.js, we can use the pg module.

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.