Today I Learned

TIL, 2023-07-29

Building Dockerfiles

FROM node:12-stretch

CMD ["node", "-e", "console.log(\"hi lol\")"]
  • Start with the node container. This comes from at least at some point, a Debian image.
  • There is only one CMD instruction.
  • docker build . --tag my-node-app
  • docker run my-node-app
  • docker images - show the images you built
  • You can version them if you want to
  • docker ps - see all running Docker processes
  • docker stop ${PID} - well, stop it
  • Can’t stop with CTRL+C, you have to handle it yourself in the Node.js code, or you can tell Docker to handle it with init flag.
  • docker run --init --publish 8000:3000 my-node-app # or you can use -p instead of --publish
    • Run the app that we have in the container (port 3000) and expose it to port 8000 (localhost:8000 locally)
FROM node:12-stretch

USER node

COPY index.js /home/node/code/index.js // Copy current index.js to that directory in the container

CMD ["node", "/home/node/code/index.js"]
  • USER instruction let’s us switch from being the root user to different user. The NODE user was already created by the image need to run useradd to add another user.
  • WORKDIR - changing directory.

  • EXPOSE ???

React-query: Parallel and Dependent Queries

Reference

  • Getting both repos and gists: Promise.all
function getReposAndGists(username) {
  return Promise.all([
    fetch(`https://api.github.com/users/${username}/repos`)
      .then((res) => res.json()),
    fetch(`https://api.github.com/users/${username}/gists`)
      .then((res) => res.json())
  ]);
}
  • useQueries - used to fetch a variable number of queries.
  • Dependent query: Use useQuery third parameter - the configuration object.

Run the issuesQuery only after the labels query has been fetched.

const labelsQuery = useQuery(
  ["repos", owner, repo, "labels"],
  () => fetch(
    `https://api.github.com/repos/${owner}/${repo}/labels`
  ).then((res) => res.json())
);

const labels = labelsQuery.data

const issuesQuery = useQuery(
  ["repos", owner, repo, "issues"],
  () => fetch(
    `https://api.github.com/repos/${owner}/${repo}/issues?labels=${labels[0].name}`
  ).then((res) => res.json()),
  {
    enabled: !!labels
  }
);

  • fetch Status - if it’s fetching, idle, or paused.
  • Deferred query: Something like when the query immediately makes a request with an empty search query.
  • encodeURIComponent()

useQuery API Reference

Reference

  • querykey - query will update when the key changes, as long as enabled is not set to false.
  • queryFn - required, but only if no default query function has been defined.
  • enabled - disable to stop the query from automatically running, used for dependent queries.

This project is maintained by daryllxd