@fluentity/core
    Preparing search index...

    Class RelationBuilder<T>

    Base class for building and managing relationships between models. Provides methods for querying related models and building API requests. This class is extended by HasOneRelationBuilder and HasManyRelationBuilder to implement specific relationship behaviors.

    class UserPosts extends RelationBuilder<Post> {
    // Custom relationship logic
    }

    Type Parameters

    • T extends Model<any>

      The type of model this relation builder works with

    Indexable

    • [key: string]: any

      Type definition for dynamic scope methods that can be added at runtime. Allows for custom query scopes to be added to the builder.

    Index

    Constructors

    • Creates a new relation builder instance. Sets up the query builder and configures the resource path.

      Type Parameters

      Parameters

      • model: T

        The model instance to build relations for

      • queryBuilder: QueryBuilder

        Query builder instance for constructing API requests

      • Optionalresource: string

        Optional custom resource name for the relation

      Returns RelationBuilder<T>

      If the model or query builder is invalid

    Properties

    queryBuilder: QueryBuilder

    Query builder instance for constructing API URLs and managing query parameters. Used internally to build the request URL and parameters.

    relatedModel: T

    The related model instance that this builder operates on. Used to create new instances of the related model.

    Accessors

    • get fluentity(): Fluentity
      Protected

      Gets the Fluentity instance for making API requests. Provides access to the singleton instance that manages API communication.

      Returns Fluentity

      The singleton Fluentity instance

      If Fluentity has not been initialized

    Methods

    • Adds filter conditions to the query. Supports more complex filtering operations than where().

      Parameters

      • filters: Record<string, any>

        Object containing filter conditions

      Returns RelationBuilder<T>

      The relation builder instance for chaining

      const posts = await user.posts.filter({
      created_at: { gt: '2023-01-01' },
      status: ['published', 'draft']
      }).all();
    • Fetches a model instance by ID from the API. Makes a GET request to retrieve the model data.

      Parameters

      • id: string | number

        The ID of the model to fetch

      Returns Promise<T>

      A promise that resolves to the fetched model instance

      If the model is not found

      const post = await user.posts.find(123);
      
    • Gets a model instance by ID without making an API request. Creates a new model instance with the given ID for local operations.

      Parameters

      • id: string | number

        The ID of the model to get

      Returns T

      A new model instance with the given ID

      const post = user.posts.id(123);
      
    • Limits the number of results returned. Restricts the query to return at most n results.

      Parameters

      • n: number

        The maximum number of results to return

      Returns RelationBuilder<T>

      The relation builder instance for chaining

      const posts = await user.posts.limit(10).all();
      
    • Sets the offset for pagination in the query results. Skips n records before starting to return results.

      Parameters

      • n: number

        The number of records to skip

      Returns RelationBuilder<T>

      The relation builder instance for chaining

      const posts = await user.posts.offset(20).limit(10).all(); // Get posts 21-30
      
    • Adds an order by clause to the query. Sorts the results by the specified field and direction.

      Parameters

      • sort: string = 'id'

        The field to order by (default: 'id')

      • direction: string = 'asc'

        The direction to order in ('asc' or 'desc', default: 'asc')

      Returns RelationBuilder<T>

      The relation builder instance for chaining

      const posts = await user.posts.orderBy('created_at', 'desc').all();
      
    • Adds a where clause to the query. Filters results based on exact field-value matches.

      Parameters

      • where: Record<string, any>

        Object containing field-value pairs to filter by

      Returns RelationBuilder<T>

      The relation builder instance for chaining

      const posts = await user.posts.where({ published: true }).all();