Categories
JavaScript Answers

How to delete items with Sequelize.js?

Sometimes, we want to delete items with Sequelize.js.

In this article, we’ll look at how to delete items with Sequelize.js.

How to delete items with Sequelize.js?

To delete items with Sequelize.js, we can use the destroy method.

For instance, we write

Model.destroy({
  where: {
    // ...
  }
})

to call Model.destroy with an object that has the where property to specify which items we want to delete.

Conclusion

To delete items with Sequelize.js, we can use the destroy method.

Categories
JavaScript Answers

How to get all count of Mongoose model?

Sometimes, we want to get all count of Mongoose model.

In this article, we’ll look at how to get all count of Mongoose model.

How to get all count of Mongoose model?

To get all count of Mongoose model, we can use the model count method.

For instance, we write

userModel.count({}, (err, count) => {
  console.log("Number of users:", count);
})

to call useModel.count to get the count of the items in the collection mapped to userModel.

We get the count from the count parameter in the callback.

Conclusion

To get all count of Mongoose model, we can use the model count method.

Categories
JavaScript Answers

How to do batch insert with Node.js Mongoose?

Sometimes, we want to do batch insert with Node.js Mongoose.

In this article, we’ll look at how to do batch insert with Node.js Mongoose.

How to do batch insert with Node.js Mongoose?

To do batch insert with Node.js Mongoose, we can use the insertMany method.

For instance, we write

const rawDocuments = [
  /* ... */
];

const mongooseDocuments = await Book.insertMany(rawDocuments)

to call Book.inseertMany with the rawDocuments array with the plain objects we want to insert as documents into the books collection.

Then we get the inserted documents from the resolved value of the returned promise as an array and assign that to mongooseDocuments.

Conclusion

To do batch insert with Node.js Mongoose, we can use the insertMany method.

Categories
JavaScript Answers

How to programmatically shut down an instance of Express.js?

Sometimes, we want to programmatically shut down an instance of Express.js.

In this article, we’ll look at how to programmatically shut down an instance of Express.js.

How to programmatically shut down an instance of Express.js?

To programmatically shut down an instance of Express.js, we can call the close method.

For instance, we write

const server = app.listen(3000);

const handler = () => {
  server.close();
};

to call the app.listen with the port number to return an Express server object.

Then we call server.close to shut down the Express instance in the handler function.

Conclusion

To programmatically shut down an instance of Express.js, we can call the close method.

Categories
JavaScript Answers

How to pipe a stream to s3.upload() with Node.js?

Sometimes, we want to pipe a stream to s3.upload() with Node.js.

In this article, we’ll look at how to pipe a stream to s3.upload() with Node.js.

How to pipe a stream to s3.upload() with Node.js?

To pipe a stream to s3.upload() with Node.js, we can use the s3.upload method with the stream.PassThrough method.

For instance, we write

const uploadFromStream = (s3) => {
  const pass = new stream.PassThrough();

  const params = {
    Bucket: BUCKET,
    Key: KEY,
    Body: pass
  };
  s3.upload(params, (err, data) => {
    console.log(err, data);
  });

  return pass;
}

inputStream
  .pipe(uploadFromStream(s3));

to create the uploadFromStream function that calls the stream.PassThrough method to return an object that we can use as the value of the Body property.

And then we call s3.upload with params that has the Bucket, Key, and Body.

Finally, we call inputStream.pipe with uploadFromStream(s3) to pipe the stream to the s3.upload method.

Conclusion

To pipe a stream to s3.upload() with Node.js, we can use the s3.upload method with the stream.PassThrough method.