9/8/2023 0 Comments Elixir ecto to sql![]() ![]() I have a lot of SQL experience – at some times, most of my programming was in SQL. The schema API is pretty small with the only real complexity in the relationship stuff, but even that is pretty standard as far as any SQL wrapper goes or you can throw those out and just stick a field :post_id, :id in the schema instead and handle it all yourself. I'm not sure if "In the majority of cases, mapping an abstraction down to a table requires looking at the mapping syntax time and again" is directed at Ecto specifically or not. You wont get the "type safe functions" that sqlc produces with that because, well, that's what changeset/query/schema are doing (without getting too deep into type discussions re: ex vs go).īut really Ecto is pretty thin but flexible, I think throwing it out for the n number of complex queries (where n is almost definitely a small fraction of the total number of queries you run unless you're in some specialised problem space) you need is maybe short sighted. Then you still have Ecto and your queries are most definitely "pure SQL" at that point. Hell, you could just forgo any Ecto.Query "helpers" and just use your SQL adapters query function. Yeah I am curious if the OP has actually hit the limit (?) of Ecto or just expecting to from experience.Īs you say, Ecto matches SQL pretty closely and you can pretty easily drop fragments down wherever you want for any "weird" stuff. ![]() ![]() I mean, Ecto schemas are just structs on steroids, which are maps on steroids, so… That’s one of the things o love about Elixir. On that note, I did a quick prototype mixing Ecto schemas with the ayesql library and it works awesome. What I’m targeting at, is replacing its query builder part with auto-generated functions out of raw SQL queries. ![]() I still believe it’s a great persistence and retrieval mechanism and it offers possibilities for validation and sanitatizing, etc. Update: I have to add a few clarification notes. Ideally, something that makes use of Ecto's changesets (for validation, etc). My quick research showed me these two options: In my Go projects, I am using sqlc, but I am yet to find a suitable alternative for Elixir. I've realized that for me, the option to simply laying out my thoughts in SQL and using a generator to turn those into functions is the better way to go. There are also cases when the query syntax would be insufficient for what I am trying to achieve, and then I'd have to drop to using raw SQL. In the majority of cases, mapping an abstraction down to a table requires looking at the mapping syntax time and again. Not because Ecto is bad, but because I've gotten used to reaching the limitations of ORMs and query DSLs in every tech stack I've worked with. Hopefully this will help some people in the future if they need to do raw SQL.I love Elixir and Phoenix, but I have long had difficulty getting up to speed with Ecto. Result.role_masters, result.role_master_parameters, result.insert_server_type_parameters If you need to do any with them at the end just grab them by doing this after a completion of transaction :role_masters, :role_master_parameters, :insert_server_type_parameters IO.inspect result.insert_server_type_parametersĪs you can see I named these queries one was :role_masters, one was :role_master_parameters, :insert_server_type_parameters.Īs multi will match on the role_masters atom from before.Īlso at the end you can see the result, well it comes out in a map of maps essentially, so my map will contain 3 maps which are named by my 3 queries: SELECT $1, $2, 'SERVER', (COALESCE(MAX(position), 0) + 1), 'Y', CURRENT_TIMESTAMP, $3įROM user_servers WHERE server_type_code = 'SERVER' and user_id = $4 "Į(MyRepo, raw_query, )Īnd one of them fails, will it auto-rollback any queries on a failure? Also is there a way to name those queries?įor example, maybe I want my first query to be named user_servers so when the transaction comes back I can retrieve it with ) So I have about 10 of these: raw_query = "INSERT INTO user_servers(user_id, server_id, server_type_code, position, active_flag, create_date, created_by) I am fairly new to Elixir as well and am trying to use ansaction instead of trying to roll my own transactions. I have about 10 that sit within a transaction. I am converting a Node.js app into Elixir and I am needing to use raw SQL queries within Ecto as I have some fairly complex SQL queries. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |