Skip to content

jangbl/typeorm-tutorial

Repository files navigation

TypeORM tutorial with Node, Typescript and Postgres

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.

Please help this repo with a ⭐️ if you find it useful! 😁

This repository is contains the code for the TypeORM tutorial playlist

TypeORM tutorial series

Please also check out my website at jangoebel.com

For updates, please follow @_jgoebel on Twitter.

Project Setup

1. Install dependencies

Run

npm i

to install all dependencies.

2. Setup Database

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.

2.1 Local Database setup

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

2.2 Using docker-compose to start a Postgres database

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.

3. Start express webserver

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.

Useful commands:

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"

About

demonstrates how to use TypeORM with Postgres

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published