Flux Database v0.0.1 FluxDatabase.DETS View Source
DETS integration on FluxDatabase
.
Configuration
The configuration expects a keyword list with:
:database
- To be:dets
.:tables
- A map with database definitions with{path, specification, index_key}
as value andtable_key
as key.path
- An atom with the absolute path to the table storage.specification
- A keyword list with :dets.open_file/2 arguments.index_key
- A string or atom defining the index key of the table.
Example
defmodule MyApp.Database do
use FluxDatabase
@impl FluxDatabase
def config do
[
database: :dets,
tables: %{
user: {
:"/path/to/storage/user",
[type: :set],
:id
},
post: {
:"/path/to/storage/post",
[type: :set],
:id
}
}
]
end
end
Usage
Whenever you need to use a function defined here, use it without the tables
parameter and by your database module. For example, if you defined your
database module as MyApp.Database
, you can use:
alias MyApp.Database
# To start your application
Database.start()
# To retrieve an item from table
Database.read(:user, 1)
# To filter items on table
Database.filter(:user, Database.all_matchspecs())
# To write an item on table
Database.write(:user, %{id: 2, name: "John Doe", email: "john@doe.com"})
Check the functions defined on this module for more information about how to use the functions in your database module.
Link to this section Summary
Functions
Filter items on table using :dets.select/2.
Retrieve an item from table using :dets.lookup/2.
Open all tables using :dets.open_file/1.
Write an item on table using :dets.insert/2.
Link to this section Functions
all_matchspecs()
View Source (since 0.0.1)all_matchspecs() :: [{{:_, :"$1"}, [], [:"$1"]}]
Default
matchspec to
retrieve all items from a table using it in filter/3
.
Use it normally by calling from this module.
Examples
iex> FluxDatabase.DETS.all_matchspecs()
[{{:_, :"$1"}, [], [:"$1"]}]
Filter items on table using :dets.select/2.
Use it from your database module without tables
parameter.
It uses matchspecs.
To get all items from your table, all_matchspecs/0
can be used.
Parameters
tables
- A map with tables settings. More information inFluxDatabase.DETS
.table_key
- A string or atom with the name of the table.matchspecs
- A matchspecs definition.
Examples
iex> tables = %{user: {:"/path/to/storage/user", [type: :set], :id}}
...> FluxDatabase.DETS.start(tables)
...> user = %{id: 1, name: "John Doe", email: "john@doe.com"}
...> FluxDatabase.DETS.write(tables, :user, user)
...> user = %{id: 2, name: "John Anderson", email: "neo@matrix.com"}
...> FluxDatabase.DETS.write(tables, :user, user)
...> FluxDatabase.DETS.filter(tables, :user, FluxDatabase.DETS.all_matchspecs())
{
:ok,
[
%{id: 1, name: "John Doe", email: "john@doe.com"},
%{id: 2, name: "John Anderson", email: "neo@matrix.com"}
]
}
Retrieve an item from table using :dets.lookup/2.
Use it from your database module without tables
parameter.
Parameters
tables
- A map with tables settings. More information inFluxDatabase.DETS
.table_key
- A string or atom with the name of the table.id
- The identifier value of the item.
Examples
iex> tables = %{user: {:"/path/to/storage/user", [type: :set], :id}}
...> FluxDatabase.DETS.start(tables)
...> user = %{id: 1, name: "John Doe", email: "john@doe.com"}
...> FluxDatabase.DETS.write(tables, :user, user)
...> FluxDatabase.DETS.read(tables, :user, 1)
{:ok, %{id: 1, name: "John Doe", email: "john@doe.com"}}
Open all tables using :dets.open_file/1.
Use it from your database module with no parameters.
Parameters
tables
- A map with tables settings. More information inFluxDatabase.DETS
.
Examples
iex> tables = %{user: {:"/path/to/storage/user", [type: :set], :id}}
...> FluxDatabase.DETS.start(tables)
:ok
Write an item on table using :dets.insert/2.
It merges the new data with the data written on table before writing the new data.
Use it from your database module without tables
parameter.
Parameters
tables
- A map with tables settings. More information inFluxDatabase.DETS
.table_key
- A string or atom with the name of the table.data
- A map with the values that will be inserted on storage. Must have theindex_key
defined on tables settings. More information inFluxDatabase.DETS
.
Examples
iex> tables = %{user: {:"/path/to/storage/user", [type: :set], :id}}
...> FluxDatabase.DETS.start(tables)
...> user = %{id: 1, name: "John Doe", email: "john@doe.com"}
...> FluxDatabase.DETS.write(tables, :user, user)
{:ok, %{id: 1, name: "John Doe", email: "john@doe.com"}}