Skip to content

Commit fadb8f2

Browse files
author
Umed Khudoiberdiev
committed
initial commit
0 parents  commit fadb8f2

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Example how to use TypeORM with TypeScript
2+
3+
1. clone repository
4+
2. run `npm i`
5+
3. run `npm start`
6+
4. enjoy!

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "typeorm-typescript-example",
3+
"version": "0.0.1",
4+
"description": "Example how to use 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-example.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/typeorm/typescript-example/issues"
17+
},
18+
"tags": [
19+
"orm",
20+
"typescript",
21+
"typescript-orm",
22+
"typeorm-sample",
23+
"typeorm-example"
24+
],
25+
"devDependencies": {
26+
"typescript": "^2.0.2"
27+
},
28+
"dependencies": {
29+
"pg": "^6.1.0",
30+
"reflect-metadata": "^0.1.8",
31+
"typeorm": "0.0.2-alpha.62"
32+
},
33+
"scripts": {
34+
"start": "tsc && node src/index.js"
35+
}
36+
}

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
@JoinTable()
18+
categories: Category[];
19+
20+
}

src/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import "reflect-metadata";
2+
import {createConnection} from "typeorm";
3+
import {Post} from "./entity/Post";
4+
import {Category} from "./entity/Category";
5+
6+
createConnection({
7+
driver: {
8+
type: "postgres",
9+
host: "localhost",
10+
port: 5432,
11+
username: "test",
12+
password: "admin",
13+
database: "test"
14+
},
15+
entities: [
16+
Post,
17+
Category
18+
],
19+
autoSchemaCreate: true
20+
}).then(async connection => {
21+
22+
const category1 = new Category();
23+
category1.name = "TypeScript";
24+
25+
const category2 = new Category();
26+
category2.name = "Programming";
27+
28+
const post = new Post();
29+
post.title = "Control flow based type analysis";
30+
post.text = `TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.`;
31+
post.categories = [category1, category2];
32+
33+
const postRepository = connection.getRepository(Post);
34+
await postRepository.persist(post);
35+
36+
console.log("Post has been saved: ", post);
37+
38+
}).catch(error => console.log("Error: ", error));

tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "2.0.2",
3+
"compilerOptions": {
4+
"lib": ["es5", "es6", "dom"],
5+
"target": "es6",
6+
"module": "commonjs",
7+
"moduleResolution": "node",
8+
"emitDecoratorMetadata": true,
9+
"experimentalDecorators": true,
10+
"sourceMap": true
11+
},
12+
"exclude": [
13+
"build",
14+
"node_modules",
15+
"typings/browser.d.ts",
16+
"typings/browser"
17+
]
18+
}

0 commit comments

Comments
 (0)