Ecto
I wrote a blog post a few weeks ago on the wonderful Elixir language. Now I’ve built a couple of crud applications with the help of Phoenix the best framework in the Elixir world right now. Now you can’t do much now days without interacting with some sort of data, and to store that data you need a database. In comes Ecto a DSL that allows you to interact with the database, it currently supports all the major SQL based database and MongoDB. Couple Ecto with Phoenix and it performs much like entity framework does in dot net, it scaffolds out the database. To create the schema for Ecto to follow you run the following command
This generates a schema which allows you to run the commands
Running these commands means that ecto will handle the whole database creation based upon the schema that Phoenix has created, this means that in minutes you get an entire crud set up almost. All you have to do now is edit the schema functions with the queries you want and bam your done. You have full crud functionality hooked up to a postgres database. Obviously this isn’t a full tutorial on how to do it more of a discussion on how easy it is to get the crud functionality up and running. With in 30 mins you can be reading and writing from a database, with another 30 you can validate this data going in and out. Elixir and Ecto make it extremely easy to get stuff done, but it doesn’t do everything for you. You still have to do many things for yourself. Phoenix and Ecto set you up with the basic database functionality but if you need to do more complex queries you will have to write your own. This is cool, so in a functional language you can chain together many functions, called piping, this allows you to feed the last line from one function into the first argument of the second function. You can even pass queries into queries, allowing for extremely complex queries. One of the coolest things is that it’s a little like GraphQL in that you write the queries in an SQL like manner but they get translated to whatever database type your using via an adapter. This makes it a lot easier to write queries, (don’t be silly like me and sit there wondering why your delete function is deleting by ID rather than the column that you passed in, this is because it has many other functions, many more than the ones that are generated for you, you just have to implement them.), to return the count of the amount of records in the database you can use the Ecto.aggregate function which takes in a query that returns a certain list of records, then counts them, then returns that number. You can do anything you want with Ecto, you just have to workout how to do it. For the most part it's not to hard, but if you stumble into the realm of complex joins and foreign key constraints then it can be quite difficult to write the proper queries that get you the data you want, but then again what query language doesn’t make it hard, well maybe GraphQL.
Give it a go, for most of you, you have probably already used it as Phoenix comes with it by default, but if you haven’t used it I would get into it as it does wonders.