Relations between entities

Take any dump class (aka DTO) and annotate it with the decorators from nukak/entity.

import { v4 as uuidv4 } from 'uuid'; import { Field, ManyToOne, Id, OneToMany, Entity, OneToOne, ManyToMany } from 'nukak/entity'; @Entity() export class Profile { /** * primary-key. * the `onInsert` callback can be used to specify a custom mechanism for auto-generating * the default value of a field when inserting a new record. */ @Id({ onInsert: uuidv4 }) id?: string; @Field() picture?: string; /** * foreign-keys are really simple to specify. */ @Field({ reference: () => User }) creatorId?: string; } @Entity() export class User { @Id({ onInsert: uuidv4 }) id?: string; @Field() name?: string; @Field() email?: string; @Field() password?: string; /** * `mappedBy` can be a callback or a string (callback is useful for auto-refactoring). */ @OneToOne({ entity: () => Profile, mappedBy: (profile) => profile.creatorId, cascade: true }) profile?: Profile; } @Entity() export class MeasureUnitCategory { @Id({ onInsert: uuidv4 }) id?: string; @Field() name?: string; @OneToMany({ entity: () => MeasureUnit, mappedBy: (measureUnit) => measureUnit.category }) measureUnits?: MeasureUnit[]; } @Entity() export class MeasureUnit { @Id({ onInsert: uuidv4 }) id?: string; @Field() name?: string; @Field({ reference: () => MeasureUnitCategory }) categoryId?: string; @ManyToOne({ cascade: 'persist' }) category?: MeasureUnitCategory; }