Categories
JavaScript Answers

How to upload a binary file to S3 using AWS SDK for Node.js?

Sometimes, we want to upload a binary file to S3 using AWS SDK for Node.js.

In this article, we’ll look at how to upload a binary file to S3 using AWS SDK for Node.js.

How to upload a binary file to S3 using AWS SDK for Node.js?

To upload a binary file to S3 using AWS SDK for Node.js, we can call putObject with a read stream.

For instance, we write

const fileStream = fs.createReadStream("F:/directory/fileName.ext");
const putParams = {
  Bucket: s3bucket,
  Key: s3key,
  Body: fileStream
};

s3.putObject(putParams, (putErr, putData) => {
  if (putErr) {
    console.error(putErr);
  } else {
    console.log(putData);
  }
});

to call putObject with the putParams object.

We set putParams.Body to fileStream to upload its content to S3 at the location set by the Bucket and Key.

Conclusion

To upload a binary file to S3 using AWS SDK for Node.js, we can call putObject with a read stream.

Categories
JavaScript Answers

How to loop through dynamic test cases with Jest?

Sometimes, we want to loop through dynamic test cases with Jest.

In this article, we’ll look at how to loop through dynamic test cases with Jest.

How to loop through dynamic test cases with Jest?

To loop through dynamic test cases with Jest, we can use the test.each method.

For instance, we write

test.each([
  [1, 1, 2],
  [1, 2, 3],
  [2, 1, 3]
])(
  '.add(%i, %i)',
  (a, b, expected) => {
    expect(a + b).toBe(expected);
  },
);

to call test.each with a nested array of values.

Then we call the returned function with a test name template string and the test callback.

a, b and expected are the entries from the nested array entry of the array we pass into each.

And then we can use the parameters for the tests.

Conclusion

To loop through dynamic test cases with Jest, we can use the test.each method.

Categories
JavaScript Answers

How to get the full file path in Node.js?

Sometimes, we want to get the full file path in Node.js.

In this article, we’ll look at how to get the full file path in Node.js.

How to get the full file path in Node.js?

To get the full file path in Node.js, we can use the path.resolve method.

For instance, we write

const path = require("path");
const absolutePath = path.resolve("/foo/bar/baz");

to call path.resolve with the relative path string to return the absolute path equivalent of the relative path.

Conclusion

To get the full file path in Node.js, we can use the path.resolve method.

Categories
JavaScript Answers

How to fix error when using “where” and “in” on a subarray with Node.js Sequelize?

Sometimes, we want to fix error when using "where" and "in" on a subarray with Node.js Sequelize.

In this article, we’ll look at how to fix error when using "where" and "in" on a subarray with Node.js Sequelize.

How to fix error when using "where" and "in" on a subarray with Node.js Sequelize?

To fix error when using "where" and "in" on a subarray with Node.js Sequelize, we can set the where property to an object with the properties being the fields we’re filtering by.

And we set the properties to arrays to search for the entries with any of the values.

For instance, we write

await Tag.findAll({
  where: {
    id: [1, 2, 3, 4]
  }
})

to call findAll with where.id set to [1, 2, 3, 4] to find the Tag items with id set to 1, 2, 3, or 4.

Conclusion

To fix error when using "where" and "in" on a subarray with Node.js Sequelize, we can set the where property to an object with the properties being the fields we’re filtering by.

And we set the properties to arrays to search for the entries with any of the values.

Categories
JavaScript Answers

How to load a Node.js module from string in memory?

Sometimes, we want to load a Node.js module from string in memory.

In this article, we’ll look at how to load a Node.js module from string in memory.

How to load a Node.js module from string in memory?

To load a Node.js module from string in memory, we can use the require-from-string module.

To install it, we run

npm install require-from-string

Then we use it by writing

const requireFromString = require('require-from-string');

const m = requireFromString('module.exports = 1');

to call requireFromString with a string with the module’s code to require it.

As a result, m is 1.

Conclusion

To load a Node.js module from string in memory, we can use the require-from-string module.