defmodule Fsdb do @moduledoc """ API module. ## Standard API ```elixir :ok = Fsdb.create("table1") {1, "row1"} = Fsdb.insert("table1", "row1") {1, "row1+"} = Fsdb.update("table1", 1, "row1+") {1, "row1+"} = Fsdb.fetch("table1", 1) {1, "row1+"} = Fsdb.delete("table1", 1) {:not_found, "table1", 1} = Fsdb.fetch("table1", 1) [] = Fsdb.list("table1") #bypass the id generator {2, "row2"} = Fsdb.save("table1", 2, "row2") {3, "row3"} = Fsdb.save("table1", 3, "row3") #FIXME tuples may not be ID ordered [{2, "row2"}, {3, "row3"}] = Fsdb.list("table1") :ok = Fsdb.drop("table1") ``` ## Extended API ```elixir path = Path.expand("~/.fsdb") {:ok, pid} = Fsdb.Server.start_link([path: path]) :ok = Fsdb.create(pid, "table1") {1, "row1"} = Fsdb.insert(pid, "table1", "row1") {1, "row1+"} = Fsdb.update(pid, "table1", 1, "row1+") {1, "row1+"} = Fsdb.fetch(pid, "table1", 1) {1, "row1+"} = Fsdb.delete(pid, "table1", 1) {:not_found, "table1", 1} = Fsdb.fetch(pid, "table1", 1) [] = Fsdb.list(pid, "table1") #bypass the id generator {2, "row2"} = Fsdb.save(pid, "table1", 2, "row2") {3, "row3"} = Fsdb.save(pid, "table1", 3, "row3") #FIXME tuples may not be ID ordered [{2, "row2"}, {3, "row3"}] = Fsdb.list(pid, "table1") :ok = Fsdb.drop(pid, "table1") :ok = Fsdb.Server.stop(pid) ``` """ ########################################## # App API ########################################## @doc """ Creates a table with name `table`. Returns `:ok`. """ def create(table) do GenServer.call(Fsdb.Server, {:create, table}) end @doc """ Same as above but addresses an specific Fsdb.Server. `pid` can be either a pid or a registered name. """ def create(pid, table) do GenServer.call(pid, {:create, table}) end @doc """ Drops the table named `table`. Returns `:ok`. """ def drop(table) do GenServer.call(Fsdb.Server, {:drop, table}) end @doc """ Same as above but addresses an specific Fsdb.Server. `pid` can be either a pid or a registered name. """ def drop(pid, table) do GenServer.call(pid, {:drop, table}) end @doc """ Returns all rows in table `table`. Returns `[{id, row}, ...]`. """ def list(table) do GenServer.call(Fsdb.Server, {:list, table}) end @doc """ Same as above but addresses an specific Fsdb.Server. `pid` can be either a pid or a registered name. """ def list(pid, table) do GenServer.call(pid, {:list, table}) end @doc """ Inserts row `row` in table `table` with auto generated ID. Returns `{id, row}`. """ def insert(table, row) do GenServer.call(Fsdb.Server, {:insert, table, row}) end @doc """ Same as above but addresses an specific Fsdb.Server. `pid` can be either a pid or a registered name. """ def insert(pid, table, row) do GenServer.call(pid, {:insert, table, row}) end @doc """ Inserts or updates row `row` in table `table` with specific id `id`. Returns `{id, row}`. """ def save(table, id, row) do GenServer.call(Fsdb.Server, {:save, table, id, row}) end @doc """ Same as above but addresses an specific Fsdb.Server. `pid` can be either a pid or a registered name. """ def save(pid, table, id, row) do GenServer.call(pid, {:save, table, id, row}) end @doc """ Fetches the row in table `table` having the id `id`. Returns `{id, row} | {:not_found, table, id}`. """ def fetch(table, id) do GenServer.call(Fsdb.Server, {:fetch, table, id}) end @doc """ Same as above but addresses an specific Fsdb.Server. `pid` can be either a pid or a registered name. """ def fetch(pid, table, id) do GenServer.call(pid, {:fetch, table, id}) end @doc """ Updates the row in table `table` having the id `id`. Returns `{id, row} | {:not_found, table, id}`. """ def update(table, id, row) do GenServer.call(Fsdb.Server, {:update, table, id, row}) end @doc """ Same as above but addresses an specific Fsdb.Server. `pid` can be either a pid or a registered name. """ def update(pid, table, id, row) do GenServer.call(pid, {:update, table, id, row}) end @doc """ Deletes the row in table `table` having the id `id`. Returns `{id, row} | {:not_found, table, id}`. """ def delete(table, id) do GenServer.call(Fsdb.Server, {:delete, table, id}) end @doc """ Same as above but addresses an specific Fsdb.Server. `pid` can be either a pid or a registered name. """ def delete(pid, table, id) do GenServer.call(pid, {:delete, table, id}) end end