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:
npm install typeorm -g
Then go to the directory where you want to create a new project and run the command:
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:
MyProject
├── src // place of your TypeScript code
│ ├── entity // place where your entities (database models) are stored
│ │ └── User.ts // sample entity
│ ├── migration // place where your migrations are stored
│ └── index.ts // start point of your application
├── .gitignore // standard gitignore file
├── ormconfig.json // ORM and database connection configuration
├── package.json // node module dependencies
├── README.md // simple readme file
└── 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:
cd MyProject
npm install
While installation is in progress, edit the ormconfig.json
file and put your own database connection configuration options in there:
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test",
"synchronize": true,
"logging": false,
"entities": [
"src/entity/**/*.ts"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
]
}
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:
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 running
typeorm init --name MyProject --database mysql --express
command.