orchid-orm icon indicating copy to clipboard operation
orchid-orm copied to clipboard

How to create materialized view?

Open mordechaim opened this issue 1 year ago • 5 comments

There's the createView() method I can use in migrations, but there's no option to make it a materialized view. How can I create one inside a migration?

mordechaim avatar Aug 12 '24 15:08 mordechaim

Seems like adding an option would be the simplest:

https://github.com/romeerez/orchid-orm/blob/c9a3f8adf970c964d7d7bd60022b6f60edf0cc06/packages/rake-db/src/migration/createView.ts#L48-L50

mordechaim avatar Aug 12 '24 16:08 mordechaim

We probably also need a way to refresh the view in js code, without the need to use raw sql

mordechaim avatar Aug 12 '24 16:08 mordechaim

I'll need to learn this topic and see what's needed besides the MATERIALIZED keyword.

We probably also need a way to refresh the view in js code

Right, I see there is a REFRESH SQL statement, it should be supported.

romeerez avatar Aug 12 '24 19:08 romeerez

Some time has passed, and I changed my view (pun intended) on managing views in orchid.

Instead of writing a migration with createView() I'm using raw SQL in the migrations/recurrent directory and it works beautifully. I use CREATE OR REPLACE since it runs on every migration.

With this approach it's much simpler to keep the view in source control, and updating the view declaration doesn't require creating new migrations every time.

Such a great feature!

mordechaim avatar Sep 01 '24 03:09 mordechaim

Cool, I finally got here to answer that I'm glad you found a way with recurring migrations and raw SQL.

When something is missing in the ORM, it's generally possible to bypass it with raw SQL, and eventually I'll cover what's missing with a TypeScript DSL.

In this case, it would be ideal to write your views (materialized or not) closer to the app logic, and to be able to rely on view types, and the migration generator should learn how to overwrite views in the recurring directory.

romeerez avatar Oct 06 '24 18:10 romeerez