- TypeORM - Discussion
- TypeORM - Useful Resources
- TypeORM - Quick Guide
- TypeORM - Working with CLI
- TypeORM - Migrations
- TypeORM with Express
- TypeORM - Working with MongoDB
- TypeORM with JavaScript
- TypeORM - Entity Listener and Logging
- TypeORM - Indices
- TypeORM - Transactions
- TypeORM - Query Operations
- TypeORM - Query Builder
- TypeORM - Working with Entity Manager
- TypeORM - Working with Repository
- TypeORM - Relations
- TypeORM - Entity
- TypeORM - Connection API
- TypeORM - Creating a Simple Project
- TypeORM - Installation
- TypeORM - Introduction
- TypeORM - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TypeORM - Creating a Simple Project
This chapter explains about how to create simple TypeORM apppcation. Let us create a new directory named ‘TypeORM’ and move into the directory.
cd /path/to/TypeORM/
Syntax
Use below command to create new project −
typeorm init --name <project-name> --database <database-name>
Example
typeorm init --name FirstProject --database mysql
Here,
FirstProject is your project name and sqpte3 is database name. After executing the above command, you could see the following response,
Project created inside /path/to/TypeORM/FirstProject directory
Now, move in to our project directory and install project dependencies using npm module,
$ cd FirstProject $ npm install
Project structure
Let us understand project structure of our newly created project, FirstProject.
FirstProject ├──> src │ ├──> entity │ │ └──> User.ts │ ├──> migration │ └──> index.ts ├──> node_modules ├──> ormconfig.json ├──> package.json ├──> package-lock.json └──> tsconfig.json
Here,
src − contains source code of your apppcation in TypeScript language. It has a file index.ts and two sub directories entity and migration.
index.ts − Your entry point to the apppcation.
entity − Contains database models.
migration − contains database migration code.
node_modules − locally saved npm modules.
ormconfig.json − Main configuration file of your apppcation. It contains database configuration details and entities configuration.
package.json − Contains node module dependencies.
package-lock.json − Auto generated file and related to package.json.
tsconfig.json − Contains TypeScript specific compiler options.
ormconfig.json file
Let us check the configuration option available for our apppcation. Open ormconfig.json file and it looks similar to this −
{ "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" ], "cp": { "entitiesDir":"src/entity", "migrationsDir":"src/migration", "subscribersDir":"src/subscriber } }
Here,
type, host, username, password, database and port options are related to database setting. mysql can be configured using below configuration −
{ "type": "mysql", "host": "localhost", "port": 3306, "username": "db_username", "password": "db_password", "database": "db_name" }
entities − refers the location of your entity classes.
migrations − refers the location of your migration classes.
subscribers − refers the location of your subscriber classes.
cp − refers the option used by TypeORM CLI to auto generate the code
Start MySql server
Before starting the apppcation, start your MySQL server or any database server used by you and make sure it is running properly.
Run apppcation
Once everything is configured, we can execute the apppcation using the below command −
npm start
You could see the following response −
> FirstProject@0.0.1 start /Users/../../TypeORM/FirstProject > ts-node src/index.ts Inserting a new user into the database... Saved a new user with id: 1 Loading users from the database... Loaded users: [ User { id: 1, firstName: Timber , lastName: Saw , age: 25 }] Here you can setup and run express/koa/any other framework.
The apppcation inserted a new user into the database and then reverse load it from the database and finally show the loaded user in the console. We have successfully created a new TypeORM apppcation, configured it and run the apppcation.
We will discuss about how the data is executed elaborately in upcoming chapters.
Advertisements