Last updated
Last updated
View entity is a class that maps to a database view. You can create a view entity by defining a new class and mark it with @ViewEntity()
:
@ViewEntity()
accepts following options:
name
- view name. If not specified, then view name is generated from entity class name.
database
- database name in selected DB server.
schema
- schema name.
expression
- view definition. Required parameter.
dependsOn
- List of other views on which the current views depends. If your view uses another view in it's definition, you can add it here so that migrations are generated in the correct order.
expression
can be string with properly escaped columns and tables, depend on database used (postgres in example):
or an instance of QueryBuilder
Note: parameter binding is not supported due to drivers limitations. Use the literal parameters instead.
Each view entity must be registered in your data source options:
To map data from view into the correct entity columns you must mark entity columns with @ViewColumn()
decorator and specify these columns as select statement aliases.
example with string expression definition:
example using QueryBuilder:
You can specify view column options in @ViewColumn
:
List of available options in ViewColumnOptions
:
name: string
- Column name in the database view.
transformer: { from(value: DatabaseType): EntityType, to(value: EntityType): DatabaseType }
- Used to unmarshal properties of arbitrary type DatabaseType
supported by the database into a type EntityType
. Arrays of transformers are also supported and are applied in reverse order when reading. Note that because database views are read-only, transformer.to(value)
will never be used.
There's support for creation of indices for materialized views if using PostgreSQL
.
However, unique
is currently the only supported option for indices in materialized views. The rest of the indices options will be ignored.
Lets create two entities and a view containing aggregated data from these entities:
then fill these tables with data and request all data from PostCategory view:
the result in postCategories
will be:
and in postCategory
:
View Column options define additional options for your view entity columns, similar to for regular entities.