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
.
Each model is defined as a JSON object with several key components:
The unique identifier for the model representing the entity or aspect the model encapsulates.
"name": "Product"
Indicates the primary data source where the model's data is fetched or stored.
"sync": "Vespa"
Points to the specific location or table where the data for the model resides within the sync source.
"dataPath": "products"
Details the attributes of the model with their types and optional flags.
"fields": {
"id": {
"type": "number"
},
"gender": {
"type": "string"
},
// ...
Describes the relationships the model has with other models.
"relations": {
"sessions": {
"relationType": "oneToMany",
"value": "SessionRecord"
}
}
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:
Product
model is identified and is synchronized with the Vespa
sync source.products
location within the sync source.SessionRecord
model through the sessions
relation.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
.sync
property indicates where the relation is synchronized, allowing for relations across different sync sources.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.