Skip to content

Schemas

In AMSDAL simply define your data models using a simple JSON format...

{
    "title": "Person",
    "type": "object",
    "properties": {
        "first_name": {
            "type": "string",
            "default": "John"
        },
        "last_name": {
            "type": "string",
            "default": "Doe"
        }
    },
    "required": []
}
...and AMSDAL automatically generates Python model classes...

class Person(Model):
    schema_type: ClassVar[SchemaTypes] = SchemaTypes.USER
    class_fingerprint: ClassVar[str] = '...'
    first_name: Optional[str] = 'John'
    last_name: Optional[str] = 'Doe'

...and any connection/database schema.

CREATE TABLE person (
    `partition_key` TEXT NOT NULL,
    `_metadata` JSON NOT NULL,
    `first_name` TEXT DEFAULT 'John',
    `last_name` TEXT DEFAULT 'Doe',
    PRIMARY KEY (partition_key)
)

Class Object

[required] title : string

A string type that represents the name of your model class (it does not allow special symbols)

Best-practice naming conventions Examples
Start each word with a capital letter. Do not separate words with underscores. This style is called camel case or pascal case. Person , MyModel

[required] type : string

All models inherit from a base model class: object

Models can also inherit from any other model by setting its type to the parent model_name:

{
    "title": "PersonWithNickname",
    "type": "Person",
    "properties": {
        "nickname": {
            "type": "string"
        }
    },
    "required": []
}

[required] properties : dictionary[ string : ClassProperty ]

A key-value object that define the list of properties in the model.

Best-practice naming conventions Examples
Use a lowercase word or words. Separate words with underscores to improve readability. age, full_name, date_of_birthday

The value of property is a JSON object that describes the property, and it's type. The format depends on the type of the property.

Built-in property types

Type Example (required)
string {"type": "string"}
number {"type": "number"}
boolean {"type": "boolean"}
array {"type": "array", "items": {"type": "string"}}
dictionary {"type": "dictionary", "items": {"key": {"type": "string"}, "value": {"type": "string}}}
anything {"type": "anything"}

The entire list of supported built-in types can be found here.

References/relationships

You can us any other model name as a type to create a property-reference to another object.

{
    "title": "Family",
    "type": "object",
    "properties": {
        "mom": {
            "type": "Person"
        },
        "dad": {
            "type": "Person" 
        },
        "children": {
            "type": "array",
            "items" : {
                "type": "Person"
            }
        }
    },
    "required": []
}

Additionally, there are few more optional properties you can use.

[optional] default : anything

A parameter that allows you to specify the default value for model's property.

For example:

{
    "title": "Person",
    "type": "object",
    "properties": {
        "age": {
            "type": "number",
            "default": 18
        }
    },
    "required": []
}

In the example if we create a new instance of Person model without specifying the age property, it will be automatically set to 18.

[optional] title : string

The title paramater represents a readable name of property as text in any format:

{
    "title": "Person",
    "type": "object",
    "properties": {
        "age": {
            "type": "number",
            "title": "Person's age"
        }
    },
    "required": []
}

[optional] options : array

The options parameter provides you ability to specify a list of available values for this property as a basic validation, for example:

{
    "title": "Person",
    "type": "object",
    "properties": {
        "country": {
            "type": "string",
            "options": [
                {
                    "value": "USA",
                    "key": "United States"
                },
                {
                    "value": "UK",
                    "key": "United Kingdom"
                }
            ]
        }
    },
    "required": []
}

In the example above we specified the list of available countries for the country property. If you try to create a new instance of Person model with the country property set to e.g. DE, AMSDAL will throw a validation error. The key is just a label or title for the value.

[required] required : array[string]

The required specifies list of property names that are required in this model.

For example:

{
    "title": "Person",
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "age": {
            "type": "number",
            "title": "Person age"
        }
    },
    "required": [
        "name"
    ]
}

In this example, the model Person has two properties, name and age, but only name is required. If you try to create a new instance of Person model without specifying a name, AMSDAL will throw a validation error.

[optional] unique : array[array[string]]

The unique allows you to specify which properties must be unique for this model.

For example:

{
    "title": "Person",
    "type": "object",
    "properties": {
        "email": {
            "type": "string"
        },
        "first_name": {
            "type": "string"
        },
        "last_name": {
            "type": "string"
        }
    },
    "required": [
        "first_name",
        "last_name"
    ],
    "unique": [
        [
            "email"
        ],
        [
            "first_name",
            "last_name"
        ]
    ]
}

Note, unique is a list of lists of property names that provide you the ability to specify composite unique constraints. In the example above we created two unique constraints:

  • the first one based on the single email property
  • the second one is a composite one based on the two properties: first_name and last_name

Model folder hyarchy

└── πŸ“ models
    └── πŸ“ my_model
        β”œβ”€β”€ πŸ“Β fixtures
            β”œβ”€β”€ πŸ“Β files
                β”œβ”€β”€ πŸ“„ my_file.png
            β”œβ”€β”€ πŸ“„ my_fixtures.json
            β”œβ”€β”€ πŸ“„ my_fixtures.csv
        β”œβ”€β”€ πŸ“Β hooks
            β”œβ”€β”€ πŸ“„ post_create.py
            β”œβ”€β”€ πŸ“„ pre_delete.py
        β”œβ”€β”€ πŸ“Β modifiers
        β”œβ”€β”€ πŸ“Β properties
        β”œβ”€β”€ πŸ“„ model.json