Git Product home page Git Product logo

ecto_observable's Introduction

Ecto Observable

Build Status Ecto.Observable Version

Ecto Observable adds "observable" functionality to Ecto.Repo.

Installation

The package can be installed by adding ecto_observable to your list of dependencies in mix.exs:

def deps do
  [
    {:ecto_observable, "~> 0.4"}
  ]
end

Documentation

See HexDocs for detailed documentation.

Example

To get started we must add observability to our repo:

defmodule Repo do
  use Ecto.Repo, otp_app: :my_app
  use Observable.Repo
end

We can now create an observer:

defmodule SubscribersObserver do
  use Observable, :observer

  # Lets ignore posts that dont have any topics.
  def handle_notify(:insert, {_repo, _old, %Post{topics: []}}) do
    :ok
  end

  def handle_notify(:insert, {_repo, _old, %Post{topics: topics}}) do
    # Do work required to inform subscribed users.
  end

  # Defined for the sake of example. Ignore me!
  def handle_notify(:update, {_repo, old, new}) do
    :ok
  end

  # Defined for the sake of example. Ignore me!
  def handle_notify(:delete, {_repo, old, new}) do
    :ok
  end
end

And modify our Post schema to notify our observer:

defmodule Post do
  use Ecto.Schema
  use Observable, :notifier

  schema "posts" do
    field(:title, :string)
    field(:body, :string)
    field(:topics, {:array, :string}, default: [])
  end

  observations do
    action(:insert, [SubscribersObserver])
    action(:update, [SubscribersObserver])
    action(:delete, [OtherObserverOne, OtherObserverTwo]) # Defined for the sake of example.
  end
end

Which allows us to use the notify functionality:

def create_post(params \\ %{}) do
  %Post{}
  |> Post.changeset(params)
  |> Repo.insert_and_notify()
end

Please see the documentation for more details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.