def _create_test_entity(columns: List[Column],
                        identifier_column: IdentifierColumn) -> Entity:
    return create_entity(
        class_name=_random_string(24),
        identifier_column=identifier_column,
        columns=columns,
    )
def _create_schema(context: Any, module_name: Optional[str] = None):
    entity_name = context.entity_name if hasattr(context, 'entity_name') else None
    operations = context.operations if hasattr(context, 'operations') else all_operations
    entity = create_entity(
        class_name=_random_string(36) if entity_name is None else entity_name,
        identifier_column=context.identifier_column,
        columns=context.columns,
        operations=operations,
    )
    module_name = _random_string(14) if module_name is None else module_name
    schema = create_schema(
        module_name='output.{}'.format(module_name),
        entities=[entity],
        file_path=['output', module_name],
    )
    context.schema = schema
    context.module_name = 'output.{}'.format(module_name)
    context.module_path = 'output/{}'.format(module_name)
Esempio n. 3
0
book_entity = create_entity(
    class_name='Book',
    identifier_column=create_identifier_column(
        name='book_id',
        type_option=TypeOption.UUID,
    ),
    columns=[
        create_column(
            name='name',
            type_option=TypeOption.string,
            index=True,
            nullable=False,
        ),
        create_column(
            name='rating',
            type_option=TypeOption.float,
            index=True,
            nullable=False,
        ),
        create_column(
            name='author_id',
            type_option=TypeOption.int,
            foreign_key_relationship=ForeignKeyRelationship(
                target_entity='author',
                target_entity_identifier_column_type=TypeOption.UUID,
                sqlalchemy_options={'ondelete': '"CASCADE"'},
            ),
        ),
        create_column(
            name='collaborator_id',
            type_option=TypeOption.int,
            nullable=True,
            foreign_key_relationship=ForeignKeyRelationship(
                target_entity='author',
                target_entity_identifier_column_type=TypeOption.UUID,
            ),
        ),
        create_column(
            name='published',
            type_option=TypeOption.date,
            # alias='date_published',
        ),
        create_column(
            name='created',
            type_option=TypeOption.datetime,
            sqlalchemy_options={
                'server_default': 'text(\'CURRENT_TIMESTAMP\')',
            },
        ),
        create_column(
            name='updated',
            type_option=TypeOption.datetime,
            sqlalchemy_options={
                'default': 'datetime.datetime.utcnow',
                'onupdate': 'datetime.datetime.utcnow',
            },
        ),
    ],
    relationships=[
        create_relationship(
            source_foreign_key_column_name='author_id',
            source_identifier_column_name='book_id',
            target_identifier_column_name='author_id',
            target_entity_class_name='Author',
            property_name='author',
            nullable=False,
            lazy=False,
            join=JoinOption.to_one,
        ),
        create_relationship(
            source_foreign_key_column_name='collaborator_id',
            source_identifier_column_name='book_id',
            target_identifier_column_name='author_id',
            target_entity_class_name='Author',
            key_alias_in_json='collaborator_id',
            property_name='collaborator',
            nullable=True,
            lazy=False,
            join=JoinOption.to_one,
        ),
        create_relationship(
            target_entity_class_name='Review',
            source_identifier_column_name='book_id',
            source_foreign_key_column_name=None,
            target_identifier_column_name='review_id',
            property_name='reviews',
            nullable=False,
            lazy=True,
            join=JoinOption.to_many,
        ),
        create_relationship(
            target_entity_class_name='Genre',
            source_identifier_column_name='book_id',
            source_foreign_key_column_name=None,
            target_identifier_column_name='genre_id',
            nullable=False,
            lazy=False,
            join=JoinOption.to_one,
            join_table='book_genre',
            property_name='genre',
        ),
        create_relationship(
            target_entity_class_name='Book',
            source_identifier_column_name='book_id',
            source_foreign_key_column_name=None,
            target_identifier_column_name='book_id',
            target_foreign_key_column_name='book1_id',
            nullable=False,
            lazy=True,
            join=JoinOption.to_many,
            join_table='related_book',
            property_name='related_books',
            secondary_join_name='book2_id',
        ),
    ],
    operations=all_operations,
    api_paths=[
        create_api_path(
            joined_entities=[
                'genre',
            ],
            route='genres',
        ),
    ],
)