This tutorial demonstrates how to use TypeORM together with express.js and Typescript to create a webserver.
It covers TypeORM migrations TypeORM relations
and demonstrates how to make use of TypeORM with an express.js webserver.
This repository is contains the code for the TypeORM tutorial playlist
Please also check out my website at jangoebel.com
For updates, please follow @_jgoebel on Twitter.
Run
npm i
to install all dependencies.
This project requires that you run a Postgres database. You can either set up a Postgres instance on your local machine or use the docker-compose to spin up a database on the fly.
Install Postgres on your local machine and adjust the ormconfig.json file
with the username, password and database name that will work with your local Postgres instance.
If you are on Mac, you can install Postgres with Homebrew
brew install postgres
and start it with:
brew services start postgres
The service can be stopped with:
brew services stop postgres
This project comes with a docker-compose file that allows you to
spin up a Postgres database effortlessly.
Run:
docker-compose up
to start a Postgres 13 container whose port is mapped to port 5432 on your machine.
To stop the Postgres database Docker container, hit Ctrl-C on your keyboard. Bear in mind that the container is now only stopped, but not removed - so the data is still there.
To remove the container and all the data that you created in it for good, first stop it with Ctrl-C and then run:
docker-compose down
If you use this option, there is no need to change the ormconfig.json file - the server will be able to connect to the database without configuration changes.
Run
npm run dev
to start up a your express.js server on pot 8080 with a hot reload functionality.
Ideal for development.
Run
npm start
to start the webserver on port 8080 without hot reload functionality.
This project was scaffoleded with the TypeORM CLI.
For reference, the commands used in the tutorial can be found in package.json file in the scripts section:
"start": "ts-node src/index.ts",
"dev": "tsnd --respawn src/index.ts",
"init": "npx typeorm init --name typeorm-tutorial --database postgres --docker",
"migrate": "ts-node ./node_modules/typeorm/cli.js migration:run",
"revert": "ts-node ./node_modules/typeorm/cli.js migration:revert",
"generate": "ts-node ./node_modules/typeorm/cli.js migration:generate -n migration -p",
"generate-dry": "ts-node ./node_modules/typeorm/cli.js migration:generate -n migration -p --dr",
"make-migration": "npx typeorm migration:create -n migrationName"
