Skip to content

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.

shared/models/index.ts
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 MeasureUnit {
@Id({ type: Number })
id?: number;
@Field({ type: String })
name?: string;
@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;
@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;
@Field({ type: Boolean })
isActive?: boolean;
@Field({ type: Date })
createdAt?: Date;
@Field({ type: 'vector', dimensions: 1536 })
embedding?: number[];
@Field({ references: () => Company })
companyId?: number;
@Field({ references: () => Tax })
taxId?: number;
@ManyToOne({ entity: () => Tax })
tax?: Tax;
@Field({ references: () => MeasureUnit })
measureUnitId?: number;
@ManyToOne({ entity: () => MeasureUnit })
measureUnit?: MeasureUnit;
}
@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;
}