Skip to content

Commit

Permalink
refactor: remove decorators
Browse files Browse the repository at this point in the history
Decorators are an experimental feature in TypeScript, which is known to have issues. Changes in Parcel means that it can no longer build the app, so their use is blocking updating Parcel to a stable release.

This change removes the ORM decorators in favour of defining entities programmatically. I'm hoping that the tests and migration-creating script are enough to have caught any mistakes, but given that areas of the app remain untested, it's hard to be sure.

Also, this change has to disable Parcel's scope hoisting, as it renames the entity classes. The rename wasn't a problem when using decorators as the generated code saw the class name left unchanged. Now, however, the ORM breaks if they are changed.

Refs #399, parcel-bundler/parcel#7293 (comment), microsoft/TypeScript#27519
  • Loading branch information
thewilkybarkid committed Nov 25, 2021
1 parent eca52cd commit e66f0d8
Show file tree
Hide file tree
Showing 47 changed files with 554 additions and 623 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"backend": {
"context": "node",
"distDir": "dist/backend",
"optimize": false
"optimize": false,
"scopeHoist": false
},
"frontend": {
"context": "browser",
Expand All @@ -53,12 +54,14 @@
"migrations-script": {
"context": "node",
"distDir": "dist/scripts",
"optimize": false
"optimize": false,
"scopeHoist": false
},
"init-script": {
"context": "node",
"distDir": "dist/scripts",
"optimize": false
"optimize": false,
"scopeHoist": false
}
},
"engines": {
Expand Down Expand Up @@ -143,7 +146,6 @@
"react-router-dom": "^5.1.2",
"react-slick": "^0.28.1",
"react-twitter-embed": "^3.0.3",
"reflect-metadata": "^0.1.13",
"replacestream": "^4.0.3",
"request-received": "0.0.3",
"restful-react": "^15.2.0",
Expand Down
1 change: 0 additions & 1 deletion src/backend/controllers/preprint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import router from 'koa-joi-router';
import 'reflect-metadata';
import { QueryOrder } from '@mikro-orm/core';
import { getLogger } from '../log.ts';
import { resolvePreprint } from '../utils/resolve.ts';
Expand Down
84 changes: 42 additions & 42 deletions src/backend/mikro-orm.config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { Options } from '@mikro-orm/core';
import {
Badge,
BaseEntity,
Comment,
Community,
Contact,
Event,
Expertise,
FullReview,
FullReviewDraft,
Group,
Key,
Persona,
Preprint,
RapidReview,
Report,
Request,
Statement,
Tag,
Template,
User,
Work,
badgeSchema,
baseEntitySchema,
commentSchema,
communitySchema,
contactSchema,
eventSchema,
expertiseSchema,
fullReviewSchema,
fullReviewDraftSchema,
groupSchema,
keySchema,
personaSchema,
preprintSchema,
rapidReviewSchema,
reportSchema,
requestSchema,
statementSchema,
tagSchema,
templateSchema,
userSchema,
workSchema,
} from './models/entities';
import config from './config';

Expand All @@ -30,27 +30,27 @@ const portString = config.dbPort ? `:${config.dbPort}` : '';

const options: Options = {
entities: [
Badge,
BaseEntity,
Comment,
Community,
Contact,
Event,
Expertise,
FullReview,
FullReviewDraft,
Group,
Key,
Persona,
Preprint,
RapidReview,
Report,
Request,
Statement,
Tag,
Template,
User,
Work,
badgeSchema,
baseEntitySchema,
commentSchema,
communitySchema,
contactSchema,
eventSchema,
expertiseSchema,
fullReviewSchema,
fullReviewDraftSchema,
groupSchema,
keySchema,
personaSchema,
preprintSchema,
rapidReviewSchema,
reportSchema,
requestSchema,
statementSchema,
tagSchema,
templateSchema,
userSchema,
workSchema,
],
type: 'postgresql',
debug: config.logLevel === 'trace',
Expand Down
3 changes: 1 addition & 2 deletions src/backend/models/badges.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EntityRepository, MikroORM, Repository } from '@mikro-orm/core';
import { EntityRepository, MikroORM } from '@mikro-orm/core';
import { Badge } from './entities';

@Repository(Badge)
export class BadgeModel extends EntityRepository<Badge> {}

export function badgeModelWrapper(db: MikroORM): BadgeModel {
Expand Down
3 changes: 1 addition & 2 deletions src/backend/models/comments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EntityRepository, MikroORM, Repository } from '@mikro-orm/core';
import { EntityRepository, MikroORM } from '@mikro-orm/core';
import { Comment } from './entities';

@Repository(Comment)
export class CommentModel extends EntityRepository<Comment> {}

export function commentModelWrapper(db: MikroORM): CommentModel {
Expand Down
3 changes: 1 addition & 2 deletions src/backend/models/communities.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { EntityRepository, MikroORM, Repository } from '@mikro-orm/core';
import { EntityRepository, MikroORM } from '@mikro-orm/core';
import { ORCID as orcidUtils } from 'orcid-utils';
import { validate as uuidValidate } from 'uuid';
import { Community, User } from './entities';
import { getLogger } from '../log';

const log = getLogger('backend:models:communities');

@Repository(Community)
export class CommunityModel extends EntityRepository<Community> {
async isMemberOf(communityId: string, userId: string): Promise<boolean> {
let user: User | null = null;
Expand Down
3 changes: 1 addition & 2 deletions src/backend/models/contacts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EntityRepository, MikroORM, Repository } from '@mikro-orm/core';
import { EntityRepository, MikroORM } from '@mikro-orm/core';
import { Contact } from './entities';

@Repository(Contact)
export class ContactModel extends EntityRepository<Contact> {}

export function contactModelWrapper(db: MikroORM): ContactModel {
Expand Down
33 changes: 16 additions & 17 deletions src/backend/models/entities/Badge.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import {
Collection,
Entity,
EntityRepositoryType,
ManyToMany,
Property,
Unique,
} from '@mikro-orm/core';
import { Collection, EntitySchema } from '@mikro-orm/core';
import { BadgeModel } from '../badges';
import { BaseEntity } from './BaseEntity';
import { Persona } from './Persona';

@Entity()
export class Badge extends BaseEntity {
[EntityRepositoryType]?: BadgeModel;

@Property()
@Unique()
name!: string;

@Property()
color?: string;

@ManyToMany({ entity: () => Persona, inversedBy: 'badges' })
personas: Collection<Persona> = new Collection<Persona>(this);

constructor(name: string, color = '#FF0000') {
Expand All @@ -30,3 +14,18 @@ export class Badge extends BaseEntity {
this.color = color;
}
}

export const badgeSchema = new EntitySchema<Badge, BaseEntity>({
class: Badge,

customRepository: () => BadgeModel,
properties: {
name: { type: 'string', unique: true },
color: { type: 'string' },
personas: {
reference: 'm:n',
entity: () => Persona,
inversedBy: (persona) => persona.badges,
},
},
});
21 changes: 12 additions & 9 deletions src/backend/models/entities/BaseEntity.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { EntitySchema } from '@mikro-orm/core';
import { v4 as uuidv4 } from 'uuid';
import { PrimaryKey, Property, Unique } from '@mikro-orm/core';

export abstract class BaseEntity {
@PrimaryKey({ hidden: true })
id!: number;

@Property({ type: 'string' })
@Unique()
uuid = uuidv4();

@Property()
createdAt: Date = new Date();

@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date();
}

export const baseEntitySchema = new EntitySchema<BaseEntity>({
name: 'BaseEntity',
abstract: true,
properties: {
id: { type: 'number', hidden: true, primary: true },
uuid: { type: 'string', unique: true },
createdAt: { type: 'Date' },
updatedAt: { type: 'Date', onUpdate: () => new Date() },
},
});
35 changes: 14 additions & 21 deletions src/backend/models/entities/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
import {
Entity,
EntityRepositoryType,
Index,
ManyToOne,
Property,
} from '@mikro-orm/core';
import { EntitySchema } from '@mikro-orm/core';
import { CommentModel } from '../comments';
import { BaseEntity } from './BaseEntity';
import { FullReview } from './FullReview';
import { Persona } from './Persona';

@Entity()
@Index({ properties: ['author'] })
@Index({ properties: ['parent'] })
export class Comment extends BaseEntity {
[EntityRepositoryType]?: CommentModel;

@Property({ columnType: 'text' })
contents!: string;

@Property()
isPublished: boolean = false;

@Property()
isFlagged: boolean = false;

@ManyToOne({ entity: () => Persona })
author!: Persona;

@ManyToOne({ entity: () => FullReview })
parent!: FullReview;

constructor(contents: string, author: Persona, parent: FullReview) {
Expand All @@ -38,3 +18,16 @@ export class Comment extends BaseEntity {
this.parent = parent;
}
}

export const commentSchema = new EntitySchema<Comment, BaseEntity>({
class: Comment,
customRepository: () => CommentModel,
indexes: [{ properties: 'author' }, { properties: 'parent' }],
properties: {
contents: { type: 'string', columnType: 'text' },
isPublished: { type: 'boolean' },
isFlagged: { type: 'boolean' },
author: { reference: 'm:1', entity: () => Persona },
parent: { reference: 'm:1', entity: () => FullReview },
},
});
Loading

0 comments on commit e66f0d8

Please sign in to comment.