Skip to content

Commit cb9593b

Browse files
committed
Exqlite.TypeExtensions
Runtime extensions to convert data types into something that can be serialized into the database.
1 parent b8035f2 commit cb9593b

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ end
4646
### Runtime Configuration
4747

4848
```elixir
49-
config :exqlite, default_chunk_size: 100
49+
config :exqlite,
50+
default_chunk_size: 100,
51+
type_extensions: [MyApp.TypeExtension]
5052
```
5153

5254
* `default_chunk_size` - The chunk size that is used when multi-stepping when
5355
not specifying the chunk size explicitly.
54-
56+
* `type_extensions`: An optional list of modules that implement the
57+
Exqlite.TypeExtension behaviour, allowing types beyond the default set that
58+
can be stored and retrieved from the database.
59+
5560
### Compile-time Configuration
5661

5762
In `config/config.exs`,

lib/exqlite/extension.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule Exqlite.TypeExtension do
2+
@moduledoc """
3+
A behaviour that defines the API for extensions providing custom data loaders and dumpers
4+
for Ecto schemas.
5+
"""
6+
7+
@doc """
8+
Takes a value and convers it to data suitable for storage in the database.
9+
"""
10+
@callback convert(value :: term) :: term
11+
end

lib/exqlite/sqlite3.ex

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,5 +481,20 @@ defmodule Exqlite.Sqlite3 do
481481
raise ArgumentError, "#{inspect(datetime)} is not in UTC"
482482
end
483483

484-
defp convert(val), do: val
484+
defp convert(val) do
485+
convert_with_type_extensions(type_extensions(), val)
486+
end
487+
488+
defp convert_with_type_extensions([], val), do: val
489+
490+
defp convert_with_type_extensions([extension | other_extensions], val) do
491+
case extension.convert(val) do
492+
nil -> convert_with_type_extensions(other_extensions, val)
493+
convert -> convert
494+
end
495+
end
496+
497+
defp type_extensions() do
498+
Application.get_env(:exqlite, :type_extensions, [])
499+
end
485500
end

0 commit comments

Comments
 (0)