Categories
JavaScript Answers

How to set the Sequelize findAll sort order in Node.js?

Sometimes, we want to set the Sequelize findAll sort order in Node.js.

In this article, we’ll look at how to set the Sequelize findAll sort order in Node.js.

How to set the Sequelize findAll sort order in Node.js?

To set the Sequelize findAll sort order in Node.js, we can set the order property.

For instance, we write

const getStaticCompanies = () => {
  return Company.findAll({
    where: {
      //...
    },
    order: [
      ['id', 'DESC'],
      ['name', 'ASC'],
    ],
    attributes: ['id', 'logo_version', 'logo_content_type', 'name', 'updated_at']
  });
};

to call Company.findAll with an object that has the order property set an array to the columns and how we want to order them.

Conclusion

To set the Sequelize findAll sort order in Node.js, we can set the order property.

Categories
JavaScript Answers

How to use a MongoDB password with “@” in it in Node.js?

Sometimes, we want to use a MongoDB password with "@" in it in Node.js.

In this article, we’ll look at how to use a MongoDB password with "@" in it in Node.js.

How to use a MongoDB password with "@" in it in Node.js?

To use a MongoDB password with "@" in it in Node.js, we should escape the ‘@’ by URL encoding it.

For instance, we write

mongoClient.connect("mongodb://username:p%40ssword@host:port/dbname?authSource=admin", {
  useNewUrlParser: true
}, (err, db) => {

});

to replace ‘@’ with %40 in our connecting string.

Then we set useNewUrlParser to true so that the escaped ‘@’ is converted back to ‘@’.

Conclusion

To use a MongoDB password with "@" in it in Node.js, we should escape the ‘@’ by URL encoding it.

Categories
JavaScript Answers

How to set an environmental variable in Node.js?

Sometimes, we want to set an environmental variable in Node.js.

In this article, we’ll look at how to set an environmental variable in Node.js.

How to set an environmental variable in Node.js?

To set an environmental variable in Node.js, we can set it as a property of the process.env property.

For instance, we write

process.env['VARIABLE'] = 'value';

or

process.env.VARIABLE = 'value';

to set the VARIABLE environment variable to 'value'.

Conclusion

To set an environmental variable in Node.js, we can set it as a property of the process.env property.

Categories
JavaScript Answers

How to get the _id of inserted document in Mongo database in Node.js?

Sometimes, we want to get the _id of inserted document in Mongo database in Node.js.

In this article, we’ll look at how to get the _id of inserted document in Mongo database in Node.js.

How to get the _id of inserted document in Mongo database in Node.js?

To get the _id of inserted document in Mongo database in Node.js, we can get it from the callback of the collection insert method.

For instance, we write

collection.insert(objectToInsert, (err) => {
  if (err) {
    return;
  }
  const objectId = objectToInsert._id;
});

to call collection.insert with objectToInsert to insert the object as a document in collection.

to get the _id of the insert document from the objectToInsert._id property.

It should be available in the callback since the callback runs after the document is inserted.

Conclusion

To get the _id of inserted document in Mongo database in Node.js, we can get it from the callback of the collection insert method.

Categories
JavaScript Answers

How to load vanilla JavaScript libraries into Node.js?

Sometimes, we want to load vanilla JavaScript libraries into Node.js.

in this article, we’ll look at how to load vanilla JavaScript libraries into Node.js.

How to load vanilla JavaScript libraries into Node.js?

To load vanilla JavaScript libraries into Node.js, we can use readFileSync to read the JavaScript file.

Then we call the runInNewContext method from the vm module to run the script in a new context.

For instance, we write

execfile.js

const vm = require("vm");
const fs = require("fs");

module.exports = (path, context = {}) => {
  const data = fs.readFileSync(path);
  vm.runInNewContext(data, context, path);
  return context;
}

to create the execfile.js module, which exports a function that calls readFileSync with the script path.

Then we call vm.runInNextContext with the data, context, and path to run the script in the given context.

And then we return the context.

Then we require execfile.js in another module and call the exported function with

app.js

const execfile = require("./execfile");
const context = execfile("example.js", {
  someGlobal: 42
});
context.getSomeGlobal()

We call execfile with the 'example.js' and a context object that incorporate into the returned context.

And then we call context.getSomeGlobalto run the global function in theexample.js` script.

Conclusion

To load vanilla JavaScript libraries into Node.js, we can use readFileSync to read the JavaScript file.

Then we call the runInNewContext method from the vm module to run the script in a new context.