Skip to content

Commit 7154f5d

Browse files
author
Umed Khudoiberdiev
committed
initial commit
0 parents  commit 7154f5d

File tree

14 files changed

+253
-0
lines changed

14 files changed

+253
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
src/**/*.js
3+
src/**/*.js.map

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[
2+
{
3+
"name": "default",
4+
"driver": {
5+
"type": "mysql",
6+
"host": "localhost",
7+
"port": 3306,
8+
"username": "test",
9+
"password": "test",
10+
"database": "test"
11+
},
12+
"autoSchemaSync": true,
13+
"entities": [
14+
"src/entity/*.js"
15+
],
16+
"subscribers": [
17+
"src/subscriber/*.js,"
18+
],
19+
"migrations": [
20+
"src/migration/*.js"
21+
],
22+
"cli": {
23+
"entitiesDir": "src/entity",
24+
"migrationsDir": "src/migration",
25+
"subscribersDir": "src/subscriber"
26+
}
27+
}
28+
]

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "typeorm-typescript-koa-example",
3+
"version": "0.0.1",
4+
"description": "Example how to use Koa 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-koa-example.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/typeorm/typescript-koa-example/issues"
17+
},
18+
"tags": [
19+
"orm",
20+
"typescript",
21+
"typescript-orm",
22+
"typeorm-sample",
23+
"typeorm-example",
24+
"typeorm-koa-example"
25+
],
26+
"devDependencies": {
27+
"typescript": "^2.1.5"
28+
},
29+
"dependencies": {
30+
"@types/koa": "^2.0.39",
31+
"@types/koa-bodyparser": "^3.0.23",
32+
"@types/koa-router": "^7.0.21",
33+
"@types/node": "^7.0.4",
34+
"body-parser": "^1.17.1",
35+
"koa": "^2.2.0",
36+
"koa-bodyparser": "^3.2.0",
37+
"koa-router": "^7.0.1",
38+
"mysql": "^2.12.0",
39+
"reflect-metadata": "^0.1.9",
40+
"typeorm": "0.0.9"
41+
},
42+
"scripts": {
43+
"start": "tsc && node src/index.js"
44+
}
45+
}

src/controller/PostGetAllAction.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {Context} from "koa";
2+
import {getEntityManager} from "typeorm";
3+
import {Post} from "../entity/Post";
4+
5+
/**
6+
* Loads all posts from the database.
7+
*/
8+
export async function postGetAllAction(context: Context) {
9+
10+
// get a post repository to perform operations with post
11+
const postRepository = getEntityManager().getRepository(Post);
12+
13+
// load a post by a given post id
14+
const posts = await postRepository.find();
15+
16+
// return loaded posts
17+
context.body = posts;
18+
}

src/controller/PostGetByIdAction.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Context} from "koa";
2+
import {getEntityManager} from "typeorm";
3+
import {Post} from "../entity/Post";
4+
5+
/**
6+
* Loads post by a given id.
7+
*/
8+
export async function postGetByIdAction(context: Context) {
9+
10+
// get a post repository to perform operations with post
11+
const postRepository = getEntityManager().getRepository(Post);
12+
13+
// load a post by a given post id
14+
const post = await postRepository.findOneById((context as any).params.id);
15+
16+
// if post was not found return 404 to the client
17+
if (!post) {
18+
context.status = 404;
19+
return;
20+
}
21+
22+
// return loaded post
23+
context.body = post;
24+
}

src/controller/PostSaveAction.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {Context} from "koa";
2+
import {getEntityManager} from "typeorm";
3+
import {Post} from "../entity/Post";
4+
5+
/**
6+
* Saves given post.
7+
*/
8+
export async function postSaveAction(context: Context) {
9+
10+
// get a post repository to perform operations with post
11+
const postRepository = getEntityManager().getRepository(Post);
12+
13+
// create a real post object from post json object sent over http
14+
const newPost = postRepository.create(context.request.body);
15+
16+
// save received post
17+
await postRepository.persist(newPost);
18+
19+
// return saved post back
20+
context.body = newPost;
21+
}

src/entity/Category.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Table, PrimaryColumn, Column} from "typeorm";
2+
3+
@Table()
4+
export class Category {
5+
6+
@PrimaryColumn("int", { generated: true })
7+
id: number;
8+
9+
@Column()
10+
name: string;
11+
12+
}

src/entity/Post.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Table, PrimaryColumn, Column, ManyToMany, JoinTable} from "typeorm";
2+
import {Category} from "./Category";
3+
4+
@Table()
5+
export class Post {
6+
7+
@PrimaryColumn("int", { generated: true })
8+
id: number;
9+
10+
@Column()
11+
title: string;
12+
13+
@Column("text")
14+
text: string;
15+
16+
@ManyToMany(type => Category, {
17+
cascadeInsert: true
18+
})
19+
@JoinTable()
20+
categories: Category[];
21+
22+
}

src/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import "reflect-metadata";
2+
import {createConnection} from "typeorm";
3+
import * as Koa from "koa";
4+
import * as Router from "koa-router";
5+
import * as bodyParser from "koa-bodyparser";
6+
import {AppRoutes} from "./routes";
7+
8+
// create connection with database
9+
// note that its not active database connection
10+
// TypeORM creates you connection pull to uses connections from pull on your requests
11+
createConnection().then(async connection => {
12+
13+
// create koa app
14+
const app = new Koa();
15+
const router = new Router();
16+
17+
// register all application routes
18+
AppRoutes.forEach(route => router[route.method](route.path, route.action));
19+
20+
// run app
21+
app.use(bodyParser());
22+
app.use(router.routes());
23+
app.use(router.allowedMethods());
24+
app.listen(3000);
25+
26+
console.log("Koa application is up and running on port 3000");
27+
28+
}).catch(error => console.log("TypeORM connection error: ", error));

0 commit comments

Comments
 (0)