Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest Nest version #18

Merged
merged 12 commits into from
May 13, 2018
Merged
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ npm-debug.log
/.nyc_output

# dist
/dist
dist
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# source
lib
index.ts
package-lock.json
tslint.json
tsconfig.json
.prettierrc
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"trailingComma": "none",
"singleQuote": true
}
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[travis-url]: https://travis-ci.org/nestjs/nest
[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
[linux-url]: https://travis-ci.org/nestjs/nest

<p align="center">A progressive <a href="http://nodejs.org" target="blank">Node.js</a> framework for building efficient and scalable server-side applications, heavily inspired by <a href="https://angular.io" target="blank">Angular</a>.</p>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
Expand All @@ -19,7 +19,6 @@
<a href="https://opencollective.com/nest#backer"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec"><img src="https://img.shields.io/badge/Donate-PayPal-dc3d53.svg"/></a>
<img src="https://img.shields.io/badge/👌-Production Ready-78c7ff.svg"/>
<a href="https://twitter.com/nestframework"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
</p>
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
Expand All @@ -41,14 +40,14 @@ $ npm i --save @nestjs/typeorm typeorm

## Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://opencollective.com/nest).
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).

## Stay in touch

- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)
* Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
* Website - [https://nestjs.com](https://nestjs.com/)
* Twitter - [@nestframework](https://twitter.com/nestframework)

## License

Nest is [MIT licensed](LICENSE).
Nest is [MIT licensed](LICENSE).
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist';
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
__export(require("./dist"));
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist';
18 changes: 11 additions & 7 deletions lib/typeorm-core.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { Module, DynamicModule, Global } from '@nestjs/common';
import { ConnectionOptions, Connection, createConnection } from 'typeorm';
import {
ConnectionOptions,
Connection,
createConnection
} from 'typeorm';
import { getConnectionToken, getEntityManagerToken } from "./typeorm.utils";
getConnectionToken,
getEntityManagerToken,
handleRetry,
} from './typeorm.utils';
import { fromPromise } from 'rxjs/observable/fromPromise';

@Global()
@Module({})
export class TypeOrmCoreModule {
static forRoot(options?: ConnectionOptions): DynamicModule {
const connectionProvider = {
provide: getConnectionToken(options),
useFactory: async () => await createConnection(options),
useFactory: async () =>
await fromPromise(createConnection(options))
.pipe(handleRetry)
.toPromise(),
};
const entityManagerProvider = {
provide: getEntityManagerToken(options),
Expand All @@ -21,7 +25,7 @@ export class TypeOrmCoreModule {
};
return {
module: TypeOrmCoreModule,
components: [entityManagerProvider, connectionProvider],
providers: [entityManagerProvider, connectionProvider],
exports: [entityManagerProvider, connectionProvider],
};
}
Expand Down
22 changes: 16 additions & 6 deletions lib/typeorm.decorators.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { Inject } from '@nestjs/common';
import { Connection, ConnectionOptions } from "typeorm";
import { Connection, ConnectionOptions } from 'typeorm';

import { getRepositoryToken, getConnectionToken, getEntityManagerToken } from './typeorm.utils';
import {
getRepositoryToken,
getConnectionToken,
getEntityManagerToken,
} from './typeorm.utils';

export const InjectRepository = (entity: Function) =>
Inject(getRepositoryToken(entity));

export const InjectConnection: (connection?: Connection|ConnectionOptions|string) => ParameterDecorator = (connection?: Connection|ConnectionOptions|string) =>
Inject(getConnectionToken(connection));
export const InjectConnection: (
connection?: Connection | ConnectionOptions | string,
) => ParameterDecorator = (
connection?: Connection | ConnectionOptions | string,
) => Inject(getConnectionToken(connection));

export const InjectEntityManager: (connection?: Connection|ConnectionOptions|string) => ParameterDecorator = (connection?: Connection|ConnectionOptions|string) =>
Inject(getEntityManagerToken(connection));
export const InjectEntityManager: (
connection?: Connection | ConnectionOptions | string,
) => ParameterDecorator = (
connection?: Connection | ConnectionOptions | string,
) => Inject(getEntityManagerToken(connection));
9 changes: 6 additions & 3 deletions lib/typeorm.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ export class TypeOrmModule {
const providers = createTypeOrmProviders();
return {
module: TypeOrmModule,
modules: [TypeOrmCoreModule.forRoot(options)],
imports: [TypeOrmCoreModule.forRoot(options)],
};
}

static forFeature(entities: Function[] = [], connection: Connection|ConnectionOptions|string = 'default'): DynamicModule {
static forFeature(
entities: Function[] = [],
connection: Connection | ConnectionOptions | string = 'default',
): DynamicModule {
const providers = createTypeOrmProviders(entities, connection);
return {
module: TypeOrmModule,
components: providers,
providers: providers,
exports: providers,
};
}
Expand Down
26 changes: 20 additions & 6 deletions lib/typeorm.providers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import { Connection, Repository } from 'typeorm';

import {
ConnectionOptions,
Connection,
Repository,
AbstractRepository,
} from 'typeorm';
import { getConnectionToken, getRepositoryToken } from './typeorm.utils';

export function createTypeOrmProviders(entities?: Function[], connection?: Connection|ConnectionOptions|string) {
const getRepository = (connection: Connection, entity) =>
connection.options.type === 'mongodb'
export function createTypeOrmProviders(
entities?: Function[],
connection?: Connection | ConnectionOptions | string,
) {
const getRepository = (connection: Connection, entity) => {
if (
entity.prototype instanceof Repository ||
entity.prototype instanceof AbstractRepository
) {
return connection.getCustomRepository(entity);
}
return connection.options.type === 'mongodb'
? connection.getMongoRepository(entity)
: connection.getRepository(entity);
};

const getCustomRepository = (connection: Connection, entity) =>
connection.getCustomRepository(entity);

const repositories = (entities || []).map((entity) => ({
const repositories = (entities || []).map(entity => ({
provide: getRepositoryToken(entity),
useFactory: (connection: Connection) => {
if (entity.prototype instanceof Repository) {
Expand Down
54 changes: 44 additions & 10 deletions lib/typeorm.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Connection, ConnectionOptions, EntityManager } from 'typeorm';
import { Observable } from 'rxjs/Observable';
import { retryWhen, scan, delay } from 'rxjs/operators';
import { Logger } from '@nestjs/common';

export function getRepositoryToken(entity: Function) {
return `${entity.name}Repository`;
Expand All @@ -10,11 +13,16 @@ export function getRepositoryToken(entity: Function) {
* a Connection, or a ConnectionOptions or a string.
* @returns {string | Function} The Connection injection token.
*/
export function getConnectionToken(connection: Connection|ConnectionOptions|string = 'default'): string|Function {
return 'default' === connection ? Connection
: 'string' === typeof connection ? `${connection}Connection`
: 'default' === connection.name || !connection.name ? Connection
: `${connection.name}Connection`;
export function getConnectionToken(
connection: Connection | ConnectionOptions | string = 'default',
): string | Function {
return 'default' === connection
? Connection
: 'string' === typeof connection
? `${connection}Connection`
: 'default' === connection.name || !connection.name
? Connection
: `${connection.name}Connection`;
}

/**
Expand All @@ -23,9 +31,35 @@ export function getConnectionToken(connection: Connection|ConnectionOptions|stri
* a Connection, or a ConnectionOptions or a string.
* @returns {string | Function} The EntityManager injection token.
*/
export function getEntityManagerToken(connection: Connection|ConnectionOptions|string = 'default'): string|Function {
return 'default' === connection ? EntityManager
: 'string' === typeof connection ? `${connection}EntityManager`
: 'default' === connection.name || !connection.name ? EntityManager
export function getEntityManagerToken(
connection: Connection | ConnectionOptions | string = 'default',
): string | Function {
return 'default' === connection
? EntityManager
: 'string' === typeof connection
? `${connection}EntityManager`
: 'default' === connection.name || !connection.name
? EntityManager
: `${connection.name}EntityManager`;
}
}

export function handleRetry<T>(source: Observable<T>): Observable<T> {
return source.pipe(
retryWhen(e =>
e.pipe(
scan((errorCount, error) => {
Logger.error(
`Unable to connect to the database. Retrying (${errorCount + 1})...`,
'',
'TypeOrmModule',
);
if (errorCount >= 10) {
throw error;
}
return errorCount + 1;
}, 0),
delay(3000),
),
),
);
}