A Model in Gadget represents a data structure that defines the shape of the data stored in the Gadget database (powered by Postgres). It consists of fields, relationships, and validations that can be used to build data models for your application.
Models in Gadget are similar to tables in traditional SQL-based relational databases.
How models work
Models operate within the database to define the structure and rules for storing data. Gadget's built-in database is relational, which means it stores data in rows within tables. In Gadget, rows are referred to as records, columns are fields, and tables are models.
How to create models
You can create a model by adding fields that will store the data of your application, and relationships that will store the id of related records from other models. Fields and relationships an take validations to ensure that only desired data is accepted, as well as configurations that define the field's behavior. The process of creating models is structured by utilizing fields, validations, and relationships.
Fields are the individual components within a model that define the attributes or properties of the data being stored. These fields can represent various types of data, such as strings, numbers, dates, or booleans, allowing for flexibility in data representation. Validations are an extension of fields containing rules and constraints applied to the fields to ensure the integrity and correctness of the data. They enable developers to enforce specific criteria, such as data type, length, presence, uniqueness, or custom conditions.
Relationships can then be used to establish connections between different models, enabling the representation of complex associations and dependencies. These relationships can be defined as belongs to, has one, has many, and has many through, providing a structured way to navigate and query related data. By combining fields, validations, and relationships, developers can create robust and organized models that accurately represent and manipulate data within their applications.
Relationships
In Gadget, relationships establish connections between models, allowing the representation of complex associations and dependencies. These relationships—belongs to, has one, has many, and has many through—provide a structured way to navigate and query related data, ensuring organized and robust models.
Each model has its own database table, with relationships being directional, meaning one model holds a reference to another. The cardinality of relationships includes:
One-to-One: A single record in one model links to a single record in another.
One-to-Many: A single record in one model relates to multiple records in another.
Many-to-Many: Multiple records in one model relate to multiple records in another.
Relationships are implemented using belongs to fields, which store the ID of the related record. In a one-to-many relationship, the ID of the "one" is stored on the "many" side. For example, in a Project-Todo relationship, each Todo stores a project, referencing its associated Project, while the Project retrieves its related Todos dynamically via the has many, relationship. Many-to-many relationships are implemented with a has many through, relationship, where a new model is created to store ids on both sides of the relation.
Data Retrieval
When fetching data, belongs to fields return full related records instead of just IDs, performing lookups at read time without caching. The has one or has many fields in the parent model enables dynamic querying of related records.
API Generation Made Easy
Gadget simplifies API generation by automatically resolving linked data. API requests including a belongs to field return full related records, eliminating the need for manual joins. Likewise, has one or has many fields allow querying related records efficiently. Using the include parameter, developers can seamlessly retrieve related data, streamlining API responses and reducing redundant queries.
Foreign Key Enforcement
For performance reasons, Gadget does not enforce foreign key constraints, meaning a belongs to field may reference a record that no longer exists.
This approach ensures efficient and flexible model relationships in Gadget.