GithubHelp home page GithubHelp logo

icedragon200 / ecto_facade Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azranel/ecto_facade

0.0 0.0 0.0 34 KB

Separate your read and write operations in replicated database setup

License: MIT License

Elixir 100.00%

ecto_facade's Introduction

EctoFacade

EctoFacade can be used as interface through which you can query multiple repositories. Main objective of this is to use separate separate repository for writes and separate repository for reads.

For example:

Lets say you have PostgreSQL database replicated to 4 different slave nodes that should be used for reads only. With this small library you can do that!

Installation

def deps do
  [
    {:ecto_facade, "~> 0.2.0"}
  ]
end

Usage

Here is example on how to use EctoFacade in your application:

defmodule MyApp.FacadeRepo do
  use EctoFacade.Repo,
    master_repo: MyApp.Repo,
    read_repos: [MyApp.ReadReplicaOne, MyApp.ReadReplicaTwo],
    algorithm: MyApp.Repo.ReadAlgorithm,
    fallback_to_master: false
end

MyApp.FacadeRepo.all(MyApp.SomeSchema) # and other operations that you would normally do with ecto repo

Options you can pass when using EctoFacade.Repo are:

  • master_repo - this is master repository that will be used for all write operations
  • read_repos - this is list of read repositories that will be used for all read operations. Which repository will be used depends on algorithm.
  • algorithm - this is module that implements behaviour EctoFacade.Algorithm and picks read repository for next read operation.
  • fallback_to_master - if set to true then read operation that won't work on read repository, will fallback to master repostiory. If set to false, it will throw exception.

Possible algorithms for selecting repository for read operation:

  • EctoFacade.Algorithms.Random - selects randomly repository
  • EctoFacade.Algorithms.Roundrobin - selects next repository in list till last and then goes again from beginning
  • EctoFacade.Algorithms.WeightRoundrobin - selects next repository in list depending on weight. Check module documentation for more info.

or your own that implement behaviour EctoFacade.Algorithm.

for more information please check documentation

How to write my own algorithm?

This is pretty straightforward. Here is implementation of EctoFacade.Algorithms.Random:

defmodule EctoFacade.Algorithms.Random do
  @moduledoc """
  Default algorithm that just returns random read repository
  """
  @behaviour EctoFacade.Algorithm

  def get_repo(repos) do
    Enum.random(repos)
  end
end

Usage (testing)

When in test just configure you FacadeRepo to use only master repository. This way you won't have issues with separate repositories with Sandbox Ecto adapter.

Documentation

Hex.pm

ecto_facade's People

Contributors

novaugust avatar hauleth avatar

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.