The type of attributes this model will have, must extend Attributes
Index signature for dynamic properties. Allows models to have additional properties beyond their defined attributes.
Creates a new model instance with the given attributes. Initializes the query builder and sets up the model's state.
The attributes to initialize the model with
Optional
queryBuilder: QueryBuilderOptional query builder instance to use instead of creating a new one
A new model instance
Optional
idUnique identifier for the model instance. Can be either a string or number, depending on the API's ID format.
Static
resourceResource endpoint for the model, used to construct API URLs. Must be set by subclasses to define the API endpoint.
Static
Optional
scopesCustom query scopes that can be applied to model queries. Each scope is a function that modifies the query builder behavior.
Protected
fluentityProtected
Gets the Fluentity instance for making API requests. Provides access to the singleton instance that manages API communication.
The singleton Fluentity instance
Protected
Gets the query builder instance for this model. Used internally for constructing API requests.
The query builder instance
Updates the model instance with new attributes and saves to the server.
Optional
attributes: Partial<T>Optional attributes to update before saving
The HTTP method to use for the update (PUT or PATCH)
Promise resolving to the updated model instance
Static
allRetrieves all records for the model. Fetches all records from the API without any filtering.
Promise resolving to an array of model instances
Static
createCreates a new record. Sends a POST request to create a new record in the API.
The data to create the record with
Promise resolving to the created model instance
Static
deleteDeletes a record by ID. Sends a DELETE request to remove a record from the API.
The ID of the record to delete
Promise that resolves when the deletion is complete
Static
filterStarts a query with filter conditions. Similar to where() but specifically for filter operations.
Filter conditions to apply, as field-value pairs
A HasManyRelationBuilder instance with filters applied
Static
findFinds a single record by ID. Fetches a specific record from the API by its ID.
The ID of the record to find
Promise resolving to a model instance
Static
idCreates a new model instance with the given ID. Useful for creating model instances when only the ID is known.
The ID to assign to the new model instance
A new model instance with the specified ID
Static
includeStarts a query with relations to include. Specifies which related models should be loaded with the query.
Single relation or array of relations to include
A HasManyRelationBuilder instance with relations included
Static
queryStarts a new query builder for the model. Returns a HasManyRelationBuilder for querying multiple records.
A HasManyRelationBuilder instance for building queries
Static
updateUpdates an existing record. Sends a PUT/PATCH request to update a record in the API.
The ID of the record to update
The data to update the record with
The HTTP method to use for the update (PUT or PATCH)
Promise resolving to the updated model instance
Static
whereStarts a query with a where clause. Shorthand for query().where() for common filtering operations.
Conditions to filter by, as field-value pairs
A HasManyRelationBuilder instance with where conditions applied
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.
Example