Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change struct to map in order to make dynamic connections (including tests) #30

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ jobs:
path: |
deps
_build
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
key: ${{ runner.os }}-mix-typesense-${{ matrix.typesense-version }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-
${{ runner.os }}-mix-typesense-${{ matrix.typesense-version }}

- name: Install Dependencies
run: |
Expand Down
5 changes: 5 additions & 0 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
alias ExTypesense.Cluster
alias ExTypesense.Collection
alias ExTypesense.Connection
alias ExTypesense.Document
alias ExTypesense.HttpClient
alias ExTypesense.Search
alias ExTypesense.TestSchema.Credential
alias ExTypesense.TestSchema.Person
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.4.3 (2024.07.03)

### Changed

* `README` regarding test and connection strings.
* Replacing connection struct to map.

## 0.4.2 (2024.06.19)

### Changed
Expand Down
40 changes: 15 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Typesense client for Elixir with support for your Ecto schemas.

**Note**: Breaking changes if you're upgrading from `0.3.x` to upcoming `0.5.x` version.
> **Note**: Breaking changes if you're upgrading from `0.3.x` to upcoming `0.5.x` version.

## Todo

Expand Down Expand Up @@ -55,13 +55,17 @@ After you have setup a [local](./guides/running_local_typesense.md) Typesense or
You can set the following config details to the config file:

```elixir
config :ex_typesense,
api_key: "xyz",
host: "localhost",
port: 8108,
scheme: "http"
if config_env() == :prod do # if you'll use this in prod environment
config :ex_typesense,
api_key: "xyz",
host: "localhost",
port: 8108,
scheme: "http"
...
```

> **Note**: If you use this for adding test in your app, you might want to add this in `config/test.exs`:

For Cloud hosted, you can generate and obtain the credentials from cluster instance admin interface:

```elixir
Expand All @@ -72,12 +76,12 @@ config :ex_typesense,
scheme: "https"
```

#### Option 2: Dynamic connection using an Ecto schema
#### Option 2: Set credentials from a map

> By default you don't need to pass connections every
> time you use a function, if you use "Option 1" above.

You may have a `Connection` Ecto schema in your app and want to pass your own creds dynamically.
You may have a `Connection` Ecto schema in your app and want to pass your own creds dynamically:

```elixir
defmodule MyApp.Credential do
Expand All @@ -89,24 +93,11 @@ defmodule MyApp.Credential do
end
```

using Connection struct
As long as the keys matches in `ExTypesense.Connection.t()`:

```elixir
credential = MyApp.Credential |> where(id: ^8888) |> Repo.one()

conn = %ExTypesense.Connection{
host: credential.node,
api_key: credential.secret_key,
port: credential.port,
scheme: "https"
}

ExTypesense.search(conn, collection_name, query)
```

or maps, as long as the keys matches in `ExTypesense.Connection.t()`

```elixir
conn = %{
host: credential.node,
api_key: credential.secret_key,
Expand All @@ -115,10 +106,9 @@ conn = %{
}

ExTypesense.search(conn, collection_name, query)

```

or convert your struct to map, as long as the keys matches in `ExTypesense.Connection.t()`
Or convert your struct to map, as long as the keys matches in `ExTypesense.Connection.t()`:

```elixir
conn = Map.from_struct(MyApp.Credential)
Expand All @@ -127,7 +117,7 @@ ExTypesense.search(conn, collection_name, query)

```

or you don't want to change the fields in your schema, thus you convert it to map
Or you don't want to change the fields in your Ecto schema, thus you convert it to map:

```elixir
conn = %Credential{
Expand Down
11 changes: 0 additions & 11 deletions lib/ex_typesense.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,8 @@ defmodule ExTypesense do
to: ExTypesense.Document

defdelegate create_document(conn \\ Connection.new(), document), to: ExTypesense.Document

@deprecated "use delete_document_by_id/3"
defdelegate delete_document(document), to: ExTypesense.Document

@deprecated "use delete_document_by_struct/2"
defdelegate delete_document(collection_name, document_id), to: ExTypesense.Document

defdelegate delete_document_by_struct(conn \\ Connection.new(), struct),
to: ExTypesense.Document

defdelegate delete_document_by_id(conn \\ Connection.new(), collection_name, document_id),
to: ExTypesense.Document

defdelegate update_document(conn \\ Connection.new(), document), to: ExTypesense.Document
defdelegate upsert_document(conn \\ Connection.new(), document), to: ExTypesense.Document

Expand Down
1 change: 1 addition & 0 deletions lib/ex_typesense/cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule ExTypesense.Cluster do
alias ExTypesense.Connection
alias ExTypesense.HttpClient

@typedoc since: "0.3.0"
@type response() :: any() | {:ok, any()} | {:error, map()}

@doc """
Expand Down
3 changes: 3 additions & 0 deletions lib/ex_typesense/collection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule ExTypesense.Collection do
:vec_dist
]

@typedoc since: "0.1.0"
@type t() :: %__MODULE__{
facet: boolean(),
index: boolean(),
Expand All @@ -44,6 +45,7 @@ defmodule ExTypesense.Collection do
vec_dist: String.t()
}

@typedoc since: "0.1.0"
@type field_type() ::
:string
| :"string[]"
Expand Down Expand Up @@ -95,6 +97,7 @@ defmodule ExTypesense.Collection do
symbols_to_index: list()
}

@typedoc since: "0.1.0"
@type response() :: t() | [t() | map()] | map() | {:error, map()}

@doc """
Expand Down
37 changes: 28 additions & 9 deletions lib/ex_typesense/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,44 @@ defmodule ExTypesense.Connection do
Fetches credentials either from application env or map.
"""

@derive {Inspect, except: [:api_key]}
defstruct host: ExTypesense.HttpClient.get_host(),
api_key: ExTypesense.HttpClient.api_key(),
port: ExTypesense.HttpClient.get_port(),
scheme: ExTypesense.HttpClient.get_scheme()
alias ExTypesense.HttpClient

@type t() :: %__MODULE__{}
# @derive {Inspect, except: [:api_key]}
# defstruct host: HttpClient.get_host(),
# api_key: HttpClient.api_key(),
# port: HttpClient.get_port(),
# scheme: HttpClient.get_scheme()

@typedoc since: "0.4.0"
@type t() :: %{
host: String.t() | nil,
api_key: String.t() | nil,
port: non_neg_integer() | nil,
scheme: String.t() | nil
}

@doc """
Fetches credentials either from application env or map.
"""
@doc since: "0.4.0"
@spec new(connection :: struct() | map()) :: ExTypesense.Connection.t()
def new(connection \\ %__MODULE__{}) when is_struct(connection) do
%__MODULE__{
@spec new(connection :: t() | map()) :: ExTypesense.Connection.t()
def new(connection \\ defaults()) when is_map(connection) do
%{
host: Map.get(connection, :host),
api_key: Map.get(connection, :api_key),
port: Map.get(connection, :port),
scheme: Map.get(connection, :scheme)
}
end

@doc since: "0.4.3"
@spec defaults :: map()
defp defaults do
%{
host: HttpClient.get_host(),
api_key: HttpClient.api_key(),
port: HttpClient.get_port(),
scheme: HttpClient.get_scheme()
}
end
end
65 changes: 2 additions & 63 deletions lib/ex_typesense/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@collections_path @root_path <> "collections"
@documents_path "documents"
@import_path "import"

@typedoc since: "0.1.0"
@type response :: :ok | {:ok, map()} | {:error, map()}

@doc """
Expand Down Expand Up @@ -369,72 +371,10 @@
HttpClient.request(conn, opts)
end

@doc """
Deletes a document by struct.
"""
@doc since: "0.4.0"
@spec delete_document_by_struct(Connection.t(), struct()) :: response()
def delete_document_by_struct(conn \\ Connection.new(), struct) when is_struct(struct) do
document_id = struct.id
collection_name = struct.__struct__.__schema__(:source)
do_delete_document(conn, collection_name, document_id)
end

@doc """
Deletes a document by collection name and document id.

## Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
...> ExTypesense.create_collection(schema)
iex> post =
...> %{
...> id: "12",
...> collection_name: "posts",
...> post_id: 22,
...> title: "the quick brown fox"
...> }
iex> ExTypesense.create_document(post)
iex> ExTypesense.delete_document("posts", 12)
{:ok,
%{
"id" => "12",
"post_id" => 22,
"title" => "the quick brown fox",
"collection_name" => "posts"
}
}
"""
@doc since: "0.4.0"
@spec delete_document_by_id(Connection.t(), String.t(), integer()) :: response()
def delete_document_by_id(conn \\ Connection.new(), collection_name, document_id)
when is_binary(collection_name) and is_integer(document_id) do
do_delete_document(conn, collection_name, document_id)
end

@doc since: "0.4.0"
@spec do_delete_document(Connection.t(), String.t(), integer()) :: response()
defp do_delete_document(conn, collection_name, document_id) do
path =
Path.join([
@collections_path,
collection_name,
@documents_path,
Jason.encode!(document_id)
])

HttpClient.request(conn, %{method: :delete, path: path})
end

@doc """
Deletes a document by struct.
"""
@doc since: "0.3.0"
@deprecated "use delete_document_by_struct/2"
@spec delete_document(struct()) :: response()
def delete_document(struct) when is_struct(struct) do
document_id = struct.id
Expand Down Expand Up @@ -472,7 +412,6 @@
}
"""
@doc since: "0.3.0"
@deprecated "use delete_document_by_id/3"
@spec delete_document(String.t(), integer()) :: response()
def delete_document(collection_name, document_id)
when is_binary(collection_name) and is_integer(document_id) do
Expand All @@ -489,6 +428,6 @@
Jason.encode!(document_id)
])

HttpClient.run(:delete, path)

Check warning on line 431 in lib/ex_typesense/document.ex

View workflow job for this annotation

GitHub Actions / test (0.25.2, 8108:8108)

ExTypesense.HttpClient.run/2 is deprecated. Use request/2 instead

Check warning on line 431 in lib/ex_typesense/document.ex

View workflow job for this annotation

GitHub Actions / test (26.0, 8108:8108)

ExTypesense.HttpClient.run/2 is deprecated. Use request/2 instead
end
end
Loading