Categories
TypeScript Answers

How to extend types in TypeScript?

To extend types in TypeScript, we can use the extends keyword.

For instance, we write

type Event = {
  name: string;
  dateCreated: string;
  type: string;
};

interface UserEvent extends Event {
  UserId: string;
}

to create the UserEvent interface that extends the Event type.

We extend it by adding the string UserId field in UserEvent and inheriting the rest from Event.

Conclusion

To extend types in TypeScript, we can use the extends keyword.

Categories
TypeScript Answers

How to pass optional parameters while omitting some other optional parameters with TypeScript?

Sometimes, we want to pass optional parameters while omitting some other optional parameters with TypeScript.

In this article, we’ll look at how to pass optional parameters while omitting some other optional parameters with TypeScript.

How to pass optional parameters while omitting some other optional parameters with TypeScript?

To pass optional parameters while omitting some other optional parameters with TypeScript, we can add ? after the optional parameter mames.

For instance, we write

export interface INotificationService {
  error(message: string, title?: string, autoHideAfter?: number);
}

class X {
  error(message: string, title?: string, autoHideAfter?: number) {
    console.log(message, title, autoHideAfter);
  }
}

new X().error("hi there", undefined, 1000);

to make the title and autoHideAfter parameters optional in the error method in the INotificationService.

Likewise, we add ? to the parameter names in the error method in the X class to make those fields optional.

Conclusion

To pass optional parameters while omitting some other optional parameters with TypeScript, we can add ? after the optional parameter mames.

Categories
TypeScript Answers

How to create an object based on an interface file definition in TypeScript?

To create an object based on an interface file definition in TypeScript, we can create a class that extends the interface.

For instance, we write

interface IModal {
  content: string;
  form: string;
  //...
  foo: (bar: string) => void;
}

class Modal implements IModal {
  content: string;
  form: string;

  foo(param: string): void {}
}

to create the IModal interface with some fields.

Then we create the Modal class that has the fields listed in IModal.

Then we can use new Modal() to create a new Modal instance.

Conclusion

To create an object based on an interface file definition in TypeScript, we can create a class that extends the interface.

Categories
JavaScript Answers

How to query referenced objects in MongoDB?

Sometimes, we want to query referenced objects in MongoDB.

In this article, we’ll look at how to query referenced objects in MongoDB.

How to query referenced objects in MongoDB?

To query referenced objects in MongoDB, we can use the $lookup operator.

For instance, we write

db.Foo.aggregate(
  { $unwind: "$bars" },
  {
    $lookup: {
      from: "bar",
      localField: "bars",
      as: "bar",
    },
  },
  {
    $match: {
      "bar.testprop": true,
    },
  }
);

to call aggregate with the $lookup property set to an object with the referenced object properties we’re looking for.

We look for the collection specified with $lookup.from.

We look for the localField in the field specified in $lookup.from and we return the result in the field with the name set as the value of as.

We look for the field values in the $bars array since we set unwind to $bars.

And we search for the items with bar.testprop set to true with $match.

Conclusion

To query referenced objects in MongoDB, we can use the $lookup operator.

Categories
JavaScript Answers

How to encrypt data with a public key in Node.js?

Sometimes, we want to encrypt data with a public key in Node.js.

In this article, we’ll look at how to encrypt data with a public key in Node.js.

How to encrypt data with a public key in Node.js?

To encrypt data with a public key in Node.js, we can use the fs.readFileSync method to read the public key.

Then we use the crypt module to encrypt our data.

For instance, we write

const crypto = require("crypto");
const path = require("path");
const fs = require("fs");

const encryptStringWithRsaPublicKey = (
  toEncrypt,
  relativeOrAbsolutePathToPublicKey
) => {
  const absolutePath = path.resolve(relativeOrAbsolutePathToPublicKey);
  const publicKey = fs.readFileSync(absolutePath, "utf8");
  const buffer = Buffer.from(toEncrypt);
  const encrypted = crypto.publicEncrypt(publicKey, buffer);
  return encrypted.toString("base64");
};

const decryptStringWithRsaPrivateKey = (
  toDecrypt,
  relativeOrAbsolutePathtoPrivateKey
) => {
  const absolutePath = path.resolve(relativeOrAbsolutePathtoPrivateKey);
  const privateKey = fs.readFileSync(absolutePath, "utf8");
  const buffer = Buffer.from(toDecrypt, "base64");
  const decrypted = crypto.privateDecrypt(privateKey, buffer);
  return decrypted.toString("utf8");
};

to create the encryptStringWithRsaPublicKey and decryptStringWithRsaPrivateKey functions.

In encryptStringWithRsaPublicKey, we get the publicKey with fs.readFileSync to read the public key file.

Then we convert the toEncrypt string to a buffer with Buffer.from.

And then we call crypto.publicEncrypt to encrypt the buffer with the publicKey.

In decryptStringWithRsaPrivateKey, we read the privateKey with fs.readFileSync.

Then we get the buffer from the toDecrypt string with Buffer.from.

Next, we call crypto.privateDecrypt with privateKey and buffer to decrypt the buffer with the privateKey.

Finally, we convert the decrypted content to a string with toString.

Conclusion

To encrypt data with a public key in Node.js, we can use the fs.readFileSync method to read the public key.

Then we use the crypt module to encrypt our data.