View Source cozodb_script_utils (cozodb v0.2.1)

Summary

Functions

Encodes an index specification into a CozoScript–formatted iolist.

Encodes a relation specification map into a CozoScript–formatted iolist.

Encodes a list of trigger specifications into a CozoScript–formatted string.

Converts a given term into a CozoScript–compatible string representation using the specified mode.

Functions

encode_index_spec(Spec)

-spec encode_index_spec(Spec :: cozodb:index_spec()) -> iolist() | no_return().

Encodes an index specification into a CozoScript–formatted iolist.

Parameters

  • Spec :: map() - An index specification map. The map must specify the index type (e.g. covering, hnsw, fts, or lsh) and include all required keys for that index type.

Returns

  • An iolist() representing the encoded index specification.
  • Raises an error (no_return()) if the specification is invalid.

Example

Spec = #{
    type => hnsw,
    dim => 128,
    m => 50,
    ef_construction => 20,
    dtype => f32,
    distance => l2,
    fields => [v],
    filter => <<"k != 'foo'">>,
    extend_candidates => false,
    keep_pruned_connections => false
},
Encoded = encode_index_spec(Spec).

encode_relation_spec/1

-spec encode_relation_spec(cozodb:relation_spec()) -> iolist() | no_return().

Encodes a relation specification map into a CozoScript–formatted iolist.

Parameters:

  • Spec :: map() - A relation specification map. It must include the keys keys and columns. If either is missing, they are defaulted to empty lists.

Returns:

  • An iolist() representing the encoded specification.
  • Raises an error (no_return()) if the specification is invalid.

Example:

Spec = #{keys => [{a, undefined}], columns => [...]},
Encoded = encode_relation_spec(Spec).

encode_triggers_spec/1

-spec encode_triggers_spec([cozodb:trigger_spec()]) -> return.

Encodes a list of trigger specifications into a CozoScript–formatted string.

Parameters

  • Specs :: [trigger_spec()] - A list of trigger specifications, where each specification is a tuple of the form {Event, Script}. Supported events include on_put, on_rm (or on_remove), and on_replace.

Returns

  • An iolist (string) with each encoded trigger separated by a newline.
  • Returns an empty list if no triggers are specified.

Example

encode_triggers_spec([{on_put, "query goes here"}]).
returns <<"on put { query goes here }">>

to_cozo_string(Term)

to_cozo_string/2

Converts a given term into a CozoScript–compatible string representation using the specified mode.

Parameters:

  • Term :: any - The term to convert.
  • Mode :: single | raw - The conversion mode:

    • single: Encloses atoms and binaries in single quotes.
    • raw: Wraps values in a custom marker.

Returns:

  • A binary containing the formatted string representation of the term.

Examples:

to_cozo_string(<<"foo">>, single) %% returns <<" 'foo' ">>.
to_cozo_string(<<"foo">>, raw)    %% returns a raw formatted string.