Skip to content

Commit c71334e

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

File tree

12 files changed

+259
-0
lines changed

12 files changed

+259
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
build/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TypeDI Service container integration with TypeORM

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "typeorm-typedi-extensions",
3+
"version": "0.0.1",
4+
"description": "Dependancy injection and service container integration with TypeORM using typedi library.",
5+
"license": "MIT",
6+
"readmeFilename": "README.md",
7+
"author": {
8+
"name": "Umed Khudoiberdiev",
9+
"email": "[email protected]"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/typeorm/typeorm-typedi-extensions.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/typeorm/typeorm-typedi-extensions/issues"
17+
},
18+
"tags": [
19+
"orm",
20+
"typescript",
21+
"typescript-orm",
22+
"typeorm-typedi-extensions",
23+
"typeorm-typedi"
24+
],
25+
"devDependencies": {
26+
"pg": "^6.1.0",
27+
"reflect-metadata": "^0.1.8",
28+
"typedi": "^0.4.2",
29+
"typeorm": "^0.0.2-alpha.62",
30+
"typescript": "^2.0.2"
31+
},
32+
"peerDependencies": {
33+
"typeorm": "^0.0.2-alpha.62",
34+
"typedi": "^0.4.2"
35+
}
36+
}

sample/sample1-simple-usage/app.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import "reflect-metadata";
2+
import {createConnection, useContainer} from "typeorm";
3+
import {Container} from "typedi";
4+
import {PostRepository} from "./repository/PostRepository";
5+
import {Post} from "./entity/Post";
6+
7+
useContainer(Container);
8+
createConnection({
9+
driver: {
10+
type: "postgres",
11+
host: "localhost",
12+
port: 5432,
13+
username: "test",
14+
password: "admin",
15+
database: "test"
16+
},
17+
entities: [
18+
__dirname + "/entity/*.js"
19+
],
20+
autoSchemaSync: true
21+
}).then(async connection => {
22+
console.log("connected");
23+
24+
const post1 = new Post();
25+
post1.title = "TypeScript 2.0";
26+
post1.text = `New TypeScript version adds control flow based type analysis features.`;
27+
28+
const post2 = new Post();
29+
post2.title = "Control flow based type analysis";
30+
post2.text = `TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.`;
31+
32+
const repository = Container.get(PostRepository);
33+
await Promise.all([
34+
repository.saveUsingRepository(post1),
35+
repository.saveUsingManager(post2)
36+
]);
37+
38+
console.log("Saved successfully.");
39+
40+
const loadedPosts = await repository.findAll();
41+
console.log("All loaded posts: ", loadedPosts);
42+
43+
}).catch(error => console.log("Error: ", error));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Table, PrimaryColumn, Column} from "typeorm";
2+
3+
@Table()
4+
export class Post {
5+
6+
@PrimaryColumn("int", { generated: true })
7+
id: number;
8+
9+
@Column()
10+
title: string;
11+
12+
@Column()
13+
text: string;
14+
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {Repository, EntityManager} from "typeorm";
2+
import {Service} from "typedi";
3+
import {Post} from "../entity/Post";
4+
import {OrmRepository} from "../../../src/decorators/OrmRepository";
5+
import {OrmEntityManager} from "../../../src/decorators/OrmEntityManager";
6+
7+
@Service()
8+
export class PostRepository {
9+
10+
@OrmEntityManager()
11+
private entityManager: EntityManager;
12+
13+
constructor(@OrmRepository(Post) private ormRepository: Repository<Post>) {
14+
}
15+
16+
saveUsingRepository(post: Post) {
17+
return this.ormRepository.persist(post);
18+
}
19+
20+
saveUsingManager(post: Post) {
21+
return this.entityManager.persist(post);
22+
}
23+
24+
findAll() {
25+
return this.ormRepository.find();
26+
}
27+
28+
}

src/decorators/OrmEntityManager.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {ConnectionManager} from "typeorm";
2+
import {Container} from "typedi";
3+
4+
/**
5+
* Allows to inject an EntityManager using typedi's Container.
6+
*/
7+
export function OrmEntityManager(connectionName: string = "default"): Function {
8+
return function(target: Object|Function, propertyName: string, index?: number) {
9+
10+
const getValue = () => {
11+
const connectionManager = Container.get(ConnectionManager);
12+
if (!connectionManager.has(connectionName))
13+
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
14+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
15+
`in your application before you established a connection and importing any entity.`);
16+
17+
const connection = connectionManager.get(connectionName);
18+
const entityManager = connection.entityManager;
19+
if (!entityManager)
20+
throw new Error(`Entity manager was not found on "${connectionName}" connection. ` +
21+
`Make sure you correctly setup connection and container usage.`);
22+
23+
return entityManager;
24+
};
25+
26+
if (index !== undefined) {
27+
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
28+
} else {
29+
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
30+
}
31+
};
32+
}

src/decorators/OrmRepository.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {ConnectionManager} from "typeorm";
2+
import {Container} from "typedi";
3+
4+
/**
5+
* Allows to inject a Repository using typedi's Container.
6+
*/
7+
export function OrmRepository(cls: Function, connectionName: string = "default"): Function {
8+
return function(target: Object|Function, propertyName: string, index?: number) {
9+
10+
const getValue = () => {
11+
const connectionManager = Container.get(ConnectionManager);
12+
if (!connectionManager.has(connectionName))
13+
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
14+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
15+
`in your application before you established a connection and importing any entity.`);
16+
17+
const connection = connectionManager.get(connectionName);
18+
return connection.getRepository(cls as any);
19+
};
20+
21+
if (index !== undefined) {
22+
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
23+
} else {
24+
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
25+
}
26+
};
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {ConnectionManager} from "typeorm";
2+
import {Container} from "typedi";
3+
4+
/**
5+
* Allows to inject a SpecificRepository using typedi's Container.
6+
*/
7+
export function OrmSpecificRepository(cls: Function, connectionName: string = "default"): Function {
8+
return function(target: Object|Function, propertyName: string, index?: number) {
9+
10+
const getValue = () => {
11+
const connectionManager = Container.get(ConnectionManager);
12+
if (!connectionManager.has(connectionName))
13+
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
14+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
15+
`in your application before you established a connection and importing any entity.`);
16+
17+
const connection = connectionManager.get(connectionName);
18+
return connection.getSpecificRepository(cls as any);
19+
};
20+
21+
if (index !== undefined) {
22+
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
23+
} else {
24+
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
25+
}
26+
};
27+
}

src/decorators/OrmTreeRepository.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {ConnectionManager} from "typeorm";
2+
import {Container} from "typedi";
3+
4+
/**
5+
* Allows to inject a TreeRepository using typedi's Container.
6+
*/
7+
export function OrmTreeRepository(cls: Function, connectionName: string = "default"): Function {
8+
return function(target: Object|Function, propertyName: string, index?: number) {
9+
10+
const getValue = () => {
11+
const connectionManager = Container.get(ConnectionManager);
12+
if (!connectionManager.has(connectionName))
13+
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
14+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
15+
`in your application before you established a connection and importing any entity.`);
16+
17+
const connection = connectionManager.get(connectionName);
18+
return connection.getTreeRepository(cls as any);
19+
};
20+
21+
if (index !== undefined) {
22+
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
23+
} else {
24+
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
25+
}
26+
};
27+
}

0 commit comments

Comments
 (0)