Categories
JavaScript Answers

How to get only the dataValues from Sequelize ORM?

Sometimes, we want to get only the dataValues from Sequelize ORM.

In this article, we’ll look at how to get only the dataValues from Sequelize ORM.

How to get only the dataValues from Sequelize ORM?

To get only the dataValues from Sequelize ORM, we can set the raw option to true.

For instance, we write

Model.findAll({
  raw: true,
  //...
});

to call findAll with an object with the raw property set to true to only return the dataValues in the query results.

Conclusion

To get only the dataValues from Sequelize ORM, we can set the raw option to true.

Categories
JavaScript Answers

How to update and return document in MongoDB?

Sometimes, we want to update and return document in MongoDB.

In this article, we’ll look at how to update and return document in MongoDB.

How to update and return document in MongoDB?

To update and return document in MongoDB, we can call findAndUpdare with the returnOriginal option set to false.

For instance, we write

collection.findOneAndUpdate({
    code
  }, {
    $set: updatedFields
  }, {
    returnOriginal: false
  },
  (err, documents) => {
    //...
    db.close();
  }
);

to call findOneAndUpdate to update the entries with code set to code with the updatedFields object values.

We set returnOriginal to false so that documents in the callback would return the updated versions of the updated documents.

Finally, we call db.close to close the database connection.

Conclusion

To update and return document in MongoDB, we can call findAndUpdare with the returnOriginal option set to false.

Categories
JavaScript Answers

How to create rooms in socket.io?

Sometimes, we want to create rooms in socket.io.

In this article, we’ll look at how to create rooms in socket.io.

How to create rooms in socket.io?

To create rooms in socket.io, we can just listen for events and call socket.join to join the room.

For instance, we write

const socket = io.connect();
socket.emit('create', 'room1');

to call socket.emit with 'create' and 'room1' to emit the create event and room room1 from client side.

Then on server side, we write

io.sockets.on('connection', (socket) => {
  socket.on('create', (room) => {
    socket.join(room);
  });
});

to call sockets.on to listen to the connection event.

Then in the connection event handler, we call socket.on again to listen to the 'create' event that we get from client side.

In it, we call socket.join with the room, which should be set to 'room1' with socket.emit to join the 'room1'.

Conclusion

To create rooms in socket.io, we can just listen for events and call socket.join to join the room.

Categories
JavaScript Answers

How to iterate over a MongoDB cursor serially with Node.js?

Sometimes, we want to iterate over a MongoDB cursor serially with Node.js.

In this article, we’ll look at how to iterate over a MongoDB cursor serially with Node.js.

How to iterate over a MongoDB cursor serially with Node.js?

To iterate over a MongoDB cursor serially with Node.js, we can use the cursor.hasNext method.

For instance, we write

const cursor = db.collection("foo").find({});
while (await cursor.hasNext()) {
  const doc = await cursor.next();
  // ...
}

to call cursor.hasNext which returns a promise.

Then we use await to wait for hasNext to return a result.

If the promise resolves to truer, then we run the loop body.

We get cursor from making a query with find.

Conclusion

To iterate over a MongoDB cursor serially with Node.js, we can use the cursor.hasNext method.

Categories
JavaScript Answers

How to add or delete columns in Sequelize CLI?

Sometimes, we want to add or delete columns in Sequelize CLI.

In this article, we’ll look at how to add or delete columns in Sequelize CLI.

How to add or delete columns in Sequelize CLI?

To add or delete columns in Sequelize CLI, we can use the sequelize migration:create command to create a migration file.

Then we call addColumn to add a column and removeColumn to remove a column in the migration file.

For instance, we we run

sequelize migration:create --name name_of_your_migration

to create a migration file.

Then we write

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.addColumn(
      'Todo',
      'completed',
      Sequelize.BOOLEAN
    );

  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.removeColumn(
      'Todo',
      'completed'
    );
  }
}

in the migration file.

We call queryInterface.addColumn with the table name, column name, and the column type to add a new column.

And we call queryInterface.removeColumn with the table name and column name to remove a column.

Conclusion

To add or delete columns in Sequelize CLI, we can use the sequelize migration:create command to create a migration file.

Then we call addColumn to add a column and removeColumn to remove a column in the migration file.