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

feat(): Add support for typeorm@0.3.0 literal repositories #383 #384

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions lib/common/typeorm.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import { DEFAULT_CONNECTION_NAME } from '../typeorm.constants';

const logger = new Logger('TypeOrmModule');

/**
* TypeORM 0.3.0 possible literal repository types.
*
* @see https://github.com/typeorm/typeorm/blob/b177e230a6fcb11f1eb71d4d431d0297436b7f6f/src/repository/Repository.ts#L29
*/
const typeormLiteralRepositoryTypes = [
'Repository',
'MongoRepository',
'TreeRepository',
];

export function isLiteralRepository(value: any) {
return typeormLiteralRepositoryTypes.includes(value.typeof);
}

/**
* This function generates an injection token for an Entity or Repository
* @param {Function} This parameter can either be an Entity or Repository
Expand All @@ -29,13 +44,37 @@ export function getRepositoryToken(
throw new CircularDependencyException('@InjectRepository()');
}
const connectionPrefix = getConnectionPrefix(connection);
const isTypeormNext = typeof Repository !== 'function';
let name;

if (
entity.prototype instanceof Repository ||
entity.prototype instanceof AbstractRepository
!isTypeormNext &&
(entity.prototype instanceof Repository ||
entity.prototype instanceof AbstractRepository)
) {
// TypeORM pre-0.3.0 repository class
return `${connectionPrefix}${getCustomRepositoryToken(entity)}`;
} else if (isTypeormNext && isLiteralRepository(entity)) {
// TypeORM 0.3.0+ literal repository
const repository: { target: Function | string } = entity as any;
name =
typeof repository.target === 'string'
? repository.target
: repository.target.name;
} else if (isTypeormNext) {
/**
* TypeORM 0.3.0+ entity can be different types - function, string or
* literal with `name` property.
*
* @see https://github.com/typeorm/typeorm/blob/b177e230a6fcb11f1eb71d4d431d0297436b7f6f/src/common/EntityTarget.ts#L7
*/
name = typeof entity === 'string' ? entity : entity.name;
} else {
// TypeORM pre-0.3.0 entity class
name = entity.name;
}
return `${connectionPrefix}${entity.name}Repository`;

return `${connectionPrefix}${name}Repository`;
}

/**
Expand Down Expand Up @@ -113,12 +152,13 @@ export function handleRetry(
): <T>(source: Observable<T>) => Observable<T> {
return <T>(source: Observable<T>) =>
source.pipe(
retryWhen(e =>
retryWhen((e) =>
e.pipe(
scan((errorCount, error: Error) => {
logger.error(
`Unable to connect to the database. Retrying (${errorCount +
1})...`,
`Unable to connect to the database. Retrying (${
errorCount + 1
})...`,
error.stack,
);
if (errorCount + 1 >= retryAttempts) {
Expand Down
7 changes: 4 additions & 3 deletions lib/typeorm.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export function createTypeOrmProviders(
entities?: Function[],
connection?: Connection | ConnectionOptions | string,
): Provider[] {
return (entities || []).map(entity => ({
return (entities || []).map((entity) => ({
provide: getRepositoryToken(entity, connection),
useFactory: (connection: Connection) => {
if (
entity.prototype instanceof Repository ||
entity.prototype instanceof AbstractRepository
typeof Repository === 'function' &&
(entity.prototype instanceof Repository ||
entity.prototype instanceof AbstractRepository)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw what about custom repositories in 0.3.0. Are they going to be deprecated/removed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can extend a repository with custom methods by calling extend() on a repository instance:

repository.extend({
  customMethod() {
     // ...
  }
});

https://github.com/typeorm/typeorm/blob/b177e230a6fcb11f1eb71d4d431d0297436b7f6f/src/repository/Repository.ts#L380

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kamilmysliwiec now (0.3.0) @EntityRepository() is deprecated. Here's how we should use instead: https://github.com/typeorm/typeorm/blob/master/docs/custom-repository.md

) {
return connection.getCustomRepository(entity);
}
Expand Down