Skip to content

Commit

Permalink
fix: use the public API for reading entity metadata
Browse files Browse the repository at this point in the history
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
thewilkybarkid committed Nov 24, 2021
1 parent 9a60750 commit e1ba4e6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/backend/controllers/community.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export default function controller(
const options = {
fields: getFields(
'Community',
communityModel.em.getMetadata(),
ctx.query.include_images
? ctx.query.include_images.split(',')
: undefined,
Expand Down Expand Up @@ -280,6 +281,7 @@ export default function controller(
const options = {
fields: getFields(
'Community',
communityModel.em.getMetadata(),
ctx.query.include_images
? ctx.query.include_images.split(',')
: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/backend/controllers/fullReview.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default function controller(
const options = {
fields: getFields(
'FullReview',
reviewModel.em.getMetadata(),
ctx.query.include_images
? ctx.query.include_images.split(',')
: undefined,
Expand Down
2 changes: 2 additions & 0 deletions src/backend/controllers/persona.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default function controller(
const options = {
fields: getFields(
'Persona',
personasModel.em.getMetadata(),
ctx.query.include_images
? ctx.query.include_images.split(',')
: undefined,
Expand Down Expand Up @@ -235,6 +236,7 @@ export default function controller(
const options = {
fields: getFields(
'Persona',
personasModel.em.getMetadata(),
ctx.query.include_images
? ctx.query.include_images.split(',')
: undefined,
Expand Down
2 changes: 2 additions & 0 deletions src/backend/controllers/preprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export default function controller(preprints, thisUser) {
const options = {
fields: getFields(
'Preprint',
preprints.em.getMetadata(),
ctx.query.include_images
? ctx.query.include_images.split(',')
: undefined,
Expand Down Expand Up @@ -358,6 +359,7 @@ export default function controller(preprints, thisUser) {
const options = {
fields: getFields(
'Preprint',
preprints.em.getMetadata(),
ctx.query.include_images
? ctx.query.include_images.split(',')
: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/backend/controllers/rapidReview.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default function controller(rapidReviews, preprints, thisUser) {
const options = {
fields: getFields(
'RapidReview',
rapidReviews.em.getMetadata(),
ctx.query.include_images
? ctx.query.include_images.split(',')
: undefined,
Expand Down
55 changes: 29 additions & 26 deletions src/backend/utils/getFields.ts
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;
},
[],
);
}

0 comments on commit e1ba4e6

Please sign in to comment.