![]() npm run migrate:create will create a new migration file in src/migrations folder.npm run migrate down will roll back the migrations.npm run migrate up will run the migrations.It’s all glued together using npm scripts in the package.json: bin/migrate.js ( see migrate.js on GitHub) which can be called with up or down as arguments. To this end we use node-migrate, “Abstract migration framework for node” with a custom “state storage” module at src/persistence/postgres-state-storage.js, you can see postgres-state-storage on GitHub, it’s lifted and slightly adapted from the node-migrate documentation. Since we’re using Postgres with the Node Postgres ( pg on npm) driver (instead of an ORM like Sequelize), we need to set up a system to get our relationa database’s schema in order. ![]() Ideally one could also use husky and/or lint-staged to run the linter/formatter on commit or push. Npm run lint will run just xo which is a lint run without overwriting any of the code. Npm run format will run xo -fix, which leans on prettier to format all the code. ![]() It’s set up with prettier and spaces instead of tabs. This project uses xo, the “JavaScript happiness style linter”. bin/start.js, which means it’ll restart if any of the JavaScript changes. Npm run dev run the same script but with nodemon. dockerignore and Dockerfile for the app are as follows:Ĭonst Server = require ( './server' ) Server. app/node_modules makes sure that the local node_modules directory (outside of Docker) doesn’t get sync-ed to the container. :/app/ syncs the local directory to /app which is the WORKDIR defined in the Dockerfile. Internally it’s accessible at postgres:5432 ( :), hence why we set _URL to start command for the app service is npm start, as defined in the Dockerfile, but docker-compose.yml overrides it CMD with npm run dev which runs the application using nodemon (auto-restart on file change).Īnother point of interest are the entries. The connection string is (username, password and database name are defined in the block of docker-compose.yaml). Postgres is exposed on the host (dev machine, not Docker containers) port 35432. The Express application is configured to listen on whatever PORT is defined in the environment, in this case, we’re looking at PORT: 3000 in. what we configure our Express app to listen on). The second 3000 in that line is the port that the app container should be listening on (ie. the dev machine, not the Docker containers) can be re-mapped this by changing the first 3000 in 3000:3000 of ). The port that’s bound on the host machine (ie. Run docker-compose up in the root of the project to bring up Postgres and the Express application server in development mode.īased on the docker-compose.yaml, the application server is bound to localhost:3000. ![]() There is only 1 pre-requisite to run the application to develop on a new machine: Docker for Desktop installed and running. If you have any question, drop a comment below.A single command to bootstrap the whole application stack This is a basic example of how you can build a CRUD rest API using Node.js, Express, Sequelize, Postres, Docker, and Docker Compose.Īll the code is available in the GitHub repository (link in the video description): We can also check the values using TablePlus Make a DELETE reuqest to localhost:3000/users/3 Make a PUT reqeust to localhost:3000/users/2 with a different body Let's update an existing user, for example the same user 2 Make a GET request to localhost:3000/users/2 Let's get a single user, for example the user 2 We should have an empty array as a response Make a GET request to localhost:3000/users This should be the output on the terminal log ( err )) Įnter fullscreen mode Exit fullscreen mode models/user ' ) // CRUD Controllers //get all users exports.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |