Use promises for 1:n m:n relations

This commit is contained in:
Amit Jakubowicz 2019-04-27 11:37:13 +02:00
parent d44103520b
commit 8aca3a0f91
5 changed files with 42 additions and 43 deletions

View file

@ -55,7 +55,7 @@ declare namespace GQL {
__typename: 'CalendarEvent';
id: string;
owner: IUser;
info: IEventInformation;
info: Array<IEventInformation | null>;
time: IEventTime;
status: any;
contact: Array<IEventContactPerson>;
@ -65,6 +65,7 @@ declare namespace GQL {
interface IEventInformation {
__typename: 'EventInformation';
language: string;
title: string;
description: string | null;
}

View file

@ -3,7 +3,7 @@ import {
PrimaryGeneratedColumn,
Column,
BaseEntity,
OneToMany, JoinColumn, CreateDateColumn
OneToMany, CreateDateColumn
} from "typeorm"
import { Event } from "../Calendar/Event.entity"
import {Session, SessionInvite} from "./Session.entity"
@ -23,13 +23,13 @@ export class User extends BaseEntity {
email: string
@OneToMany(type => Event, event => event.owner)
events: Event[]
events: Promise<Event[]>
@OneToMany(type => Session, session => session.user)
sessions: Promise<Session[]>
@OneToMany(type => SessionInvite, invite => invite.user)
sessionInvites: SessionInvite[]
sessionInvites: Promise<SessionInvite[]>
@CreateDateColumn()
created: Date

View file

@ -5,8 +5,6 @@ import {
BaseEntity,
OneToMany,
ManyToOne,
BeforeInsert,
PrimaryColumn
} from "typeorm"
import { User } from "../Auth/User.entity"
import { DateTime } from "luxon"
@ -74,10 +72,12 @@ export class Event extends BaseEntity {
@OneToMany(type => EventOccurrence, occurrence => occurrence.event, {
cascade: true
})
occurrences: EventOccurrence[]
occurrences: Promise<EventOccurrence[]>
@OneToMany(type => EventInformation, eventInfo => eventInfo.event)
info: EventInformation[]
@OneToMany(type => EventInformation, eventInfo => eventInfo.event, {
cascade: true
})
info: Promise<EventInformation[]>
@Column(type => EventTime)
time: EventTime
@ -103,25 +103,22 @@ export class Event extends BaseEntity {
singleOccurrence.end = this.time.end
singleOccurrence.utcStart = toUTC(this.time.start, this.time.timeZone)
singleOccurrence.utcEnd = toUTC(this.time.end, this.time.timeZone)
this.occurrences = [singleOccurrence]
this.occurrences = Promise.resolve([singleOccurrence])
} else {
const ruleSet = rrulestr(this.time.recurrence)
const allDates = ruleSet.all(
(occurenceDate, i) => i < 30
)
this.occurrences = allDates
.map(occurenceDate => {
const occ = new EventOccurrence()
occ.timeZone = this.time.timeZone
occ.start = this.time.start
occ.end = this.time.end
occ.utcStart = toUTC(this.time.start, this.time.timeZone)
occ.utcEnd = toUTC(this.time.end, this.time.timeZone)
occ.event = this
return occ
})
const allDates = ruleSet.all((occurenceDate, i) => i < 30)
const occurrences = allDates.map(occurenceDate => {
const occ = new EventOccurrence()
occ.timeZone = this.time.timeZone
occ.start = this.time.start
occ.end = this.time.end
occ.utcStart = toUTC(this.time.start, this.time.timeZone)
occ.utcEnd = toUTC(this.time.end, this.time.timeZone)
occ.event = this
return occ
})
this.occurrences = Promise.resolve(occurrences)
}
return this
}
@ -129,24 +126,18 @@ export class Event extends BaseEntity {
@Entity()
export class EventInformation {
@PrimaryColumn()
eventIdAndLang: string
@PrimaryGeneratedColumn("uuid")
id: string
@Column()
language: string
@Column()
title: string
@Column()
@Column({ nullable: true })
description: string
@ManyToOne(type => Event, event => event.info)
event: Event
@BeforeInsert()
updatePrimaryColumn() {
this.eventIdAndLang = `${this.event.id}_${this.language}`
}
}
@Entity()
export class EventOccurrence extends BaseEntity {
@PrimaryGeneratedColumn("uuid")

View file

@ -34,6 +34,10 @@ const resolvers: ResolverMap = {
Event: {
owner: async (event: Event, args, context, info) => {
return event.owner
},
info: async (event: Event) => {
console.log("event.info", await event.info)
return event.info
}
},
@ -49,14 +53,16 @@ const resolvers: ResolverMap = {
}
const event = new Event()
event.owner = context.user
event.info = input.info.map(infoInput => {
const eventInformation = new EventInformation()
eventInformation.language = infoInput.language
eventInformation.title = infoInput.title
eventInformation.description = infoInput.description
eventInformation.event = event
return eventInformation
})
event.info = Promise.resolve(
input.info.map(infoInput => {
const eventInformation = new EventInformation()
eventInformation.language = infoInput.language
eventInformation.title = infoInput.title
eventInformation.description = infoInput.description
eventInformation.event = event
return eventInformation
})
)
event.time = {
recurrence: input.time.recurrence,
timeZone: input.time.timeZone,

View file

@ -36,7 +36,7 @@ input RequestInviteInput {
type CalendarEvent {
id: ID!
owner: User!
info: EventInformation!
info: [EventInformation]!
time: EventTime!
status: EventStatus!
contact: [EventContactPerson!]!
@ -87,6 +87,7 @@ scalar Language
# Event information for presentation
type EventInformation {
language: String!
title: String!
description: String
}