The Entity module provides methods for reading and writing JSONB data to a workflow's entity. The instance methods exposed by this class are available for use from within a running workflow.

//entityWorkflow.ts
import { workflow } from '@hotmeshio/hotmesh';

export async function entityExample(): Promise<void> {
const entity = await workflow.entity();
await entity.set({ user: { id: 123 } });
await entity.merge({ user: { name: "John" } });
const user = await entity.get("user");
// user = { id: 123, name: "John" }
}

Methods

  • Appends a value to an array at the specified path

    Parameters

    • path: string
    • value: any

    Returns Promise<any[]>

    const entity = await workflow.entity();
    await entity.append("items", { id: 1, name: "New Item" });
  • Deletes a value from the entity by path

    Parameters

    • path: string

    Returns Promise<any>

    const entity = await workflow.entity();
    await entity.delete("user.email");
  • Gets a value from the entity by path

    Type Parameters

    • T

    Parameters

    • Optionalpath: string

    Returns Promise<T>

    const entity = await workflow.entity();
    const user = await entity.get("user");
    const email = await entity.get("user.email");
  • Increments a numeric value at the specified path

    Parameters

    • path: string
    • value: number = 1

    Returns Promise<number>

    const entity = await workflow.entity();
    await entity.increment("counter", 5);
  • Deep merges the provided object with the existing entity

    Type Parameters

    • T

    Parameters

    • value: T

    Returns Promise<T>

    const entity = await workflow.entity();
    await entity.merge({ user: { email: "john@example.com" } });
  • Prepends a value to an array at the specified path

    Parameters

    • path: string
    • value: any

    Returns Promise<any[]>

    const entity = await workflow.entity();
    await entity.prepend("items", { id: 0, name: "First Item" });
  • Removes an item from an array at the specified path and index

    Parameters

    • path: string
    • index: number

    Returns Promise<any[]>

    const entity = await workflow.entity();
    await entity.remove("items", 0); // Remove first item
  • Sets the entire entity object. This replaces any existing entity.

    Type Parameters

    • T

    Parameters

    • value: T

    Returns Promise<T>

    const entity = await workflow.entity();
    await entity.set({ user: { id: 123, name: "John" } });
  • Sets a value at the specified path only if it doesn't already exist

    Parameters

    • path: string
    • value: any

    Returns Promise<any>

    const entity = await workflow.entity();
    await entity.setIfNotExists("user.id", 123);
  • Toggles a boolean value at the specified path

    Parameters

    • path: string

    Returns Promise<boolean>

    const entity = await workflow.entity();
    await entity.toggle("settings.enabled");
  • Creates an efficient GIN index for a specific entity field to optimize queries.

    Parameters

    • entity: string
    • field: string
    • hotMeshClient: HotMesh
    • indexType: "gin" = 'gin'

    Returns Promise<void>

    await Entity.createIndex('user', 'email', hotMeshClient);
    await Entity.createIndex('user', 'status', hotMeshClient);
  • Finds entity records matching complex conditions using JSONB/SQL queries. This is a readonly operation that queries across all entities of a given type.

    Parameters

    • entity: string
    • conditions: Record<string, any>
    • hotMeshClient: HotMesh
    • Optionaloptions: {
          limit?: number;
          offset?: number;
      }
      • Optionallimit?: number
      • Optionaloffset?: number

    Returns Promise<any[]>

    // Basic find with simple conditions
    const activeUsers = await Entity.find(
    'user',
    { status: 'active', country: 'US' },
    hotMeshClient
    );

    // Complex query with comparison operators
    const seniorUsers = await Entity.find(
    'user',
    {
    age: { $gte: 65 },
    status: 'active',
    'preferences.notifications': true
    },
    hotMeshClient,
    { limit: 10, offset: 0 }
    );

    // Query with multiple conditions and nested objects
    const premiumUsers = await Entity.find(
    'user',
    {
    'subscription.type': 'premium',
    'subscription.status': 'active',
    'billing.amount': { $gt: 100 },
    'profile.verified': true
    },
    hotMeshClient,
    { limit: 20 }
    );

    // Array conditions
    const taggedPosts = await Entity.find(
    'post',
    {
    'tags': { $in: ['typescript', 'javascript'] },
    'status': 'published',
    'views': { $gte: 1000 }
    },
    hotMeshClient
    );
  • Finds entity records matching a specific field condition using JSONB/SQL queries. Supports various operators for flexible querying across all entities of a type.

    Parameters

    • entity: string
    • field: string
    • value: any
    • operator:
          | "="
          | ">="
          | "<="
          | "!="
          | ">"
          | "<"
          | "LIKE"
          | "IN" = '='
    • hotMeshClient: HotMesh
    • Optionaloptions: {
          limit?: number;
          offset?: number;
      }
      • Optionallimit?: number
      • Optionaloffset?: number

    Returns Promise<any[]>

    // Basic equality search
    const activeUsers = await Entity.findByCondition(
    'user',
    'status',
    'active',
    '=',
    hotMeshClient,
    { limit: 20 }
    );

    // Numeric comparison
    const highValueOrders = await Entity.findByCondition(
    'order',
    'total_amount',
    1000,
    '>=',
    hotMeshClient
    );

    // Pattern matching with LIKE
    const gmailUsers = await Entity.findByCondition(
    'user',
    'email',
    '%@gmail.com',
    'LIKE',
    hotMeshClient
    );

    // IN operator for multiple values
    const specificProducts = await Entity.findByCondition(
    'product',
    'category',
    ['electronics', 'accessories'],
    'IN',
    hotMeshClient
    );

    // Not equals operator
    const nonPremiumUsers = await Entity.findByCondition(
    'user',
    'subscription_type',
    'premium',
    '!=',
    hotMeshClient
    );

    // Date comparison
    const recentOrders = await Entity.findByCondition(
    'order',
    'created_at',
    new Date('2024-01-01'),
    '>',
    hotMeshClient,
    { limit: 50 }
    );
  • Finds a specific entity record by its ID using direct JSONB/SQL queries. This is the most efficient method for retrieving a single entity record.

    Parameters

    • entity: string
    • id: string
    • hotMeshClient: HotMesh

    Returns Promise<any>

    // Basic findById usage
    const user = await Entity.findById('user', 'user123', hotMeshClient);

    // Example with type checking
    interface User {
    id: string;
    name: string;
    email: string;
    preferences: {
    theme: 'light' | 'dark';
    notifications: boolean;
    };
    }

    const typedUser = await Entity.findById<User>('user', 'user456', hotMeshClient);
    console.log(typedUser.preferences.theme); // 'light' | 'dark'

    // Error handling example
    try {
    const order = await Entity.findById('order', 'order789', hotMeshClient);
    if (!order) {
    console.log('Order not found');
    return;
    }
    console.log('Order details:', order);
    } catch (error) {
    console.error('Error fetching order:', error);
    }