hauleth

hauleth

EctoView - package for creating and managing SQL views

Ecto supports SQL views, even though these aren’t used that often. I think that one of the main reasons why these aren’t used is that creating them in migrations isn’t that simple (another one is that plain, non-materialised, views aren’t that needed when using Ecto).

I decided to write small library that will help with that.

This provide 2 functionalities:

Simplify writing views by utilising familiar Ecto.Query:

defmodule MyApplication.Repo.Migration.CreateViewFoo do
  use Ecto.Migration
  use EctoView.Migration # This line **MUST** be after `use Ecto.Migration`

  def change do
    query = from foo in "foos", # This **MUST** select from table name, no schemas allowed
      where: foo.bar == 2137,
      select: %{ # This **MUST** be a map of selected fields
        a: foo.a,
        b: foo.b
      }

    create view("foos_view", query)
  end
end

This will create view foos_view that is created with query SELECT a AS a, b AS b FROM foos.

There are restrictions for queries though - due to nature of migrations, we cannot rely on schemas in application, so the query must use raw table names passed as string. In addition to that, we need to provide select which must be a map of selected values.

Materialised views can be created the same way as plain views, just use materialised_view/2 instead of view/2.

Additional functionality is helper function for refreshing materialised views:

defmodule MyApplication.Repo do
  use Ecto.Repo, # …
  use EctoView

  # …
end

And now you can use Repo.refresh_materialized_view("my_view") to refresh content of that view.

There currently is no mechanism to automate refreshments of materialised views and that is left to the user to implement one.

First Post!

al2o3cr

al2o3cr

Haven’t tried it yet, but would it work to declare the specific schema-bits that the migration cares about locally (inside defmodule MyApplication.Repo.Migration.CreateViewFoo)?

That’s an Elixir translation of an approach I’ve used before in Rails migrations.

Most Liked

hauleth

hauleth

Theoretically you could, but IMHO that wouldn’t be that different from simply using select in query with a map. Writing something like:

defmodule MyApplication.Repo.Migration.CreateViewFoo do
  use Ecto.Migration
  use EctoView.Migration # This line **MUST** be after `use Ecto.Migration`

  defmodule Foo do
    use EctoView.Schema

    migration_schema "foos" do
      field :a
      field :b
      field :bar
    end
  end

  def change do
    query = from foo in Foo,
      where: foo.bar == 2137,

    create view("foos_view", query)
  end
end

For me isn’t more readable. It introduces needless new module and a lot of visual cruft, where simple Ecto.Query can contain all that information without need for such magic.

Where Next?

Popular in Announcing Top

handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
kip
ex_cldr provides localisation and internationalisation support based upon the data from the Unicode CLDR project. Unicode released CLDR ...
407 13366 120
New
ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
Flo0807
Hello everyone! I am excited to share our heart project Backpex with you. After building several Phoenix applications, we realized that...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement