Skip to content

Latest commit

 

History

History
127 lines (90 loc) · 4.5 KB

reference.md

File metadata and controls

127 lines (90 loc) · 4.5 KB

Reference

Lerna

# Init. an indep. versioned monorepo
npx lerna init --independent

# Create a scoped package
npx lerna create @graphql-chat/api --private

# Install a dev dep to a package
npx lerna add nodemon --scope=@graphql-chat/api --dev

# Install deps across packages
npx lerna bootstrap --hoist

# Run watch script across packages
npx lerna run --parallel watch

MongoDB

# Start a container in the background on port 27017 with 'root' user on the 'admin' database
docker run -d --name mongodb -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=secret mongo

# Run the mongo CLI client as 'root' against 'admin' database and connect to 'chat'
docker exec -it mongodb mongo -u root -p secret --authenticationDatabase admin chat

# Inside the client, create an admin user for the 'chat' database
db.createUser({
  user: 'admin', pwd: 'secret', roles: ['readWrite', 'dbAdmin']
})

# Verify that you can connect to mongo through the exposed port on your host machine
curl 127.0.0.1:27017
# It looks like you are trying to access MongoDB over HTTP on the native driver port.

# Connect as admin
docker exec -it mongodb mongo -u admin -p secret chat

Redis

docker run -d --name redisdb -p 6379:6379 redis redis-server --requirepass secret

docker exec -it redisdb redis-cli -a secret

Docker

# Build a container, tag with a name
docker build -t chat-api .

# Run a container in detached mode
docker run -d -p 3000:3000 chat-api

# SSH into the container
docker exec -it chat-api sh

# Remove dangling images
docker rmi $(docker images --quiet --filter "dangling=true")

# Remove stopped containers
docker rm $(docker ps -a -q)

docker-compose

Docker best practices

See this and this

  • Run node with USER node instead of root
  • Use FROM node:alpine base image
  • Don't map node_modules volume to your container
    • local node_modules may contain OS-specific (Mac, Windows) binaries
  • Make sure to pass environment vars, not shell vars
    • use export and not simply source .env or . .env (see this)
      • otherwise, make sure to use set -a (see this)
    • echo $DB_USERNAME vs. printenv | grep DB_USERNAME (see this)
  • Make env vars configurable

Nginx

Testing

  1. jest + ts-jest & @shelf/jest-mongodb presets
// jest.config.js
module.exports = merge.recursive(ts, mongo, { ... })
  • parallel, but doesn't expose options for a one-time global setup
    • globalSetup/globalTeardown run in separate processes (can't share global vars)
    • setupFiles/setupFilesAfterEnv run for each test file (one mongod process per file (!))
  • requires mongodb-memory-server with a mongod binary (70+ MB) which needs to be cached in CI
  • could run a pretest script, but posttest is not guaranteed to be reached
  • could make it work with a custom testEnvironment (see this)
  1. mocha + ts-node + mongodb-memory-server
mocha -r ts-node/register src/**/__tests__/*.ts
  • sequential, but allows for a one-time global setup/teardown
  • cannot require .d.ts files, so can't declare global funcs
  • poor linting, "plugin:mocha/recommended" doesn't work, use "env": ["mocha": true]
  1. apollo-server-testing
  • doesn't respect express middleware (and thus express-session, thus no auth