Vercel Logo
DOCS
DEVELOPER GUIDELINE
DEFINING MODELS

Defining Models

Models in Syncraft serve as blueprints for the various entities within your system. They provide structured representations of these entities by detailing the fields, their types, and the relationships they share with other models. This structured approach ensures data integrity and consistency across different sync sources. Below is a walkthrough on how to define models using the given examples: Product, SessionRecord, and Users.

Model Structure

Each model is defined as a JSON object with several key components:

Name

The unique identifier for the model representing the entity or aspect the model encapsulates.

"name": "Product"

Sync

Indicates the primary data source where the model's data is fetched or stored.

"sync": "Vespa"

DataPath

Points to the specific location or table where the data for the model resides within the sync source.

"dataPath": "products"

Fields

Details the attributes of the model with their types and optional flags.

"fields": { "id": { "type": "number" }, "gender": { "type": "string" }, // ...

Relations

Describes the relationships the model has with other models.

"relations": { "sessions": { "relationType": "oneToMany", "value": "SessionRecord" } }

Defining a Model

Now, let's define a Product model based on the provided structure:

{ "name": "Product", "sync": "Vespa", "dataPath": "products", "fields": { "id": { "type": "number" }, "gender": { "type": "string" }, // ... other fields }, "relations": { "sessions": { "relationType": "oneToMany", "value": "SessionRecord" } } }

In this definition:

  • The Product model is identified and is synchronized with the Vespa sync source.
  • Data for this model resides in the products location within the sync source.
  • The model has various fields each with specified types.
  • It has a one-to-many relationship with the SessionRecord model through the sessions relation.

Defining Relations

In the SessionRecord model, we define two relations: one to Users and another to Product.

"relations": { "user": { "relationType": "manyToOne", "value": "Users", "sync": "Postgres" }, "product": { "relationType": "manyToOne", "value": "Product", "sync": "Vespa" } }

Here:

  • SessionRecord has a many-to-one relation with Users and Product.
  • The sync property indicates where the relation is synchronized, allowing for relations across different sync sources.

Working with Fields and Relations

The fields and relations you define in your models determine the structure and connections of your data. It's crucial to have a well-thought-out model definition to ensure data integrity and enable efficient data retrieval and manipulation.

Defining models in Syncraft is straightforward yet powerful, allowing for a flexible and robust data architecture that can evolve with your system's needs.

This guide provides a basic understanding of defining models in Synccraft. As you get more comfortable with these concepts, you'll find it easier to define and work with models to shape your data in ways that serve your application's needs efficiently.

Learn More
Developer Guideline
Continue Your Journey
Query & Select