Skip to content

Allow registering via a registry#7

Open
vhgn wants to merge 2 commits intojoojscript:mainfrom
vhgn:main
Open

Allow registering via a registry#7
vhgn wants to merge 2 commits intojoojscript:mainfrom
vhgn:main

Conversation

@vhgn
Copy link

@vhgn vhgn commented Dec 2, 2025

Description

Allows to use SurrealEx with a Registry without any wrapper

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@vhgn
Copy link
Author

vhgn commented Dec 2, 2025

I was not able to run the tests locally, but it worked in a separate project

defmodule Db do
  def exec(id, statement, bindings) do
    with {:ok, [result]} <- exec_many(id, statement, bindings) do
      {:ok, result}
    end
  end

  def exec_many(id, statement, bindings) do
    with {:ok, %SurrealEx.Domain.ExecutionSuccess{result: result}} <-
           SurrealEx.query(via(id), statement, bindings) do
      {:ok, result}
    end
  end

  def start(id) do
    {namespace, database} = get_info(id)

    DynamicSupervisor.start_child(
      Db.DynamicSupervisor,
      {SurrealEx.Socket, name: via(id), database: database, namespace: namespace}
    )
  end

  def started?(id) do
    case Registry.lookup(Db.Registry, id) do
      [] -> false
      _ -> true
    end
  end

  defp via(id) do
    {:via, Registry, {Db.Registry, id}}
  end

  defp get_info({namespace, database}) do
    {"#{Mix.env()}_#{namespace}", to_string(database)}
  end

  defp get_info(namespace) do
    {"#{Mix.env()}_#{namespace}", "main"}
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant