NodeJS / JavaScript: Use NVM and .nvmrc to switch NodeJS versions automatically per project folder on Mac OS X

Your various software development projects are going to require you to use different versions of Node.js to compile and run; to make this process a little bit quicker, you can utilize NVM and .nvmrc files places in your project root folders to automatically switch versions when you change into them.

Let’s start by installing nvm using Brew:

brew install nvm

For those of us using Zsh as our Mac OS X shell, append the following to ~/.zshrc in your user home directory; it handles the automatic version switching per code project folder:

# If a .nvmrc file is present in the folder that we just changed into, switch to the version specified in it.
autoload -U add-zsh-hook
load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path=".nvmrc"

  if [ -f "$nvmrc_path" ]; then
    local nvmrc_node_version=$(cat "$nvmrc_path")
    if [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use "$nvmrc_node_version"
    fi
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

Next, you likely need to install a couple of different versions of Node.js; for example, to find the latest exact version of v18 to install, run:

nvm ls-remote 18

Now all we need to do is put a .nvmrc file in each project folder to switch to an installed version; if my ~/code/yllus_site/ folder needs version v18.20.8, a file at ~/code/yllus_site/.nvmrc should simply consist of:

 18.20.8 

Reference:

Creating another MySQL superuser on Amazon Aurora or Amazon RDS

Normally, creating a second (or third, fourth…) MySQL superuser who has all available database privileges is as simple as executing:

GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'%' IDENTIFIED BY 'password';

However, MySQL database in Amazon RDS and Amazon Aurora only allow one superuser to exist; however, you can neatly replicate the effect of having a superuser (the same full set of privileges) by modifying your SQL query to the following:

GRANT EXECUTE, PROCESS, SELECT, SHOW DATABASES, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, DELETE, CREATE VIEW, INDEX, EVENT, DROP, TRIGGER, REFERENCES, INSERT, CREATE USER, UPDATE, RELOAD, LOCK TABLES, REPLICATION SLAVE, REPLICATION CLIENT, CREATE TEMPORARY TABLES ON *.* TO 'newuser'@'%' WITH GRANT OPTION;

References:

Use Amazon S3 and CloudFront to create a proper HTTP and HTTPS redirect for a domain name

Fairly often we purchase a new domain or decommission an existing one and need to redirect all queries to it to a specific URL on a wholly different domain and website. Practically all services where you can register domain like GoDaddy, name.com and Namecheap offer a redirect function, but nearly all fail to work if the user attempts to go to a HTTPS URL on the domain you are redirecting.

If you’re already making use of Amazon AWS Cloud, a very low cost and foolproof solution is to utilize Amazon S3’s static website hosting feature along with Amazon CloudFront to handle the redirect:

  1. Create a new Amazon S3 bucket (eg. domain-redirect-olddomaincom) with ACLs disabled, the checkbox to block all public access unchecked (as we wish to allow public access) and bucket versioning disabled; create the bucket.
  2. Edit the newly created S3 bucket; under the Properties tab, edit the Static Website Hosting properties.
  3. In the Static Website Hosting properties page:
    • Static website hosting: Enable
    • Hosting type: Host a static website
    • Index document: index.html
    • Redirection rules: Copy/paste the following, with the appropriate changes made to the values of HostName and ReplaceKeyPrefixWith:
      [
          {
              "Redirect": {
                  "HostName": "www.newdomain.com",
                  "HttpRedirectCode": "301",
                  "Protocol": "https",
                  "ReplaceKeyPrefixWith": "path/to/redirect/to/"
              }
          }
      ]
  4. Hit the Save Changes button to finalize the redirect using the S3 bucket; it should work immediately and allow you to test it via the Bucket Website Endpoint URL shown on the page.
  5. Create a new Amazon CloudFront distribution, making sure to:
    1. Create Origin: Ensure that it points to the static website endpoint of your new S3 bucket (eg. domain-redirect-olddomaincom.s3-website.ca-central-1.amazonaws.com, not the default of domain-redirect-olddomaincom.s3.ca-central-1.amazonaws.com)
    2. Alternate domain name (CNAME): Enter all versions of the domain you wish to be handled and redirected (eg. olddomain.com and www.olddomain.com)
    3. Custom SSL certificate: Request a certificate that handles the domains you entered above; this is the (free) option that allows your newly set up domain redirect to respond properly to both HTTP and HTTPS requests
  6. Add the Amazon CloudFront domain (eg. d1gyvh82u10kd6.cloudfront.net) to your domain name’s DNS records for the root and subdomains you wish to redirect.

You’re done! While a bit long-winded as a process, this appears to be the cheapest and most foolproof way to set up a domain redirect for the long term.

 

Get your MySQL table sizes in MB via a SQL query

Figuring out what tables are making your database excessively large can be really helpful when working with production database exports and keeping various environments up to date. This SQL query outputs that information for all databases on a given server:

SELECT
	table_schema AS `Database`,
	table_name AS `Table`,
	ROUND(((data_length + index_length) / 1024 / 1024), 2) AS `Size in MB`
FROM information_schema.TABLES
ORDER BY table_schema, (data_length + index_length) DESC;

Reference:

Generate MySQL INSERT statements for a few existing records

Occasionally, you’ll want to duplicate a couple of records from your production MySQL database for local use without dumping the entire table (which could be huge). As a solution, the mysqldump command allows you to specify a table and a WHERE query for that table, allowing you to select specific record ID #s and retrieve only those records as INSERT statements:

mysqldump -h {host} -P {port} -u {username} -p{password} {database_name} {table_name} --where="ID = 1" --no-create-info --no-create-db

Reference:

Upgrade a deprecated AWS Elastic Beanstalk environment in place using the CLI

When a major PHP version change (eg. 5.7 to 7.0, or 7.0 to 8.0) occurs, Amazon AWS Elastic Beanstalk typically does not allow for automated platform version updates but instead requires the cloning of an interface and a CNAME switch. For less available environments, use the following steps to upgrade-in-place using the AWS command line interface:

  1. Take a look at the list of currently supported PHP platforms on Elastic Beanstalk; you’ll want to decide which Solution Stack Name (eg. “64bit Amazon Linux 2 v3.4.1 running PHP 8.1”) corresponds to the platform you wish to upgrade to.
  2. In the Elastic Beanstalk web configuration page, get the environment ID for the environment you wish to upgrade (eg. “e-jwfjvy57r3”).
  3. Run the aws command to upgrade the environment in place:
    aws elasticbeanstalk update-environment --solution-stack-name "64bit Amazon Linux 2 v3.4.1 running PHP 8.1" --environment-id "e-2iuwivbgzc" --region "ca-central-1"

Reference:

Measure current IOPS usage on a Linux server

This is most easily measured by installing the sysstat package:

sudo yum install sysstat -y

And running the sar command, looking at the value of tps:

sar -d 1

Getting a recursive list of files sorted by last modified date in Linux

Here’s a quick way to get a recursive list of files, sorted in descending order by the last modified date, from any folder in *nix:

find . -type f -printf "%T@ %p\0" | sort -zk1nr | xargs -0 printf "%s\n"

Google’s comprehensive, up-to-date list of mobile phone and device resolutions

The fine folks at Google have a page up on their Material Design site that lists popular device resolutions, including exactly what values you’d want to input into Google Chrome to accurately emulate that device’s screen:

What makes a great product manager?

There’s a terrific post by Brandon Chu on Medium titledĀ MVPM: Minimum Viable Product Manager that does a great job at explaining that while it’s impossible to have in-depth knowledge of the technical, business and user experience legs of the product tripod, knowing key elements in each area makes you so much more ableĀ to handle the decision making that you’ll be doing every minute of the day.