Loopers97

Loopers97

Still really new to elixir, but I’m trying to get some experience with it. I’m working on trying to hook a new api up to an existing legacy mssql db using sso, that’s filled with stored procedures. I’m currently using ecto 3.4.6 and TDS 2.0. I’m able to pattern match a pid and start link, and query the db in iex.

I’m not quite sure what the code in the module is going to look like, and figured I’d ask for help from the community.

Do I have to do the
{:ok, pid} = Tds.start_link(“sso vars”)
Tds.query(pid, “[customer].gt_customer #{id}”, )
Everytime I want to get something out of the db?

Does Ecto.Repo not do any of start link for me? Should I even be using Repo, if all my interactions with the data store are through stored procs?

Are there any examples anywhere of table type parameters to Tds.query()?

I’ve scoured the internet and not found much guidance, but I doubt I’m the only person that has had to deal with this. Is there some resources somewhere that’d help? I own ecto and absinthe graphql books. Their material primarily focuses on built in adapters.

Showing Posts 1 to 4

Loopers97

Loopers97 OP

For future people like me. I know this seems ridiculous, it felt ridiculous writing it. I welcome any advice anyone has to clean this up.

Put the start link in repo
{:ok, pid} = Tds.start_link([db connection string vars])
pid

Account module
{:ok, res} = Repo.query(“[schema].[sp_name] #{id}”)
res
|> result_to_maps #produce a list of maps
|> Enum.map(fn r → Repo.load(MODULE, r) end) load list of maps into repo

def result_to_maps(%Tds.Result{columns: col_numbers, rows: rows}) do
Enum.map(rows, fn row → row_to_map(col_numbers, row) end)
end

def row_to_map(col_numbers, values) do
Stream.zip(col_numbers, values)
|> Enum.into(, & &1)
end

Also I used embedded_schema because I don’t have access to the table and they’re not snake_casing because I ask them nicely.

embedded_schema do
field :first_name, :string, source: :FirstName
end

chouzar

chouzar

My approach for doing MSSQL stored procdure queries was to skip Ecto and use the Tds driver directly.

I would load the SQL files from the priv directory and pass parameters to it by transforming a Map or KeywordList into the required format and appending to the file. So far has worked ok :+1:

I’m not an expert on Ecto.Repo but your approach looks solid (not sure what Repo.load does); libraries like yesql or ayesql might also help you but haven’t tried them for stored procedures.


As for process startup it looks quite fine; not sure if you already know about Supervision Trees or Applications but it is also one of the ways to start long-running systems in erlang-otp.

Loopers97

Loopers97 OP

Thanks for the advice. I’ll try to reimplement using the libraries you suggested. I was hoping to get use out of Ecto.Changeset, but… you might be right to just skip over Ecto entirely.

chouzar

chouzar

Hi @Loopers97 sorry for taking so long.

The suggested libraries are very cool, but keep in mind that your approach is totally ok.

Changesets will help you with casting, data validation and readable errors; but if you only want to validate passed params to the stored procedure you could give a fair shot to the Norm library.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews