-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use the public API for reading entity metadata
Rather than misusing a private decorator-specific API, this change uses the ORM's metadata storage instead. A slight issue here is that accessing the entity manager from a repository is technically not a public API, but the code doing so isn't (yet) TypeScript and needs a lot of work anyway. Refs #398, #399
- Loading branch information
1 parent
9a60750
commit e1ba4e6
Showing
6 changed files
with
37 additions
and
26 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
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
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 |
---|---|---|
@@ -1,40 +1,43 @@ | ||
import 'reflect-metadata'; | ||
import { EntityProperty, FindOptions } from '@mikro-orm/core'; | ||
import { FindOptions, MetadataStorage } from '@mikro-orm/core'; | ||
import * as entities from '../models/entities'; | ||
|
||
type Fields<T> = NonNullable<FindOptions<T>['fields']>; | ||
|
||
export function getFields<T extends keyof typeof entities>( | ||
type: T, | ||
meta: MetadataStorage, | ||
includes?: string[], | ||
recurse = 2, | ||
): Fields<T> { | ||
return Object.values(entities[type].prototype.__meta.properties).reduce< | ||
Fields<T> | ||
>((props, prop: EntityProperty<typeof entities[T]>) => { | ||
if (!(prop.hidden === true)) { | ||
if (prop.reference === 'scalar') { | ||
if ( | ||
prop.type !== 'BlobType' || | ||
(includes && includes.includes(prop.name)) | ||
) { | ||
props.push(prop.name); | ||
return Object.values(meta.get(type).props).reduce<Fields<T>>( | ||
(props, prop) => { | ||
if (!(prop.hidden === true)) { | ||
if (prop.reference === 'scalar') { | ||
if ( | ||
prop.type !== 'BlobType' || | ||
(includes && includes.includes(prop.name)) | ||
) { | ||
props.push(prop.name); | ||
} | ||
return props; | ||
} | ||
return props; | ||
} | ||
if (recurse > 0) { | ||
if (prop.reference === 'm:1') { | ||
props.push(prop.name); | ||
if (recurse > 0) { | ||
if (prop.reference === 'm:1') { | ||
props.push(prop.name); | ||
} | ||
props.push({ | ||
[prop.name]: getFields( | ||
prop.type as keyof typeof entities, | ||
meta, | ||
includes, | ||
recurse - 1, | ||
), | ||
}); | ||
} | ||
props.push({ | ||
[prop.name]: getFields( | ||
prop.type as keyof typeof entities, | ||
includes, | ||
recurse - 1, | ||
), | ||
}); | ||
} | ||
} | ||
return props; | ||
}, []); | ||
return props; | ||
}, | ||
[], | ||
); | ||
} |