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, ManyToMany, ManyToOne, OneToMany, OneToOne,} from 'uql-orm';
@Entity()export class Company { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string | null;
@Field({ type: String }) country?: string | null;
@Field({ type: Date }) createdAt?: Date | null;}
/** 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 | null;
@Field({ type: Date, softDelete: true }) deletedAt?: Date | null;}
@Entity()export class MeasureUnitCategory { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string | null;
@OneToMany({ entity: () => MeasureUnit, mappedBy: (measureUnit) => measureUnit.category, }) measureUnits?: MeasureUnit[];}
@Entity()export class MeasureUnit { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string | null;
@Field({ references: () => MeasureUnitCategory }) categoryId?: number | null;
@ManyToOne({ entity: () => MeasureUnitCategory, references: (measureUnit) => measureUnit.categoryId, }) category?: MeasureUnitCategory;
@Field({ type: Date, softDelete: true }) deletedAt?: Date | null;}
@Entity()export class Profile { @Id({ type: Number }) id?: number;
@Field({ type: String }) picture?: string | null;
@Field({ type: String }) bio?: string | null;
@Field({ references: () => User }) userId?: number | null;
@OneToOne({ entity: () => User, references: (profile) => profile.userId }) user?: User;}
@Entity()export class User { @Id({ type: Number }) id?: number;
@Field({ type: String, unique: true }) email?: string | null;
@Field({ type: String }) name?: string | null;
@Field({ type: String }) password?: string | null;
/** `'active'`, `'pending'`, `'banned'`. */ @Field({ type: String }) status?: string | null;
@Field({ type: Number }) age?: number | null;
@Field({ type: Boolean }) active?: boolean | null;
@Field({ type: Date }) createdAt?: Date | null;
@Field({ type: Number }) creatorId?: number | null;
@Field({ references: () => Company }) companyId?: number | null;
@ManyToOne({ entity: () => Company, references: (user) => user.companyId }) 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 | null;
@Field({ type: String }) body?: string | null;
@Field({ type: Boolean }) published?: boolean | null;
/** Upvotes, which the [raw projection](/querying/querier#raw-projections-in-select) example ranks on. */ @Field({ type: Number }) points?: number | null;
@Field({ type: Date }) createdAt?: Date | null;
@Field({ references: () => User }) authorId?: number | null;
@Field({ references: () => User }) creatorId?: number | null;
@ManyToOne({ entity: () => User, references: (post) => post.authorId }) author?: User;}
@Entity()export class Item { @Id({ type: Number }) id?: number;
@Field({ type: String }) name?: string | null;
@Field({ type: String }) description?: string | null;
@Field({ type: String }) code?: string | null;
@Field({ type: Number }) price?: number | null;
/** What the [row-locking](/querying/locking) examples decrement under a lock. */ @Field({ type: Number }) stock?: number | null;
@Field({ type: Boolean }) isActive?: boolean | null;
@Field({ type: Date }) createdAt?: Date | null;
@Field({ type: 'vector', dimensions: 1536 }) embedding?: number[] | null;
@Field({ references: () => Company }) companyId?: number | null;
@ManyToOne({ entity: () => Company, references: (item) => item.companyId }) company?: Company;
@Field({ references: () => Tax }) taxId?: number | null;
@ManyToOne({ entity: () => Tax, references: (item) => item.taxId }) tax?: Tax;
@Field({ references: () => MeasureUnit }) measureUnitId?: number | null;
@ManyToOne({ entity: () => MeasureUnit, references: (item) => item.measureUnitId, }) 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 | null;}
/** The junction `Item.tags` counts through. */@Entity()export class ItemTag { @Id({ type: Number }) id?: number;
@Field({ references: () => Item }) itemId?: number | null;
@Field({ references: () => Tag }) tagId?: number | null;}
/** 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 | null;
@Field({ references: () => Company }) companyId?: number | null;}
/** 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 | null;
@Field({ type: Number }) amount?: number | null;
@Field({ type: Number }) customerId?: number | null;
@Field({ type: Date }) createdAt?: Date | null;}
@Entity()export class Invoice { @Id({ type: Number }) id?: number;
@Field({ type: Number }) amount?: number | null;
@Field({ type: Number }) total?: number | null;
@Field({ type: Boolean }) paid?: boolean | null;
@Field({ type: Date }) createdAt?: Date | null;
@Field({ references: () => Company }) companyId?: number | null;}