ent-framework
  • Ent Framework
  • Getting Started
    • Code Structure
    • Connect to a Database
    • Create Ent Classes
    • VC: Viewer Context and Principal
    • Ent API: insert*()
    • Built-in Field Types
    • Ent API: load*() by ID
    • N+1 Selects Solution
    • Automatic Batching Examples
    • Ent API: select() by Expression
    • Ent API: loadBy*() Unique Key
    • Ent API: update*()
    • Ent API: deleteOriginal()
    • Ent API: count() by Expression
    • Ent API: exists() by Expression
    • Ent API: selectBy() Unique Key Prefix
    • Ent API: upsert*()
    • Privacy Rules
    • Validators
    • Triggers
    • Custom Field Types
  • Ent API: Configuration and Types
  • Scalability
    • Replication and Automatic Lag Tracking
    • Sharding and Microsharding
    • Sharding Terminology
    • Locating a Shard and ID Format
    • Sharding Low-Level API
    • Shard Affinity and Ent Colocation
    • Inverses and Cross Shard Foreign Keys
    • Shards Rebalancing and pg-microsharding Tool
    • Connection Pooling
  • Advanced
    • Database Migrations and pg-mig Tool
    • Ephemeral (Symbol) Fields
    • Atomic Updates and CAS
    • Custom Field Refactoring
    • VC Flavors
    • Query Cache and VC Caches
    • Loaders and Custom Batching
    • PostgreSQL Specific Features
    • Query Planner Hints
    • Cluster Maintenance Queries
    • Logging and Diagnostic Tools
    • Composite Primary Keys
    • Passwords Rotation
  • Architecture
    • Abstraction Layers
    • Ent Framework, Meta’s TAO, entgo
    • JIT in SQL Queries Batching
    • To JOIN or not to JOIN
Powered by GitBook
On this page
  • Ent.loadX(vc, id): Ent
  • Ent.loadNullable(vc, id): Ent | null
  • Ent.loadIfReadableNullable(vc, id): Ent | null

Was this helpful?

Edit on GitHub
  1. Getting Started

Ent API: load*() by ID

There is a basic primitive used very frequently: having some Ent ID, load this Ent into memory.

app/api/comments/[id]/route.ts
import { EntComment } from "@/ents/EntComment";
import { getServerVC } from "@/ents/getServerVC";
import { NextApiRequest } from "next";
import { NextResponse } from "next/server";

export async function GET(
  _req: NextApiRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const vc = await getServerVC();
  const comment = await EntComment.loadX(vc, (await params).id);
  return NextResponse.json({ message: comment.message });
}

There are several versions of load* static methods on each Ent class:

Ent.loadX(vc, id): Ent

Loads an Ent by ID.

Throws EntNotFoundError if there is no such Ent in the database, or EntNotReadableError if the VC has no permissions to read it.

Ent.loadNullable(vc, id): Ent | null

loads an Ent by ID if it exists in the database, otherwise returns null.

If an Ent with such ID exists, but the VC doesn't have permissions to access it, the call will throw EntNotReadableError.

Ent.loadIfReadableNullable(vc, id): Ent | null

This is a special method designed to return null in two cases: when an Ent with the specified ID does not exist, or when the user lacks the necessary permissions to read it. Basically, it never throws.

Permissions are enforced by the privacyLoad rules of the Ent, which were briefly introduced earlier and will be covered in more detail later.

In most of the cases, prefer loadX() and rely on the outer try-catch blocks, as opposed to loadNullable() with manual null-checking. Let the framework do its job. And you likely almost never need to use loadIfReadableNullable(): it's a smell.

There is intentionally no method which loads multiple Ents at once taking an array of IDs. Read further on, why.

PreviousBuilt-in Field TypesNextN+1 Selects Solution

Last updated 3 months ago

Was this helpful?