All Projects → Qqwy → elixir-revisionair_ecto

Qqwy / elixir-revisionair_ecto

Licence: other
A Revisionair adapter based on Ecto. Allows you to persist and keep track of revisions of your data structures in any of Ecto's supported databases.

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to elixir-revisionair ecto

fat ecto
Query mechanism for Ecto
Stars: ✭ 20 (+11.11%)
Mutual labels:  ecto, elixir-library
ecto autoslug field
Automatically create slugs for Ecto schemas.
Stars: ✭ 138 (+666.67%)
Mutual labels:  ecto, elixir-library
ex loader
Load a single beam file, apps (a set of beams), or an erlang release (a set of apps) to a node.
Stars: ✭ 12 (-33.33%)
Mutual labels:  elixir-library
ecto trail
EctoTrail allows to store Ecto changeset changes in a separate audit_log table.
Stars: ✭ 51 (+183.33%)
Mutual labels:  ecto
ecto profiler
Project for Ecto DB profiling
Stars: ✭ 16 (-11.11%)
Mutual labels:  ecto
emojix
Simple emoji library for Elixir 💩
Stars: ✭ 21 (+16.67%)
Mutual labels:  elixir-library
music db
A playground for Ecto using a simple music database
Stars: ✭ 29 (+61.11%)
Mutual labels:  ecto
ex operation
A library for making domain operations in Elixir
Stars: ✭ 33 (+83.33%)
Mutual labels:  ecto
gradle-GitVersioner
generates a project version for the given git project to distinguish between builds
Stars: ✭ 80 (+344.44%)
Mutual labels:  revision
querie
Compose Ecto query from the client side
Stars: ✭ 20 (+11.11%)
Mutual labels:  ecto
mongodb ecto
MongoDB adapter for Ecto
Stars: ✭ 348 (+1833.33%)
Mutual labels:  ecto
phoenix pagination
Simple pagination for Ecto and Phoenix that uses plain EEx templates.
Stars: ✭ 20 (+11.11%)
Mutual labels:  ecto
re
"What Are You Syncing About?" – Ninjadev's submission for Revision 2017
Stars: ✭ 69 (+283.33%)
Mutual labels:  revision
autorevision
A script for extracting version information useful in release/build scripting.
Stars: ✭ 73 (+305.56%)
Mutual labels:  revision
strong migrations
Catch unsafe migrations in your Elixir application
Stars: ✭ 58 (+222.22%)
Mutual labels:  ecto
contextual
🌈 Generate your Ecto contexts using this macro and eliminate boilerplate
Stars: ✭ 18 (+0%)
Mutual labels:  ecto
director
Director is a production-ready supervisor and manager for Erlang/Elixir processes that focuses on speed, performance and flexibility.
Stars: ✭ 62 (+244.44%)
Mutual labels:  elixir-library
algoliax
Algolia integration to elixir application
Stars: ✭ 38 (+111.11%)
Mutual labels:  ecto
nested filter
Providing Map#drop (by key or value) and Map#take functionality for nested maps
Stars: ✭ 32 (+77.78%)
Mutual labels:  elixir-library
monetized
A lightweight solution for handling and storing money.
Stars: ✭ 46 (+155.56%)
Mutual labels:  ecto

RevisionairEcto

Hex.pm

A Revisionair adapter based on Ecto. Allows you to persist and keep track of revisions of your data structures in any of Ecto's supported databases.

The things that you want to keep track of do not necessarily need to be (Ecto-backed) models/schemas. Any data structure can be used (even things that are not structs).

Installation

First, install the library by adding revisionair_ecto to your list of dependencies in mix.exs:

def deps do
  [{:revisionair_ecto, "~> 1.0"}]
end

Then, create a migration (mix ecto.gen.migration revisions_table) akin to the following:

defmodule RevisionairEcto.Repo.Migrations.RevisionsTable do
  use Ecto.Migration

  def change do
    create table(:revisions, primary_key: false) do
      add :item_type, :string, null: false
      # If you want to use UUIDs instead, alter the following line to
      # add :item_id, :uuid, null: false
      add :item_id, :integer, null: false
      # If you want to use JSON serialization instead, alter the following line to
      # add :encoded_item, :json, null: false
      add :encoded_item, :binary, null: false
      add :metadata, :map, null: false
      add :revision, :integer, null: false
    end

    create unique_index(:revisions, [:item_type, :item_id, :revision])
  end
end

Finally, in your config/config.exs, add the following lines to configure Revisonair and RevisionairEcto:

# Set default Revisionair storage adapter.
config :revisionair, storage: RevisionairEcto

# Default repo used by RevisionairEcto:
config :revisionair_ecto, repo: RevisionairEcto.Repo

# Uncomment if you use UUIDs instead of numerical ids:
# config :revisionair_ecto, item_id_type: :uuid

# Uncomment if you use JSON serialization instead of binary term storage:
# config :revisionair_ecto, serialization_format: :json

# Uncomment if you use a different table than "revisions" to store the revisions information:
# config :revisionair_ecto, revisions_table: "table_name"

Of course, any of these settings can also be specified in the options parameter when calling any of the Revisionair functions:

Revisionair.store_revision(my_post, [storage: RevisionairEcto, storage_options: [repo: MyOtherRepo, revisions_table: "my_revisions", item_id_type: :uuid, serialization_format: :json]])

(This also allows overriding certain settings only in certain special locations.)

Usage

The functions in this module should not be used directly, rather, the functions that the Revisionair module exposes should be used.

As these function calls will hit the database directly, make sure that especially store_revision is used within a Repo transaction to ensure that the revision is only stored if the other database operations could be performed successfully.

Example:

{:ok, post} = Repo.transaction fn ->
  post = Repo.insert!(%Post{title: "Test", content: "Lorem ipsum"})
  
  :ok = Revisionair.store_revision(post, Post, post.id)
  
  # if using JSON serialization, you must also pass a whitelist of attributes you wish to serialize:
  # :ok = Revisionair.store_revision(post, Post, post.id, [storage_options: [attributes: [:id, :content, :title]]])

  post
end

Revisionair.newest_revision(post)
Revisionair.list_revisions(post)
Revisionair.get_revision(post, 0)

Changelog

  • 1.2.2 Removes unused struct_name column from migration examples.
  • 1.2.1 Updates migration examples (in README and tests) to no longer include unneccesary primary key and superfluous index.
  • 1.2.0 Adds the possibility to serialize data using a JSON format instead of the Erlang Term Format.
  • 1.1.0 Upgrade to Ecto 3.
  • 1.0.2 Removes superfluous and noisy logging call.
  • 1.0.1 Updates Revisionair version.
  • 1.0.0 First Stable Version.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].