@fluentity/core
    Preparing search index...

    Class Model<T>

    Base class for all models in the ORM. Provides core functionality for interacting with the API and managing model data. Handles CRUD operations, relationships, and query building.

    class User extends Model<UserAttributes> {
    static resource = 'users';
    }

    Type Parameters

    • T extends Attributes = Attributes

      The type of attributes this model will have, must extend Attributes

    Indexable

    • [key: string]: any

      Index signature for dynamic properties. Allows models to have additional properties beyond their defined attributes.

    Index

    Constructors

    • Creates a new model instance with the given attributes. Initializes the query builder and sets up the model's state.

      Type Parameters

      Parameters

      • attributes: T

        The attributes to initialize the model with

      • OptionalqueryBuilder: QueryBuilder

        Optional query builder instance to use instead of creating a new one

      Returns Model<T>

      A new model instance

      If required attributes are missing

    Properties

    id?: string | number

    Unique identifier for the model instance. Can be either a string or number, depending on the API's ID format.

    resource: string

    Resource endpoint for the model, used to construct API URLs. Must be set by subclasses to define the API endpoint.

    static resource = 'users';
    
    scopes?: Record<string, (query: RelationBuilder<any>) => RelationBuilder<any>>

    Custom query scopes that can be applied to model queries. Each scope is a function that modifies the query builder behavior.

    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

    • get queryBuilder(): QueryBuilder
      Protected

      Gets the query builder instance for this model. Used internally for constructing API requests.

      Returns QueryBuilder

      The query builder instance

    Methods

    • Deletes the model instance from the server.

      Returns Promise<void>

      Promise that resolves when the deletion is complete

      If the deletion fails

      await user.delete();
      
    • Retrieves the current model instance from the server. Updates the local instance with fresh data from the API.

      Returns Promise<Model<T>>

      Promise resolving to the updated model instance

      If the record is not found

      await user.get(); // Refreshes user data from the server
      
    • Saves the current model instance to the server. Creates a new record if the model doesn't have an ID, updates existing record otherwise.

      Returns Promise<Model<T>>

      Promise resolving to the saved model instance

      If the save operation fails

      user.name = 'John Doe';
      await user.save(); // Creates or updates the user
    • Converts the model instance to a plain object. Recursively converts nested model instances to plain objects.

      Returns Record<string, any>

      A plain object representation of the model's attributes

      const userData = user.toObject();
      
    • Updates the model instance with new attributes and saves to the server.

      Parameters

      • Optionalattributes: Partial<T>

        Optional attributes to update before saving

      • method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" = Methods.PUT

        The HTTP method to use for the update (PUT or PATCH)

      Returns Promise<Model<T>>

      Promise resolving to the updated model instance

      If the update fails

      await user.update({ name: 'John Doe' });
      
    • Retrieves all records for the model. Fetches all records from the API without any filtering.

      Type Parameters

      Parameters

      Returns Promise<T[]>

      Promise resolving to an array of model instances

      const allUsers = await User.all();
      
    • Creates a new record. Sends a POST request to create a new record in the API.

      Type Parameters

      Parameters

      Returns Promise<T>

      Promise resolving to the created model instance

      If the creation fails

      const user = await User.create({ name: 'John', email: 'john@example.com' });
      
    • Deletes a record by ID. Sends a DELETE request to remove a record from the API.

      Type Parameters

      Parameters

      • this: Constructor<T>
      • id: string | number

        The ID of the record to delete

      Returns Promise<void>

      Promise that resolves when the deletion is complete

      If the deletion fails

      await User.delete(123);
      
    • Starts a query with filter conditions. Similar to where() but specifically for filter operations.

      Type Parameters

      Parameters

      • this: Constructor<T>
      • filters: Record<string, any>

        Filter conditions to apply, as field-value pairs

      Returns HasManyRelationBuilder<T>

      A HasManyRelationBuilder instance with filters applied

      const users = await User.filter({ age: { gt: 18 } }).all();
      
    • Finds a single record by ID. Fetches a specific record from the API by its ID.

      Type Parameters

      Parameters

      • this: Constructor<T>
      • id: string | number

        The ID of the record to find

      Returns Promise<T>

      Promise resolving to a model instance

      If the record is not found

      const user = await User.find(123);
      
    • Creates a new model instance with the given ID. Useful for creating model instances when only the ID is known.

      Type Parameters

      Parameters

      • this: Constructor<T>
      • id: string | number

        The ID to assign to the new model instance

      Returns T

      A new model instance with the specified ID

      const user = User.id(123);
      
    • Starts a query with relations to include. Specifies which related models should be loaded with the query.

      Type Parameters

      Parameters

      • this: Constructor<T>
      • relations: string | string[]

        Single relation or array of relations to include

      Returns HasManyRelationBuilder<T>

      A HasManyRelationBuilder instance with relations included

      const users = await User.include(['posts', 'profile']).all();
      
    • Starts a new query builder for the model. Returns a HasManyRelationBuilder for querying multiple records.

      Type Parameters

      Parameters

      Returns HasManyRelationBuilder<T>

      A HasManyRelationBuilder instance for building queries

      const users = await User.query().where({ active: true }).all();
      
    • Updates an existing record. Sends a PUT/PATCH request to update a record in the API.

      Type Parameters

      Parameters

      • this: Constructor<T>
      • id: string | number

        The ID of the record to update

      • data: A

        The data to update the record with

      • method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" = Methods.PUT

        The HTTP method to use for the update (PUT or PATCH)

      Returns Promise<T>

      Promise resolving to the updated model instance

      If the update fails

      const user = await User.update(123, { name: 'John Doe' });
      
    • Starts a query with a where clause. Shorthand for query().where() for common filtering operations.

      Type Parameters

      Parameters

      Returns HasManyRelationBuilder<T>

      A HasManyRelationBuilder instance with where conditions applied

      const activeUsers = await User.where({ active: true }).all();