# 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# 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 chatdocker run -d --name redisdb -p 6379:6379 redis redis-server --requirepass secret
docker exec -it redisdb redis-cli -a secret# 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)- CLI ref
- environment vars
- basic node.js guide
- specific services
docker-compose up chat-db chat-cache - start & rebuild
docker-compose up --build
- Run node with
USER nodeinstead ofroot - Use
FROM node:alpinebase image - Don't map
node_modulesvolume to your container- local
node_modulesmay contain OS-specific (Mac, Windows) binaries
- local
- Make sure to pass environment vars, not shell vars
- Make env vars configurable
jest+ts-jest&@shelf/jest-mongodbpresets
// jest.config.js
module.exports = merge.recursive(ts, mongo, { ... })- parallel, but doesn't expose options for a one-time global setup
globalSetup/globalTeardownrun in separate processes (can't shareglobalvars)setupFiles/setupFilesAfterEnvrun for each test file (one mongod process per file (!))
- requires
mongodb-memory-serverwith a mongod binary (70+ MB) which needs to be cached in CI - could run a
pretestscript, butposttestis not guaranteed to be reached - could make it work with a custom
testEnvironment(see this)
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.tsfiles, so can't declare global funcs - poor linting,
"plugin:mocha/recommended"doesn't work, use"env": ["mocha": true]
apollo-server-testing
- doesn't respect
expressmiddleware (and thusexpress-session, thus no auth