#First steps in using a erlang package from hex (e.g. mysql)

1 messages · Page 1 of 1 (latest)

swift reef
#

Hey, sorry for n00b questions but I didn't see this in baby steps form... How can I use say the mysql package from hex?
I did gleam new foo then gleam add mysql and added import mysql in a test module and ran gleam test:

/tmp/foo$ gleam test
  Resolving versions
  Compiling gleam_stdlib
  Compiling gleeunit
  Compiling castore
  Compiling elixir_make
  Compiling excal
make: Nothing to be done for `all'.
  Compiling mysql
escript: exception error: undefined function rebar3:main/1
  in function  escript:run/2 (escript.erl, line 750)
  in call from escript:start/1 (escript.erl, line 277)
  in call from init:start_em/1
  in call from init:do_boot/3
error: Shell command failure```
bold echo
#

You can't import mysql directly in Gleam.

#

You'll need to use the ffi api to access mysql functions.

#

Apart from that, looks like there might be an issue with your rebar3 installation.

ocean steeple
#

rebar3 is missing

swift reef
#

ffi .. ok thanks. I'm not even sure what rebar is... erlang build tool?

#

not sure why I thought gleam had transparent erlang interop -- derp

bold echo
#

Yeah, rebar3 is the most common Erlang build tool.

swift reef
#

thanks

bold echo
#

ffi is super easy in Gleam. Check the language tour.

swift reef
#

fwiw I'm investigating migrating a ~30kloc python project at work to gleam and seeing how to cover the hard dependencies -- rrule parsing and expansion, mysql client, sqlite, jwt, libphonenumber, etc.

#

yeah I skimmed that whole thing this morning actually. it was super quick to grok but I missed that bit about erlang needing ffi -- all good

#

thanks for the fast responses guys!

bold echo
#

Installing Erlang and Elixir with your OS package manager is usually the simplest way to get going, ime.

swift reef
#

yeah I use asdf but it appears I have erlang installed via asdf yet I have rebar3 in /usr/local/bin which for me implies homebrew ... ignore me, it's a local setup thing. thanks

abstract condor
#

I’m not sure anyone has made a MySQL client yet

#

Might need some extra work there

#

Bit confused by that error message, not sure what’s happening there

#

Some makefile mischief perhaps

pearl current
#

also, It may not be the case, but I found i got ^ that error when some of my tooling in erlang were compiled with different releases.

#

R14 and R24 according to my notes.

abstract condor
#

Oooh that could be it

swift reef
#

I had erlang installed via asdf and gleam via homebrew. After removing erlang from asdf, it was still hanging around in its "shims" even after reshim-ing. I removed the plugin entirely and now erlang, rebar3 etc are from homebrew when I installed gleam... ok.

#

I'm going to try the ffi for the mysql package on hex -

#

are there more docs or examples for ffi from gleam to erlang? the tour has the external functions section but it's a bit light on details (for a erl-n00b like me)...

bold echo
#

The official Gleam libraries have a bunch of examples, for sure.

swift reef
#

maybe this is useful to see how someone new to this world sees this stuff? I'm looking at https://hexdocs.pm/mysql/ and figured let's start with start_link. This takes on the erlang side a tuple of options like host, username, password etc. On the gleam side, I don't see how to create that options tuple via the ffi api. It returns a pid for the generic server bit so I assume I want this to return an Int from the gleam api side ... maybe something like this:

#
  host: String,
  port: Int,
  user: String,
  password: String
) -> Int
  "mysql" "start_link"
#

but that's obviously not sending any options... etc

#

(I've gone from hello, world to this so be patient... :))

bold echo
#

One thing to remember is that your Gleam function doesn't need to have 100% the same signature as whatever ffi function you want to use, and often it's better that it doesn't.

#

You can write thin wrapper functions in an Erlang module and use those from Gleam.

swift reef
#

yeah I'm already changing it to not require a map as I read that isn't as recommended in gleam

#

(that's all hand-wavy with "map" vs "tuple of options" etc)

bold echo
#

Yeah, data structures are one thing. Most Erlang stuff shouldn't be a problem there. But the other thing is you probably don't need or want to expose 100% of the e.g. options a function accepts.