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

Was this helpful?

Edit on GitHub
  1. Getting Started

Ent API: exists() by Expression

This is another privacy-unaware API call, similar to count().

Ent.exists(vc, { field: "...", ... }): boolean

Returns true if there isat least one Ent in the database matching the where condition. Works across multiple microshards too.

In terms of the logic, exists() call is similar to count() > 0 check, with two performance optimizations:

  1. It uses EXISTS SQL clause, which doesn’t read more tuples from the database than needed (as opposed to count() aggregate).

  2. During the run, it severely reduces the weight (basically, the probability) of seqscan to happen (with SET enable_seqscan=off directive merged with the query). I.e. it implies that you must have a good index covering the where condition.

As all API calls in Ent Framework, multiple parallel exists() calls are batched into a single SQL query:

const [exists1, exists2] = await Promise.all([
  EntTopic.exists(vc, { creator_id: "123" }),
  EntTopic.exists(vc, { updated_at: { $gt: new Date("2024-01-01") } }),
]);

This sends the following SQL query to the underlying database:

SET enable_seqscan=off;
SELECT EXISTS (SELECT true FROM topics WHERE creator_id='123')
  UNION ALL
SELECT EXISTS (SELECT true FROM topics WHERE created_at>'...')

The exists() call is even more useful to build custom privacy checks than count(), because it’s faster and almost guarantees using an index.

PreviousEnt API: count() by ExpressionNextEnt API: selectBy() Unique Key Prefix

Last updated 5 months ago

Was this helpful?