Skip to content

Commit

Permalink
mikroORM change to reflect metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
haphuong96 committed Sep 18, 2023
1 parent 48d0bc4 commit 5505dc1
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ COPY . .

EXPOSE 3000

RUN npm run build
# RUN npm run build

CMD ["npm", "run", "start"]

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start": "node dist/main",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
Expand Down
4 changes: 2 additions & 2 deletions src/configs/mikro-orm.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Options } from '@mikro-orm/core';
import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
// import { TsMorphMetadataProvider } from '@mikro-orm/reflection';

const config: Options = {
entities: ['./dist/**/*.entity.js'],
entitiesTs: ['./src/**/*.entity.ts'],
metadataProvider: TsMorphMetadataProvider,
// metadataProvider: TsMorphMetadataProvider,
migrations: {
tableName: 'mikro_orm_migrations',
path: './dist/migrations',
Expand Down
4 changes: 2 additions & 2 deletions src/modules/chat/entities/conversation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export class Conversation {
})
participants = new Collection<User>(this);

@OneToMany({ mappedBy: 'conversation' })
@OneToMany({ entity: () => Message, mappedBy: 'conversation' })
messages = new Collection<Message>(this);

@OneToMany({ mappedBy: 'conversation', persist: false })
@OneToMany({ entity: () => Message, mappedBy: 'conversation', persist: false })
lastMessage = new Collection<Message>(this);

@Property({ persist: false })
Expand Down
10 changes: 5 additions & 5 deletions src/modules/chat/entities/message.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export class Message {
@PrimaryKey()
id!: number;

@Property({ length: 255})
@Property({ length: 255 })
message!: string;

@ManyToOne()
@ManyToOne({ entity: () => Conversation })
conversation!: Conversation;

@ManyToOne()
@ManyToOne({ entity: () => User })
sender!: User;

@Enum(() => MessageType)
Expand All @@ -31,5 +31,5 @@ export class Message {
}

export enum MessageType {
MESSAGE = 'message',
}
MESSAGE = 'message',
}
7 changes: 3 additions & 4 deletions src/modules/chat/entities/participant.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ import { Message } from './message.entity';

@Entity()
export class ConversationParticipant {
@ManyToOne()
@ManyToOne({ entity: () => Conversation })
conversation!: Conversation;

@ManyToOne({name: 'user_id'})
@ManyToOne({ entity: () => User, name: 'user_id' })
participant!: User;

@ManyToOne()
@ManyToOne({ entity: () => Message })
lastReadMessage?: Message;

@PrimaryKey()
id!: number;

}
11 changes: 7 additions & 4 deletions src/modules/item/entities/item-image.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ManyToOne,
OneToOne,
wrap,
EntityDTO
EntityDTO,
} from '@mikro-orm/core';
import { Item } from './item.entity';
import { AzureStorageBlob } from '../../azure-blob-storage/blob.entity';
Expand All @@ -22,7 +22,7 @@ export class ItemImage {
@OneToOne()
thumbnail!: AzureStorageBlob;

@ManyToOne()
@ManyToOne({ entity: () => Item })
item!: Item;

/**
Expand All @@ -31,8 +31,11 @@ export class ItemImage {
* @returns
*/
toJSON(...args: any[]): { [p: string]: any } {
const o : EntityDTO<this> = wrap(this, true).toObject(...args); // do not forget to pass rest params here
const o: EntityDTO<this> = wrap(this, true).toObject(...args); // do not forget to pass rest params here

return { url: o['image']['imgPath'], thumbnailUrl: o['thumbnail']['imgPath'] };
return {
url: o['image']['imgPath'],
thumbnailUrl: o['thumbnail']['imgPath'],
};
}
}
14 changes: 8 additions & 6 deletions src/modules/item/entities/item.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Item {
@PrimaryKey()
id!: number;

@ManyToOne()
@ManyToOne({ entity: () => User })
owner!: User;

@ManyToOne()
Expand All @@ -33,7 +33,11 @@ export class Item {
@Enum(() => ItemStatus)
status!: ItemStatus;

@OneToMany({ mappedBy: 'item', cascade: [Cascade.REMOVE] })
@OneToMany({
entity: () => ItemImage,
mappedBy: 'item',
cascade: [Cascade.REMOVE],
})
img? = new Collection<ItemImage>(this);

@Property({ length: 255 })
Expand All @@ -54,7 +58,7 @@ export class Item {
@Property({ type: types.datetime, defaultRaw: `current_timestamp()` })
lastUpdatedDatetime!: Date;

@ManyToMany({ mappedBy: 'favoriteItems' })
@ManyToMany({ entity: () => User, mappedBy: 'favoriteItems' })
favoritedBy = new Collection<User>(this);

@Property({ persist: false })
Expand Down Expand Up @@ -84,12 +88,10 @@ export class Item {
}
}



export enum ItemStatus {
DRAFT = 'DRAFT',
PUBLISHED = 'PUBLISHED',
RESERVED = 'RESERVED',
SOLD = 'SOLD',
HIDDEN = 'HIDDEN',
}
}
2 changes: 1 addition & 1 deletion src/modules/market/entities/campus.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class CampusLocation {
@Property({ length: 255 })
campusName!: string;

@ManyToMany({ mappedBy: 'campuses' })
@ManyToMany({ entity: () => University, mappedBy: 'campuses' })
universities = new Collection<University>(this);

constructor(id: number, campusName: string) {
Expand Down
5 changes: 4 additions & 1 deletion src/modules/market/entities/university-campus.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export class UniversityCampus {
@ManyToOne()
campusLocation!: CampusLocation;

@OneToMany( { mappedBy: 'universityCampusLocation'})
@OneToMany({
entity: () => PickUpPoint,
mappedBy: 'universityCampusLocation',
})
pickUpPoints? = new Collection<PickUpPoint>(this);
}
5 changes: 4 additions & 1 deletion src/modules/market/entities/university.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export class University {
@Property({ length: 255, hidden: true })
emailAddressDomain!: string;

@ManyToMany( { pivotEntity: () => UniversityCampus })
@ManyToMany({
entity: () => CampusLocation,
pivotEntity: () => UniversityCampus,
})
campuses = new Collection<CampusLocation>(this);

constructor(id: number, universityName: string, emailAddressDomain: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class User {
@ManyToMany({ entity: () => Conversation, mappedBy: 'participants'})
conversations = new Collection<Conversation>(this);

@ManyToMany({ pivotTable: 'user_favorite_item'})
@ManyToMany({ entity: () => Item, pivotTable: 'user_favorite_item'})
favoriteItems = new Collection<Item>(this);

constructor(user: {
Expand Down

0 comments on commit 5505dc1

Please sign in to comment.