prorm API Reference
    Preparing search index...

    Function MaterializedView

    • Mark a Model subclass as a database materialized view.

      • Sets the tableName option to the view name so findAll() etc. work correctly.
      • Registers materialized view metadata so the ORM can generate CREATE MATERIALIZED VIEW DDL.
      • Adds a refresh() static method for refreshing the view data.
        @MaterializedView({
      name: 'user_stats_view',
      query: `
      SELECT user_id, COUNT(*) as order_count
      FROM orders
      GROUP BY user_id
      `,
      uniqueIndex: 'user_stats_view_user_id_key',
      })
      class UserStats extends Model<UserStatsAttributes> {
      declare userId: number;
      declare orderCount: number;
      }

      // After Model.init() — generate and create the materialized view:
      const sql = MaterializedViewRegistry.getSQL(UserStats);
      await prorm.query(sql);

      // Or let the ORM create it automatically:
      await MaterializedViewRegistry.createMaterializedView(prorm, UserStats);

      // Query it just like a normal model:
      const stats = await UserStats.findAll();

      // Refresh the materialized view:
      await UserStats.refresh();
      // Or concurrently (requires unique index):
      await UserStats.refresh({ concurrently: true });

      Returns (target: Function) => void