Git Product home page Git Product logo

translixir's Introduction

Translixir

hex.pm hex.pm hex.pm hex.pm github.com

Elixir client for Crux DB, the general purpose database with bitemporal Datalog and SQL.

  • Via Docker with a crux-standalone version docker-hub. Current Docker image juxt/crux-standalone:20.09-1.11.0. Via github dependency
  • Via HTTP using the HTTP API. Via github dependency
  • Missing entity_history with time-stamps

Bitemporal Crux

Crux is optimised for efficient and globally consistent point-in-time queries using a pair of transaction-time and valid-time timestamps.

Ad-hoc systems for bitemporal recordkeeping typically rely on explicitly tracking either valid-from and valid-to timestamps or range types directly within relations. The bitemporal document model that Crux provides is very simple to reason about and it is universal across the entire database, therefore it does not require you to consider which historical information is worth storing in special "bitemporal tables" upfront.

One or more documents may be inserted into Crux via a put transaction at a specific valid-time, defaulting to the transaction time (i.e. now), and each document remains valid until explicitly updated with a new version via put or deleted via delete.

Why?

Time Purpose
transaction-time Used for audit purposes, technical requirements such as event sourcing.
valid-time Used for querying data across time, historical analysis.

transaction-time represents the point at which data arrives into the database. This gives us an audit trail and we can see what the state of the database was at a particular point in time. You cannot write a new transaction with a transaction-time that is in the past.

valid-time is an arbitrary time that can originate from an upstream system, or by default is set to transaction-time. Valid time is what users will typically use for query purposes.

Reference crux bitemporality and value of bitemporality

Installation

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

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

Creating a Crux Client

All operations with Translixir required a Translixir.Client. You can instantiate a new Agent containing the required information for a request with Translixir.Client.new(host, port) and set authorization header token with Translixir.Client.auth(<pid>, token).

# Simple Client
  {:ok, client} = Client.new("localhost","3000")
  expected = %Client{host: "localhost", port: "3000"}
  assert Client.get(client) == expected


# Client with Authorization
  {:ok, client} = Client.new("localhost","3000")
  Client.auth(client, "token")
  expected = %Client{host: "localhost", port: "3000", auth: "token"}
  assert Client.get(client) == expected

Building a tx_log::Action to Insert in database

  • use module Translixir.Model.Action
alias Translixir.Model.Action

actions =
  Action.new()
  |> Action.add_action(Action.evict(:hello))
  |> Action.add_action(
    Action.delete(:my_id, DateTime.from_naive!(~N[2020-10-10 13:26:08.003], "Etc/UTC"))
  )
  |> Action.actions()

assert actions ==
          "[[:crux.tx/evict :hello] [:crux.tx/delete :my_id #inst \"2020-10-10T13:26:08.003%2B00:00\"]]"

Possible Actions:

alias Translixir.Model.Action

actual = Action.put(:my_id, %{first_name: "test", last_name: "wow"})
expected = "[:crux.tx/put {:crux.db/id :my_id, :first_name \"test\", :last_name \"wow\"}]"
assert actual == expected

actual =
  Action.put(
    :my_id,
    %{first_name: "test", last_name: "wow"},
    DateTime.from_naive!(~N[2020-10-10 13:26:08.003], "Etc/UTC")
  )
expected =
  "[:crux.tx/put {:crux.db/id :my_id, :first_name \"test\", :last_name \"wow\"} #inst \"2020-10-10T13:26:08.003%2B00:00\"]"
assert actual == expected

actual = Action.delete(:my_id)
expected = "[:crux.tx/delete :my_id]"
assert actual == expected

actual = Action.delete(:my_id, DateTime.from_naive!(~N[2020-10-10 13:26:08.003], "Etc/UTC"))
expected = "[:crux.tx/delete :my_id #inst \"2020-10-10T13:26:08.003%2B00:00\"]"
assert actual == expected

assert Action.evict(3) == "[:crux.tx/evict 3]"
assert Action.evict(:my_id) == "[:crux.tx/evict :my_id]"

Building a Query

  • Use module Translixir.Model.Query
alias Translixir.Model.Query

actual =
  Query.find(%{}, ["?h", "?q"])
  |> Query.where(["?p1 :name ?n", "?p1 :is-sql true"])
  |> Query.build()
expected = "{:query {:find [?h ?q], :where [[?p1 :name ?n] [?p1 :is-sql true]]}}"
assert actual == expected

assert catch_error(Query.find(%{}, [":hello", ":world"])) ==
          %RuntimeError{message: "All keys should be atoms or strings starting with `?`"}

actual =
  Query.find(%{}, ["?h", "?q"])
  |> Query.where(["?p1 :name ?n", "?p1 :is-sql ?s"])
  |> Query.args(["?s true"])
  |> Query.build()
expected =
  "{:query {:args [{?s true}], :find [?h ?q], :where [[?p1 :name ?n] [?p1 :is-sql ?s]]}}"
assert actual == expected

actual =
  Query.find(%{}, ["?h", "?q"])
  |> Query.where(["?p1 :name ?n", "?p1 :is-sql ?s"])
  |> Query.limit(5)
  |> Query.offset(20)
  |> Query.build()
expected =
  "{:query {:find [?h ?q], :limit 5, :offset 20, :where [[?p1 :name ?n] [?p1 :is-sql ?s]]}}"
assert actual == expected

actual =
  Query.find(%{}, [:h, :q])
  |> Query.where(["?p1 :name ?n", "?p1 :is-sql ?s"])
  |> Query.order_by(["?s true"])
  |> Query.with_full_results()
  |> Query.build()
expected =
  "{:query {:find [?h ?q], :full-results? true, :order-by [[?s true]], :where [[?p1 :name ?n] [?p1 :is-sql ?s]]}}"
assert actual == expected

Functions examples

alias Translixir.Http.Client
alias Translixir.Model.Action

client = Client.new("localhost", "3000")
put = Action.new()
  |> Action.add_action(Action.put(:hello, %{first_name: "Hello", last_name: "World"}))
  |> Action.add_action(Action.delete(:delete_id))
  |> Action.actions()

client |> Translixir.tx_log(put)
# {:ok, %{"crux.tx/tx-id": 7, "crux.tx/tx-time": ~U[2020-10-25 04:33:41.102Z]}}

client |> Translixir.entity(:hello) |> IO.inspect()
# {:ok, %{"crux.db/id": :hello, first_name: "Hello", last_name: "World"}}

License

LGPL. Copyright (c) 2020 Julia Naomi.

translixir's People

Contributors

kianmeng avatar naomijub avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

kianmeng barboza

translixir's Issues

Entity_history should have time ranges

It should be possible to send start and end times for valid_time and tx_time in function entity_history to support timed based queries.

Possible solution:

Struct HistoryTimeRange containing fields with DateTime like:

%HistoryTimeRange{
  start_valid_time: DateTime.t(),
  end_valid_time: DateTime.t(),
  start_tx_time: DateTime.t(),
  endt_tx_time: DateTime.t(),
}

Include formatter

lint: format
	mix credo --strict

format:
	mix format
{:credo, "~> 1.0.0", only: [:dev, :test, :ci], runtime: false}

Include Specs

deps-outdated:
	mix hex.outdated

dialyzer:
	 mix dialyzer --format dialyxir
 {:dialyxir, "~> 0.5", only: [:dev], runtime: false},

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.