Skip to content

Commit 2a374ed

Browse files
refactor: format code with prettier
1 parent d5644a0 commit 2a374ed

File tree

10 files changed

+207
-197
lines changed

10 files changed

+207
-197
lines changed

sample/sample1-simple-usage/app.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
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";
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';
66

77
useContainer(Container);
88
createConnection({
9-
type: "mysql",
10-
host: "localhost",
11-
port: 3306,
12-
username: "test",
13-
password: "test",
14-
database: "test",
15-
entities: [
16-
Post
17-
],
18-
synchronize: true
19-
}).then(async connection => {
20-
console.log("connected");
9+
type: 'mysql',
10+
host: 'localhost',
11+
port: 3306,
12+
username: 'test',
13+
password: 'test',
14+
database: 'test',
15+
entities: [Post],
16+
synchronize: true,
17+
})
18+
.then(async connection => {
19+
console.log('connected');
2120

2221
const post1 = new Post();
23-
post1.title = "TypeScript 2.0";
22+
post1.title = 'TypeScript 2.0';
2423
post1.text = `New TypeScript version adds control flow based type analysis features.`;
2524

2625
const post2 = new Post();
27-
post2.title = "Control flow based type analysis";
26+
post2.title = 'Control flow based type analysis';
2827
post2.text = `TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.`;
2928

3029
const repository = Container.get(PostRepository);
31-
await Promise.all([
32-
repository.saveUsingRepository(post1),
33-
repository.saveUsingManager(post2)
34-
]);
30+
await Promise.all([repository.saveUsingRepository(post1), repository.saveUsingManager(post2)]);
3531

36-
console.log("Saved successfully.");
32+
console.log('Saved successfully.');
3733

3834
const loadedPosts = await repository.findAll();
39-
console.log("All loaded posts: ", loadedPosts);
40-
41-
}).catch(error => console.log("Error: ", error));
35+
console.log('All loaded posts: ', loadedPosts);
36+
})
37+
.catch(error => console.log('Error: ', error));
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
1+
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
22

33
@Entity()
44
export class Post {
5+
@PrimaryGeneratedColumn()
6+
id: number;
57

6-
@PrimaryGeneratedColumn()
7-
id: number;
8+
@Column()
9+
title: string;
810

9-
@Column()
10-
title: string;
11-
12-
@Column()
13-
text: string;
14-
15-
}
11+
@Column()
12+
text: string;
13+
}
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
import {EntityManager, Repository} from "typeorm";
2-
import {Service} from "typedi";
1+
import { EntityManager, Repository } from 'typeorm';
2+
import { Service } from 'typedi';
33

4-
import {Post} from "../entity/Post";
5-
import {InjectRepository} from "../../../src/decorators/InjectRepository";
6-
import {InjectManager} from "../../../src/decorators/InjectManager";
4+
import { Post } from '../entity/Post';
5+
import { InjectRepository } from '../../../src/decorators/InjectRepository';
6+
import { InjectManager } from '../../../src/decorators/InjectManager';
77

88
@Service()
99
export class PostRepository {
10+
@InjectManager()
11+
private entityManager: EntityManager;
1012

11-
@InjectManager()
12-
private entityManager: EntityManager;
13+
constructor(@InjectRepository(Post) private InjectRepository: Repository<Post>) {}
1314

14-
constructor(@InjectRepository(Post) private InjectRepository: Repository<Post>) {
15-
}
15+
saveUsingRepository(post: Post) {
16+
return this.InjectRepository.save(post);
17+
}
1618

17-
saveUsingRepository(post: Post) {
18-
return this.InjectRepository.save(post);
19-
}
19+
saveUsingManager(post: Post) {
20+
return this.entityManager.save(post);
21+
}
2022

21-
saveUsingManager(post: Post) {
22-
return this.entityManager.save(post);
23-
}
24-
25-
findAll() {
26-
return this.InjectRepository.find();
27-
}
28-
29-
}
23+
findAll() {
24+
return this.InjectRepository.find();
25+
}
26+
}

src/decorators/InjectConnection.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
import {ConnectionManager} from "typeorm";
2-
import {Container} from "typedi";
1+
import { ConnectionManager } from 'typeorm';
2+
import { Container, Constructable } from 'typedi';
33

44
/**
55
* Allows to inject an Connection using typedi's Container.
66
*/
7-
export function InjectConnection(connectionName: string = "default"): Function {
8-
return function(object: Object|Function, propertyName: string, index?: number) {
9-
Container.registerHandler({ object, index, propertyName, value: () => {
10-
const connectionManager = Container.get(ConnectionManager);
11-
if (!connectionManager.has(connectionName))
12-
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
13-
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
14-
`in your application before you established a connection and importing any entity.`);
7+
export function InjectConnection(connectionName: string = 'default'): Function {
8+
return function (object: Object | Function, propertyName: string, index?: number) {
9+
Container.registerHandler({
10+
object: object as Constructable<unknown>,
11+
index,
12+
propertyName,
13+
value: () => {
14+
const connectionManager = Container.get(ConnectionManager);
15+
if (!connectionManager.has(connectionName))
16+
throw new Error(
17+
`Cannot get connection "${connectionName}" from the connection manager. ` +
18+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
19+
`in your application before you established a connection and importing any entity.`
20+
);
1521

16-
return connectionManager.get(connectionName);
17-
}});
18-
};
22+
return connectionManager.get(connectionName);
23+
},
24+
});
25+
};
1926
}

src/decorators/InjectManager.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
import {ConnectionManager} from "typeorm";
2-
import {Container} from "typedi";
1+
import { ConnectionManager } from 'typeorm';
2+
import { Constructable, Container } from 'typedi';
33

44
/**
55
* Allows to inject an EntityManager using typedi's Container.
66
*/
7-
export function InjectManager(connectionName: string = "default"): Function {
8-
return function(object: Object|Function, propertyName: string, index?: number) {
9-
Container.registerHandler({ object, index, propertyName, value: () => {
10-
const connectionManager = Container.get(ConnectionManager);
11-
if (!connectionManager.has(connectionName))
12-
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
13-
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
14-
`in your application before you established a connection and importing any entity.`);
7+
export function InjectManager(connectionName: string = 'default'): Function {
8+
return function (object: Object | Function, propertyName: string, index?: number) {
9+
Container.registerHandler({
10+
object: object as Constructable<unknown>,
11+
index,
12+
propertyName,
13+
value: () => {
14+
const connectionManager = Container.get(ConnectionManager);
15+
if (!connectionManager.has(connectionName))
16+
throw new Error(
17+
`Cannot get connection "${connectionName}" from the connection manager. ` +
18+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
19+
`in your application before you established a connection and importing any entity.`
20+
);
1521

16-
const connection = connectionManager.get(connectionName);
17-
const entityManager = connection.manager;
18-
if (!entityManager)
19-
throw new Error(`Entity manager was not found on "${connectionName}" connection. ` +
20-
`Make sure you correctly setup connection and container usage.`);
22+
const connection = connectionManager.get(connectionName);
23+
const entityManager = connection.manager;
24+
if (!entityManager)
25+
throw new Error(
26+
`Entity manager was not found on "${connectionName}" connection. ` +
27+
`Make sure you correctly setup connection and container usage.`
28+
);
2129

22-
return entityManager;
23-
}});
24-
};
30+
return entityManager;
31+
},
32+
});
33+
};
2534
}

0 commit comments

Comments
 (0)