View Source LadybugEx.Schema (LadybugEx v0.2.0)

Schema management functions for LadybugDB databases.

This module provides functions to create and manage node tables, relationship tables, and indexes in a LadybugDB database.

Summary

Functions

Creates an index on specified columns of a table.

Creates an index on specified columns, raising on error.

Creates a node table with the specified properties.

Creates a node table, raising on error.

Creates a relationship table with the specified properties.

Gets information about a table's schema.

Drops a node table, raising on error.

Drops a relationship table.

Drops a relationship table, raising on error.

Lists all node tables in the database.

Lists all relationship tables in the database.

Types

property_def()

@type property_def() ::
  {atom() | String.t(), property_type()}
  | {atom() | String.t(), property_type(), keyword()}

property_type()

@type property_type() ::
  :string
  | :int8
  | :int16
  | :int32
  | :int64
  | :uint8
  | :uint16
  | :uint32
  | :uint64
  | :int128
  | :float
  | :double
  | :float_array
  | :double_array
  | :bool
  | :date
  | :timestamp
  | :timestamp_tz
  | :timestamp_ns
  | :timestamp_ms
  | :timestamp_sec
  | :interval
  | :uuid
  | :blob
  | :decimal
  | {:list, property_type()}
  | {:array, property_type()}
  | {:array, property_type(), pos_integer()}
  | {:map, property_type(), property_type()}

Functions

create_index(conn, table_name, columns)

@spec create_index(
  LadybugEx.Connection.t(),
  String.t(),
  atom() | String.t() | [atom() | String.t()]
) ::
  {:ok, :created} | {:error, String.t()}

Creates an index on specified columns of a table.

Parameters

  • conn - A connection reference
  • table_name - The name of the table
  • columns - Column or list of columns to index

Examples

iex> LadybugEx.Schema.create_index(conn, "Person", :email)
{:ok, :created}

iex> LadybugEx.Schema.create_index(conn, "Person", [:age, :city])
{:ok, :created}

create_index!(conn, table_name, columns)

@spec create_index!(
  LadybugEx.Connection.t(),
  String.t(),
  atom() | String.t() | [atom() | String.t()]
) ::
  :created

Creates an index on specified columns, raising on error.

Same as create_index/3 but raises an exception on error.

create_node_table(conn, table_name, properties, opts \\ [])

@spec create_node_table(
  LadybugEx.Connection.t(),
  String.t(),
  [property_def()],
  keyword()
) ::
  {:ok, :created} | {:error, String.t()}

Creates a node table with the specified properties.

Parameters

  • conn - A connection reference
  • table_name - The name of the node table to create
  • properties - A list of property definitions
  • opts - Table creation options

Property Definition

Properties can be defined as:

  • {name, type} - Simple property definition
  • {name, type, opts} - Property with options like primary key

Options

  • :primary_key - Specify the primary key column(s)

Examples

iex> LadybugEx.Schema.create_node_table(conn, "Person", [
...>   {:name, :string},
...>   {:age, :int64},
...>   {:email, :string}
...> ], primary_key: [:name])
{:ok, :created}

iex> LadybugEx.Schema.create_node_table(conn, "Product", [
...>   {:id, :int64, primary_key: true},
...>   {:name, :string},
...>   {:price, :double},
...>   {:tags, {:list, :string}}
...> ])
{:ok, :created}

create_node_table!(conn, table_name, properties, opts \\ [])

@spec create_node_table!(
  LadybugEx.Connection.t(),
  String.t(),
  [property_def()],
  keyword()
) :: :created

Creates a node table, raising on error.

Same as create_node_table/4 but raises an exception on error.

create_rel_table(conn, table_name, from_table, to_table, properties \\ [], opts \\ [])

@spec create_rel_table(
  LadybugEx.Connection.t(),
  String.t(),
  String.t(),
  String.t(),
  [property_def()],
  keyword()
) :: {:ok, :created} | {:error, String.t()}

Creates a relationship table with the specified properties.

Parameters

  • conn - A connection reference
  • table_name - The name of the relationship table to create
  • from_table - The source node table
  • to_table - The destination node table
  • properties - A list of property definitions
  • opts - Table creation options

Options

  • :multiplicity - Can be :one_to_one, :one_to_many, :many_to_many (default)

Examples

iex> LadybugEx.Schema.create_rel_table(conn, "KNOWS",
...>   "Person", "Person",
...>   [
...>     {:since, :date},
...>     {:strength, :double}
...>   ]
...> )
{:ok, :created}

iex> LadybugEx.Schema.create_rel_table(conn, "OWNS",
...>   "Person", "Product",
...>   [
...>     {:quantity, :int64},
...>     {:purchase_date, :date}
...>   ],
...>   multiplicity: :one_to_many
...> )
{:ok, :created}

create_rel_table!(conn, table_name, from_table, to_table, properties \\ [], opts \\ [])

@spec create_rel_table!(
  LadybugEx.Connection.t(),
  String.t(),
  String.t(),
  String.t(),
  [property_def()],
  keyword()
) :: :created

Creates a relationship table, raising on error.

Same as create_rel_table/6 but raises an exception on error.

describe_table(conn, table_name)

@spec describe_table(LadybugEx.Connection.t(), String.t()) ::
  {:ok, map()} | {:error, String.t()}

Gets information about a table's schema.

Parameters

  • conn - A connection reference
  • table_name - The name of the table

Examples

iex> LadybugEx.Schema.describe_table(conn, "Person")
{:ok, %{
  name: "Person",
  type: "NODE",
  columns: [
    %{name: "name", type: "STRING", is_primary: true},
    %{name: "age", type: "INT64", is_primary: false}
  ]
}}

drop_node_table(conn, table_name, opts \\ [])

@spec drop_node_table(LadybugEx.Connection.t(), String.t(), keyword()) ::
  {:ok, :dropped} | {:error, String.t()}

Drops a node table.

Parameters

  • conn - A connection reference
  • table_name - The name of the table to drop
  • opts - Drop options

Options

  • :cascade - Not currently supported by LadybugDB (ignored)

Examples

iex> LadybugEx.Schema.drop_node_table(conn, "Person")
{:ok, :dropped}

iex> LadybugEx.Schema.drop_node_table(conn, "Person", cascade: true)
{:ok, :dropped}

drop_node_table!(conn, table_name, opts \\ [])

@spec drop_node_table!(LadybugEx.Connection.t(), String.t(), keyword()) :: :dropped

Drops a node table, raising on error.

Same as drop_node_table/3 but raises an exception on error.

drop_rel_table(conn, table_name, opts \\ [])

@spec drop_rel_table(LadybugEx.Connection.t(), String.t(), keyword()) ::
  {:ok, :dropped} | {:error, String.t()}

Drops a relationship table.

Parameters

  • conn - A connection reference
  • table_name - The name of the relationship table to drop
  • opts - Drop options

Examples

iex> LadybugEx.Schema.drop_rel_table(conn, "KNOWS")
{:ok, :dropped}

drop_rel_table!(conn, table_name, opts \\ [])

@spec drop_rel_table!(LadybugEx.Connection.t(), String.t(), keyword()) :: :dropped

Drops a relationship table, raising on error.

Same as drop_rel_table/3 but raises an exception on error.

list_node_tables(conn)

@spec list_node_tables(LadybugEx.Connection.t()) ::
  {:ok, [String.t()]} | {:error, String.t()}

Lists all node tables in the database.

Parameters

  • conn - A connection reference

Examples

iex> LadybugEx.Schema.list_node_tables(conn)
{:ok, ["Person", "Product", "Order"]}

list_rel_tables(conn)

@spec list_rel_tables(LadybugEx.Connection.t()) ::
  {:ok, [String.t()]} | {:error, String.t()}

Lists all relationship tables in the database.

Parameters

  • conn - A connection reference

Examples

iex> LadybugEx.Schema.list_rel_tables(conn)
{:ok, ["KNOWS", "OWNS", "PURCHASED"]}