├── .gitignore ├── README.md ├── ormconfig.json ├── package.json ├── src ├── controller │ ├── PostGetAllAction.ts │ ├── PostGetByIdAction.ts │ └── PostSaveAction.ts ├── entity │ ├── Category.ts │ └── Post.ts ├── index.ts ├── migration │ └── .gitkeep ├── routes.ts └── subscriber │ └── .gitkeep └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | src/**/*.js 3 | src/**/*.js.map -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Example how to use Express and TypeORM with TypeScript 2 | 3 | 1. clone repository 4 | 2. run `npm i` 5 | 3. edit `ormconfig.json` and change your database configuration (you can also change a database type, but don't forget to install specific database drivers) 6 | 4. run `npm start` 7 | 5. open `http://localhost:3000/posts` and you'll empty array 8 | 6. use curl, postman or other tools to send http requests to test your typeorm-based API 9 | 10 | ## How to use CLI? 11 | 12 | 1. install `typeorm` globally: `npm i -g typeorm` 13 | 2. run `typeorm -h` to show list of available commands -------------------------------------------------------------------------------- /ormconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "mysql", 3 | "host": "localhost", 4 | "port": 3306, 5 | "username": "test", 6 | "password": "test", 7 | "database": "test", 8 | "synchronize": true, 9 | "entities": [ 10 | "src/entity/*.js" 11 | ], 12 | "subscribers": [ 13 | "src/subscriber/*.js" 14 | ], 15 | "migrations": [ 16 | "src/migration/*.js" 17 | ], 18 | "cli": { 19 | "entitiesDir": "src/entity", 20 | "migrationsDir": "src/migration", 21 | "subscribersDir": "src/subscriber" 22 | } 23 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typeorm-typescript-express-example", 3 | "version": "0.0.1", 4 | "description": "Example how to use Express and TypeORM with TypeScript.", 5 | "license": "MIT", 6 | "readmeFilename": "README.md", 7 | "author": { 8 | "name": "Umed Khudoiberdiev", 9 | "email": "pleerock.me@gmail.com" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/typeorm/typescript-express-example.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/typeorm/typescript-express-example/issues" 17 | }, 18 | "tags": [ 19 | "orm", 20 | "typescript", 21 | "typescript-orm", 22 | "typeorm-sample", 23 | "typeorm-example", 24 | "typeorm-express-example" 25 | ], 26 | "devDependencies": { 27 | "@types/body-parser": "^1.19.2", 28 | "@types/express": "^4.17.17", 29 | "@types/node": "^18.15.11", 30 | "typescript": "^5.0.4" 31 | }, 32 | "dependencies": { 33 | "body-parser": "^1.20.2", 34 | "express": "^4.18.2", 35 | "mysql": "^2.18.1", 36 | "typeorm": "^0.3.14" 37 | }, 38 | "scripts": { 39 | "start": "tsc && node src/index.js" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/controller/PostGetAllAction.ts: -------------------------------------------------------------------------------- 1 | import {Request, Response} from "express"; 2 | import {getManager} from "typeorm"; 3 | import {Post} from "../entity/Post"; 4 | 5 | /** 6 | * Loads all posts from the database. 7 | */ 8 | export async function postGetAllAction(request: Request, response: Response) { 9 | 10 | // get a post repository to perform operations with post 11 | const postRepository = getManager().getRepository(Post); 12 | 13 | // load posts 14 | const posts = await postRepository.find(); 15 | 16 | // return loaded posts 17 | response.send(posts); 18 | } 19 | -------------------------------------------------------------------------------- /src/controller/PostGetByIdAction.ts: -------------------------------------------------------------------------------- 1 | import {Request, Response} from "express"; 2 | import {getManager} from "typeorm"; 3 | import {Post} from "../entity/Post"; 4 | 5 | /** 6 | * Loads post by a given id. 7 | */ 8 | export async function postGetByIdAction(request: Request, response: Response) { 9 | 10 | // get a post repository to perform operations with post 11 | const postRepository = getManager().getRepository(Post); 12 | 13 | // load a post by a given post id 14 | const post = await postRepository.findOne(request.params.id); 15 | 16 | // if post was not found return 404 to the client 17 | if (!post) { 18 | response.status(404); 19 | response.end(); 20 | return; 21 | } 22 | 23 | // return loaded post 24 | response.send(post); 25 | } 26 | -------------------------------------------------------------------------------- /src/controller/PostSaveAction.ts: -------------------------------------------------------------------------------- 1 | import {Request, Response} from "express"; 2 | import {getManager} from "typeorm"; 3 | import {Post} from "../entity/Post"; 4 | 5 | /** 6 | * Saves given post. 7 | */ 8 | export async function postSaveAction(request: Request, response: Response) { 9 | 10 | // get a post repository to perform operations with post 11 | const postRepository = getManager().getRepository(Post); 12 | 13 | // create a real post object from post json object sent over http 14 | const newPost = postRepository.create(request.body); 15 | 16 | // save received post 17 | await postRepository.save(newPost); 18 | 19 | // return saved post back 20 | response.send(newPost); 21 | } -------------------------------------------------------------------------------- /src/entity/Category.ts: -------------------------------------------------------------------------------- 1 | import {Entity, PrimaryGeneratedColumn, Column} from "typeorm"; 2 | 3 | @Entity() 4 | export class Category { 5 | 6 | @PrimaryGeneratedColumn() 7 | id: number; 8 | 9 | @Column() 10 | name: string; 11 | 12 | } -------------------------------------------------------------------------------- /src/entity/Post.ts: -------------------------------------------------------------------------------- 1 | import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable} from "typeorm"; 2 | import {Category} from "./Category"; 3 | 4 | @Entity() 5 | export class Post { 6 | 7 | @PrimaryGeneratedColumn() 8 | id: number; 9 | 10 | @Column() 11 | title: string; 12 | 13 | @Column("text") 14 | text: string; 15 | 16 | @ManyToMany(type => Category, { 17 | cascade: true 18 | }) 19 | @JoinTable() 20 | categories: Category[]; 21 | 22 | } -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import "reflect-metadata"; 2 | import {createConnection} from "typeorm"; 3 | import {Request, Response} from "express"; 4 | import * as express from "express"; 5 | import * as bodyParser from "body-parser"; 6 | import {AppRoutes} from "./routes"; 7 | 8 | // create connection with database 9 | // note that it's not active database connection 10 | // TypeORM creates connection pools and uses them for your requests 11 | createConnection().then(async connection => { 12 | 13 | // create express app 14 | const app = express(); 15 | app.use(bodyParser.json()); 16 | 17 | // register all application routes 18 | AppRoutes.forEach(route => { 19 | app[route.method](route.path, (request: Request, response: Response, next: Function) => { 20 | route.action(request, response) 21 | .then(() => next) 22 | .catch(err => next(err)); 23 | }); 24 | }); 25 | 26 | // run app 27 | app.listen(3000); 28 | 29 | console.log("Express application is up and running on port 3000"); 30 | 31 | }).catch(error => console.log("TypeORM connection error: ", error)); 32 | -------------------------------------------------------------------------------- /src/migration/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/typeorm/typescript-express-example/c393449b23c29b2e25a1c4dc42bec9ccd84285c1/src/migration/.gitkeep -------------------------------------------------------------------------------- /src/routes.ts: -------------------------------------------------------------------------------- 1 | import {postGetAllAction} from "./controller/PostGetAllAction"; 2 | import {postGetByIdAction} from "./controller/PostGetByIdAction"; 3 | import {postSaveAction} from "./controller/PostSaveAction"; 4 | 5 | /** 6 | * All application routes. 7 | */ 8 | export const AppRoutes = [ 9 | { 10 | path: "/posts", 11 | method: "get", 12 | action: postGetAllAction 13 | }, 14 | { 15 | path: "/posts/:id", 16 | method: "get", 17 | action: postGetByIdAction 18 | }, 19 | { 20 | path: "/posts", 21 | method: "post", 22 | action: postSaveAction 23 | } 24 | ]; -------------------------------------------------------------------------------- /src/subscriber/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/typeorm/typescript-express-example/c393449b23c29b2e25a1c4dc42bec9ccd84285c1/src/subscriber/.gitkeep -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.4.2", 3 | "compilerOptions": { 4 | "lib": ["es5", "es6"], 5 | "target": "es6", 6 | "module": "commonjs", 7 | "moduleResolution": "node", 8 | "emitDecoratorMetadata": true, 9 | "experimentalDecorators": true, 10 | "sourceMap": true 11 | }, 12 | "exclude": [ 13 | "node_modules" 14 | ] 15 | } 16 | --------------------------------------------------------------------------------