Categories
JavaScript Answers

How to change working directory in my current shell context when running a Node.js script?

Sometimes, we want to change working directory in my current shell context when running a Node.js script.

In this article, we’ll look at how to change working directory in my current shell context when running a Node.js script.

How to change working directory in my current shell context when running a Node.js script?

To change working directory in my current shell context when running a Node.js script, we can use the process.chdir method.

For instance, we write

console.log('Starting directory', process.cwd());
try {
  process.chdir('/tmp');
  console.log('New directory', process.cwd());
} catch (err) {
  console.log(err);
}

to get the current working directory with process.cwd.

Then we call process.chdir to change the current working directory to /tmp.

Conclusion

To change working directory in my current shell context when running a Node.js script, we can use the process.chdir method.

Categories
JavaScript Answers

How to read keystrokes from stdin with Node.js?

Sometimes, we want to read keystrokes from stdin with Node.js.

In this article, we’ll look at how to read keystrokes from stdin with Node.js.

How to read keystrokes from stdin with Node.js?

To read keystrokes from stdin with Node.js, we can use the process.stdin method.

For instance, we write

const {
  stdin
} = process;

stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');

stdin.on('data', (key) => {
  if (key === '\u0003') {
    process.exit();
  }
  process.stdout.write(key);
});

to call setRawMode to true to let us listen to keystrokes.

Then we call resume to resume the parent process.

Next we call setEncoding to 'utf8' to set the encoding of the data we get from the 'data' event to 'utf8'.

Next, we call stdin.on with 'data' and a event handler callback to listen for the data event.

In the callback, we check if ctrl+c with key === '\u0003'.

If it’s true, we call process.exit to exit the program.

Otherwise, we call process.stdout.write to write the key value to the screen.

Conclusion

To read keystrokes from stdin with Node.js, we can use the process.stdin method.

Categories
JavaScript Answers

How to create and use enums in Mongoose?

Sometimes, we want to create and use enums in Mongoose.

In this article, we’ll look at how to create and use enums in Mongoose.

How to create and use enums in Mongoose?

To create and use enums in Mongoose, we can set the enum property when we create our schema.

For instance, we write

const UserSchema = new Schema({
  userType: {
    type: String,
    enum: ['user', 'admin'],
    default: 'user'
  },
})

to create the UserSchema with the userType column.

We make it a string column by setting type to String.

And we set enum to an array of possible values we want the userType to have.

Also, we set the default value for userType to 'user'.

Conclusion

To create and use enums in Mongoose, we can set the enum property when we create our schema.

Categories
JavaScript Answers

How to write JSON object to a JSON file with fs.writeFileSync?

Sometimes, we want to write JSON object to a JSON file with fs.writeFileSync.

In this article, we’ll look at how to write JSON object to a JSON file with fs.writeFileSync.

How to write JSON object to a JSON file with fs.writeFileSync?

To write JSON object to a JSON file with fs.writeFileSync, we can convert the JSON object to a string before we write it.

For instance, we write

fs.writeFileSync('../data/freqs.json', JSON.stringify(output));

to call fs.writeFileSync with the JSON file path and the stringified JSON object.

We call JSON.stringify to stringify output.

Conclusion

To write JSON object to a JSON file with fs.writeFileSync, we can convert the JSON object to a string before we write it.

Categories
JavaScript Answers

How to make a collection store unique entries if the entry not null with Mongoose?

Sometimes, we want to make a collection store unique entries if the entry not null with Mongoose.

In this article, we’ll look at how to make a collection store unique entries if the entry not null with Mongoose.

How to make a collection store unique entries if the entry not null with Mongoose?

To make a collection store unique entries if the entry not null with Mongoose, we can specify a unique constraint when we define the schema.

For instance, we write

const UsersSchema = new Schema({
  name: {
    type: String,
    trim: true,
    index: true,
    required: true
  },
  email: {
    type: String,
    trim: true,
    index: {
      unique: true,
      partialFilterExpression: {
        email: {
          $type: "string"
        }
      }
    }
  }
});

to define UserSchema with the email field required to be unique for each entry.

The constraint is set by setting index.unique to true.

Conclusion

To make a collection store unique entries if the entry not null with Mongoose, we can specify a unique constraint when we define the schema.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.