Quick Start

The quickest way to get started with TypeORM is to use its CLI commands to generate a starter project.Quick start works only if you are using TypeORM in a NodeJS application.If you are using other platforms, proceed to the step-by-step guide.

First, install TypeORM globally:

  1. npm install typeorm -g

Then go to the directory where you want to create a new project and run the command:

  1. typeorm init --name MyProject --database mysql

Where name is the name of your project and database is the database you’ll use.Database can be one of the following values: mysql, mariadb, postgres, cockroachdb, sqlite, mssql, oracle, mongodb,cordova, react-native, expo, nativescript.

This command will generate a new project in the MyProject directory with the following files:

  1. MyProject
  2. ├── src // place of your TypeScript code
  3. ├── entity // place where your entities (database models) are stored
  4. └── User.ts // sample entity
  5. ├── migration // place where your migrations are stored
  6. └── index.ts // start point of your application
  7. ├── .gitignore // standard gitignore file
  8. ├── ormconfig.json // ORM and database connection configuration
  9. ├── package.json // node module dependencies
  10. ├── README.md // simple readme file
  11. └── tsconfig.json // TypeScript compiler options

You can also run typeorm init on an existing node project, but be careful - it may override some files you already have.

The next step is to install new project dependencies:

  1. cd MyProject
  2. npm install

While installation is in progress, edit the ormconfig.json file and put your own database connection configuration options in there:

  1. {
  2. "type": "mysql",
  3. "host": "localhost",
  4. "port": 3306,
  5. "username": "test",
  6. "password": "test",
  7. "database": "test",
  8. "synchronize": true,
  9. "logging": false,
  10. "entities": [
  11. "src/entity/**/*.ts"
  12. ],
  13. "migrations": [
  14. "src/migration/**/*.ts"
  15. ],
  16. "subscribers": [
  17. "src/subscriber/**/*.ts"
  18. ]
  19. }

Particularly, most of the time you’ll only need to configurehost, username, password, database and maybe port options.

Once you finish with configuration and all node modules are installed, you can run your application:

  1. npm start

That’s it, your application should successfully run and insert a new user into the database.You can continue to work with this project and integrate other modules you need and startcreating more entities.

You can generate an even more advanced project with express installed by runningtypeorm init --name MyProject --database mysql --express command.