Skip to content

Commit

Permalink
fix(ts-morph): fix discovery of IdentifiedReference with ts-morph
Browse files Browse the repository at this point in the history
Closes #1088
  • Loading branch information
B4nan committed Nov 18, 2020
1 parent 9d855eb commit d94bd91
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export {
Constructor, Dictionary, PrimaryKeyType, PrimaryKeyProp, Primary, IPrimaryKey, FilterQuery, IWrappedEntity, EntityName, EntityData, Highlighter,
AnyEntity, EntityProperty, EntityMetadata, QBFilterQuery, PopulateOptions, Populate, Loaded, New, LoadedReference, LoadedCollection,
GetRepository, EntityRepositoryType, MigrationObject, DeepPartial, PrimaryProperty,
GetRepository, EntityRepositoryType, MigrationObject, DeepPartial, PrimaryProperty, Cast, IsUnknown,
} from './typings';
export * from './enums';
export * from './errors';
Expand Down
4 changes: 3 additions & 1 deletion packages/reflection/src/TsMorphMetadataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class TsMorphMetadataProvider extends MetadataProvider {
}

this.processWrapper(prop, 'IdentifiedReference');
this.processWrapper(prop, 'Reference');
this.processWrapper(prop, 'Collection');
}

Expand Down Expand Up @@ -111,7 +112,8 @@ export class TsMorphMetadataProvider extends MetadataProvider {
}

private processWrapper(prop: EntityProperty, wrapper: string): void {
const m = prop.type.match(new RegExp(`^${wrapper}<(\\w+),?.*>$`));
const type = prop.type.replace(/import\(.*\)\./g, '');
const m = type.match(new RegExp(`^${wrapper}<(\\w+),?.*>$`));

if (!m) {
return;
Expand Down
4 changes: 2 additions & 2 deletions tests/reflection/TsMorphMetadataProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { MongoDriver } from '@mikro-orm/mongodb';
import { Collection as Collection_, MikroORM, Options, Reference as Reference_, ReferenceType } from '@mikro-orm/core';
import { AnyEntity, Collection as Collection_, MikroORM, Options, PrimaryProperty, Reference as Reference_, ReferenceType, Cast, IsUnknown } from '@mikro-orm/core';
import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
import { Author, Book, Publisher, BaseEntity, BaseEntity3, BookTagSchema, Test, FooBaz } from './entities';
import FooBar from './entities/FooBar';

// we need to define those to get around typescript issues with reflection (ts-morph would return `any` for the type otherwise)
export class Collection<T> extends Collection_<T> { }
export class Reference<T> extends Reference_<T> { }
export type IdentifiedReference<T, PK extends keyof T = 'id' & keyof T> = { [K in PK]: T[K] } & Reference<T>;
export type IdentifiedReference<T extends AnyEntity<T>, PK extends keyof T | unknown = PrimaryProperty<T>> = true extends IsUnknown<PK> ? Reference<T> : ({ [K in Cast<PK, keyof T>]?: T[K] } & Reference<T>);

describe('TsMorphMetadataProvider', () => {

Expand Down

0 comments on commit d94bd91

Please sign in to comment.