Skip to content
NewFrom Mongoose to SQL: migrating MongoDB to PostgreSQL7 min read

Example Entities

The query examples in this section import these entities from one shared module. Every field and relation their $select, $where, $sort and $populate name is declared here.

shared/models/index.ts
import {
f,
Entity,
Field,
ManyToMany,
ManyToOne,
OneToMany,
OneToOne,
} from 'uql-orm';
@Entity()
export class Company {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.string().nullable())
name?: string | null;
@Field(f.string().nullable())
country?: string | null;
@Field(f.date().nullable())
createdAt?: Date | null;
}
/** A lookup table, soft-deletable, which is why the joins below carry `deletedAt IS NULL`. */
@Entity()
export class Tax {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.string().nullable())
name?: string | null;
@Field(f.date().nullable().softDelete())
deletedAt?: Date | null;
}
@Entity()
export class MeasureUnitCategory {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.string().nullable())
name?: string | null;
@OneToMany(() => MeasureUnit, {
mappedBy: (measureUnit) => measureUnit.category,
})
measureUnits?: MeasureUnit[];
}
@Entity()
export class MeasureUnit {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.string().nullable())
name?: string | null;
@Field(f.references(() => MeasureUnitCategory).nullable())
categoryId?: number | null;
@ManyToOne(() => MeasureUnitCategory, {
references: (measureUnit) => measureUnit.categoryId,
})
category?: MeasureUnitCategory;
@Field(f.date().nullable().softDelete())
deletedAt?: Date | null;
}
@Entity()
export class Profile {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.string().nullable())
picture?: string | null;
@Field(f.string().nullable())
bio?: string | null;
@Field(f.references(() => User).nullable())
userId?: number | null;
@OneToOne(() => User, { references: (profile) => profile.userId })
user?: User;
}
@Entity()
export class User {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.string().nullable().unique())
email?: string | null;
@Field(f.string().nullable())
name?: string | null;
@Field(f.string().nullable())
password?: string | null;
/** `'active'`, `'pending'`, `'banned'`. */
@Field(f.string().nullable())
status?: string | null;
@Field(f.number().nullable())
age?: number | null;
@Field(f.boolean().nullable())
active?: boolean | null;
@Field(f.date().nullable())
createdAt?: Date | null;
@Field(f.number().nullable())
creatorId?: number | null;
@Field(f.references(() => Company).nullable())
companyId?: number | null;
@ManyToOne(() => Company, { references: (user) => user.companyId })
company?: Company;
@OneToOne(() => Profile, {
mappedBy: (profile) => profile.user,
cascade: true,
})
profile?: Profile;
@OneToMany(() => Post, { mappedBy: (post) => post.author })
posts?: Post[];
}
@Entity()
export class Post {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.string().nullable())
title?: string | null;
@Field(f.string().nullable())
body?: string | null;
@Field(f.boolean().nullable())
published?: boolean | null;
/** `null` until published, which the [null placement](/querying/sorting#where-nulls-land) examples sort on. */
@Field(f.date().nullable())
publishedAt?: Date | null;
/** Upvotes, which the [raw projection](/querying/querier#raw-projections-in-select) example ranks on. */
@Field(f.number().nullable())
points?: number | null;
@Field(f.date().nullable())
createdAt?: Date | null;
@Field(f.references(() => User).nullable())
authorId?: number | null;
@Field(f.references(() => User).nullable())
creatorId?: number | null;
@ManyToOne(() => User, { references: (post) => post.authorId })
author?: User;
}
@Entity()
export class Item {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.string().nullable())
name?: string | null;
@Field(f.string().nullable())
description?: string | null;
@Field(f.string().nullable())
code?: string | null;
@Field(f.number().nullable())
price?: number | null;
/** What the [row-locking](/querying/locking) examples decrement under a lock. */
@Field(f.number().nullable())
stock?: number | null;
@Field(f.boolean().nullable())
isActive?: boolean | null;
@Field(f.date().nullable())
createdAt?: Date | null;
@Field(f.vector(1536).nullable())
embedding?: number[] | null;
@Field(f.references(() => Company).nullable())
companyId?: number | null;
@ManyToOne(() => Company, { references: (item) => item.companyId })
company?: Company;
@Field(f.references(() => Tax).nullable())
taxId?: number | null;
@ManyToOne(() => Tax, { references: (item) => item.taxId })
tax?: Tax;
@Field(f.references(() => MeasureUnit).nullable())
measureUnitId?: number | null;
@ManyToOne(() => MeasureUnit, {
references: (item) => item.measureUnitId,
})
measureUnit?: MeasureUnit;
/** What the [relation `$size`](/querying/relations) and [`$count`](/querying/counting) examples tally. */
@ManyToMany(() => Tag, { through: () => ItemTag })
tags?: Tag[];
}
@Entity()
export class Tag {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.string().nullable())
name?: string | null;
}
/** The junction `Item.tags` counts through. */
@Entity()
export class ItemTag {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.references(() => Item).nullable())
itemId?: number | null;
@Field(f.references(() => Tag).nullable())
tagId?: number | null;
}
/** What the [transaction](/querying/transactions) examples debit, so they have a balance to get wrong. */
@Entity()
export class Account {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.number().nullable())
balance?: number | null;
@Field(f.references(() => Company).nullable())
companyId?: number | null;
}
/** What the [aggregate](/querying/aggregate) examples group and total. */
@Entity()
export class Order {
@Field(f.number().autoIncrement())
id?: number;
/** `'pending'`, `'shipped'`, `'cancelled'`. */
@Field(f.string().nullable())
status?: string | null;
@Field(f.number().nullable())
amount?: number | null;
@Field(f.number().nullable())
customerId?: number | null;
@Field(f.date().nullable())
createdAt?: Date | null;
}
@Entity()
export class Invoice {
@Field(f.number().autoIncrement())
id?: number;
@Field(f.number().nullable())
amount?: number | null;
@Field(f.number().nullable())
total?: number | null;
@Field(f.boolean().nullable())
paid?: boolean | null;
@Field(f.date().nullable())
createdAt?: Date | null;
@Field(f.references(() => Company).nullable())
companyId?: number | null;
}