Search code examples
nestjstypeorm

Materialized View from NestJS/TypeORM project


I'm trying to create a Materialized view from my NestJS app using TypeORM. The database is a Postgres.

View Entities unfortunately doesn't match requirements: https://www.bookstack.cn/read/TypeORM/view-entities.md

Wished behaviour: just like models, a materialized view is defined in the NestJS project, with the option 'synchronize:true' : the project creates the view on running if it doesn't exist, if it exist, it just sync with it (just like models).

Is there any leads that would help me achieving this?


Solution

  • What I ended up doing was defining the entity as a ViewEntity, setting {materialized:true}.

    However, as you noted, typeorm doesn't handle this so well. I ended up disabling synchronization in the ormconfiguration and running migrations when I wanted to change things in the entity.

    However, once the entity is created, typeorm works out of the box really well.