Example Entities
Every example in this section queries the entities below, imported from one shared module. The $select, $where, $sort and $populate keys you see on those pages are the fields and relations of these classes, so this is the page to keep open beside the others.
import { Entity, Field, Id, ManyToOne, OneToMany, OneToOne } from 'uql-orm';
@Entity()export class Company { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string;
@Field({ type: String }) country?: string;
@Field({ type: Date }) createdAt?: Date;}
/** A lookup table, soft-deletable, which is why the joins below carry `deletedAt IS NULL`. */@Entity()export class Tax { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string;
@Field({ type: Date, softDelete: true }) deletedAt?: Date;}
@Entity()export class MeasureUnitCategory { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string;
@OneToMany({ entity: () => MeasureUnit, mappedBy: (measureUnit) => measureUnit.category, }) measureUnits?: MeasureUnit[];}
@Entity()export class MeasureUnit { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string;
@Field({ references: () => MeasureUnitCategory }) categoryId?: number;
@ManyToOne({ entity: () => MeasureUnitCategory }) category?: MeasureUnitCategory;
@Field({ type: Date, softDelete: true }) deletedAt?: Date;}
@Entity()export class Profile { @Id({ type: Number }) id?: number;
@Field({ type: String }) picture?: string;
@Field({ type: String }) bio?: string;
@Field({ references: () => User }) userId?: number;
@OneToOne({ entity: () => User }) user?: User;}
@Entity()export class User { @Id({ type: Number }) id?: number;
@Field({ type: String, unique: true }) email?: string;
@Field({ type: String }) name?: string;
@Field({ type: String }) password?: string;
/** `'active'`, `'pending'`, `'banned'`. */ @Field({ type: String }) status?: string;
@Field({ type: Number }) age?: number;
@Field({ type: Boolean }) active?: boolean;
@Field({ type: Date }) createdAt?: Date;
@Field({ type: Number }) creatorId?: number;
@Field({ references: () => Company }) companyId?: number;
@ManyToOne({ entity: () => Company }) company?: Company;
@OneToOne({ entity: () => Profile, mappedBy: (profile) => profile.user, cascade: true, }) profile?: Profile;
@OneToMany({ entity: () => Post, mappedBy: (post) => post.author }) posts?: Post[];}
@Entity()export class Post { @Id({ type: Number }) id?: number;
@Field({ type: String }) title?: string;
@Field({ type: String }) body?: string;
@Field({ type: Boolean }) published?: boolean;
/** Upvotes, which the [raw projection](/querying/querier#raw-projections-in-select) example ranks on. */ @Field({ type: Number }) points?: number;
@Field({ type: Date }) createdAt?: Date;
@Field({ references: () => User }) authorId?: number;
@Field({ references: () => User }) creatorId?: number;
@ManyToOne({ entity: () => User }) author?: User;}
@Entity()export class Item { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string;
@Field({ type: String }) description?: string;
@Field({ type: String }) code?: string;
@Field({ type: Number }) price?: number;
/** What the [row-locking](/querying/locking) examples decrement under a lock. */ @Field({ type: Number }) stock?: number;
@Field({ type: Boolean }) isActive?: boolean;
@Field({ type: Date }) createdAt?: Date;
@Field({ type: 'vector', dimensions: 1536 }) embedding?: number[];
@Field({ references: () => Company }) companyId?: number;
@ManyToOne({ entity: () => Company }) company?: Company;
@Field({ references: () => Tax }) taxId?: number;
@ManyToOne({ entity: () => Tax }) tax?: Tax;
@Field({ references: () => MeasureUnit }) measureUnitId?: number;
@ManyToOne({ entity: () => MeasureUnit }) measureUnit?: MeasureUnit;
/** What the [relation `$size`](/querying/relations) and [`$count`](/querying/counting) examples tally. */ @ManyToMany({ entity: () => Tag, through: () => ItemTag }) tags?: Tag[];}
@Entity()export class Tag { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string;}
/** The junction `Item.tags` counts through. */@Entity()export class ItemTag { @Id({ type: Number }) id?: number;
@Field({ references: () => Item }) itemId?: number;
@Field({ references: () => Tag }) tagId?: number;}
/** What the [transaction](/querying/transactions) examples debit, so they have a balance to get wrong. */@Entity()export class Account { @Id({ type: Number }) id?: number;
@Field({ type: Number }) balance?: number;
@Field({ references: () => Company }) companyId?: number;}
/** What the [aggregate](/querying/aggregate) examples group and total. */@Entity()export class Order { @Id({ type: Number }) id?: number;
/** `'pending'`, `'shipped'`, `'cancelled'`. */ @Field({ type: String }) status?: string;
@Field({ type: Number }) amount?: number;
@Field({ type: Number }) customerId?: number;
@Field({ type: Date }) createdAt?: Date;}
@Entity()export class Invoice { @Id({ type: Number }) id?: number;
@Field({ type: Number }) amount?: number;
@Field({ type: Number }) total?: number;
@Field({ type: Boolean }) paid?: boolean;
@Field({ type: Date }) createdAt?: Date;
@Field({ references: () => Company }) companyId?: number;}