Skip to content

Commit

Permalink
feat(core): add Rel<T> and Ref<T> relation types
Browse files Browse the repository at this point in the history
- `Rel<T>` is an identity type that can be used to get around issues
  with cycles in bidirectional relations, mainly when using ESM together
  with reflect-metadata.
- `Ref<T, PK>` is a shortcut for `IdentifiedReference`.
  • Loading branch information
B4nan committed Nov 1, 2022
1 parent 7519e68 commit 44acefb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Expand Up @@ -7,7 +7,7 @@ export {
Constructor, ConnectionType, Dictionary, PrimaryKeyType, PrimaryKeyProp, Primary, IPrimaryKey, ObjectQuery, FilterQuery, IWrappedEntity, EntityName, EntityData, Highlighter,
AnyEntity, EntityClass, EntityProperty, EntityMetadata, QBFilterQuery, PopulateOptions, Populate, Loaded, New, LoadedReference, LoadedCollection, IMigrator, IMigrationGenerator,
GetRepository, EntityRepositoryType, MigrationObject, DeepPartial, PrimaryProperty, Cast, IsUnknown, EntityDictionary, EntityDTO, MigrationDiff,
IEntityGenerator, ISeedManager, EntityClassGroup, OptionalProps, RequiredEntityData, CheckCallback, SimpleColumnMeta,
IEntityGenerator, ISeedManager, EntityClassGroup, OptionalProps, RequiredEntityData, CheckCallback, SimpleColumnMeta, Rel, Ref,
} from './typings';
export * from './enums';
export * from './errors';
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/typings.ts
Expand Up @@ -199,6 +199,13 @@ export type EntityDictionary<T> = EntityData<T> & Dictionary;
type Relation<T> = {
[P in keyof T as T[P] extends unknown[] | Record<string | number | symbol, unknown> ? P : never]?: T[P]
};

/** Identity type that can be used to get around issues with cycles in bidirectional relations. */
export type Rel<T> = T;

/** Shortcut for `IdentifiedReference`. */
export type Ref<T, PK extends keyof T | unknown = PrimaryProperty<T>> = IdentifiedReference<T, PK>;

export type EntityDTOProp<T> = T extends Scalar
? T
: T extends LoadedReference<infer U>
Expand Down

0 comments on commit 44acefb

Please sign in to comment.