Categories
JavaScript Answers

How to use the Node.js PostgreSql module?

To use the Node.js PostgreSql module, we create a connection pool.

For instance, we write

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

const pool = new Pool({
  connectionString: DATABASE_URL,
  ssl: false,
  max: 20,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 2000,
});
module.exports = {
  query: (text, params) => pool.query(text, params),
};

to create a new Pool object to to create the connection pool.

Then we call pool.query to make a query to the database with the text SQL command and params parameters.

Categories
JavaScript Answers

How to fix 404 when attempting to publish new package to NPM?

To fix 404 when attempting to publish new package to NPM, we log into npm before publishing.

To do this, we run

npm login

to log into npm to publish the package.

Categories
JavaScript Answers

How to download and unzip files in Node.js cross-platform?

To download and unzip files in Node.js cross-platform, we use the zlib library.

For instance, we write

const zlib = require("zlib");

zlib.gunzip(gzipBuffer, (err, result) => {
  if (err) {
    return console.error(err);
  }

  console.log(result);
});

to call gunzip to unzip the gzipBuffer file.

We get the unzipped file from the result parameter in the callback.

Categories
JavaScript Answers

How to update each dependency in package.json to the latest version with Yarn and JavaScript?

To update each dependency in package.json to the latest version with Yarn and JavaScript, we run yarn upgrade.

For instance, we run

yarn upgrade <package-name> --latest

to upgrade the package with yarn upgrade to the latest version with the --latest flag.

Categories
JavaScript Answers

How to make Sequelize OR query with Node.js?

To make Sequelize OR query with Node.js, we call findAll with an object with the where property.

For instance, we write

Post.findAll({
  where: {
    authorId: {
      [Op.or]: [12, 13],
    },
  },
});

to call findAll with an object to query the Post items with authorId equal to 12 or 13, which is

SELECT * FROM post WHERE authorId = 12 OR authorId = 13;