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

Configuring a connection string dynamically #18

Merged
merged 36 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1ec08ac
Configuring a connection string dynamically
jaeyson May 21, 2024
d95b986
add option to add map instead of connection struct
jaeyson May 21, 2024
09ba015
add connection_test.exs
jaeyson May 21, 2024
aa469e3
add coveralls.io test coverage badge
jaeyson May 21, 2024
93633d1
update functions to use default connection
jaeyson May 22, 2024
8c0d691
update docs
jaeyson May 22, 2024
2f252cb
update ci.yml
jaeyson May 22, 2024
df216fd
update ci.yml
jaeyson May 22, 2024
17aa1c5
update mix.lock
jaeyson May 22, 2024
39d74e7
update mix.exs
jaeyson May 22, 2024
043d2b6
update module docs in HttpClient.ex
jaeyson May 22, 2024
099c536
check formatted
jaeyson May 23, 2024
476f3fb
update ci.yml
jaeyson May 23, 2024
8fb2767
use container workflow instead of services
jaeyson May 23, 2024
2991301
update ci.yml
jaeyson May 23, 2024
b802be5
update ci.yml
jaeyson May 23, 2024
18ab10a
add matrix strategy
jaeyson May 23, 2024
0f83869
version int to string
jaeyson May 23, 2024
6d1fffe
disable check format
jaeyson May 23, 2024
f7c6019
add curl health check
jaeyson May 23, 2024
1fe1075
add health check for typesense
jaeyson May 23, 2024
bc57cb4
update ci.yml
jaeyson May 23, 2024
5cdb303
update ci.yml
jaeyson May 23, 2024
dac2e1a
update ci.yml
jaeyson May 23, 2024
4dd42be
update ci.yml
jaeyson May 23, 2024
b86385b
update ci.yml
jaeyson May 23, 2024
75a02d0
add matrix for typesense port
jaeyson May 23, 2024
41eaaf0
update ci.yml
jaeyson May 23, 2024
e464336
single compose file for both arch
jaeyson Jun 2, 2024
3cd8819
source the bash file instead of running with dot
jaeyson Jun 2, 2024
d82b952
remove bash script
jaeyson Jun 4, 2024
49d3236
update docs
jaeyson Jun 8, 2024
da8061d
update ci.yml
jaeyson Jun 8, 2024
a421f5e
unlock mix deps unused
jaeyson Jun 8, 2024
4a5403d
update ci.yml
jaeyson Jun 8, 2024
a739206
update ci.yml
jaeyson Jun 9, 2024
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
22 changes: 11 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
docker run -d \
-p ${{ matrix.typesense-port }} \
--name typesense \
-v /tmp/typesense:/data \
-v /tmp/typesense-data:/data \
typesense/typesense:${{ matrix.typesense-version}} \
--api-key=xyz \
--data-dir /data \
Expand All @@ -64,24 +64,24 @@ jobs:
path: |
deps
_build
key: ${{ runner.os }}-mixtest-${{ hashFiles('**/mix.lock') }}
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mixtest-
${{ runner.os }}-mix-

- name: Install Dependencies
run: |
mix local.rebar --if-missing
mix local.hex --if-missing
mix deps.get

# - name: Find unused dependencies
# run: mix deps.unlock --check-unused
- name: Find unused dependencies
run: mix deps.unlock --check-unused

# - name: Check retired dependencies
# run: mix hex.audit
- name: Check retired dependencies
run: mix hex.audit

# - name: Security audit of dependencies
# run: mix deps.audit
- name: Security audit of dependencies
run: mix deps.audit

- name: Compile project
run: mix compile --all-warnings
Expand All @@ -95,5 +95,5 @@ jobs:
- name: Run tests
run: mix test

# - name: Post test coverage to Coveralls
# run: mix coveralls.github
- name: Post test coverage to Coveralls
run: mix coveralls.github
53 changes: 46 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,84 @@ 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.0 (2024.05.20)

### Added

* Connection module for dynamic loading of credentials.
* Default connection config when running commands (e.g. create collections, etc.).

### Changed

* Refactor `ExTypesense.HttpClient` on how to handle request.
* Bumped dependencies' version.
* Dropped usage of `:httpc` in favor of using [`Req library`](https://hex.pm/packages/req).

### Deprecated

* Some functions from `Document` and `HttpClient` where soft depcrated in order to incorporate the `Connection` module for dynamic connections loaded from Ecto schema. If you read the docs, you might notice it's marked with `deprecated` and encourages to use the newer ones.

## 0.3.5 (2023.08.13)

### Fixed

* Fixed typos

## 0.3.4 (2023.07.12)

### Changed

* Remove string conversion on struct id when deleting a document.

## 0.3.3 (2023.07.11)

* Add index_multiple_documents/1 clause for accepting struct args
### Added

* Add index_multiple_documents/1 clause for accepting struct args.

## 0.3.2 (2023.07.11)

* Maps struct pk to document's id
* Update http request timeout to `3,600` seconds
### Changed

* Maps struct pk to document's id.
* Update http request timeout to `3,600` seconds.

## 0.3.1 (2023.07.11)

* Increase connection timeout
### Changed

* Increase connection timeout.

## 0.3.0 (2023.06.20)

* Fixed url request path for aliases
### Fixed

* Fixed url request path for aliases.

### Changed
* Refactor functions inside collection and document.
* Changed return values from `{:ok, t()}` to `t()` only.
* Added cheatsheet section on docs
* Parse schema field types for `float`, `boolean`, `string`, `integer` and a list with these corresponding types.

### Added
* Added cheatsheet section on docs.

## 0.2.2 (2023.01.26)

### Changed

* Updated docs

## 0.2.1 (2023.01.22)

* Returned an ecto query instead of list of results
### Changed

* Returned an ecto query instead of list of results.

## 0.2.0 (2023.01.20)

### Added

* Added search function which returns a list of structs or empty.

## 0.1.0 (2023.01.20)
Expand Down
84 changes: 79 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
[![Hex.pm](https://img.shields.io/hexpm/v/ex_typesense)](https://hex.pm/packages/ex_typesense)
[![Hexdocs.pm](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/ex_typesense)
[![Hex.pm](https://img.shields.io/hexpm/l/ex_typesense)](LICENSE)
[![Typesense badge](https://img.shields.io/badge/Typesense-v0.25.2-darkblue)](https://typesense.org/docs/0.25.2/api)
[![Typesense badge](https://img.shields.io/badge/Typesense-v26.0-darkblue)](https://typesense.org/docs/26.0/api)
[![Coverage Status](https://coveralls.io/repos/github/jaeyson/ex_typesense/badge.svg?branch=main)](https://coveralls.io/github/jaeyson/ex_typesense?branch=main)

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.

## Todo

- creating collection using auto schema detection
Expand All @@ -25,7 +28,7 @@ Add `:ex_typesense` to your list of dependencies in the Elixir project config fi
def deps do
[
# From default Hex package manager
{:ex_typesense, "~> 0.3"}
{:ex_typesense, "~> 0.4"}

# Or from GitHub repository, if you want to latest greatest from main branch
{:ex_typesense, git: "https://github.com/jaeyson/ex_typesense.git"}
Expand All @@ -35,11 +38,23 @@ end

## Getting started

### 0. Run local Typesense instance

```bash
# Linux
docker compose -f linux.yml up -d

# Mac OS, the difference is using arm64 arch
docker compose -f osx.yml up -d
```

More info on spinning a local instance: https://typesense.org/docs/guide/install-typesense

### 1. Add credential to config

After you have setup a [local](./guides/running_local_typesense.md) Typesense or [Cloud hosted](https://cloud.typesense.org) instance, you can set the following config details to the config file:

For local instance:
#### (Option 1) Set credentials via config (e.g. `config/runtime.exs`)

```elixir
config :ex_typesense,
Expand All @@ -59,9 +74,68 @@ config :ex_typesense,
scheme: "https"
```

#### (Option 2) Dynamic connection using an Ecto schema

> 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.

```elixir
defmodule MyApp.Credential do
schema "credentials" do
field :node, :string
field :secret_key, :string
field :port, :integer
end
end
```

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

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

# or maps, as long as the keys matches in ExTypesense.Connection.t()
conn = %{
host: credential.node,
api_key: credential.secret_key,
port: credential.port,
scheme: "https"
}

# or convert your struct to map, as long as the keys matches in ExTypesense.Connection.t()
conn = Map.from_struct(MyApp.Credential)

# or you don't want to change the fields in your schema, thus you convert it to map
conn = %Credential{
node: "localhost",
secret_key: "xyz",
port: 8108,
scheme: "http"
}

conn =
conn
|> Map.from_struct()
|> Map.drop([:node, :secret_key])
|> Map.put(:host, conn.node)
|> Map.put(:api_key, conn.secret_key)

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

### 2. Create a collection

#### Using Ecto
There are 2 ways to create a collection, either via [Ecto schema](https://hexdocs.pm/ecto/Ecto.Schema.html) or using map ([an Elixir data type](https://hexdocs.pm/elixir/keywords-and-maps.html#maps-as-key-value-pairs)):

#### Option 1: using Ecto

In this example, we're adding `person_id` that points to the id of `persons` schema.

Expand Down Expand Up @@ -110,7 +184,7 @@ Next, create the collection from a module name.
ExTypesense.create_collection(Person)
```

#### Using Maps
#### Option 2: using map

```elixir
schema = %{
Expand Down
6 changes: 6 additions & 0 deletions coveralls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"skip_files": [
"test",
"deps"
]
}
15 changes: 4 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
version: "3"
services:
typesense:
image: docker.io/typesense/typesense:0.25.2
image: docker.io/typesense/typesense:26.0
container_name: typesense
tty: true
restart: on-failure
environment:
- TYPESENSE_DATA_DIR=/data
- TYPESENSE_API_KEY=xyz
ports:
- "8108:8108"
volumes:
- ./typesense-data:/data
ports:
# for internal status of the typesense server
- 8107:8107
# for actual typesense server
- 8108:8108
command: '--data-dir /data --api-key=xyz --enable-cors'
25 changes: 21 additions & 4 deletions guides/running_local_typesense.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ machine:
- Docker Compose: <https://docs.docker.com/compose/install/> - Docker Compose
helps manage multi-container applications like Typesense.

Or use the [one line command](https://github.com/docker/docker-install) to install both:

```bash
# this is slightly edited so that you don't
# have to save the file on the machine
curl -sSL https://get.docker.com/ | sh
```

## Setting up

We are using Docker compose to bootstrap a local Typesense instance from a
sample `docker-compose.yml` file.

sample docker compose file.

Clone the `ex_typesense` GitHub repository:

Expand All @@ -33,9 +39,16 @@ Navigate to the cloned GitHub repository start the Typesense instance:

```bash
cd ex_typesense
docker-compose up -d

# using Linux
docker compose -f linux.yml up -d

# or using Mac OS, the difference is using arm64 arch
docker compose -f osx.yml up -d
```

More info on spinning a local instance: https://typesense.org/docs/guide/install-typesense

Once you've started Typesense, you can verify its installation by accessing the
health endpoint through a browser or `curl` in the terminal:

Expand All @@ -48,5 +61,9 @@ In a separate terminal, you can view the logs of your local Typesense instance
using following command:

```bash
docker-compose logs -f
# using Linux
docker compose -f linux.yml logs -f

# or using Mac OS
docker compose -f osx.yml logs -f
```
Loading