Skip to content

Commit

Permalink
Configuring a connection string dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeyson committed Jun 9, 2024
1 parent de0ce72 commit 0a0e058
Show file tree
Hide file tree
Showing 21 changed files with 898 additions and 378 deletions.
82 changes: 58 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,93 @@ on:
branches: ["main"]

jobs:
check-commit-message:
name: Check commit message for skipping build(s)
runs-on: ubuntu-latest
# skips CI and prints cli message, without fail build badge
if: contains(github.event.head_commit.message, '[skip ci]')
steps:
- name: print message via cli
run: echo "no need to build, based from commit message"
# https://docs.github.com/en/actions/managing-workflow-runs/skipping-workflow-runs
# Workflows that would otherwise be triggered using `on: push` or
# `on: pull_request` won't be triggered if you add any of the
# following strings to the commit message in a push, or the HEAD
# commit of a pull request:
# - [skip ci]
# - [ci skip]
# - [no ci]
# - [skip actions]
# - [actions skip]

test:
runs-on: ubuntu-latest

strategy:
matrix:
typesense-version: [0.25.1]
typesense-version: ['0.25.2', '26.0']
typesense-port: ['8108:8108']

env:
MIX_ENV: test
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

services:
typesense:
image: typesense/typesense:${{ matrix.typesense-version }}

steps:
- uses: actions/checkout@v3
- uses: erlef/setup-beam@v1
- name: Checkout repo
uses: actions/checkout@v4

- name: Start Typesense
run: |
docker run -d \
-p ${{ matrix.typesense-port }} \
--name typesense \
-v /tmp/typesense-data:/data \
typesense/typesense:${{ matrix.typesense-version}} \
--api-key=xyz \
--data-dir /data \
--enable-cors
- name: Curl Typesense
run: sleep 1 && curl http://localhost:8108/health

- name: Setup Elixir/OTP
uses: erlef/setup-beam@v1
with:
otp-version: '25'
elixir-version: '1.14.x'

- uses: actions/cache@v2
id: mix-cache
- name: Cache dependencies/builds
uses: actions/cache@v4
with:
path: |
deps
key: ${{ runner.os }}-mix-${{ hashFiles('mix.lock') }}
_build
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-
- name: Start Typesense
uses: jirevwe/[email protected]
with:
typesense-version: ${{ matrix.typesense-version }}
typesense-api-key: xyz

- name: Install Dependencies
run: |
mix local.rebar --force
mix local.hex --force
mix local.rebar --if-missing
mix local.hex --if-missing
mix deps.get
mix format --check-formatted
- name: Find unused dependencies
run: mix deps.unlock --check-unused

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

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

- name: Compile project
run: mix compile
run: mix compile --all-warnings

- name: run static analysis
run: mix credo --all --strict

# - name: check format files
# run: mix format --check-formatted

- name: Run tests
run: mix test

- 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'
Loading

0 comments on commit 0a0e058

Please sign in to comment.