-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge feat-typeorm-create-connection
- Loading branch information
Showing
17 changed files
with
258 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
import {Configuration, registerProvider} from "@tsed/di"; | ||
import {Connection, ConnectionOptions, getConnectionManager} from "typeorm"; | ||
import {createConnection} from "@tsed/typeorm"; | ||
import {Connection, ConnectionOptions} from "typeorm"; | ||
|
||
export const CONNECTION = Symbol.for("CONNECTION"); // declare your own | ||
export const CONNECTION = Symbol.for("CONNECTION"); // declare your own symbol | ||
export type CONNECTION = Connection; // Set alias types (optional) | ||
|
||
const CONNECTION_NAME = "default"; // change the name according to your server configuration | ||
|
||
registerProvider({ | ||
provide: CONNECTION, | ||
deps: [Configuration], | ||
async useAsyncFactory(configuration: Configuration) { | ||
const settings = configuration.get<ConnectionOptions>("customSettings")!; | ||
|
||
const connection = getConnectionManager().create(settings!); | ||
await connection.connect(); | ||
|
||
// Add hook to close connection when server is killed | ||
// @ts-ignore | ||
connection.$onDestroy = () => connection.close(); | ||
const settings = configuration.get<ConnectionOptions[]>("typeorm")!; | ||
const connectionOptions = settings.find(o => o.name === CONNECTION_NAME); | ||
|
||
return connection; | ||
return createConnection(connectionOptions!); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {EntityRepository} from "@tsed/typeorm"; | ||
import {Repository} from "typeorm"; | ||
import {User} from "../entity/User"; | ||
|
||
@EntityRepository(User) | ||
export class UserRepository extends Repository<User> { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
docs/tutorials/snippets/typeorm/typeorm-injection-entity-repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {Inject, Injectable} from "@tsed/di"; | ||
import {UserRepository} from "./repository/UserRepository"; | ||
|
||
@Injectable() | ||
export class OtherService { | ||
constructor(userRepository: UserRepository) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {applyDecorators, Type} from "@tsed/core"; | ||
import {Configuration, registerProvider} from "@tsed/di"; | ||
import {EntityRepository as TypeORMEntityRepository, getCustomRepository} from "typeorm"; | ||
import {TypeORMModule} from "../TypeORMModule"; | ||
|
||
/** | ||
* Declare a new typeORM EntityRepository which can injected to TsED service. | ||
* @param model | ||
* @decorator | ||
*/ | ||
export function EntityRepository(model: any) { | ||
return applyDecorators(TypeORMEntityRepository(model), (target: Type<any>) => { | ||
registerProvider({ | ||
provide: target, | ||
imports: [TypeORMModule], | ||
deps: [Configuration], | ||
useFactory(configuration: Configuration) { | ||
if (configuration.get("typeorm")) { | ||
return getCustomRepository(target); | ||
} | ||
|
||
return {}; | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {ConnectionOptions, getConnectionManager} from "typeorm"; | ||
|
||
const connections = new Map(); | ||
|
||
export async function createConnection(connectionOptions: ConnectionOptions) { | ||
const connectionManager = getConnectionManager(); | ||
const name = connectionOptions.name!; | ||
|
||
if (!connections.has(name)) { | ||
const connection = connectionManager.create(connectionOptions!); | ||
connections.set(name, connection.connect()); | ||
} | ||
|
||
await connections.get(name); | ||
|
||
const connection = connectionManager.get(name); | ||
|
||
// Add hook to close connection when server is killed | ||
// @ts-ignore | ||
connection.$onDestroy = connection.$onDestroy || (() => connection.isConnected && connection.close()); | ||
|
||
return connection; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {ServerLoader, ServerSettings} from "@tsed/common"; | ||
import * as Path from "path"; | ||
import "../../src/TypeORMModule"; | ||
import "./connections/ConnectionProvider"; | ||
|
||
const cookieParser = require("cookie-parser"), | ||
bodyParser = require("body-parser"), | ||
compress = require("compression"), | ||
methodOverride = require("method-override"); | ||
|
||
const rootDir = Path.resolve(__dirname); | ||
|
||
@ServerSettings({ | ||
rootDir, | ||
port: 8001, | ||
httpsPort: false, | ||
logger: { | ||
level: "info", | ||
logRequest: true | ||
} | ||
}) | ||
export class Server extends ServerLoader { | ||
public $beforeRoutesInit(): void { | ||
this | ||
.use(bodyParser.json()) | ||
.use( | ||
bodyParser.urlencoded({ | ||
extended: true | ||
}) | ||
) | ||
.use(cookieParser()) | ||
.use(compress({})) | ||
.use(methodOverride()); | ||
} | ||
} |
Oops, something went wrong.