Mark a Model subclass as a database materialized view.
tableName
refresh()
@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 }); Copy
@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 });
Mark a Model subclass as a database materialized view.
tableNameoption to the view name so findAll() etc. work correctly.refresh()static method for refreshing the view data.